#!/bin/bash
#(c) Copyright Barry Kauler December 2013, bkhome.org
#Licence GPL3 (refer: /usr/share/doc/legal).
#150415 code removed from 'installquirky'.
#151006 ftp spider at ibiblio no longer works.
#151107 no longer using /var/local/frugal-save-remember-mntpt-folder
#151108 fallback finding q.sfs.
#151109 always want q.sfs to be compressed.
#151112 help button for entering save-folder.
#151127 replace BOOT_ with INSTALL_, ex BOOT_DEV with INSTALL_DEV
#151202 no longer using /etc/rc.d/install.log. tweaks.
#151213 no longer hardcode DEV1FS. see also /sbin/initNEW, /etc/rc.d/rc.sysinit, /usr/local/install_quirky/install-quirky-to-*
#160419 bung vmlinuz into q.sfs /boot, if isn't.
#160420 /etc/rc.d/install.log still in use, add INSTALLED_MODE=frugal.

#note, exported from installquirky: RXVTEXE ATADRIVES Qarch Qfw. ex: Qarch=amd64 Qfw=UEFI
export TEXTDOMAIN=installquirky
export OUTPUT_CHARSET=UTF-8

#150402 detect if running inside a self-extracted archive: 150414
WDcurrent="`pwd`"
PRE=''
if [ -f ${WDcurrent}/DISTRO_SPECS ];then
 [ -f ${WDcurrent}/gtkdialog ] || exit 1 #precaution.
 . ${WDcurrent}/DISTRO_SPECS
 . ${WDcurrent}/install-funcs
 PRE="${WDcurrent}/"
else
 . /etc/DISTRO_SPECS
 . /usr/local/install_quirky/install-funcs #has find_working_place_func
fi
DEF_FOLDER="${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}"

CR='
'
exit_func() { #150414 these variables set in find_working_place_func
 cd "$WDcurrent"
 ${PRE}busybox sync
 [ $TMPWKG_MNTPTflag ] && [ $TMPWKG_MNTPTflag -eq 1 ] && ${PRE}busybox umount $TMPWKG_MNTPT
 #[ "$TMPWKG_MNTPT" ] && [ -d $TMPWKG_MNTPT ] && ${PRE}busybox rm -rf $TMPWKG_MNTPT
 [ $TMPWKG_PARTMNTflag ] && [ $TMPWKG_PARTMNTflag -eq 1 ] && ${PRE}busybox umount $TMPWKG_PARTMNT
 [ $1 -eq 99 ] && return 0
 exit $1
}


COMBOITEMS2=""
PARTS="$(${PRE}probepart -k | ${PRE}busybox grep -E 'ext2|ext3|ext4|f2fs|minix|reiser|ntfs|msdos|fat' | ${PRE}busybox cut -f 3 -d '/')"
[ "$PARTS" == "" ] && exit
for APART in $PARTS
do
 DEVA="$(${PRE}busybox echo -n $APART | ${PRE}busybox cut -f 1 -d '|')"
 FSB="$(${PRE}busybox echo -n $APART | ${PRE}busybox cut -f 2 -d '|')"
 ONESIZEK="$(${PRE}busybox echo -n $APART | ${PRE}busybox cut -f 3 -d '|')"
 #code from pmount...
 if [ $ONESIZEK -gt 1048576 ];then #1024*1024
  ONESIZE="`LANG=C ${PRE}busybox dc $ONESIZEK 1048576 \/ p`" #150228
  ONESIZE="`LANG=C ${PRE}busybox printf "%.1f" $ONESIZE`G" #150228
 else
  if [ $ONESIZEK -gt 99 ];then
   ONESIZE="`${PRE}busybox expr $ONESIZEK \/ 1024`M"
  else
   ONESIZE="`LANG=C ${PRE}busybox dc $ONESIZEK 1024 \/ p`" #150228
   ONESIZE="`LANG=C ${PRE}busybox printf "%.1f" $ONESIZE`M" #150228
  fi
 fi
 [ "$ONESIZE" = "0.0M" ] && ONESIZE=""
 COMBOITEMS2="${COMBOITEMS2}<item>${DEVA} (${FSB}, ${ONESIZE})</item>"
