#!/bin/bash
#(c) Copyright Barry Kauler July 2017, barryk.org
#Licence GPL3 (refer: /usr/share/doc/legal).
#170722 first release. based on second half of previous install-quirky-to-frugal
#170727 path fix.
#171109 non-english fdisk breaks script.

#note, exported from installquirky: RXVTEXE ATADRIVES Qarch Qfw. ex: Qarch=amd64 Qfw=UEFI
export TEXTDOMAIN=installquirky
export OUTPUT_CHARSET=UTF-8
[ ! "$RXVTEXE" ] && RXVTEXE='urxvt'

INSTMODE="frugal"
INSTMODEtr="$(gettext 'frugal')"
REQDSPACE=1200

###source files###
if [ -e ./get-quirky-src ];then #170727
. ./get-quirky-src
else
 if [ -e /usr/local/install_quirky/get-quirky-src ];then
. /usr/local/install_quirky/get-quirky-src
 fi
fi
#...exit with MNTPTsrc path to quirky source files vmlinuz, initrd.q, q.sfs
#...also current dir set to $TMPWKG_MNTPT

###choose target###
#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>"
TARGETXML="  <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>"

export IQ_DLG2bb="<window title=\"$(gettext "Quirky Installer: frugal")\" icon-name=\"gtk-convert\">
<vbox>
 <frame $(gettext 'Target')>
  ${TARGETXML}
 </frame>
 <hbox>
  <button><label>$(gettext "CONTINUE")</label><action type=\"exit\">continuefrugal</action></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>"
RETSTRING2b="$(${PRE}gtkdialog --program=IQ_DLG2bb --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 /


#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.
  echo "Executing: unsquashfs -p 1 -d ${TMPWKG_MNTPT}/q ${DESTMNTPT}/${DESTFOLDER}/q.sfs"
  ${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 #170722 very slow, do not sync
  RET1=0 #151108
  [ ! -d ${TMPWKG_MNTPT}/q/audit ] && RET1=1 #151108
  if [ $RET1 -eq 0 ];then
   #see also 7build-live-cd:
   echo "Modifying: ${TMPWKG_MNTPT}/q"
   ${PRE}busybox sed -i '/installquirky/d' ${TMPWKG_MNTPT}/q/root/Choices/ROX-Filer/PuppyPin #remove "install" icon
   
   #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 #170722 do not 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")...
   echo "Executing: mksquashfs ${TMPWKG_MNTPT}/q ${DESTMNTPT}/${DESTFOLDER}/q.sfs"
   ${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
   #170722 sync is slow, try fsync...
   #${PRE}busybox sync
   ${PRE}busybox fsync ${DESTMNTPT}/${DESTFOLDER}/q.sfs
   echo "Removing: ${TMPWKG_MNTPT}/q"
   ${PRE}busybox rm -rf ${TMPWKG_MNTPT}/q
   echo "Reconstructing: ${DESTMNTPT}/${DESTFOLDER}/initrd.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 ..
  echo "removing: ${TMPWKG_MNTPT}/initrd-tree"
  ${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 `LANG=C ${PRE}fdisk -l | ${PRE}busybox grep "^Disk /dev/" | ${PRE}busybox cut -d' ' -f2 | ${PRE}busybox cut -d':' -f1 | ${PRE}busybox tr '\n' ' '` #171109
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###
