#150415 detect mounted ro f.s.
#151107 added save_session_func(), called from /usr/sbin/savesession
#151108 exclude some dirs from s.sfs. 151110 fix a window width.
#151123 BOOT_DEV, BOOT_FS, BOOT_FOLDER must be provide at entry save_session_frugal_func().
#151128 save_session_frugal_func() no longer uses install_mode. BOOT_ to INSTALL_
#151204 probepart returns /dev/zram0, exclude. aufs layer, exclude /file/tmpwkgdir (problem with mksquashfs).
#151205 revert 151204 exclude /file/tmpwkgdir
#151210 want save_session_frugal_func() to work without X. refer: /usr/sbin/xorgwizard-cli

#return a folder or mount-point that has sufficient space to process installation:
find_working_place_func() {
 TMPWKG_MNTPT=""
 TMPWKG_MNTPTflag=0
 TMPWKG_PARTMNT=""
 TMPWKG_PARTMNTflag=0
 PASSED_WANTSIZEM=${1} #amount of space required.
 PASSED_EXCLDEV="$2" #passed param, device to exclude.
 [ "$PASSED_EXCLDEV" == "" ] && PASSED_EXCLDEV='xxx'
 [ -d /file/tmpwkgdir ] && ${PRE}busybox rm -rf /file/tmpwkgdir #150416 moved up
 FSSTATS="$(${PRE}busybox stat -f /)"
 FSBLKSIZE=`${PRE}busybox echo -n "$FSSTATS" | ${PRE}busybox grep -o 'Block size: [0-9]*' | ${PRE}busybox cut -f 3 -d ' '` #ex: 4096
 FSAVAILBLKS=`${PRE}busybox echo -n "$FSSTATS" | ${PRE}busybox grep -o 'Available: [0-9]*' | ${PRE}busybox cut -f 2 -d ' '` #ex: 847888
 FSFREEBYTES=`${PRE}busybox expr $FSAVAILBLKS \* $FSBLKSIZE`
 FSFREEM=`${PRE}busybox expr $FSFREEBYTES \/ 1048576` #1024*1024=1048576
 FSNEEDMplus=`${PRE}busybox expr $PASSED_WANTSIZEM + 100`
 #if [ "$(df | grep '/initrd/q_ro/q_sfs')" == "" ];then #151204 test if a aufs layered f.s. 151205 revert.
  if [ $FSFREEM -ge $FSNEEDMplus ];then
   ${PRE}busybox mkdir -p /file/tmpwkgdir
   TMPWKG_MNTPT="/file/tmpwkgdir"
   return 0
  fi
 #fi
 #find a partition to use as wkg space, and f.s.... 151204 only allow /dev/s* or m*
 for APART in `${PRE}probepart -m | ${PRE}busybox grep '^/dev/[sm]' | ${PRE}busybox grep -v "${PASSED_EXCLDEV}" | ${PRE}sort -n -r -k 3 -t '|' | ${PRE}busybox grep -E 'vfat|ntfs|ext2|ext3|ext4|reiserfs|xfs|f2fs' | ${PRE}busybox tr '\n' ' '`
 do
  PARTDEV="$(${PRE}busybox echo -n "$APART" | ${PRE}busybox cut -f 1 -d '|' | ${PRE}busybox cut -f 3 -d '/')"
  PARTFS="$(${PRE}busybox echo -n "$APART" | ${PRE}busybox cut -f 2 -d '|')"
  PARTSIZEM=`${PRE}busybox echo -n "$APART" | ${PRE}busybox cut -f 3 -d '|'`
  [ $PARTSIZEM -lt $FSNEEDMplus ] && break #coz have sorted by reducing size.
  TMPWKG_PARTMNT="$(${PRE}busybox mount | ${PRE}busybox grep "^/dev/${PARTDEV} " | ${PRE}busybox cut -f 3 -d ' ')"
  if [ "$TMPWKG_PARTMNT" == "" ];then
   ${PRE}busybox mkdir -p /mnt/${PARTDEV}
   ${PRE}busybox mount -t ${PARTFS} -o rw,noatime /dev/${PARTDEV} /mnt/${PARTDEV}
   [ $? -ne 0 ] && continue
   TMPWKG_PARTMNT="/mnt/${PARTDEV}"
   TMPWKG_PARTMNTflag=1
  fi
  if [ "$(${PRE}busybox mount | grep "^/dev/${PARTDEV} " | grep "rw")" != "" ];then #150415
   [ -d ${TMPWKG_PARTMNT}/file/tmpwkgdir ] && ${PRE}busybox rm -rf ${TMPWKG_PARTMNT}/file/tmpwkgdir #150416 moved up.
   [ -f ${TMPWKG_PARTMNT}/tmpwkgfs.q ] && rm -f ${TMPWKG_PARTMNT}/tmpwkgfs.q #150416 moved up.
   PARTSTATS="$(${PRE}busybox stat -f $TMPWKG_PARTMNT)"
   PARTBLKSIZE=`${PRE}busybox echo -n "$PARTSTATS" | ${PRE}busybox grep -o 'Block size: [0-9]*' | ${PRE}busybox cut -f 3 -d ' '` #ex: 4096
   PARTAVAILBLKS=`${PRE}busybox echo -n "$PARTSTATS" | ${PRE}busybox grep -o 'Available: [0-9]*' | ${PRE}busybox cut -f 2 -d ' '` #ex: 847888
   PARTFREEBYTES=`${PRE}busybox expr $PARTAVAILBLKS \* $FSBLKSIZE`
   PARTFREEM=`${PRE}busybox expr $PARTFREEBYTES \/ 1048576` #1024*1024=1048576
   if [ $PARTFREEM -gt $FSNEEDMplus ];then
    if [ "$PARTFS" != "ntfs" -a "$PARTFS" != "vfat" ];then
     ${PRE}busybox mkdir -p ${TMPWKG_PARTMNT}/file/tmpwkgdir
     TMPWKG_MNTPT="${TMPWKG_PARTMNT}/file/tmpwkgdir"
     return 0
    fi
    ${PRE}popup "level=top background='#FFE000'|<big>$(gettext 'Please wait, creating temporary working file:') ${TMPWKG_PARTMNT}/tmpwkgfs.q</big>"
    ${PRE}busybox dd if=/dev/zero of=${TMPWKG_PARTMNT}/tmpwkgfs.q bs=1M count=$PASSED_WANTSIZEM conv=fsync
    if [ $? -eq 0 ];then
     ${PRE}busybox killall popup
     ${PRE}busybox mkfs.ext2 -F -m 0 ${TMPWKG_PARTMNT}/tmpwkgfs.q #150415 add -F (force)
     if [ $? -eq 0 ];then
      ${PRE}busybox sync
      ${PRE}busybox mkdir -p ${TMPWKG_PARTMNT}/tmpmntpt.q
      ${PRE}busybox mount -o loop,rw -t ext2 ${TMPWKG_PARTMNT}/tmpwkgfs.q ${TMPWKG_PARTMNT}/tmpmntpt.q
      TMPWKG_MNTPT="${TMPWKG_PARTMNT}/tmpmntpt.q"
      TMPWKG_MNTPTflag=1
      return 0
     fi
    fi
    ${PRE}busybox killall popup
   fi
  fi
  ${PRE}busybox sync #redundant
  [ -f ${TMPWKG_PARTMNT}/tmpwkgfs.q ] && ${PRE}busybox rm -f ${TMPWKG_PARTMNT}/tmpwkgfs.q
  [ $TMPWKG_PARTMNTflag -eq 1 ] && ${PRE}busybox umount ${TMPWKG_PARTMNT}
  TMPWKG_MNTPT=""
  TMPWKG_MNTPTflag=0
  TMPWKG_PARTMNT=""
  TMPWKG_PARTMNTflag=0
 done
 return 1 #fail.
}