done

itemDL='<item>Download</item>'
export IQ_DLG2="<window title=\"$(gettext "Quirky Installer: frugal")\" icon-name=\"gtk-convert\">
<vbox>
 <text><label>$(gettext 'You need to specify where the Quirky files are, that are to be installed.')</label></text>
 <text use-markup=\"true\"><label>\"$(gettext "This can be a <b>live-CD</b>, a <b>.iso</b> file, or a folder that has the <b>vmlinuz</b>, <b>q.sfs</b> and <b>initrd.q</b> files.")\"</label></text>
 <text use-markup=\"true\"><label>\"<big>$(gettext 'If you have not yet downloaded the Quirky ISO file, select <b>Download</b> from the drop-down list.')</big>\"</label></text>
 <text><label>$(gettext 'Please choose where the Quirky files are:')</label></text>
 <hbox><combobox><variable>COMBO1</variable><item>CD/DVD</item><item>ISO file</item><item>Folder</item>${itemDL}</combobox></hbox>
 <hbox>
  <button><label>$(gettext "CONTINUE")</label><action type=\"exit\">continuefrugal</action></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>"
RETSTRING2="$(${PRE}gtkdialog --program=IQ_DLG2 --center)"
[ $? -ne 0 ] && exit
eval "$RETSTRING2"
[ "$EXIT" != "continuefrugal" ] && exit
    
DEFPATH='/file/downloads'
if [ ! -d /file/downloads ];then
 DEFPATH="/root/Downloads"
 mkdir -p /root/Downloads
fi

###find a working space###
find_working_place_func 1200 xxx #150414 pass reqd space, exclude-devs. returns TMPWKG_MNTPT
if [ $? -ne 0 ];then
 errTITLE="$(gettext 'Insufficient working space')"
 errMSG="$(gettext 'Unable to find sufficient free working space. Aborting installation.')"
 ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
 exit_func 1
fi
cd $TMPWKG_MNTPT

