#!/bin/bash
#(c) Copyright Barry Kauler December 2013, bkhome.org
#Licence GPL3 (refer: /usr/share/doc/legal).
#131219 installs quirky 6.x to a hard drive partition.
#131227 no longer put image-file into target.
#131231 vfat is ok as working partition.
#140108 version sort.
#140120 work in new quirky build system.
#140123 if installed to a removable drive, the grub "root=/dev/$TARGETPART" is not adequate.
#140212 install .sfs of deployed .usfs.xz, to /audit/reference
#140213 record date of creation of reference .sfs
#140213 merged install-quirky-to-partition.sh into 4install-quirky-to-partition
#141021 code was in wrong place.
#150109 check that 'sort' supports --version-sort option.
#150129 bump minimum recommended size from 2GB to 8GB.
#150225 mksquashfs use only 1 core.
#150402 can run inside self-extracting-archive. called from installquirky. gui.
#150406 fixes.
#150414 install-funcs. establish working area with sufficient space.
#150414 online files now in 'releases' folder. fix LANG=C
#150417 fix LD_PRELOAD
#151004 now exporting LD_PRELOAD in script 'installquirky' (which calls this one).
#151006 ftp spider at ibiblio no longer works.
#151007 remove unnecessary popup.
#151007 install failing when host is precise 5.7.1. reason: cp: /lib/libc.so.6: version `GLIBC_2.17' not found (detected in 'installquirky')
#151007 improve unmounting and temp-file removal at exit.
#151116 render combobox wider.
#151202 no longer using /etc/rc.d/install.log. note, Qarch, Qfw hardcoded below.
#151209 do not want "install" and "save" icons on desktop with full install.
#151213 no longer hardcode DEV1FS. see also /sbin/initNEW, /etc/rc.d/rc.sysinit, /usr/local/install_quirky/install-quirky-to-*
#160419 iso builds no longer have vmlinuz in /boot in q.sfs.
#160420 /etc/rc.d/install.log still in use, add INSTALLED_MODE=full.

#150402 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
fi

CR='
'

#151202 may need to ask in future...
Qarch='amd64'
Qfw='UEFI'
export Qarch Qfw

##150417 fix LD_PRELOAD, want actual file, not symlink...
#if [ "$PRE" ];then
# LIBLZMA="$(find ${PRE} -mindepth 1 -maxdepth 1 -type f -name 'liblzma.so.*' | head -n 1)"
#else
# LIBLZMA="$(find /usr/lib -mindepth 1 -maxdepth 1 -type f -name 'liblzma.so.*' | head -n 1)"
#fi