#151107 called from /usr/sbin/savesession...
save_session_frugal_func() {
  #151123 INSTALL_DEV, INSTALL_FS, INSTALL_FOLDER must be provide at entry this func.
  DEFASK='false'
  [ -s /var/local/frugal-save-ask-flag ] && DEFASK="$(cat /var/local/frugal-save-ask-flag)"
  
  HELPICON="gtk-index"
  M_close="`gettext 'Close'`"
  
  export SS_DLG9="<window title=\"$(gettext "Save Session")\" icon-name=\"gtk-convert\">
  <vbox>
   <text><label>$(gettext 'Saving the session will include all settings, downloaded files and installed packages.')</label></text>
   
   <frame $(gettext 'Information')>
    <text><label>\"$(gettext 'This is where the session will be saved')${CR}$(gettext 'Partition:') ${INSTALL_DEV}${CR}$(gettext 'Folder:') ${INSTALL_FOLDER}\"</label></text>
   </frame>
   
   <text><label>\"$(gettext 'Quirky is running in RAM, and sessions are not automatically saved. However, if you tick this checkbox, Quirky will ask whether you want to save at every shutdown.')${CR}$(gettext 'Note: the checkbox state will be saved even if you click the Cancel button below.')\"</label></text>
   <hbox><checkbox><variable>CHECKASK</variable><default>${DEFASK}</default><label>$(gettext 'Ask to save at every shutdown')</label></checkbox></hbox>
   
   <text use-markup=\"true\"><label>\"<b>$(gettext 'The <b>save</b> icon on the desktop may be clicked at any time that you wish to save a session.')</b>\"</label></text>
   <hbox>
    <text><label>$(gettext "Click to save session:")</label></text>
    <button><label>$(gettext 'SAVE')</label><action type=\"exit\">savenow</action></button>
   </hbox>
   
   <hbox>
    <button cancel></button>
   </hbox>
  </vbox>
  </window>"
  if [ $DISPLAY ];then #151210
   RETSTRING9="$(gtkdialog --program=SS_DLG9 --center)"
   [ $? -ne 0 ] && return 1
   eval "$RETSTRING9"
   echo -n "$CHECKASK" > /var/local/frugal-save-ask-flag
   [ "$EXIT" != "savenow" -a "$EXIT" != "othersave" ] && return 1
  else
   EXIT='savenow'
  fi
  
  if [ "$EXIT" == "savenow" ];then
   [ ! "$INSTALL_DEV" ] && return 1 #precaution.
   KEEPMNT=1
   DESTMNTPT="$(mount | grep "^/dev/${INSTALL_DEV} " | cut -f 3 -d ' ')"
   if [ "$DESTMNTPT" == "" ];then
    mkdir -p /mnt/${INSTALL_DEV}
    busybox mount -t ${INSTALL_FS} /dev/${INSTALL_DEV} /mnt/${INSTALL_DEV}
    if [ $? -eq 0 ];then
     DESTMNTPT="/mnt/${INSTALL_DEV}"
     KEEPMNT=0
    fi
   fi
   if [ "$DESTMNTPT" == "" ];then
    errTITLE="$(gettext 'Error saving session')"
    errMSG="$(gettext 'Sorry, unable to mount partition:') ${INSTALL_DEV}"
    [ $DISPLAY ] && popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
    EXIT="othersave"
   else
    WAITMSG="$(gettext 'Partition:') ${INSTALL_DEV}${CR}$(gettext 'Folder:') ${INSTALL_FOLDER}${CR}$(gettext 'Saving session, please wait...')"
    [ $DISPLAY ] && popup "placement=center background=#FFC080 level=top|<big>${WAITMSG}</big>" #daemonizes
    mkdir -p ${DESTMNTPT}/${INSTALL_FOLDER}
    rm -f ${DESTMNTPT}/${INSTALL_FOLDER}/s.sfs 2>/dev/null
    echo -e 'dev\nproc\nsys\ntmp\nroot/.XLOADED' > /tmp/ss-excl-dirs #151108 note, do want /var, /run symlinks to be saved.
    [ -d /file/tmpwkgdir ] && echo 'file/tmpwkgdir' >> /tmp/ss-excl-dirs #151108 refer find_working_place_func() above.
    [ -d /initrd/q_rw/initrd ] && echo 'initrd' >> /tmp/ss-excl-dirs #151205 otherwise aufs layers stuff things up.
    [ -d /initrd/q_rw/lost+found ] && echo 'lost+found' >> /tmp/ss-excl-dirs #151205
    if [ $DISPLAY ];then #151210
     urxvt -borderLess -geometry 80x3+0+0 -bg orange -fg black -title "$(gettext 'Building:') s.sfs $(gettext 'Folder:') ${DESTFOLDER}" -e mksquashfs /initrd/q_rw ${DESTMNTPT}/${INSTALL_FOLDER}/s.sfs -ef /tmp/ss-excl-dirs
    else
     mksquashfs /initrd/q_rw ${DESTMNTPT}/${INSTALL_FOLDER}/s.sfs -ef /tmp/ss-excl-dirs
    fi
    sync
    [ $DISPLAY ] && killall popup
   fi
   [ $KEEPMNT -eq 0 ] && busybox umount $DESTMNTPT
  fi
  return 0
}

###END###