case "$COMBO1" in
 CD/DVD)
  SRCFLG='cd'
  SRCXML="<text use-markup=\"true\"><label>\"<b>$(gettext 'Please insert the Quirky CD or DVD, if not already inserted'). Do it now.</b>\"</label></text>"
 ;;
 "ISO file")
  SRCFLG='iso'
  SRCXML="<text><label>$(gettext 'You have chosen to install from an ISO file.')</label></text>
   <text><label>$(gettext 'Please choose the .iso file:')</label></text>
   <chooser><variable>CHOOSE1</variable><default>${DEFPATH}</default><width>600</width><height>400</height></chooser>"
 ;;
 Folder)
  SRCFLG='dir'
  SRCXML="<text><label>$(gettext 'You have chosen to install from a folder that has the Quirky files.')</label></text>
   <text use-markup=\"true\"><label>$(gettext "Find the folder that has <b>vmlinuz</b>, <b>q.sfs</b> and <b>initrd.q</b>:")</label></text>
   <chooser><variable>CHOOSE1</variable><default>/</default><width>600</width><height>400</height></chooser>"
 ;;
 *) #150322 Download
  ${PRE}busybox ping -4 -c 1 www.google.com
  if [ $? -ne 0 ];then
   ${PRE}popup "terminate=ok background='#FF8080'|<b>$(gettext 'Download ISO: error')</b>${CR}$(gettext 'Sorry, there is no Internet connection. Aborting.')"
   exit_func 1
  fi
  ${PRE}popup "level=top background='#FFE000'|$(gettext 'Please wait, searching online...')"
  #DL_URL="ftp://distro.ibiblio.org/quirky/quirky6/${DISTRO_TARGETARCH}/releases" #150414
  DL_URL="http://distro.ibiblio.org/quirky/quirky6/${DISTRO_TARGETARCH}/releases" #151006
  #ISOs="$(LANG=C wget -r -4 -t 2 -T 20 --waitretry=20 --spider -S --no-parent --no-directories -A '*.iso' ${DL_URL}/ 2>&1 | grep -o "${DL_URL}/.*\.iso$" | sort -u)" #ex: ftp://distro.ibiblio.org/quirky/quirky6/amd64/releases/april-7.0.2/april64-7.0.2.iso
  #151006...
  ISOs="$(LANG=C wget -r -4 -t 2 -T 20 --waitretry=20 --spider -S --no-parent --no-directories -A '*.iso' --recursive --level=2 ${DL_URL}/ 2>&1 | grep -o "${DL_URL}/.*\.iso$" | grep '^http://' | sort -u)" #ex: http://distro.ibiblio.org/quirky/quirky6/amd64/releases/april-7.0.2/april64-7.0.2.iso
  ${PRE}busybox killall popup
  if [ ! "$ISOs" ];then
   ${PRE}popup "level=top background='#FF8080' terminate=ok|$(gettext 'Could not find any ISO files. Maybe ibiblio.org is down. Aborting.')"
   exit_func 1
  fi
  ITEMSISO=''
  for aISO in `${PRE}busybox echo -n "$ISOs" | ${PRE}busybox tr '\n' ' '`
  do
   aITEM="$(${PRE}busybox basename $aISO)"
   #if [ "$DISTRO_VERSION" ];then
   # aVER="$(echo -n $aITEM | cut -f 2 -d '-' | rev | cut -f 2- -d '.' | rev)"
   # if vercmp $aVER lt $DISTRO_VERSION;then
   #  continue
   # fi
   #fi
   ITEMSISO="${ITEMSISO}<item>${aITEM}</item>"
  done
  export IQ_DLG_DL_ISO="<window title=\"$(gettext "Quirky Installer: frugal")\" icon-name=\"gtk-convert\">
  <vbox>
   <text><label>$(gettext 'Online ISO files have been found. You need to choose the most appropriate one to download.')</label></text>
   <hbox>
    <text><label>$(gettext 'Choose the ISO file:')</label></text>
    <combobox><variable>COMBODL</variable>${ITEMSISO}</combobox>
   </hbox>
   <hbox>
    <button><label>$(gettext "DOWNLOAD")</label><action type=\"exit\">downloadiso</action></button>
    <button cancel></button>
   </hbox>
  </vbox>
  </window>"
  RETSTRINGDL="$(${PRE}gtkdialog --program=IQ_DLG_DL_ISO --center)"
  [ $? -ne 0 ] && exit_func 0
  eval "$RETSTRINGDL"
  [ "$EXIT" != "downloadiso" ] && exit_func 0
  URLiso="$(${PRE}busybox echo "$ISOs" | ${PRE}busybox grep "${COMBODL}$" | ${PRE}busybox head -n 1)"
  URLdir="$(${PRE}busybox dirname $URLiso)"
  [ -f ./sha1sums.txt ] && rm -f ./sha1sums.txt
  if [ `which download_file` ];then #150323 in most puppies.
   download_file ${URLdir}/sha1sums.txt
  else
   LANG=C wget -4 ${URLdir}/sha1sums.txt
  fi
  if [ $? -ne 0 ];then
   ${PRE}popup "level=top background='#FF8080' terminate=ok|$(gettext 'Could not download ISO file. Aborting.')"
   exit_func 1
  fi
  [ -f ./${COMBODL} ] && [ ! -d ./${COMBODL} ] && rm -f ./${COMBODL}
  if [ `which download_file` ];then #150323 in most puppies.
   download_file $URLiso
  else
   DLmsg="$(gettext 'Downloading:')" #150414
   LANG=C ${RXVTEXE} -geometry 80x3+0+0 -bg orange -fg black -title "${DLmsg} ${URLiso}" -e wget -4 $URLiso #150414
  fi
  ${PRE}busybox sync
  SHA1SUMa="$(${PRE}busybox grep -w "$COMBODL" ./sha1sums.txt | ${PRE}busybox cut -f 1 -d ' ')"
  SHA1SUMb="$(${PRE}busybox sha1sum ./${COMBODL} | cut -f 1 -d ' ')"
  if [ "$SHA1SUMa" != "$SHA1SUMb" ];then
   ${PRE}popup "level=top background='#FF8080' terminate=ok|$(gettext 'ISO downloaded, but checksum is wrong. Aborting.')"
   rm -f ./sha1sums.txt
   rm -f ./${COMBODL}
   exit_func 1
  fi
  SRCFLG='iso'
  PWD2="`pwd`"
  SRCXML="<text><label>$(gettext 'You have chosen to install from an ISO file.')</label></text>
   <text><label>$(gettext 'The file has been downloaded:') ${PWD2}/${COMBODL}</label></text>"
  CHOOSE1="${PWD2}/${COMBODL}"
 ;;