DELME1=""; DELME2="" #151007
exit_func() { #150414 these variables set in find_working_place_func
 [ "$DELME1" ] && ${PRE}busybox rm -f $DELME1 2>/dev/null #151007 this is copied .usfs.xz file, $IMGFILEZ.
 cd "$WDcurrent"
 ${PRE}busybox sync
 if [ "$TMPWKG_MNTPT" ];then #151007
  if [ -d ${TMPWKG_MNTPT}/mntpt ];then
   if [ "$(${PRE}busybox mount | ${PRE}busybox cut -f 3 -d ' ' | ${PRE}busybox grep "^${TMPWKG_MNTPT}/mntpt$")" != "" ];then
    ${PRE}busybox umount ${TMPWKG_MNTPT}/mntpt
   fi
  fi
 fi
 [ "$DELME2" ] && ${PRE}busybox rm -f $DELME2 2>/dev/null #151007 this is expanded .usfs.xz file, $IMGFILE.
 [ $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
}

###choose target partition###
M_00="$(gettext 'Quirky will be downloaded and installed to a partition on a hard drive.')"
M_01="$(gettext 'Any kind of hard drive is acceptable, including conventional magnetic platter or SSD (Solid State Disk). Removable drives, such as USB drives, are also acceptable. The partition must ALREADY have a filesystem. For normal magnetic media, ext2, ext3, or ext4 is recommended. For SSD, f2fs is recommended.')"
M_02="$(gettext 'The partition will need to be at least 8GB in size, and all of the partition will be erased.')"
M_03="$(gettext 'If you do not have a suitable available partition, abort this script!')"

PITEMS=""
ALLPARTS="$(${PRE}probepart -k | ${PRE}busybox grep -E 'ext2|ext3|ext4|f2fs|reiserfs|btrfs|minix|xfs' | ${PRE}busybox sed -e 's%^/dev/%%' | ${PRE}busybox tr '\n' ' ')"
for APART in $ALLPARTS
do
 ADEV="$(${PRE}busybox echo -n "$APART" | ${PRE}busybox cut -f 1 -d '|')"
 AFS="$(${PRE}busybox echo -n "$APART" | ${PRE}busybox cut -f 2 -d '|')"
 ASIZEK="$(${PRE}busybox echo -n "$APART" | ${PRE}busybox cut -f 3 -d '|')"
 if [ $ASIZEK -gt 1048576 ];then #1024*1024
  xONESIZE="`LANG=C ${PRE}busybox dc $ASIZEK 1048576 \/ p`"
  ONESIZE="`LANG=C ${PRE}busybox printf "%.1f" $xONESIZE`G"
 else
  if [ $ASIZEK -gt 99 ];then
   ONESIZE="`${PRE}busybox expr $ASIZEK \/ 1024`M"
  else
   xONESIZE="`LANG=C ${PRE}busybox dc $ASIZEK 1024 \/ p`"
   ONESIZE="`LANG=C ${PRE}busybox printf "%.1f" $xONESIZE`M"
  fi
 fi
 PITEMS="${PITEMS}<item>${ADEV} FS:${AFS} SIZE:${ONESIZE}</item>"
done
if [ "$PITEMS" == "" ];then
 errMSG="$(gettext 'Could not find any partitions to install to. A partition must have ext2, ext3, ext4, reiserfs, f2fs, btrfs or minix filesystem. Aborting install.')"
 ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>$(gettext 'ERROR: no partitions found')</b>${CR}${errMSG}"
 exit
fi
while [ true ];do ###
 export IQ_DLGp="<window title=\"$(gettext "Quirky Installer: partition")\" icon-name=\"gtk-convert\">
 <vbox>
  <text><label>${M_00}</label></text>
  <text><label>${M_01}</label></text>
  <text><label>${M_02}</label></text>
  <text><label>${M_03}</label></text>
  <frame $(gettext 'Choose partition')>
   <text use-markup=\"true\"><label>\"<b>$(gettext 'Choose partition to install to:')</b>\"</label></text>
   <text><label>$(gettext 'Please be careful to choose the correct partition. It must be at least 8GB in size.')</label></text>
   <hbox><vbox width-request=\"260\"><combobox><variable>COMBOp</variable>${PITEMS}</combobox></vbox></hbox>
  </frame>
  <hbox>
   <button><label>$(gettext 'CONTINUE')</label><action type=\"exit\">continue</action></button>
   <button cancel></button>
  </hbox>
 </vbox>
 </window>"
 RETSTRINGp="$(${PRE}gtkdialog --program=IQ_DLGp --center)"
 [ $? -ne 0 ] && exit
 eval "$RETSTRINGp"
 [ "$EXIT" != "continue" ] && exit
 TARGETPART="$(${PRE}busybox echo -n "$COMBOp" | ${PRE}busybox cut -f 1 -d ' ')"
 TARGETFS="$(${PRE}busybox echo -n "$COMBOp" | ${PRE}busybox cut -f 2 -d ':' | ${PRE}busybox cut -f 1 -d ' ')"
 #make sure it is unmounted:
 MNTD="$(${PRE}busybox mount | ${PRE}busybox grep "^/dev/${TARGETPART} " | ${PRE}busybox cut -f 1 -d ' ')"
 if [ "$MNTD" ];then
  ${PRE}busybox umount $MNTD
  if [ $? -ne 0 ];then
   errTITLE="$(gettext 'ERROR, partition mounted:') ${MNTD}"
   errMSG="$(gettext 'The partition is mounted, and cannot unmount it. You need to unmount it and try again.')"
   ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
   continue
  fi
 fi
 #Check that fdisk/gdisk utility is working...
 FDISK='fdisk'
 TARGETDRIVE="`${PRE}busybox echo -n "$TARGETPART" | ${PRE}busybox cut -c 1-3`"
 #special case, SD card /dev/mmcblk0p1...
 [ "$TARGETDRIVE" = "mmc" ] && TARGETDRIVE="`${PRE}busybox echo -n "$TARGETPART" | ${PRE}busybox sed -e 's/p[0-9]$//'`"
 #[ "$Qfw" == "UEFI" ] && FDISK='gdisk'
 GDCHK="$(${PRE}${FDISK} -l /dev/${TARGETDRIVE} 2>/dev/null | ${PRE}busybox grep "^/dev/${TARGETPART} ")"
 if [ "$GDCHK" = "" ];then
  errTITLE="$(gettext 'Partition:') ${TARGETPART}"
  errMSG="$(gettext 'Unable to read this partition. Perhaps you should try a different partition.')"
  ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
  continue
 fi
 break
done ###

###find a working space###
find_working_place_func 1200 $TARGETPART #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

###preamble and select .usfs.xz### 151202 .iso
while [ true ];do #####
 export IQ_DLGM="<window title=\"$(gettext "Quirky Installer: partition")\" icon-name=\"gtk-convert\">
 <vbox>
  <text><label>$(gettext 'Quirky is available as a file. The Installer can download it for you, if not already.')</label></text>
  <frame $(gettext 'Download')>
   <text><label>$(gettext 'The file to be installed is named like this:') ${DISTRO_FILE_PREFIX}-${DISTRO_VERSION}.iso</label></text>
   <text use-markup=\"true\"><label>\"$(gettext 'If you have already downloaded the <b>*.iso</b> file that you wish to install, click <b>CONTINUE</b> to select it in the next window. Otherwise, click <b>DOWNLOAD</b>.')\"</label></text>
   <hbox>
    <button><label>$(gettext 'DOWNLOAD')</label><action type=\"exit\">download</action></button>
   </hbox>
  </frame>
  <hbox>
   <text><label>$(gettext 'File already downloaded:')</label></text>
   <button><label>$(gettext 'CONTINUE')</label><action type=\"exit\">continue</action></button>
   <button cancel></button>
  </hbox>
 </vbox>
 </window>"
 RETSTRINGM="$(${PRE}gtkdialog --program=IQ_DLGM --center)"
 [ $? -ne 0 ] && exit_func 0
 eval "$RETSTRINGM"
 [ "$EXIT" != "download" -a "$EXIT" != "continue" ] && exit_func 0

 #download .usfs.xz file... 151202 .iso file...
 DEFPATH='/file/downloads'
 if [ ! -d /file/downloads ];then
  DEFPATH="/root/Downloads"
  mkdir -p /root/Downloads
 fi
 if [ "$EXIT" == "download" ];then
  ${PRE}busybox ping -4 -c 1 www.google.com
  if [ $? -ne 0 ];then
   ${PRE}popup "level=top terminate=ok background='#FF8080'|<b>$(gettext 'Download: error')</b>${CR}$(gettext 'Sorry, there is no Internet connection.')"
   continue
  fi
  ${PRE}popup "level=top background='#FFE000'|<big>$(gettext 'Please wait, searching online...')</big>"
  #DL_URL="ftp://distro.ibiblio.org/quirky/quirky6/${Qarch}/releases" #150414
  DL_URL="http://distro.ibiblio.org/quirky/quirky6/${Qarch}/releases" #151006
  FILEs="$(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)" #151006 ex: http://distro.ibiblio.org/quirky/quirky6/amd64/releases/april-7.0.2/april64-7.0.2.iso

  ${PRE}busybox killall popup
  if [ ! "$FILEs" ];then
   ${PRE}popup "level=top background='#FF8080' terminate=ok|<big>$(gettext 'Could not find any .iso files. Maybe ibiblio.org is down.')</big>"
   continue
  fi
  ITEMSFILE=''
  for aFILE in `${PRE}busybox echo -n "$FILEs" | ${PRE}busybox tr '\n' ' '`
  do
   aITEM="$(${PRE}busybox basename $aFILE)"
   ITEMSFILE="${ITEMSFILE}<item>${aITEM}</item>"
  done
  export IQ_DLG_DL_FILE="<window title=\"$(gettext "Quirky Installer: partition")\" 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 file:')</label></text>
    <combobox><variable>COMBODL</variable>${ITEMSFILE}</combobox>
   </hbox>
   <hbox>
    <button><label>$(gettext "DOWNLOAD")</label><action type=\"exit\">downloadfile</action></button>
    <button cancel></button>
   </hbox>
  </vbox>
  </window>"
  RETSTRINGDL="$(${PRE}gtkdialog --program=IQ_DLG_DL_FILE --center)"
  [ $? -ne 0 ] && exit_func 0
  eval "$RETSTRINGDL"
  [ "$EXIT" != "downloadfile" ] && continue
  URLfile="$(${PRE}busybox echo "$FILEs" | ${PRE}busybox grep "${COMBODL}$" | ${PRE}busybox head -n 1)"
  URLdir="$(${PRE}busybox dirname $URLfile)"
  [ -f ./sha1sums.txt ] && rm -f ./sha1sums.txt
  if [ `${PRE}busybox 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|<big>$(gettext 'Could not download file.')</big>"
   continue
  fi
  [ -f ./${COMBODL} ] && [ ! -d ./${COMBODL} ] && ${PRE}busybox rm -f ./${COMBODL}
  if [ `which download_file` ];then #150323 in most puppies.
   download_file $URLfile
  else #RXVTEXE exported from installquirky:
   DLmsg="$(gettext 'Downloading:')" #150414
   LANG=C ${RXVTEXE} -geometry 80x3+0+0 -bg orange -fg black -title "${DLmsg} ${URLfile}" -e wget -4 $URLfile
  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|<big>$(gettext 'File downloaded, but checksum is wrong. Aborting.')</big>"
   ${PRE}busybox rm -f ./sha1sums.txt
   ${PRE}busybox rm -f ./${COMBODL}
   continue
  fi
  SRCFLG='file'
  PWD2="`${PRE}busybox pwd`"
  SRCXML="<text><label>$(gettext 'The file has been downloaded:') ${PWD2}/${COMBODL}</label></text>"
  CHOOSE1="${PWD2}/${COMBODL}"
 else
  [ "$EXIT" != "continue" ] && continue
  export IQ_DLG_DL_FILE="<window title=\"$(gettext "Quirky Installer: partition")\" icon-name=\"gtk-convert\">
  <vbox>
   <text><label>$(gettext 'Please choose the .iso file:')</label></text>
   <chooser><variable>CHOOSE1</variable><default>${DEFPATH}</default><width>600</width><height>400</height></chooser>
   <hbox>
    <button><label>$(gettext "CONTINUE")</label><action type=\"exit\">continue</action></button>
    <button cancel></button>
   </hbox>
  </vbox>
  </window>"
  RETSTRINGDL="$(${PRE}gtkdialog --program=IQ_DLG_DL_FILE --center)"
  [ $? -ne 0 ] && exit_func 0
  eval "$RETSTRINGDL"
  [ "$EXIT" != "continue" ] && continue
 fi

 #CHOOSE1 has the .usfs.xz file. 151202 .iso file.
 [ -d "$CHOOSE1" ] && CHOOSEPATH="$CHOOSE1"
 [ -f "$CHOOSE1" ] && CHOOSEPATH="$(${PRE}busybox dirname "$CHOOSE1")"
 FILENAME="$(${PRE}busybox basename "$CHOOSE1")"
 EXT="$(${PRE}busybox echo -n "$FILENAME" | ${PRE}busybox rev | ${PRE}busybox cut -f 1 -d '.' | ${PRE}busybox rev)" #151202
 if [ "$EXT" != "iso" ];then
  errMSG="$(gettext 'Sorry, this is not a .iso file:') ${FILENAME}"
  ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>$(gettext 'ERROR: wrong file')</b>${CR}${errMSG}"
  continue
 fi
 
 ${PRE}busybox killall popup
 break
done #####

#150414 chosen .usfs.xz file may not be in working area, copy it there: 151202 .iso file:
if [ ! -f ./$FILENAME ];then
 cp -a -f $CHOOSE1 ./
 DELME1="`pwd`/${FILENAME}" #151007 mark for removal.
fi
IMGFILEZ="$FILENAME"

FILEPREFIX="$(${PRE}busybox echo -n "$IMGFILEZ" | ${PRE}busybox cut -f 1 -d '-')" #140130

###write to target drive###
#151202 file now .iso, want q.sfs inside it...
#${PRE}popup "level=top background='#FFE000'|<b>$(gettext 'File:') ${IMGFILEZ}</b>${CR}$(gettext 'Please wait, expanding file...')"
#IMGFILE="$(${PRE}busybox basename ${IMGFILEZ} .xz)"
#echo "Uncompressing ${IMGFILEZ} to ${IMGFILE}..."
#${PRE}busybox rm -f ${IMGFILE} 2>/dev/null
#if [ "$PRE" ];then
# ${PRE}xz --decompress --stdout ${IMGFILEZ} > ${IMGFILE} #151004
#else
# xz --decompress --stdout ${IMGFILEZ} > ${IMGFILE}
#fi
##151007 host precise 5.7.1, seems to be failing here... no, later
#if [ $? -ne 0 ];then
# ${PRE}busybox killall popup
# errMSG="$(gettext 'File:') ${IMGFILEZ}${CR}$(gettext 'Unable to decompress file, aborting.')"
# ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>$(gettext 'ERROR: file corrupted')</b>${CR}${errMSG}"
# [ -f ./${IMGFILE} ] && rm -f ./${IMGFILE}
# #[ -d mntpt ] && rm -rf mntpt
# ${PRE}busybox sync
# exit_func 1
#fi
[ -d isomntpt ] && ${PRE}busybox rm -rf isomntpt
${PRE}busybox mkdir isomntpt
${PRE}busybox mount -t iso9660 -o loop,ro $IMGFILEZ isomntpt
${PRE}busybox cp -a -f isomntpt/q.sfs ./
${PRE}busybox cp -a -f isomntpt/initrd.q ./
${PRE}busybox cp -a -f isomntpt/vmlinuz ./
${PRE}busybox cp -a -f isomntpt/logo.16 ./
${PRE}busybox sync
${PRE}busybox umount isomntpt
${PRE}busybox rmdir isomntpt
IMGFILE='q.sfs'

#151007 mark IMGFILE for deletion in exit_func()...
DELME2="`pwd`/${IMGFILE}"

${PRE}busybox sync
${PRE}busybox killall popup

FSUUID="$(${PRE}busybox blkid /dev/${TARGETPART} | ${PRE}busybox grep -o 'UUID="[^"]*' | ${PRE}busybox cut -f 2 -d '"')" #'

###write to target###
export IQ_DLGs="<window title=\"$(gettext "Quirky Installer: partition")\" icon-name=\"gtk-convert\">
<vbox>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'Final sanity check, is this correct?:') <big>${TARGETPART}</big></b>\"</label></text>
 <text><label>$(gettext 'Quirky will be installed to partition:') ${TARGETPART} $(gettext 'Filesystem:') ${TARGETFS}</label></text>
 <text><label>$(gettext 'Anything previously in the partition will be erased.')</label></text>
 <text><label>$(gettext 'Note, if you already have Quirky installed on the partition, and wish to upgrade it, this is not the application to use. Instead, install a Service Pack PET.')</label></text>
 <text><label>\"  \"</label></text>
 <text><label>$(gettext 'Technical note: Temporary working folder: ')$(pwd)</label></text>
 <hbox>
  <button><label>$(gettext 'CONTINUE')</label><action type=\"exit\">continue</action></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>"
RETSTRINGs="$(${PRE}gtkdialog --program=IQ_DLGs --center)"
[ $? -ne 0 ] && exit_func 0
eval "$RETSTRINGs"
[ "$EXIT" != "continue" ] && exit_func 0


${PRE}busybox mkdir -p /mnt/$TARGETPART
${PRE}busybox mount -t $TARGETFS /dev/$TARGETPART /mnt/$TARGETPART
if [ $? -ne 0 ];then
 errTITLE="$(gettext 'Partition:') ${TARGETPART}"
 errMSG="$(gettext 'failed to mount partition. Aborting install.')"
 ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
 exit_func 1
fi

${PRE}popup "level=top background='#FFE000'|<b>$(gettext 'Partition:') ${TARGETPART}</b>${CR}$(gettext 'Please wait, erasing contents of partition...')"
${PRE}busybox rm -rf /mnt/$TARGETPART/*
${PRE}busybox rm -rf /mnt/$TARGETPART/.* 2> /dev/null
${PRE}busybox sync
${PRE}busybox killall popup
${PRE}busybox sleep 0.5

${PRE}popup "level=top background='#FFE000'|<b>$(gettext 'Partition:') ${TARGETPART}</b>${CR}$(gettext 'Please wait, writing Quirky files to partition...')"
${PRE}busybox mkdir -p mntpt
${PRE}busybox mount -t squashfs -o loop,ro ${IMGFILE} mntpt
if [ $? -ne 0 ];then
 errTITLE="$(gettext 'File:') ${IMGFILE}"
 errMSG="$(gettext 'Failed to mount file. Aborting install.')"
 ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
 #151007 ${PRE}busybox rm -f ${IMGFILE}
 exit_func 1
fi

#151007 still trying find where precise 5.7.1 is failing...
if [ ! -f mntpt/etc/DISTRO_SPECS ];then
 errTITLE="$(gettext 'File:') ${IMGFILE}"
 errMSG="$(gettext 'File did not mount properly. Aborting install.')"
 ${PRE}popup "level=top background=#FF8080 terminate=ok|<b>${errTITLE}</b>${CR}${errMSG}"
 #${PRE}busybox rm -f ${IMGFILE}
 exit_func 1
fi

TARGETVERSION="$(${PRE}busybox grep '^DISTRO_VERSION=' mntpt/etc/DISTRO_SPECS | ${PRE}busybox cut -f 2 -d '=' | ${PRE}busybox cut -f 2 -d "'" | ${PRE}busybox cut -f 1 -d ' ')" #ex: 6.0.1'

##140212
#${PRE}popup "level=top background='#FFE000'|<b>$(gettext 'Creating:') ${FILEPREFIX}-${TARGETVERSION}.sfs</b>${CR}$(gettext 'Please wait...')"
#echo
#echo "Creating ${FILEPREFIX}-${TARGETVERSION}.sfs"
#echo "This will be copied to /audit/reference in the target drive, and
#may be used in future for recovery purposes. Creating now..."
#rm -f ${FILEPREFIX}-${TARGETVERSION}.sfs 2>/dev/null
#${RXVTEXE} -geometry 80x3+0+0 -bg orange -fg black -title "$(gettext 'Creating:') ${FILEPREFIX}-${TARGETVERSION}.sfs" -e mksquashfs mntpt ${FILEPREFIX}-${TARGETVERSION}.sfs -processors 1
#sync
#killall popup

#151007 ${PRE}popup "level=top background='#FFE000'|<b>$(gettext 'Partition:') ${TARGETPART}</b>${CR}$(gettext 'Please wait, writing to partition...')"
${PRE}cp -a -f --remove-destination mntpt/* /mnt/$TARGETPART/ 2> /tmp/installquirky-cp-errlog

#160419 iso builds no longer have vmlinuz in /boot in q.sfs...
[ -f ./vmlinuz ] && [ ! -f /mnt/$TARGETPART/boot/vmlinuz ] && ${PRE}busybox cp -a ./vmlinuz /mnt/$TARGETPART/boot/

${PRE}busybox sync
${PRE}busybox killall popup
${PRE}busybox umount mntpt

#fix f.s.... 151213 no longer required...
#tPTN1="s%.*MARK_DEV1FS_LINE.*% DEV1FS='${TARGETFS}' #MARK_DEV1FS_LINE%"
#tPTN2="s%DEV1FS='f2fs'.*%DEV1FS='${TARGETFS}' #MARK_DEV1FS_LINE%"
#${PRE}busybox sed -i -e "$tPTN1" /mnt/${TARGETPART}/sbin/init
#${PRE}busybox sed -i -e "$tPTN2" /mnt/${TARGETPART}/etc/rc.d/rc.sysinit

#160420 /etc/rc.d/install.log still in use, add INSTALLED_MODE=full...
echo 'INSTALLED_MODE=full' >> /mnt/${TARGETPART}/etc/rc.d/install.log

#151209 do not want "install" and "save" icons on desktop with full install...
${PRE}busybox sed -i '/installquirky/d' /mnt/${TARGETPART}/root/Choices/ROX-Filer/PuppyPin #remove "install" icon
${PRE}busybox sed -i '/savesession/d' /mnt/${TARGETPART}/root/Choices/ROX-Filer/PuppyPin #remove "install" icon
${PRE}busybox rm -f ./${IMGFILE} #131227
[ -f vmlinuz ] && ${PRE}busybox rm -f vmlinuz
[ -f initrd.q ] && ${PRE}busybox rm -f initrd.q
[ -f logo.16 ] && ${PRE}busybox rm -f logo.16
${PRE}busybox sync

##140212
#echo
#echo "Now copying ${FILEPREFIX}-${TARGETVERSION}.sfs to /audit/reference
#in partition ${TARGETPART}, please wait..."
#cp -a -f ${FILEPREFIX}-${TARGETVERSION}.sfs /mnt/${TARGETPART}/audit/reference/
#DATE="$(date +%Y%m%d%H%M)" #140213 141021
#echo "${DATE} ${FILEPREFIX}-${TARGETVERSION}.sfs" > /mnt/${TARGETPART}/audit/reference/dates #140213 141021

${PRE}busybox sync
${PRE}busybox umount /mnt/${TARGETPART}

#${PRE}busybox rm -f ${FILEPREFIX}-${TARGETVERSION}.sfs

#140123 if installed to a removable drive, the grub "root=/dev/$TARGETPART" is not adequate...
TARGETDRIVE="$(${PRE}busybox echo -n "$TARGETPART" | ${PRE}busybox sed -e 's%[0-9]$%%' -e 's%[0-9]$%%' -e 's%p$%%')" #ex: mmcblk0p1 becomes mmcblk0, sdb12 becomes sdb
TARGETNUM="$(${PRE}busybox echo -n "$TARGETPART" | ${PRE}busybox rev | ${PRE}busybox grep -o '^[0-9]*' | ${PRE}busybox rev)" #ex: mmcblk0p1 becomes 1, sdb12 becomes 12
[ "`${PRE}busybox echo -n "$TARGETNUM" | ${PRE}busybox wc -m`" = "1" ] && TARGETNUM="0${TARGETNUM}" #want two digits.
FDISKINFO="$(${PRE}fdisk -l /dev/${TARGETDRIVE} 2>/dev/null)"
DISKUUID="$(${PRE}busybox echo "$FDISKINFO" | ${PRE}busybox grep '^Disk identifier:' | ${PRE}busybox tr -s ' ' | ${PRE}busybox cut -f 3 -d ' ' | ${PRE}busybox sed -e 's%^0x%%')" #ex: 2669a57d

GRUB4DOS="title Quirky ${TARGETVERSION} (full install on ${TARGETPART})
  uuid ${FSUUID}
  kernel /boot/vmlinuz root=PARTUUID=${DISKUUID}-${TARGETNUM} rootwait rw"

export IQ_DLGf="<window title=\"$(gettext "Quirky Installer: partition")\" icon-name=\"gtk-convert\">
<vbox>
  <text use-markup=\"true\"><label>\"$(gettext 'Installation finished!')\"</label></text>
 <text><label>You will need to configure LILO, GRUB, or whatever boot manager you use, to boot from this partition. For example, for GRUB4DOS, put this in menu.lst:</label></text>
 <text use-markup=\"true\" selectable=\"true\"><label>\"<span bgcolor='white'>${GRUB4DOS}</span>\"</label></text>
 <frame $(gettext 'Notes')>
  <text><label>$(gettext 'The UUID of the filesystem was obtained by running:') blkid /dev/${TARGETPART}</label></text>
  <text><label>$(gettext 'This may not work if the drive is removable, such as USB or SD-card:') root=/dev/${TARGETPART}</label></text>
  <text><label>$(gettext 'But, this will always work:') root=PARTUUID=${DISKUUID}-${TARGETNUM}</label></text>
  <text><label>$(gettext 'Disk-UUID:') ${DISKUUID} $(gettext 'This is obtained from: ') fdisk -l /dev/${TARGETDRIVE}</label></text>
  <text><label>$(gettext 'rootwait is optional, essential for external USB drives.')</label></text>
  <text><label>$(gettext 'Unlike Puppy Linux, Quirky only recognises three Quirky-specific kernel boot parameters:') 'qfix=bak', 'qfix=nox' and 'qfix=fsck'</label></text>
 </frame>
 <hbox>
  <button><label>$(gettext 'OK')</label><action type=\"exit\">ok</action></button>
 </hbox>
</vbox>
</window>"
RETSTRINGf="$(${PRE}gtkdialog --program=IQ_DLGf --center)"

exit_func 0
###END###
