#(c) Copyright Barry Kauler July 2017, barryk.org
#Licence GPL3 (refer: /usr/share/doc/legal).
#this script is inlined in install-quirky-to-frugal, -drive, -partition
#note, this script was originally the first half of install-quirky-to-frugal

#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}"
export PRE #170721
mkdir -p /tmp/install_quirky #170721

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
}

###list partitions###
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

###choose source###
#170721 choose where to get quirky files, extracted to choose_quirky_files_func in install-funcs
choose_quirky_files_func ${INSTMODEtr}
[ $? -ne 0 ] && exit 1
COMBO1="$(cat /tmp/install_quirky/COMBO1)"

DEFPATH='/file/downloads'
if [ ! -d /file/downloads ];then
 DEFPATH="/root/Downloads"
 mkdir -p /root/Downloads
fi

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

###locate source###
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
  #170721 code extracted to download_iso_func() in install-funcs
  download_iso_func ${INSTMODE}
  RETVAL=$?
  [ $RETVAL -eq 98 ] && exit_func 0
  [ $RETVAL -ne 0 ] && exit_func $RETVAL
  COMBODL="$(cat /tmp/install_quirky/COMBODL)"
  SRCFLG='iso'
  PWD2="`${PRE}busybox 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

export IQ_DLG2b="<window title=\"$(gettext "Quirky Installer:") ${INSTMODEtr}\" icon-name=\"gtk-convert\">
<vbox>
 <frame $(gettext 'Source')>
  ${SRCXML}
 </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

MNTPTsrc=''
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

#exit with MNTPTsrc path to quirky source files vmlinuz, initrd.q, q.sfs
###END###