esac

#151112
HELPICON="gtk-index"
M_close="`gettext 'Close'`"
H_folder="$(gettext 'Type any unique name for the folder, without spaces. It must be a name that does not already exist on the partition.')${CR}$(gettext 'You may also specify a path, for example <b>quirky/werewolf-7.3</b>, and the path will be created if it does not exist.')${CR}${CR}$(gettext 'Note1: A leading <b>/</b> will be removed if you type it in.')${CR}$(gettext 'Note2: If you type spaces, they will be removed.')${CR}$(gettext 'Note3: Currently only 2-deep is supported. So, quirky/saves/werewolf-7.3 is not supported.')"
export DLG_HELP_FOLDER="<window resizable=\"false\" title=\"$(gettext 'Help: save folder')\" icon-name=\"${HELPICON}\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${H_folder}\"</label><variable>DLG_HELP_FOLDER</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP_FOLDER</action></button></hbox></vbox></window>"
  
export IQ_DLG2b="<window title=\"$(gettext "Quirky Installer: frugal")\" icon-name=\"gtk-convert\">
<vbox>
 <frame $(gettext 'Source')>
  ${SRCXML}
 </frame>
 <frame $(gettext 'Target')>
  <hbox>
   <text><label>$(gettext 'Choose the partition to install Quirky to:')</label></text>
   <combobox><variable>COMBO2</variable>${COMBOITEMS2}</combobox>
  </hbox>
  <hbox>
   <text><label>$(gettext 'Type a name that you want for the folder:')</label></text>
   <entry><default>${DEF_FOLDER}</default><variable>DESTFOLDER</variable></entry>
   <button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP_FOLDER</action></button>
  </hbox>
 </frame>
 <hbox>
  <button><label>$(gettext "CONTINUE")</label><action type=\"exit\">continuefrugal</action></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>"
RETSTRING2b="$(${PRE}gtkdialog --program=IQ_DLG2b --center)"
[ $? -ne 0 ] && exit_func 0
eval "$RETSTRING2b"
[ "$EXIT" != "continuefrugal" ] && exit_func 0
DESTFOLDER="$(${PRE}busybox echo -n "$DESTFOLDER" | ${PRE}busybox tr -d ' ')" #151108 remove spaces.
DESTFOLDER="$(${PRE}busybox echo -n "$DESTFOLDER" | ${PRE}busybox sed -e 's%^/%%')" #151112 remove a leading /
DESTFOLDER="$(${PRE}busybox echo -n "$DESTFOLDER" | ${PRE}busybox sed -e 's%/.*/%/%g')" #151202 remove multiple /

if [ "$SRCFLG" == "cd" ];then
 #find the CD...
 OPTICALS="$(${PRE}probedisk | ${PRE}busybox grep 'optical' | ${PRE}busybox cut -f 1 -d '|' | ${PRE}busybox cut -f 3 -d '/')"
 if [ "$OPTICALS" == "" ];then
  errMSG="
$(gettext 'Sorry, no optical drives found. Did you insert the CD?')"
  ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${errMSG}"
  exit_func 1
 fi
 for AOPTICAL in $OPTICALS
 do
  [ "$AOPTICAL" == "" ] && continue
  MNTPTsrc="$(${PRE}busybox mount | ${PRE}busybox grep "^/dev/${AOPTICAL} " | ${PRE}busybox cut -f 3 -d ' ')"
  [ "$MNTPTsrc" != "" ] && break
  ${PRE}busybox mkdir -p /mnt/${AOPTICAL}
  ${PRE}busybox mount -t iso9660 /dev/${AOPTICAL} /mnt/${AOPTICAL}
  if [ $? -eq 0 ];then
   MNTPTsrc="/mnt/${AOPTICAL}"
   break
  fi
 done
 if [ "$MNTPTsrc" == "" ];then
  errMSG="$(gettext 'Sorry, no optical media found.')"
  ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
  exit_func 1
 fi
 if [ ! -f ${MNTPTsrc}/initrd.q ];then
  errMSG="$(gettext 'Sorry, this is not a Quirky live-CD.')"
  ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
  ${PRE}busybox umount $MNTPTsrc
  exit_func 1
 fi
else
 #CHOOSE1 has the src file
 [ -d "$CHOOSE1" ] && MNTPTsrc="$CHOOSE1"
 [ -f "$CHOOSE1" ] && MNTPTsrc="$(${PRE}busybox dirname "$CHOOSE1")"
 if [ "$SRCFLG" == "iso" ];then
  FILENAME="$(${PRE}busybox basename "$CHOOSE1")"
  EXT="$(${PRE}busybox echo -n "$FILENAME" | ${PRE}busybox rev | ${PRE}busybox cut -c 1,2,3,4 | ${PRE}busybox rev)"
  if [ "$EXT" != ".iso" -a "$EXT" != ".ISO" ];then
   errMSG="$(gettext 'Sorry, this is not an ISO file:') ${FILENAME}"
   ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
   exit_func 1
  fi
  ${PRE}busybox mkdir -p /mnt/iso
  ${PRE}busybox mount -t iso9660 "$CHOOSE1" /mnt/iso #-o loop not needed.
  if [ $? -ne 0 ];then
   errMSG="$(gettext 'Sorry, unable to mount:') ${FILENAME}"
   ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
   exit_func 1
  fi
  MNTPTsrc="/mnt/iso"
     
 else
  FILENAME='initrd.q'
  if [ ! -f "$MNTPTsrc"/initrd.q ];then
   errMSG="$(gettext 'You chose folder:') ${MNTPTsrc}${CR}$(gettext "Sorry, this does not have the 'vmlinuz', 'q.sfs' and 'initrd.q' files.")"
   ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
   exit_func 1
  fi
 fi
fi

#copy files to destination...
DESTDEV="$(${PRE}busybox echo -n "$COMBO2" | ${PRE}busybox cut -f 1 -d ' ')"
DESTFS="$(${PRE}busybox echo -n "$COMBO2" | ${PRE}busybox cut -f 2 -d '(' | ${PRE}busybox cut -f 1 -d ',')"
DESTMNTPT="$(${PRE}busybox mount | ${PRE}busybox grep "^/dev/${DESTDEV} " | ${PRE}busybox cut -f 3 -d ' ')"
if [ "$DESTMNTPT" == "" ];then
 ${PRE}busybox mkdir -p /mnt/${DESTDEV}
 ${PRE}busybox mount -t ${DESTFS} /dev/${DESTDEV} /mnt/${DESTDEV}
 [ $? -eq 0 ] && DESTMNTPT="/mnt/${DESTDEV}"
fi
if [ "$DESTMNTPT" == "" ];then
 errMSG="$(gettext 'Sorry, unable to mount partition:') ${DESTDEV}"
 ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
 exit_func 1
fi
WAITMSG="$(gettext 'Copying files, please wait...')"
${PRE}popup "placement=center background=#FFC080 level=top|<big>${WAITMSG}</big>" #daemonizes
${PRE}busybox mkdir -p ${DESTMNTPT}/${DESTFOLDER}
${PRE}busybox cp -a -f ${MNTPTsrc}/vmlinuz ${DESTMNTPT}/${DESTFOLDER}/
${PRE}busybox cp -a -f ${MNTPTsrc}/initrd.q ${DESTMNTPT}/${DESTFOLDER}/
RET1=$?
#150323 test if src folder already has q.sfs extracted from initrd.q:
[ -f ${MNTPTsrc}/q.sfs ] && ${PRE}busybox cp -a -f ${MNTPTsrc}/q.sfs ${DESTMNTPT}/${DESTFOLDER}/
${PRE}busybox sync
if [ $RET1 -eq 0 ];then
 #need to remove "install" icon off desktop...
     
 #well, TMPWKG_MNTPT is already the current dir.
 if [ $RET1 -eq 0 ];then
  ${PRE}busybox rm -rf ${TMPWKG_MNTPT}/initrd-tree 2>/dev/null
  ${PRE}busybox mkdir ${TMPWKG_MNTPT}/initrd-tree
  cd ${TMPWKG_MNTPT}/initrd-tree #no busybox 'cd' applet.
  ${PRE}busybox cat ${DESTMNTPT}/${DESTFOLDER}/initrd.q | ${PRE}busybox cpio -i -d -m
  ${PRE}busybox sync
  if [ -f ./q.sfs ];then #150323 refer up a bit.
   [ -f ${DESTMNTPT}/${DESTFOLDER}/q.sfs ] && ${PRE}busybox rm -f ${DESTMNTPT}/${DESTFOLDER}/q.sfs
   ${PRE}busybox mv -f q.sfs ${DESTMNTPT}/${DESTFOLDER}/q.sfs #150317 move q.sfs outside of the initrd.
   ${PRE}busybox sync
  fi
  if [ ! -f ${DESTMNTPT}/${DESTFOLDER}/q.sfs ];then
   if [ -f /initrd/q_ro/q.sfs ];then #151108 fallback. this file exists in running live-cd or frugal.
    ${PRE}busybox cp -a /initrd/q_ro/q.sfs ${DESTMNTPT}/${DESTFOLDER}/
    ${PRE}busybox sync
   else
    ${PRE}popup "background=#FF8080 terminate=ok process=wait|Partition: ${DESTDEV}${CR}Folder: ${DESTFOLDER}${CR}Something has gone wrong, q.sfs is not in the folder. Aborting."
    exit_func 1
   fi
  fi
  #151108 do in terminal...
  [ -d ${TMPWKG_MNTPT}/q ] && ${PRE}busybox rm -rf ${TMPWKG_MNTPT}/q #precaution.
  ${RXVTEXE} -borderLess -geometry 80x3+0+0 -bg orange -fg black -title "$(gettext 'Expanding:') q.sfs $(gettext 'Folder:') ${TMPWKG_MNTPT}/q" -e ${PRE}unsquashfs -p 1 -d ${TMPWKG_MNTPT}/q ${DESTMNTPT}/${DESTFOLDER}/q.sfs
  sync
  RET1=0 #151108
  [ ! -d ${TMPWKG_MNTPT}/q/audit ] && RET1=1 #151108
  if [ $RET1 -eq 0 ];then
   #see also 7build-live-cd:
   ${PRE}busybox sed -i '/installquirky/d' ${TMPWKG_MNTPT}/q/root/Choices/ROX-Filer/PuppyPin #remove "install" icon
   
   #fix f.s. (as in service_pack's)... should be ok, but do as precaution... 151213 no longer required...
   #PART2FS="ext2" #zram
   #tPTN1="s%.*MARK_DEV1FS_LINE.*%DEV1FS='${PART2FS}' #MARK_DEV1FS_LINE %"
   #${PRE}busybox sed -i -e "$tPTN1" ${TMPWKG_MNTPT}/q/sbin/init
   #${PRE}busybox sed -i -e "$tPTN1" ${TMPWKG_MNTPT}/q/etc/rc.d/rc.sysinit
   
   #160419 bung vmlinuz into q.sfs, if isn't...
   [ ! -f ${TMPWKG_MNTPT}/q/boot/vmlinuz ] && [ -f ${DESTMNTPT}/${DESTFOLDER}/vmlinuz ] && ${PRE}busybox cp -a ${DESTMNTPT}/${DESTFOLDER}/vmlinuz ${TMPWKG_MNTPT}/q/boot/
   #160420...
   echo 'INSTALLED_MODE=frugal' >> ${TMPWKG_MNTPT}/q/etc/rc.d/install.log
   
#   #set default for file-chooser in 'savesession' script: 150317 fix:
#   ${PRE}busybox echo -n "${DESTMNTPT}/${DESTFOLDER}" > ${TMPWKG_MNTPT}/q/var/local/frugal-save-remember-mntpt-folder
   #get some target variables:
   ${PRE}busybox sed -e 's%^DISTRO_%INSTALL_%' ${TMPWKG_MNTPT}/q/etc/DISTRO_SPECS > /tmp/iq_distro_specs
   . /tmp/iq_distro_specs
   ${PRE}busybox sync
   #now put it together again...
   ${PRE}busybox rm -f ${DESTMNTPT}/${DESTFOLDER}/q.sfs
   #mksquashfs ${TMPWKG_MNTPT}/q q.sfs -comp xz -processors 1
   #150219 removed  "-processors 1"
   #150317 do not compress (removed "-comp xz", instead "-noI -noD -noF -noX -no-exports")...
   ${RXVTEXE} -borderLess -geometry 80x3+0+0 -bg orange -fg black -title "$(gettext 'Rebuilding:') q.sfs $(gettext 'Folder:') ${DESTFOLDER}" -e ${PRE}mksquashfs ${TMPWKG_MNTPT}/q ${DESTMNTPT}/${DESTFOLDER}/q.sfs #151109 -noI -noD -noF -noX -no-exports
   ${PRE}busybox sync
   ${PRE}busybox rm -rf ${TMPWKG_MNTPT}/q
   ${PRE}busybox rm -f ${DESTMNTPT}/${DESTFOLDER}/initrd.q 2> /dev/null
   #150317 init script in initrd will read this:
   ${PRE}busybox echo -e "INSTALL_DEV=${DESTDEV}\nINSTALL_FS=${DESTFS}\nINSTALL_FOLDER=${DESTFOLDER}" > INSTALL_SPECS
   ${PRE}busybox sync
   ${PRE}busybox find . | ${PRE}busybox cpio -o -H newc > ${DESTMNTPT}/${DESTFOLDER}/initrd.q  #.cpio
   ${PRE}busybox sync
  fi
  cd ..
  ${PRE}busybox rm -rf initrd-tree
  cd $WDcurrent
 fi
fi
${PRE}busybox sync
${PRE}busybox umount ${DESTMNTPT}
${PRE}busybox umount ${MNTPTsrc}
exit_func 99 #do not exit.
${PRE}busybox killall popup
    
if [ $RET1 -ne 0 ];then
 errMSG="$(gettext 'Sorry, there was an error. Aborting.')"
 ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
 exit 1
fi
    
#some gymnastics for working out grub1 drive numbers...
DRV_TYPE="`${PRE}busybox echo -n "$DESTDEV" | ${PRE}busybox cut -b 1-3`" #ex: sda
GRUB_DRVN=0
for ONE_TYPE in `${PRE}fdisk -l | ${PRE}busybox grep "^Disk /dev/" | ${PRE}busybox cut -d' ' -f2 | ${PRE}busybox cut -d':' -f1 | ${PRE}busybox tr '\n' ' '`
do
 [ "/dev/$DRV_TYPE" = "$ONE_TYPE" ] && break
 GRUB_DRVN=`${PRE}busybox expr $GRUB_DRVN + 1`
done
GRUB_PARTN=`${PRE}busybox echo -n "$DESTDEV" | ${PRE}busybox cut -b 4-5`
if [ $GRUB_PARTN ];then
 GRUB_PARTN=`${PRE}busybox expr $GRUB_PARTN - 1`
 GRUB_PARTN=",$GRUB_PARTN"
fi
GRUB1="title ${INSTALL_NAME} ${INSTALL_VERSION} frugal in ${DESTDEV} dir ${DESTFOLDER}
  rootnoverify (hd${GRUB_DRVN}${GRUB_PARTN})
  kernel /${DESTFOLDER}/vmlinuz rootwait rw
  initrd /${DESTFOLDER}/initrd.q"

GRUB4DOSa="title ${INSTALL_NAME} ${INSTALL_VERSION} frugal in ${DESTDEV} dir ${DESTFOLDER}
  find --set-root --ignore-floppies /${DESTFOLDER}/initrd.q
  kernel /${DESTFOLDER}/vmlinuz rootwait rw
  initrd /${DESTFOLDER}/initrd.q"
  
#150303
FSUUID="$(${PRE}busybox blkid /dev/${DESTDEV} | ${PRE}busybox grep -o 'UUID="[^"]*' | ${PRE}busybox cut -f 2 -d '"')" #'
GRUB4DOSb="title ${INSTALL_NAME} ${INSTALL_VERSION} frugal in ${DESTDEV} dir ${DESTFOLDER}
  uuid ${FSUUID}
  kernel /${DESTFOLDER}/vmlinuz rw
  initrd /${DESTFOLDER}/initrd.q"
  
export IQ_DLG3="<window title=\"$(gettext "Quirky Installer: frugal")\" icon-name=\"gtk-convert\">
<vbox>
 <text use-markup=\"true\" justify=\"0\"><label>\"<b>$(gettext 'Success! Quirky has been installed in frugal mode')</b>\"</label></text>
 <hbox>
  <vbox>
   <text><label>\" \"</label></text>
   <text xalign=\"0\" justify=\"0\"><label>$(gettext 'Partition:') ${DESTDEV}  $(gettext 'Folder:') ${DESTFOLDER}</label></text>
   <text xalign=\"0\" justify=\"0\"><label>$(gettext 'You will need to make appropriate entries in the boot manager. Suggestions are given alongside.')</label></text>
   <text xalign=\"0\" justify=\"0\"><label>Press TAB key to highlight a suggestion, then CTRL-C to copy to clipboard. CTRL-V to paste into boot manager.</label></text>
   <text xalign=\"0\" justify=\"0\"><label>Please copy and paste before closing this window!</label></text>
  </vbox>
  <vbox>
   <frame>
    <text xalign=\"0\" justify=\"0\"><label>$(gettext 'GRUB V1:')</label></text>
    <text use-markup=\"true\" selectable=\"true\"><label>\"<span bgcolor='white'>${GRUB1}</span>\"</label></text>
    <text xalign=\"0\" justify=\"0\"><label>$(gettext 'GRUB4DOS:')</label></text>
    <text use-markup=\"true\" selectable=\"true\"><label>\"<span bgcolor='white'>${GRUB4DOSa}</span>\"</label></text>
    <text xalign=\"0\" justify=\"0\"><label>$(gettext 'Alternative for GRUB4DOS:')</label></text>
    <text use-markup=\"true\" selectable=\"true\"><label>\"<span bgcolor='white'>${GRUB4DOSb}</span>\"</label></text>
   </frame>
  </vbox>
 </hbox>
 <hbox><button ok></button></hbox>
</vbox>
</window>"
${PRE}gtkdialog --program=IQ_DLG3 --center


###END###
