#!/bin/sh
#(c) Copyright Barry Kauler 2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (/usr/share/doc/legal/lgpl-2.1.txt)
# BK july 2007:
#/usr/sbin/puppyinstaller-217 is heavily modified by Dougal (based on
# puppyinstaller from v2.14). Was used in 216 and 217.
#/usr/sbin/puppyinstaller-bk is the v2.14 one with minimal mods. for 2.17.1+
#aug 2007 updated for 2.20: 'root=/dev/ram0' removed as using initramfs.
#v3.94 BK dec 2007: no longer any hd* drives.
#v3.95 BK jan 2008: handle Classmate laptop.
#v3.96 BK jan 2008: improvements for full hd install, warning about drive nums.
#v3.97 BK feb 2008: fully support /dev/hd* notation for ide drives.
#v3.97 BK feb 2008: fix mbr recognition error in flash pen drive.
#v3.97 BK feb 2008: new ComboFormat installation for Flash drives.
#v3.98 BK mar 2008: bugfix for install to idehd or satahd.
#v4.02 /lib/modules/firmware.dep.<kern>,DOTconfig-K<kern> moved to /etc/modules. ...um, no mods this script.
#v404 usb install, now have 'pfix=copy'. bugfix extlinux now needs '-i'.
#v412 /etc/DISTRO_SPECS, renamed pup_xxx.sfs, pup_save.2fs etc.
#w013, w015 march 2009: updated for Woof.
#w460 zdrv has new name format, ex zu500269.sfs.
#w464, w478 checking /etc/puppyversion is history.
#w482 fix handling if separate zdrv file.
#w482 copy modules from 'initrd' folder to full hd install.
#v423 kirk: ext4 support.
#v424 coolpup: removed the '*' from ${DISTRO_FILE_PREFIX}-${NEWVER}*.sfs, tidyup pidof.
#v425 tentative support for install to scsi hd.
#v430 full hd install, wipe all, rc.update was not running properly.
#091211 modified to work with GRUB4DOS and shinobar's grub4dosconfig.
#100113 TARGETEXES has files that got copied from initrd. ditto /lib/keymaps, /lib/consolefonts.
#100207 gecko edubook, disktype sees internal sd as both partitions and a superfloppy.
#100321 Now have boot menu for installations that use Syslinux/Extlinux.
#100412 Fix from EdV: prevent display irrelevant partitions.
#100719 warning frugal, do not install to folder if want save session to entire partition.
#100720 removed 'nosmp' kernel boot param.
#100913 new simplified puppy filenames.
#100916 no longer have DISTRO_DEVXSFS in /etc/DISTRO_SPECS.(it was only there briefly)
#100916 a second help screen at bootup, file help2.msg.
#101107 fix frugal install, puppy now searches 2-deep.
#101221 yaf-splash fix.
#110126 no longer using SATADRIVES variable in /etc/rc.d/PUPSTATE. support install to card, /dev/mmcblk*. remove zip/ls120 install.
#110422 DISTRO_VERSION variable now has dotted format. note, also now using full dotted version# in puppy filenames.
#110505 support sudo for non-root user.
#110814 convert script from gtkdialog2 to gtkdialog3+.
#111011 bump to gtkdialog4. small fixes.
#120126 jemimah: clear SELECTIONS variable, add ntfs exclusion.
#120202 rodin.s: internationalized
#120310 npierce: fix some grep operations.
#120323 replace 'xmessage' with 'pupmessage'.
#120329 Xdialog bug --yesno supposed to "yes" "No" buttons, but they are "OK" "Cancel".
#120823 rerwin: preserve user-modified files in full-install upgrade.
#121116 LANG extraction was broken.
#130128 handle 'udf' f.s. (see also pmount, pup_event_frontend_d, drive_all, probepart).
#130203 change probedisk2 to probedisk.
#130216 partial support f2fs.
#130526 npierce: fix for GPT partitions. ref: http://www.murga-linux.com/puppy/viewtopic.php?p=704919#704919
#130613 /run is required by eudev. full-hd-install, always ask wipe partition, may need create /dev/console, /dev/null.
#130722 new method to update desktop drive icons. see also /usr/sbin/gparted_shell, bootflash.
#140705 major overhaul of guis and conditions.

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

export TEXTDOMAIN=puppyinstaller
export OUTPUT_CHARSET=UTF-8
. gettext.sh
export LANGORG="$(locale | grep '^LANG=' | cut -d '=' -f 2)" #121116

Yes_lbl="$(gettext 'Yes')"
No_lbl="$(gettext 'No')"

BG="grey20" 
FG="#FFF2A2"

GTKDIALOGEXE="gtkdialog"
#[ ! "$GTKDIALOGEXE" ] && GTKDIALOGEXE="`which gtkdialog`"
#[ ! "$GTKDIALOGEXE" ] && exit 1

KERNELVER="`uname -r`"

. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS
NEWVER="$DISTRO_VERSION"

fsfunc() { #f.s. type param passed in
 case $1 in
 Ext2)     FSTYPE="ext2" ;;
 ReiserFS) FSTYPE="reiserfs" ;;
 Ext3)     FSTYPE="ext3" ;;
 Ext4)     FSTYPE="ext4" ;;
 FAT16)    FSTYPE="vfat" ;;
 FAT32)    FSTYPE="vfat" ;;
 NTFS)     FSTYPE="ntfs" ;;
 ISO9660)  FSTYPE="iso9660" ;;
 JFS)      FSTYPE="jfs" ;;
 XFS)      FSTYPE="xfs" ;;
 UDF)      FSTYPE="udf" ;; #130128
 F2FS|f2fs)	   FSTYPE="f2fs" ;; #140630 #140715 CAPS|lower) for when 'disktype' is fixed, uses guess_fstype
 *)        FSTYPE="" ;; #100412
 esac 
}

# deleted ispupfunc() ... didn't work at all, wasn't worth fixing. 140701
# replaced with linux_func.. idea from shinobar #140703
linux_func() { #two params: fstype partition
	#returns $DISTRO;windows, version of already-installed Puppy or other Linux if any.
	#set -x ###debug
	unset DISTRO
	# check for Puppy frugal .. some wild guessing/assumptions
	DISTRO=""
	[ ! $2 ] && return 0
	echo "${2}_button=true" >> /tmp/buttonstate # for windows buttons
	mkdir -p /mnt/$2
	ISPUPMNTPT="`mount | grep "^/dev/$2" | tr -s " " | cut -f 3 -d " "|head -n1`"
	if [ "$ISPUPMNTPT" = "" ];then
	  ISPUPMNTPT="/mnt/$2"
	  mount -t $1 /dev/$2 /mnt/$2
	  [ $? -ne 0 ] && return 0
	fi
	# find puppy. Should find most versions back to puppy 2. Forget puppy 1
	# should handle: pup-4.2, pup_432, lupu-500, spup-052, puppy_slacko_5.9.3.x, puppy-214x
	#echo "$ISPUPMNTPT" ###debug
	ISFILE=`find "$ISPUPMNTPT" -type f -name '*up*[0-9]*.sfs' -maxdepth 2 2>/dev/null`
	#echo $ISFILE ###debug
	TEMP=/tmp/num${2}
	printf "$ISFILE" > $TEMP
	echo "" >> $TEMP # add a carriage return for 'wc'
	#echo -n "This many " ###debug
	#wc -l $TEMP ###debug
	NUMBER_FOUND=`grep 'sfs$' $TEMP|wc -l`
	[ "$NUMBER_FOUND" -ge 1 ] && echo "found $NUMBER_FOUND pups" || echo "no pups found"
	[ "$NUMBER_FOUND" -gt 1 ] && DISTRO="Many frugal Puppies"
	if [ "$NUMBER_FOUND" -eq 1 ];then #make a pretty name
	  # now some gymnastics to try and give it a pretty name
	  # first strip direcory
	  ISNAME=`grep 'sfs$' $TEMP`
	  echo "name is $ISNAME"
	  SFSNAME="${ISNAME##*/}"
	  # puppy_slacko_5.9.3.sfs = Puppy Slacko 5.9.3 or pup-431 = Pup 431 .. we care not about the DOTS!
	  DISTROX="`echo ${SFSNAME%.*}|tr '_' '\n'|tr '-' '\n'|sed 's%^\(.\)%\U\1%g'|tr '\n' ' '`"
	  # filesystem
	  case "$1" in
	   *fat|ntfs)echo "continuing search" ;;
	   *)DISTRO="${DISTROX}" ;;
	  esac
	  rm $TEMP
	  #echo 1 ###debug
	  [ "$DISTRO" ] && echo "distro is $DISTRO" || echo "no distro"
	fi
	if [ ! "$DISTRO" ];then # look for full puppy or other linux
	   # check for Puppy full.. far more accurate.
	   PUPFULL=`find "$ISPUPMNTPT"/etc -type f -name 'DISTRO_SPECS' -maxdepth 1 2>/dev/null`
	   # if found source the thing
	   if [ -f "$PUPFULL" ];then . "$PUPFULL"
	     DISTRO="$DISTRO_NAME $DISTRO_VERSION full"
	   fi
	   #echo 2 ###debug
	   [ "$DISTRO" ] && echo "distro is $DISTRO" || echo "no distro"
	fi
	if [ ! "$DISTRO" ];then # look again ..older pup
	   # check for Puppy old (pre woof)
	   PUPFULLOLD=`find "$ISPUPMNTPT"/etc -maxdepth 1 -type f -name 'puppyversion' 2>/dev/null`
	   # if found source the thing
	   if [ -f "$PUPFULLOLD" ];then 
	     VERSION=`cat $PUPFULL`
	     DISTRO="Puppy $VERSION full"
	   fi
	   #echo 3 ###debug
	   [ "$DISTRO" ] && echo "distro is $DISTRO" || echo "no distro"
	fi
	# other linux
	if [ ! "$DISTRO" ];then # check for /etc/os-release, newish standard supported by all major distros
	  OLINUX=`find "$ISPUPMNTPT"/etc -maxdepth 1 -type f -name 'os-release' 2>/dev/null`
	  echo "OLINUX=$OLINUX"
	  if [ -f "$OLINUX" ];then
	    . "$OLINUX" # source it..
	    DISTRO="$PRETTY_NAME"
	  fi
	  #echo 4 ###debug
	  [ "$DISTRO" ] && echo "distro is $DISTRO" || echo "no distro"
	fi
	# last chance.. check for vmlinuz
	if [ ! "$DISTRO" ];then
	  KERNELS=$(find -L "$ISPUPMNTPT" -maxdepth 2 -type f -iname 'vmlinuz*' 2>/dev/null) #shinobar, but reduced depth to 2
	  [ "$KERNELS" ] && DISTRO="Unknown Linux"
	  # could be a frugal pup though
	  FRUGPUP=`find "$ISPUPMNTPT" -type f -name '*up*[0-9]*.sfs' -maxdepth 2 2>/dev/null`
	  [ "$FRUGPUP" ] && DISTRO=''
	  #echo 5 ###debug
	  [ "$DISTRO" ] && echo "distro is $DISTRO" || echo "no distro"
	fi
	# finally check for windows
	if [ ! "$DISTRO" ];then
	  case "$1" in
	    *fat|ntfs)
	    echo $ISPUPMNTPT
	    DISTROW=''
	    WINSTALL=`find "$ISPUPMNTPT" -type d -maxdepth 1 -iname 'windows' 2>/dev/null`
	      ANCW=`find "$ISPUPMNTPT" -type f -maxdepth 2 -name AUTOEXEC.BAT 2>/dev/null`
	      OLDW=`find "$ISPUPMNTPT" -type f -maxdepth 2 -name boot.ini 2>/dev/null`
	      MEDW=`find "$ISPUPMNTPT" -type f -maxdepth 2 -name 'BCD' 2>/dev/null`
	      NEWW=`find "$ISPUPMNTPT" -type d -maxdepth 3 -name 'EFI' 2>/dev/null` # windows server 2008 is 3 deep
	      SIXTY4W=`find "$ISPUPMNTPT"/Windows -type d -maxdepth 2 -name 'SysWOW64' 2>/dev/null`
	    if [ "$WINSTALL" != "" ];then
	      if [ "$ANCW" != "" ];then DISTROW="Ancient Windows"
	         [ "$OLDW" != "" ] && DISTROW="Old Windows"
	      elif [ "$MEDW" != "" ] || [ "$NEWW" != "" ];then DISTROW="Older Windows"
	        [ "$NEWW" != "" ] && DISTROW="New Windows"
	        [ "$SIXTY4W" != "" ] && DISTROW="Windows 64 bit"
	      fi
	    elif  [ "$MEDW" != "" ] || [ "$NEWW" != "" ];then
	        DISTROW="Windows boot"
	        [ "$NEWW" != "" ] && DISTROW="New Windows boot"
	    else
	      DISTROW=""
	    fi
	    echo "is windows? $DISTROW"
	    [ "$DISTROW" ] && echo "$DISTROW" >>/tmp/win 
	    if [ "$DISTROW" != "" ];then
		  sed -i "s%${2}_button\=true%${2}_button=false%" /tmp/buttonstate # need '_' delimiter #140705
		  [ ! "$DISTROX" ] && DISTROX="none"
		  if [ "$DISTROX" != "none" ];then
		    DISTRO="$DISTROW and $DISTROX"
		  else
		    DISTRO="$DISTROW"
		  fi
	    else
		  DISTRO="$DISTROX"
	    fi
	    ;;
	  esac
	fi
	[ ! "$DISTRO" ] && DISTRO="none"
	umount /mnt/$2 2>/dev/null
	unset DISTROX
	# done
}

# function to determine if there is substantial free space on a physical drive
free_space_func() {
	# check what is used
	USED=`probepart -k | grep "$1" | cut -d '|' -f3` #KB
	t=0
	for k in `echo "$USED"`;do
	   t=$(($t + $k))
	done
	# check size of disk
	s=`blockdev --getsize64 "$1"` #B
	s=$(($s / 1024)) #KB
	# calc freespace
	f=$(($s - $t))
	f=$((f / 1024)) #MB
}

gpartedfixfunc() { #params: $DRVSEL
 GPDRV="$1"
 gparted /dev/$GPDRV &
 sleep 2
 #changed from pupmessage 140630
 xmessage -bg '#EBADE2' -center -title "$(gettext 'Puppy Universal Installer')" \
 "$(gettext 'GParted is a partition manager, available in Puppy, a very simple tool
for deleting, creating and resizing partitions.')

`eval_gettext \"Regarding 'ntfs' or 'vfat', of course do not change these to ext2 or ext3 if the
partition has Windows or DOS installed on it! In the case of USB Flash drives,
these normally come with a 'fat16' partition which is okay, but you will be
restricted to saving to a '\\\${DISTRO_FILE_PREFIX}save' file. This is fine and most people opt for
this, particularly as the drive can be used also to save and transfer files with
Windows. However, a Flash drive can be changed to ext2/3 and still boots fine,
with the advantage that the entire partition can be used to save to, rather than
a '\\\${DISTRO_FILE_PREFIX}save' file. If in doubt, stay with 'vfat'.\"`

$(gettext 'WARNING: changing the filesystem will wipe everything in the partition!')

`gettext \"One more thing. When you run GParted, right-click on the partition and you will
see a menu option to 'Manage flags'. Make sure the 'boot' flag is set -- don't
forget that!\"`

`gettext \"If you have a USB Flash 'key' drive that you have installed Puppy to but it will
not boot, it may be that the partition(s) is confusing the PC's BIOS.
Personally, I was able to fix such a USB drive by wiping the existing partition
and then create it again using GParted.
The steps to do this are very simple: when GParted is running,
 1. delete all partitions on the USB device. 
 2. Create a new primary FAT16 partition, to fill the entire drive.
 3. Click the 'Apply' button to write changes to the USB drive.
 4. Set the 'boot' configuration flag (right-click on partition, 
    choose 'manage flags').\"`

$(gettext 'If GParted is installed, it is now running and you can do the above.
Quiting GParted will return you to the Puppy Universal Installer...')

$(gettext 'One final note for USB: after exiting GParted, do a mouse-over the desktop drive
icon(s) -- if it(they) has not updated, unplug then replug the USB drive.')" &
 MSGID=$!
 while [ 1 ];do
  [ "`pidof gparted`" = "" ] && break #v424
  sleep 1
 done
 [ ! "`which gparted`" = "" ] && pupkill $MSGID 2>/dev/null
 #w015 need to refresh desktop drive icons(s)...
 sync
 #130722 ...this file was read by the old /sbin/pup_event_frontend_d big script.
 #now have a binary daemon, send this uevent to it, then /usr/local/pup_event/frontend_change will be called...
 echo change > /sys/block/${GPDRV}/uevent
}

fixusbfunc() { #params: $DESTPART $DESTFS
 echo '#!/bin/sh
DESTPART="$1" #partition being installed to
DESTFS="$2"   #filesystem of destination partition
echo "'$(gettext 'This little section of the Puppy Universal Installer is currently only')'"
echo "'$(gettext 'for fixing USB Flash or CF-card-in-IDE-adaptor, after you have gone thru')'"
echo "'$(gettext 'all the steps but it still will not boot.')'"
echo
echo "'$(gettext 'You are trying to install to')' $DESTPART, '$(gettext 'but it is not booting...')'"
echo "'$(gettext 'Well, I am assuming that the BIOS in your PC is able to boot from USB.')'"
echo "'$(gettext '-- for CF-IDE the drive looks like an ordinary hard drive, so that should')'"
echo "   '$(gettext 'be okay for any BIOS.')'"
echo
echo "'$(gettext 'This is the first release of this script, and it is a bit limited...')'"
echo "'$(gettext 'and only handles a partition with a msdos/vfat filesystem.')'"
if [ "$DESTFS" = "vfat" ];then
 echo "'$(gettext '...which is okay,')' $DESTPART '$(gettext 'does have a')' $DESTFS '$(gettext 'filesystem.')'"
 echo "   '$(gettext 'But, we will now wipe it and recreate it.')'"
else
 echo "'$(gettext '...currently')' $DESTPART '$(gettext 'has a')' $DESTFS '$(gettext 'filesystem.')'"
 echo "   '$(gettext 'This script is not yet designed for that, so cannot continue.')'"
 echo
 echo -n "'$(gettext 'Press ENTER key to exit:')' "
 read nogo
 exit
fi
echo
echo "'$(gettext 'Please be sure that you are wiping the right partition!!!')'"
echo "'$(gettext 'Everything in')' $DESTPART '$(gettext 'is going to be wiped. If you do not want that,')'"
echo "'$(gettext 'press any letter on the keyboard then ENTER key, or')'"
echo -n "'$(gettext 'Press ENTER key only to continue script:')' "
read querygo
echo
echo "'$(gettext 'Recreating msdos filesystem on')' $DESTPART..."
mkdosfs /dev/$DESTPART
sync
echo "'$(gettext 'Fixing the Master Boot Record (MBR)...')'"
#DESTDRV="`echo -n "$DESTPART" | cut -b 1-3`"
DESTDRV="`echo -n "$DESTPART" | sed -e "s/[0-9]*$//" -e "s/p$//"`" #110126 mmcblk0p1 becomes mmcblk0
#dd if=/usr/lib/syslinux/mbrfat.bin of=/dev/$DESTDRV
dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/mbr.bin of=/dev/$DESTDRV
sync
echo "'$(gettext '...done')'"
echo
echo "'$(gettext 'Okay, we can now go back to the main part of the Puppy Universal')'"
echo "'$(gettext 'Installer and install Puppy to the')' $DESTPART '$(gettext 'partition.')'"
echo -n "'$(gettext 'Press ENTER key to continue:')' "
read yesgo
' > /tmp/fixusb.sh
 chmod 755 /tmp/fixusb.sh
 xterm -bg "$BG" -fg "$FG" -title "$(gettext 'Puppy Universal Installer')" -geometry 80x25 -e /tmp/fixusb.sh
}

unmountcdfunc() {
 if [ ! "$CDDRIVE" = "" ];then
  if [ ! "`mount | grep "/dev/$CDDRIVE"`" = "" ];then
   rox -D "`mount | grep "/dev/$CDDRIVE" | tr -s " " | cut -f 3 -d " "`"
   #do not kill any processes
   sync
   umount /dev/$CDDRIVE 2> /dev/null
   #mount > /etc/mtab  #v2.10 remove.
   [ "`mount | grep "/dev/$CDDRIVE"`" = "" ] && eject /dev/$CDDRIVE
  fi
 fi
}

locate_puppy_src_files() {
SRCPATH=""
CDDRIVE=""
rm -f /tmp/srcpath.txt
mount | tr -s " " | cut -f 3 -d " " |
while read ONEDIR
do
 INCYES=0
 [ -f ${ONEDIR}/${DISTRO_PUPPYSFS} ] && INCYES=`expr $INCYES + 1` #v3.01 v424 100913
 [ -f $ONEDIR/vmlinuz ] && INCYES=`expr $INCYES + 1`
 [ -f $ONEDIR/initrd.gz ] && INCYES=`expr $INCYES + 1`
 if [ $INCYES -eq 3 ];then
  echo -n "$ONEDIR" > /tmp/srcpath.txt
  break
 fi
done
[ -f /tmp/srcpath.txt ] && SRCPATH="`cat /tmp/srcpath.txt`"
if [ "$SRCPATH" = "" ];then
 Xdialog --wmclass "gtkdialog2" --left --buttons-style text \
 --title "$(gettext 'Puppy universal installer')" \
 --ok-label "CD" --cancel-label "$(gettext 'DIRECTORY')" \
 --yesno "$(eval_gettext "Puppy consists of 2 to 4 files:\
\nvmlinuz      The Linux kernel\ninitrd.gz    The initial ramdisk.\
\n${DISTRO_PUPPYSFS}  This has all the Puppy files (sometimes inside initrd.gz).\
\n             (if built inside initrd.gz then only initrd.gz is required)\
\n${DISTRO_ZDRVSFS} Extra kernel drivers and firmware (optional).\
\n             (most builds have a usable selection of drivers in \
${DISTRO_PUPPYSFS})\
\nIf you booted Puppy from a live-CD, those files will be on it.\
\nOtherwise, they are in the .iso file and can be extracted -- in that\
\ncase, just click on the .iso file and it will be mounted (opened) in\
\na directory (do not forget to click on the file later to unmount it!)\
\n\nWhere are the Puppy files?...")" 0 0
 RETVAL=$?
 if [ $RETVAL -eq 0 ];then #CD
  CDDRIVE="`readlink /dev/cdrom`"
  CDDRIVE="`basename $CDDRIVE`"
  CDDRVINFO="`probedisk | grep "/dev/$CDDRIVE" | cut -f 3 -d '|'`"
  Xdialog --wmclass "gtkdialog2" --left \
  --title "$(gettext 'Puppy universal installer')" \
  --msgbox "$(eval_gettext "Please insert the Puppy live-CD (if not already) \
  into the drive.\nNote, in case your PC has two CD/DVD drives, \
  Puppy is expecting\nthe live-CD to be inserted into $CDDRIVE, which is \
  described as:\n $CDDRVINFO \n\nAfter inserting live-CD, \
  click OK button...")" 0 0
  mkdir /mnt/$CDDRIVE 2> /dev/null
  mount -t iso9660 /dev/$CDDRIVE /mnt/$CDDRIVE
  if [ -f /mnt/$CDDRIVE/initrd.gz ];then
   SRCPATH="/mnt/$CDDRIVE"
  else
   Xdialog --wmclass "gtkdialog2" --left \
--title "$(gettext 'Puppy universal installer: ERROR')" \
--msgbox "$(gettext 'Sorry, Puppy is not on the CD.\nPlease mount the CD \
using one of the mount programs\n(see File Managers menu) \
then click OK button...')" 0 0
   RETVAL=1
  fi 
 fi
 if [ ! $RETVAL -eq 0 ];then #DIRECTORY
  SRCFPATH="`Xdialog --wmclass "gtkdialog2" --left \
--backtitle "$(eval_gettext "Please find the latest Puppy files\nvmlinuz, \
initrd.gz and ${DISTRO_PUPPYSFS},\nTHEN HIGHLIGHT ANY ONE OF THEM\nand \
click the OK button")" --title "$(gettext 'Puppy universal installer')" \
--stdout --no-buttons --no-cancel --fselect "*" 0 0`"
  [ ! $? -eq 0 ] && exit
  SRCPATH="`dirname $SRCFPATH`"
 fi
fi
SRCPATH=`echo "${SRCPATH}" | head -n 1`
#sanity check...
if [ ! -f ${SRCPATH}/${DISTRO_PUPPYSFS} ];then #v3.01 v424 100913
 [ "$SRCPATH" = "/mnt/$CDDRIVE" ] && eject /dev/$CDDRIVE
 Xdialog --wmclass "gtkdialog2" --left \
 --title "$(gettext 'Puppy universal installer: ERROR')" \
 --msgbox "`eval_gettext \"Oh dear, a 'sanity check' has failed.\
 \nThis file: \\\$SRCPATH/\\\${DISTRO_PUPPYSFS}\ndoes not exist.\nThe above \
 is supposed to be path-to-where-puppy-files-are/\\\${DISTRO_PUPPYSFS}\
 \nClick OK button to quit...\"`" 0 0
 exit
fi
} #end locate_puppy_src_files function.

grub4dos_func() {
	#really need gtkdialog here.. add a "readme" button or something... all this says is "trust me" }:-)
	GR4MSG=$(gettext "You now have the option to install a bootloader named 'grub4dos'. \
\nIt is a very simple exercise and will find all of your previous \
\nWindows and Linux installations and create a simple boot menu so \
\nyou can choose which Operating System to boot at power on. \
\n\nWould you like to install Grub4dos?")
	Xdialog --title "$gettext 'Puppy Universal Installer - Grub4dos'" \
	--yesno "$GR4MSG" 0 0 0
	case "$?" in
	  0)exec grub4dosconfig; exit ;;
	  *)echo "no" ;;
	esac
}
fatal() {
   [ $XPID ] && kill $XPID
   MSG=$(gettext "Failed to copy files.")
   export FATAL='<window title="'$(gettext 'Puppy Universal Installer: ERROR')'"><vbox>
   <hbox>
       <pixmap  icon_size="6"><input file stock="gtk-dialog-error"></input></pixmap>
     <text><label>"'$MSG'"</label></text>
   </hbox>
   <hbox><button ok></button></hbox>
   </vbox></window>'
   gtkdialog -c -p FATAL
   unmountcdfunc
   exit 1
}
f2fs_func() {
	type f2fs-usb-installer &>/dev/null
	case "$?" in
	0)exec f2fs-usb-installer 
	  exit;;
	*)F2MSG="$(eval_gettext "You need the <b>f2fs-usb-installer</b> to install to an f2fs partition")"
	  F3MSG="$(gettext "You can decide to repartition the drive to another format using GParted")"
	  export F2SGUI='<window title="'$(gettext 'Puppy Universal Installer: F2FS')'">
	    <vbox>
	      <frame F2fs Installer or repartition?>
	        <text use-markup="true" yalign="0" space-expand="true" space-fill="true">
	          <label>"'"$F2MSG"'"</label>
	        </text>
	        <hseparator></hseparator>
	        <text use-markup="true" yalign="0" space-expand="true" space-fill="true">
	          <label>"'"$F3MSG"'"</label>
	        </text>
	        <hbox homogeneous="true">
	          <button>
	            <input file stock="gtk-harddisk"></input>
	            <label>GParted</label>
	            <action type="exit">gparted</action>
	          </button>
	          <button cancel></button>
	        </hbox>
	      </frame>
	    </vbox>
	  </window>'
	  eval `gtkdialog -p F2SGUI -c`
	  case "$EXIT" in
	    gparted)gpartedfixfunc $DRVSEL
	          continue;;
	    *)exit;;
	  esac
	  ;;
	esac
}

tmpsel=/tmp/selections

# gui
[ -s $HOME/.config/ptheme/gtkdialog_active ] && . $HOME/.config/ptheme/gtkdialog_active 
[ ! "$SVG_BAR_COLOR_TOTAL" ] && SVG_BAR_COLOR_TOTAL='#111' 
[ ! "$SVG_BAR_COLOR_TEXT" ] && SVG_BAR_COLOR_TEXT='#eee' #background color 

export PUI=' 
<window title="'$(gettext 'Puppy Universal Installer')'" icon-name="gtk-preferences"> 
<vbox> 
  '"`/usr/lib/gtkdialog/xml_info 0 puppy_install.svg 60 "$(gettext "If you wish to install Puppy to a removable media, such as a USB Flash or hard drive or CD/DVD disc, <b>please insert it right now</b>, before proceeding. Then, choose the media that you want to install Puppy to.")"`"' 
  <frame '$(gettext 'Puppy Universal Installer')'> 
    <hbox space-expand="true" space-fill="true"> 
      <eventbox name="bg" above-child="false" visible-window="true" space-expand="false" space-fill="false"> 
        <hbox space-expand="false" space-fill="false"> 
          <vbox margin="5"> 
            <text angle="90" wrap="false" use-markup="true" yalign="0.5" space-expand="true" space-fill="true"><label>"<big><big><span fgcolor='"'$SVG_BAR_COLOR_TEXT'"'><b>USB</b></span></big></big>"</label></text> 
          </vbox> 
        </hbox> 
      </eventbox> 
      <vbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>USB</b> '$(gettext 'Flash drive')'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon flashcard_usb.svg big`"' 
              <action type="exit">usbflash</action> 
            </button> 
          </vbox> 
        </hbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>USB</b> '$(gettext 'hard drive')'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon harddisk_usb.svg big`"' 
              <action type="exit">usbhd</action> 
            </button> 
          </vbox> 
        </hbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>USB</b> '$(gettext "CF Flash drive, later move CF card to IDE/SATA \ 
internal adaptor")'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon flashcard_usb_2.svg big`"' 
              <action type="exit">usb2ataflash</action> 
            </button> 
          </vbox> 
        </hbox> 
      </vbox> 
    </hbox> 

    <hseparator></hseparator> 

    <hbox space-expand="true" space-fill="true"> 
      <eventbox name="bg" above-child="false" visible-window="true" space-expand="false" space-fill="false"> 
        <hbox space-expand="false" space-fill="false"> 
          <vbox margin="5"> 
            <text angle="90" wrap="false" use-markup="true" yalign="0.5" space-expand="true" space-fill="true"><label>"<big><big><span fgcolor='"'$SVG_BAR_COLOR_TEXT'"'><b>'$(gettext 'Internal')'</b></span></big></big>"</label></text> 
          </vbox> 
        </hbox> 
      </eventbox> 
      <vbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext '<b>Internal</b> (IDE or SATA) hard drive')'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon harddisk.svg big`"' 
              <action type="exit">atahd</action> 
            </button> 
          </vbox> 
        </hbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext '<b>Internal</b> IDE/SATA Flash drive 
(ex: CF card, SD memory card)')'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon flashcard.svg big`"' 
              <action type="exit">ataflash</action> 
            </button> 
          </vbox> 
        </hbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext '<b>Internal</b> USB Flash drive (ex: uDiskOnChip)')'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon card_usb.svg big`"' 
              <action type="exit">iusbflash</action> 
            </button> 
          </vbox> 
        </hbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext 'Ancient true-SCSI hard drive')'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon harddisk_scsi.svg big`"' 
              <action type="exit">scsihd</action> 
            </button> 
          </vbox> 
        </hbox> 
      </vbox> 
    </hbox> 

    <hseparator></hseparator> 

    <hbox space-expand="true" space-fill="true"> 
      <eventbox name="bg" above-child="false" visible-window="true" space-expand="false" space-fill="false"> 
        <hbox space-expand="false" space-fill="false"> 
          <vbox margin="5"> 
            <text angle="90" wrap="false" use-markup="true" yalign="0.5" space-expand="true" space-fill="true"><label>"<big><big><span fgcolor='"'$SVG_BAR_COLOR_TEXT'"'><b>CD</b></span></big></big>"</label></text> 
          </vbox> 
        </hbox> 
      </eventbox> 
      <vbox> 
        <hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext '<b>Optical</b> drive (CD/DVD/BD)')'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon cdrom.svg big`"' 
              <action type="exit">cd</action> 
            </button> 
          </vbox> 
        </hbox> 
      </vbox> 
    </hbox> 
  </frame> 
  <hbox space-expand="false" space-fill="false"> 
    <button> 
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"' 
      <label>'$(gettext 'Quit')'</label> 
      <action type="exit">EXIT</action> 
    </button> 
  </hbox> 
</vbox> 
</window>' 

echo ' 
style "bg" { bg [NORMAL] = "'$SVG_BAR_COLOR_TOTAL'" } 
widget "*bg*" style "bg"' > /tmp/gtkrc_pui 
export GTK2_RC_FILES=/tmp/gtkrc_pui:/root/.gtkrc-2.0 

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme 
RETVAL="`gtkdialog -p PUI 2>/dev/null`" 
eval "$RETVAL" 
[ ! "`grep -E "usbflash|usbhd|usb2ataflash|iusbflash|ataflash|atahd|scsihd|cd" <<< "$EXIT"`" ] && exit 
PMEDIA=$EXIT

# builds woofified gui for selected install type
gui_func() {
	#set -x ###debug
	echo -n '' > /tmp/drivegui
	message="$1"
	case "$message" in
	  1)installmessage=$(gettext "Choose which drive to install to:") ;;
	  *)installmessage="$message" ;;
	esac
	cat $tmpsel | while read d;do
	  s_icon=`echo "$d" | grep -E "usbflash|usbhd|ataflash|atahd|scsihd|cd"`
	  if [ "$s_icon" != "" ];then
	    case "$s_icon" in
	      usbflash)m_icon=flashcard_usb.svg;TYPE=USB ;;
	      cd)m_icon=cdrom.svg;TYPE=CD ;;
	      ataflash)m_icon=flashcard_usb.svg;TYPE="Internal Flash" ;;
	      atahd)m_icon=harddisk.svg;TYPE="Hard Disk" ;;
	      usbhd)m_icon=harddisk_usb.svg;TYPE="USB Hard Drive" ;;
	      scsihd)m_icon=harddisk_scsi.svg;TYPE="SCSI Hard Disk" ;;
	    esac
	    echo "$TYPE" > /tmp/type
	    continue # got icon, reitterate
	  fi
	  echo "$d" | grep -q 'size' || continue
	  drv=`echo "$d" | cut -d ' ' -f1`
	  Exit=$drv
	  case "$drv" in
	    sr*)Exit="exit" ;;
	  esac
	  desc=`echo "$d" | cut -d ' ' -f2,3,4,5,6,7 | tr -d '"'`
	  echo '<hbox space-expand="true" space-fill="true"> 
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$drv'</b> '"$desc"'"</label></text> 
          <vbox space-expand="false" space-fill="false"> 
            <button> 
              '"`/usr/lib/gtkdialog/xml_button-icon $m_icon big`"' 
              <action type="exit">'$Exit'</action> 
            </button> 
          </vbox> 
        </hbox>' >> /tmp/drivegui
	done
	DRVS=`cat /tmp/drivegui`
	TYPE=`cat /tmp/type`
	export dlg1='<window title="'$(gettext 'Puppy Universal Installer')'" icon-name="gtk-preferences" window-position="1"> 
<vbox> 
  <frame '$(gettext 'Puppy Universal Installer')'> 
    '"`/usr/lib/gtkdialog/xml_info 0 puppy_install.svg 60 "$installmessage"`"' 

	<hbox space-expand="true" space-fill="true"> 
      <eventbox name="bg" above-child="false" visible-window="true" space-expand="false" space-fill="false"> 
        <hbox space-expand="false" space-fill="false"> 
          <vbox margin="5"> 
            <text angle="90" wrap="false" use-markup="true" yalign="0.5" space-expand="true" space-fill="true"><label>"<big><big><span fgcolor='"'$SVG_BAR_COLOR_TEXT'"'><b>'"$TYPE"'</b></span></big></big>"</label></text> 
          </vbox> 
        </hbox> 
      </eventbox> 
      <vbox> 
        
        '"$DRVS"'
        
      </vbox> 
    </hbox> 
  </frame> 
  <hbox space-expand="false" space-fill="false"> 
    <button> 
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"' 
      <label>'$(gettext 'Quit')'</label> 
      <action type="exit">EXIT</action> 
    </button> 
    '"`/usr/lib/gtkdialog/xml_scalegrip`"' 
  </hbox> 
</vbox> 
</window>'

echo ' 
style "bg" { bg [NORMAL] = "'$SVG_BAR_COLOR_TOTAL'" } 
widget "*bg*" style "bg"' > /tmp/gtkrc_sel 
export GTK2_RC_FILES=/tmp/gtkrc_sel:/root/.gtkrc-2.0 

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
RETVAL="`gtkdialog -p dlg1`" 
eval "$RETVAL"

echo "$EXIT" > /tmp/retval
[ "$EXIT" = EXIT ] && exit
}

TAGPMEDIA="$PMEDIA" #remember the tag chosen above.
[ "$TAGPMEDIA" = "iusbflash" ] && PMEDIA="usbflash"
FINALPMEDIA="$PMEDIA"
if [ "$PMEDIA" = "usb2ataflash" ];then
 PMEDIA="usbflash"
 FINALPMEDIA="ataflash"
fi
echo "debug pmedia $PMEDIA"
echo "$PMEDIA" > $tmpsel

############BIG LOOP###############
while [ 1 ];do #BIG LOOP

PROBEDISK="`probedisk 2> /tmp/probediskerr.txt`"
case $PMEDIA in
 usbflash)
  USBDRIVES="`echo "$PROBEDISK" | grep '|usbdrv|' | cut -f 3 -d '/' | cut -f 1 -d '|' | tr "\n" " "`"
  SELECTIONS=""
  for ONEUSB in $USBDRIVES
  do
   [ "$ATADRIVES" != "" ] && [ "`echo "$ATADRIVES" | grep "$ONEUSB"`" != "" ] && continue 
   [ "$TAGPMEDIA" = "usbflash" ] && [ "`cat /sys/block/$ONEUSB/removable`" != "1" ] && continue
   [ "$TAGPMEDIA" = "iusbflash" ] && [ "`cat /sys/block/$ONEUSB/removable`" != "0" ] && continue
   USBINFO="`echo "$PROBEDISK" | grep "/dev/$ONEUSB" | head -n 1 | cut -f 3 -d '|' | tr '$' " " | tr ',' ' ' | tr -s " " | sed -e 's/ $//'`"
   USBSIZE="`disktype /dev/$ONEUSB | grep "Block device" | cut -f 2 -d ',' | tr -s " " | cut -f 1-4 -d " "`"
   SELECTIONS="$ONEUSB \"${USBINFO}, ${USBSIZE}\""
   echo "$SELECTIONS" >> $tmpsel
  done
 ;;
 usbhd)
  USBDRIVES="`echo "$PROBEDISK" | grep '|usbdrv|' | cut -f 3 -d '/' | cut -f 1 -d '|' | tr "\n" " "`"
  SELECTIONS=""
  for ONEUSB in $USBDRIVES
  do
   [ "$ATADRIVES" != "" ] && [ "`echo "$ATADRIVES" | grep "$ONEUSB"`" != "" ] && continue 
   [ "`cat /sys/block/$ONEUSB/removable`" != "0" ] && continue
   USBINFO="`echo "$PROBEDISK" | grep "/dev/$ONEUSB" | head -n 1 | cut -f 3 -d '|' | tr '$' " " | tr -s " " | sed -e 's/ $//g'`"
   USBSIZE="`disktype /dev/$ONEUSB | grep "Block device" | cut -f 2 -d ',' | tr -s " " | cut -f 1-4 -d " "`"
   SELECTIONS="$ONEUSB \"${USBINFO}, ${USBSIZE}\""
   echo "$SELECTIONS" >> $tmpsel
  done
 ;;
 cd)
  #find cd drives... 
  CDDRIVES="`echo "$PROBEDISK" | grep '|optical|' | cut -f 3 -d '/' | cut -f 1 -d '|' | tr "\n" " "`"
  SELECTIONS=""
  for ONEHD in $CDDRIVES
  do
   HDINFO="`echo "$PROBEDISK" | grep "/dev/$ONEHD" | head -n 1 | cut -f 3 -d '|' | tr '$' " " | tr -s " " | sed -e 's/ $//'`"
   HDSIZE="`disktype /dev/$ONEHD | grep "Block device" | cut -f 2 -d ',' | tr -s " " | cut -f 1-4 -d " "`"
   CDERROR=$(gettext "Installing to CD/DVD is not supported by this program. Please use the 'Remaster Puppy Live CD' program or save back to CD/DVD at shut down which requires a CD-RW or DVD-RW.")
   [ "$HDSIZE" = "" ] && gtkdialog-splash -bg orange -timeout 12 -close box -text "$CDERROR" && exit
   SELECTIONS="$ONEHD \"${HDINFO}, ${HDSIZE}\""
   echo "$SELECTIONS" >> $tmpsel
  done
  gui_func "$CDERROR"
  exit
 ;;
 # removed atazip 140630
 atahd|ataflash|scsihd) #v425 added scsihd
  #at bootup, init script creates ATADRIVES='' in /etc/rc.d/PUPSTATE...
  HDDRIVES="$ATADRIVES"
  #110126 need to include any memory card...
  CARDDRIVES="`echo "$PROBEDISK" | grep '|card|' | cut -f 3 -d '/' | cut -f 1 -d '|' | tr '\n' ' '`"
  for ONEHD in $HDDRIVES $CARDDRIVES
  do
   HDINFO="`echo "$PROBEDISK" | grep "/dev/$ONEHD" | head -n 1 | cut -f 3 -d '|' | tr '$' " " | tr -s " " | sed -e 's/ $//'`"
   HDSIZE="`disktype /dev/$ONEHD | grep "Block device" | cut -f 2 -d ',' | tr -s " " | cut -f 1-4 -d " "`"
   SELECTIONS="$ONEHD \"${HDINFO}, ${HDSIZE}\""
   echo "$SELECTIONS" >> $tmpsel
  done
 ;;
 *)
  pupmessage -bg '#FF8080' "$(gettext 'Not yet implemented')"
  exit
 ;;
esac

if [ "$SELECTIONS" = "" ];then
 MSGCHOOSE=$(gettext 'Nothing to choose. If a plugin device, did you plug it in?')
 Xdialog --wmclass "gtkdialog2" --left \
 --title "$(gettext 'Puppy Universal Installer')" \
 --ok-label "$(gettext 'QUIT')" \
 --msgbox "$MSGCHOOSE" 0 0
 exit
fi

gui_func 1 #precaution
SPLSHMSG=$(gettext "Please Wait..")
gtkdialog-splash -bg yellow -close box -text  "$SPLSHMSG" &
SPID=$!

unset SELECTIONS #120126 jemimah: if you choose to run gparted the drive selection
#window shows your drive twice in the drive selection window after you quit gparted.
echo "$PMEDIA" > $tmpsel

DRVSEL="`cat /tmp/retval`"
[ "$DRVSEL" = "" ] && exit

DRVINFO="`disktype /dev/$DRVSEL`" # disktype fails to identify f2fs
SUPERFS="" #100207 need extra test on gecko edubook...
if [ "`echo "$DRVINFO" | grep '^Partition 1: '`" = "" ];then
 SUPERFS="`echo "$DRVINFO" | grep "file system" | grep "^[a-zA-Z]" | tr -s " " | cut -f 1 -d " "`"
 #130128 cd/dvd, the f.s. name does not start on 1st column...
 [ "$SUPERFS" ] || \
 SUPERFS="`echo "$DRVINFO" | grep "file system" | head -n 1 | tr -s " " | cut -f 2 -d " "`"
 #...hmm, don't think will install to an optical media, but with udf you never know.
 # one last try #140630
 [ "$SUPERFS" ] || SUPERFS=`guess_fstype /dev/$DRVSEL` # for f2fs
fi
DRVSIZE="`echo "$DRVINFO" | grep "Block device" | cut -f 2 -d ',' | tr -s " " | cut -f 1-4 -d " "`"
#DRVSIZM="`echo "$DRVSIZE" | cut -f 3 -d " "`" #cut can be ambiguous depending if there is leading space
DRVSIZM="`echo "$DRVSIZE" | awk '{print $2}'`"
[ "`echo "$DRVSIZE" | grep "GiB"`" != "" ] && DRVSIZM="`LANG=C dc $DRVSIZM 1000 \* p`" #120202
IDRVSIZM="`echo -n "$DRVSIZM" | cut -f 1 -d '.'`" #truncate to integer.

DRVMSG=""
#PUPINST=""
if [ "$SUPERFS" ];then
 fsfunc $SUPERFS #returns FSTYPE
 linux_func $FSTYPE $DRVSEL
 #[ $? -ne 0 ] && PUPINST=", Puppy version $PUPVEROLD installed"
 [ "$DISTRO" = "none" ] && INST="" || INST=", $DISTRO installed"
 DRVMSG="${DRVSEL}:  ${FSTYPE}, ${DRVSIZE}${INST}
"
else
 #has mbr and partitions...
 echo -n "" > /tmp/pidrvmsg.txt
 echo "$DRVINFO" |
 while read ONELINE
 do
  #100207 edubook: after showing partitions, shows a superfloppy, ignore...
  if [ "`echo "$ONELINE" | grep 'file system' | grep '^[a-zA-Z]'`" != "" ];then #100207
   [ "`echo "$DRVINFO" | grep "$ONELINE"  | grep '^[a-zA-Z]'`" != "" ] && break #need any leading spaces.
  fi
   #130526 npierce: fix for GPT partitions... 
   #ref: http://www.murga-linux.com/puppy/viewtopic.php?p=704919#704919
  xPARTNUM="`echo "$ONELINE" | grep -E '^Partition ..?:' | tr -s " " | cut -f 1 -d ':' | cut -f 2 -d " "`"
  if [ ! "$xPARTNUM" = "" ];then #110126
   PARTNUM="$xPARTNUM"
   PARTSIZE="`echo "$ONELINE" | cut -f 2 -d ':' | tr -s " " | cut -f 2-3 -d " "`"
   [ "`echo -n "$DRVSEL" | grep 'mmcblk'`" != "" ] && PARTNUM="p${PARTNUM}" #ex: mmcblk0p1
  fi
  PARTITIONFS="`echo "$ONELINE" | grep "file system" | cut -f 1 -d " "`"
  if [ ! "$PARTITIONFS" ];then # might be f2fs #140710 but check it's not extended (unknown)
    UNK=`guess_fstype /dev/${DRVSEL}${xPARTNUM} 2>/dev/null` 
    [ "$UNK" = "unknown" ] && continue || PARTITIONFS="$UNK"
  fi
  if [ "$PARTITIONFS" ];then
   fsfunc $PARTITIONFS #sets FSTYPE.
   #find out if partition has puppy, or has ${DISTRO_PUPPYSFS} file...
   if [ "$FSTYPE" ];then #100412 fix from EdV.
    echo $FSTYPE
    #PUPINST=", Puppy version installed" #110422 $PUPVEROLD deleted ispupfunc() #140701
    linux_func $FSTYPE ${DRVSEL}${PARTNUM}
    [ "$DISTRO" = "none" ] && INST="" || INST=", $DISTRO installed"
    echo "${DRVSEL}${PARTNUM}: ${FSTYPE}, size ${PARTSIZE}${INST}" >> /tmp/pidrvmsg.txt #${PUPINST}
   fi
  fi
 done
 F2FSINST=`grep -o '2: f2fs' /tmp/pidrvmsg.txt`
 FIRSTPART=`grep -o -n MiB /tmp/pidrvmsg.txt` #first line is first partition
 if [ "$F2FSINST" ] && [ "$FIRSTPART" = "1:MiB" ];then #hack
   FIRSTPARTSIZE=`grep "1:" /tmp/pidrvmsg.txt | awk '{print $4}'` #should be safe as we filtered for MiB
   if [ "$FIRSTPARTSIZE" -lt 100 ];then #asumption that a small part is boot for f2fs
     sed -i -e 's%Unknown Linux installed%Boot partition for f2fs%' \
		    -e 's%full%f2fs full%' /tmp/pidrvmsg.txt
     sed -i '1s%true%false%' /tmp/buttonstate
     echo '1' > /tmp/is_f2fs
   fi
 fi
 DRVMSG="`cat /tmp/pidrvmsg.txt`"
fi

[ "$PMEDIA" = "cd" ] || free_space_func /dev/"$DRVSEL" #returns free space (unpartitioned on drive)
freespace=$f #MB
if [ "$freespace" -gt 32 ];then #I would reclaim less than that but..
unit=MB
  if [ "$freespace" -gt 1024 ];then
    unit=GB
    freespace=$(($freespace / 1024)) #GB
  fi
  export FDLG='<window title="Puppy Universal Installer" window-position="1">
    <vbox>
      <hbox space-expand="true" space-fill="false">
        <frame>
          <text use-markup="true"><label>"<span fgcolor='"'green'"'><big><b>Unpartitioned Space!</b></big></span>"</label></text>
          <text use-markup="true">
            <label>"'$(eval_gettext "Puppy has found that you have some unpartitioned space on your $DRVSEL drive.")'"</label>
          </text>
          <text use-markup="true"><label>"<big><big><b>'"$freespace $unit"'</b></big></big>"</label></text>
          <text use-markup="true">
            <label>"'$(eval_gettext "We recommend that you use the <b>GParted</b> partition manager program in the next window to reclaim this space by making a new partition or click the <b>Web</b> button to learn about Gparted and run this program later.")'"</label>
          </text>
        </frame>
      </hbox>
      <hbox homogeneous="true">
        <button ok></button>
        <button>
          <label>Web</label>
          <input file>/usr/share/pixmaps/puppy/internet.svg</input>
          <width>20</width>
          <action type="exit">web</action>
        </button>
      </hbox>
    </vbox>
  </window>'
  eval `gtkdialog -p FDLG -c`
  if [ "$EXIT" = "web" ];then
    kill -9 $SPID
    exec ${DEFAULTBROWSER} http://gparted.org/documentation.php
    exit
  fi
fi

DLGTXT=""
CHOICES="`echo "$DRVMSG" | grep ':' | cut -f 1 -d ':' | tr "\n" " "`"
cnt=0
for ONECHOICE in $CHOICES
do
 state=`grep ${ONECHOICE} /tmp/buttonstate`
 state=${state##*=}
 ACTION=${ONECHOICE}
 [ -f "/tmp/is_f2fs" ] && ACTION=f2fs_install
  
 DLGTXT="$DLGTXT
   <hbox space-expand=\"false\" space-fill=\"false\">  
    <text space-expand=\"true\" space-fill=\"true\" xalign=\"0.2\"><label>$(gettext 'Install Puppy to') ${ONECHOICE}:</label></text>
    <button sensitive=\"$state\">
     <input file>/usr/share/pixmaps/puppy/puppy_install.svg</input>
     <width>24</width>
     <variable>${ONECHOICE}btn</variable>
     <action>Exit:${ACTION}</action>
    </button>
   </hbox>
"
cnt=$(($cnt + 1)) # for hbox height
done
echo $cnt > /tmp/cnt
case "$cnt" in
  1|2)h=100;; #min
  3)h=120;;
  4)h=160;;
  5)h=200;;
  *)h=240;; #max
esac

if [ "$FINALPMEDIA" = "usbflash" ];then
 MSGex1="<text><label>`gettext \"IMPORTANT: If you use GParted, after creating the partition be sure to set the 'boot' flag (right-click: Manage flags). Also, for a USB Flash drive choose fat16 filesystem as easier to boot from (than fat32) -- however a ext2 or ext3 partition will also work.\"`</label></text>
<text><label>`gettext \"Extra note: Gparted may show invalid partitions on the drive, marked with a '!' icon -- delete it/them and recreate a partition (and make it bootable)\"`</label></text>"
else
 MSGex1="<text><label>`gettext \"IMPORTANT: If you use GParted, after creating the partition be sure to set the 'boot' flag (right-click: Manage flags).\"`</label></text>
<text><label>`gettext \"Extra note: Gparted may show invalid partitions on the drive, marked with a '!' icon -- delete it/them and recreate a partition (and make it bootable)\"`</label></text>"
fi
if [ "$DLGTXT" = "" ];then
 DLGTXT="<text><label>`eval_gettext \"No partitions on drive \\\${DRVSEL}! It is recommended that you run GParted now to create a partition:\"`</label></text>
    <button>
     <input file>/usr/share/icons/hicolor/24x24/apps/gparted.png</input>
     <width>24</width>
     <label>GParted</label>
     <action>EXIT:RUNGPARTED</action>
    </button>
$MSGex1
<text><label>\" \"</label></text>
"
fi

DLGEXT=""

DLGEXT1='
   <frame '$(gettext "Filesystem in partition")'>
    <hbox space-expand="true" space-fill="false">
     <text space-expand="true" space-fill="true" xalign="0" width-chars="75"><label>"'"`eval_gettext "WARNING: Notice the filesystem in the intended destination, is that what you want? In particular, if the f.s. is 'vfat' or 'ntfs' you might want to replace these with a Linux ext2 or ext3 f.s. if the partition can be taken over totally for Puppy's use. If you are installing to an internal drive of a PC, it is highly recommended that the f.s. be a Linux ext2 or ext3. Puppy can install to vfat/ntfs but limited to "frugal" with "${DISTRO_FILE_PREFIX}save" only. FAT is particularly limited, slow and also has severe bootup options, so get rid of it if at all possible. Click the button to run GParted, which will enable you to examine and modify the partitions."`"'"</label></text>
     <vbox>
      <button image-position="Bottom" width-request="100" tooltip-text="GParted is a partition manager much like Partition Magic (TM)">
		<label>'GParted'</label>
		<input file>/usr/share/icons/hicolor/48x48/apps/gparted.png</input>
		<action>EXIT:RUNGPARTED</action>
	  </button>
      
     </vbox>
    </hbox>
   </frame>'
if [ "`echo "$DRVMSG" | cut -f 1 -d ':' | grep "[a-zA-Z]$"`" = "" ];then
#ask if want superfloppy install...
DLGEXT='
   <frame Superfloppy>
   <hbox space-expand="true" space-fill="false">
    <text space-expand="true" space-fill="true" xalign="0" width-chars="75"><label>"'"`eval_gettext "WARNING: NOT NORMALLY RECOMMENDED. Do you want to install Puppy in Superfloppy mode, that is, no MBR and no partitions? ...in that case, the drive will be accessed as /dev/$DRVSEL, without a partition number. This may be a good choice for booting USB Flash drive from PC with a quirky BIOS. Click button if yes:"`"'"</label></text>
    <vbox>
     <button image-position="Bottom" width-request="100" tooltip-text="SuperFloppy!">
      <label>'SuperFloppy'</label>
	  <input file>/usr/share/pixmaps/puppy/puppy_save.svg</input>
      <width>48</width>
      <action>EXIT:REFORMAT_${DRVSEL}</action>
     </button>
    </vbox>
   </hbox>
   </frame>
'
else #drive is a superfloppy.
 DLGEXT="$DLGEXT
   <frame Superfloppy on USB>
   <hbox space-expand=\"true\" space-fill=\"false\">
    <text space-expand=\"true\" space-fill=\"true\" xalign=\"0\" width-chars=\"75\"><label>`eval_gettext \"The drive is currently in Superfloppy mode and has no MBR and no partitions. It is accessed as /dev/\\\$DRVSEL, without a partition number (this may be a good choice for booting USB Flash drive from PC with a quirky BIOS). Do you want to partition the drive so that it has a /dev/\\\${DRVSEL}1 partition, in which Puppy can be installed? If so, click button:\"`</label></text>
    <vbox>
     <button image-position=\"Bottom\" width-request=\"100\" tooltip-text=\"SuperFloppy!\">
      <label>\"SuperFloppy\"</label>
	  <input file>/usr/share/pixmaps/puppy/puppy_save.svg</input>
      <width>48</width>
      <action>EXIT:REFORMAT_${DRVSEL}</action>
     </button>
    </vbox>
   </hbox>
   </frame>
"
fi
case $PMEDIA in
atahd|ataflash|scsihd|usbhd)DLGEXT="";;
esac
kill -9 $SPID

# maybe 2 windows installations.
#140707 #dont break
while read win;do
  t=`echo $win | grep 'false'`
  if [ "$t" ];then
    part=${t%%_*}
    action=''$action'
    <action>if true enable:'${part}'btn</action>
	<action>if false disable:'${part}'btn</action>'
 fi
done < /tmp/buttonstate
#echo "$action" ###debug
[ -f /tmp/win ] && \
DISTROZ=`grep -E "Older Windows|New Windows|Windows 64 bit|New Windows boot|Windows boot" /tmp/win|head -n1` #doesn't matter which one
if [ "$DISTROZ" ];then
  case "$DISTROZ" in
    "Older Windows"|"New Windows"|"Windows 64 bit"|"New Windows boot"|"Windows boot") #140711 added
    WMSG=$(gettext "CAUTION: Be sure your Windows partition is defragmented and <u>not hibernated</u>!")
    ;;
    *)WMSG=$(gettext "CAUTION: Be sure your Windows is defragmented!")
    ;;
  esac
fi
DLGWIN=''``
if grep -q 'false' /tmp/buttonstate;then
  if grep -q 'f2fs' /tmp/pidrvmsg.txt;then
    echo "f2fs install"
  else
    DLGWIN='<text use-markup="true" space-expand="true" space-fill="true" xalign="0" width-chars="80">
      <label>"<span fgcolor='"'darkred'"'><b>'$WMSG'</b></span>"</label>
    </text>
    <checkbox>
      <label>"'$(gettext "Check this box to enable the button to install to your Windows installation partition.")'"</label>
      <default>false</default>
	  <variable>cb0</variable>
	  '$action'
    </checkbox>'
  fi
fi

DLG2="
<window title=\"$(gettext 'Puppy Universal Installer')\">
 <vbox>
  
  $DLGWIN
  
  <hbox height-request=\"$h\" width-request=\"780\">
   <vbox space-expand=\"true\" space-fill=\"true\">
    <hbox>
      <vbox scrollable=\"true\">
      
      <text><label>$(gettext 'Here is what Puppy has found out about the chosen drive:')</label></text>
      <text use-markup=\"true\"><label>\"<tt>${DRVMSG}</tt>\"</label></text>
      
      </vbox>
     </hbox>
    </vbox>
     
     <vbox space-expand=\"true\" space-fill=\"false\" scrollable=\"true\">
$DLGTXT

    </vbox>
   </hbox>
   
$DLGEXT1
   
$DLGEXT
  
  <hbox>
   <button>
    <input file stock=\"gtk-redo\"></input>
    <label>$(gettext 'Go back and choose a different drive')</label>
    <action>EXIT:GOBACK</action>
   </button>
   <button cancel></button>
  </hbox>
 </vbox>
</window>
"

RETTXT=$(echo "$DLG2" | ${GTKDIALOGEXE} --stdin -c)
eval "$RETTXT"
rm /tmp/{buttonstate,win,cnt,num*,ntfsmnterr*,is_f2fs} 2>/dev/null #cleanup
#returns EXIT="Cancel" or EXIT="abort"...]
case "$EXIT" in 
  Cancel|abort) exit ;;
  RUNGPARTED)gpartedfixfunc $DRVSEL ; continue ;; #BIG LOOP
  f2fs_install)f2fs_func ; exit ;;
  GOBACK)continue ;; #BIG LOOP
esac

#sanity check...
SANITYPART="`echo -n "$RETTXT" | grep '^EXIT' | cut -f 2 -d '"' | cut -f 2 -d '_'`" #'geany
smPTN="^/dev/${SANITYPART} " #111011
SANITYMNTPT="`mount | grep "$smPTN" | tr -s " " | cut -f 3 -d " "`"

if [ ! "$SANITYMNTPT" = "" ];then
 SANITYMSG1="$SANITYPART $(gettext 'currently mounted, need to unmount it!!!')"
 SANITYMSG2="$SANITYPART $(gettext 'currently mounted read-write.')"
 SANITYMSG3="`eval_gettext \"\\\$SANITYPART does not have \\\${DISTRO_FILE_PREFIX}save.3fs file in it.\"`"
 SANITYRORW="`mount | grep "/$SANITYPART" | tr -s " " | cut -f 6 -d " "`"
 [ ! "`echo -n "$SANITYMNTPT" | grep "/initrd/"`" = "" ] && SANITYMSG1="$SANITYPART $(gettext 'cannot be unmounted!')"
 [ "$SANITYRORW" = "(ro)" ] && SANITYMSG2="$SANITYPART $(gettext 'is mounted read-only!')"
 [ -f $SANITYMNTPT/${DISTRO_FILE_PREFIX}save.3fs ] && SANITYMSG3="`eval_gettext \"\\\$SANITYPART has personal file \\\${DISTRO_FILE_PREFIX}save.3fs in it!\"`"
 Xdialog --wmclass "gtkdialog2" --left \
--title "$(gettext 'Puppy universal installer: SANITY CHECK')" \
--ok-label "$(gettext 'CONTINUE')" --cancel-label "$(gettext 'QUIT')" \
--yesno "`eval_gettext \"A partition sanity check before continuing:\
\n\\\$SANITYMSG1 \n\\\$SANITYMSG2 \n\\\$SANITYMSG3 \n\nIf the above report \
is that the partition is mounted, you need to unmount it\nright now, but if \
report is it cannot be unmounted, then there may be install\ndifficulties -- \
certainly, in the case of install to hard drive you will only\nbe able to do \
an 'option1' install (explained further on).\nIf the partition has \
\\\${DISTRO_FILE_PREFIX}save.3fs in it, that is the reason it cannot be\
\nunmounted -- it is currently in use by Puppy. If booting Puppy from \
live-CD,\nif \\\${DISTRO_FILE_PREFIX}save.3fs does not exist then Puppy will \
run totally in RAM, which will\nsolve the problem -- but you should not \
delete or rename \\\${DISTRO_FILE_PREFIX}save.3fs right\nnow as it is in \
use. Instead, boot some other OS then rename the file, then\nboot Puppy \
live-CD.\n\nBottom-line, unmount the partition now, if you can, otherwise \
CONTINUE script\nwith the restricted install choice, or QUIT script now and \
try to fix the\nproblem of the partition getting mounted...\"`" 0 0
 [ ! $? -eq 0 ] && exit
fi

if [ ! "`echo "$RETTXT" | grep 'EXIT:REFORMAT'`" = "" ];then
 #RETTXT will have EXIT="REFORMAT_sda" or EXIT="REFORMAT_sda1", depending whether
 #we want to format in superfloppy mode or with a MBR and a partition...
 DESTPART="`echo "$RETTXT" | grep '^EXIT' | grep 'REFORMAT' | cut -f 2 -d '"' | cut -f 2 -d '_'`" #'geany
 if [ "`echo "$DESTPART" | grep "[0-9]$"`" = "" ];then
  #want superfloppy...
DLG3="
<window title=\"$(gettext 'Puppy Universal Installer')\">
 <vbox>
   <text><label>`eval_gettext \"You have chosen to format /dev/\\\${DESTPART} as a Superfloppy. You now need to choose whether to create a vfat or a ext3 filesystem on the drive. Ext3 is the most flexible (and recommended) choice, however if you need to be able to read the drive from Windows then choose vfat. Note, however, there are tools for accessing ext2/3 drives in Windows.\"`</label></text>
   <text><label>`eval_gettext \"A further clarification: if you choose ext3, then the entire drive will be immediately available for personal data storage, whereas if you choose vfat this installer will have to create a file with a ext3 filesystem inside it (\\\${DISTRO_FILE_PREFIX}save.3fs) -- that file will contain all your personal files. Press FORMAT_ext3 or FORMAT_vfat button to commence formatting...\"`</label></text>
  <hbox>
   <button>
    <label>$(gettext 'FORMAT_ext3')</label>
    <action>EXIT:ext3</action>
   </button>
   <button>
    <label>$(gettext 'FORMAT_vfat')</label>
    <action>EXIT:vfat</action>
   </button>
  </hbox>
 </vbox>
</window>
"
  RETTXT="`echo "$DLG3" | ${GTKDIALOGEXE} --stdin -c`"

  [ ! "`echo "$RETTXT" | grep 'abort'`" = "" ] && exit
  DESTFS="`echo "$RETTXT" | grep 'EXIT:' | cut -f 2 -d ':'`"
  echo '#!/bin/sh' > /tmp/formatsuper.sh
  echo -e "echo -n \"$(gettext 'SANITY CHECK: formatting /dev/')${DRVSEL}, $(gettext 'ENTER to continue or CTRL-C to quit:') \""  >> /tmp/formatsuper.sh  # $(gettext 'SANITY CHECK: formatting /dev/') $(gettext 'ENTER to continue or CTRL-C to quit:') for xgettext to detect lines
  echo 'read mooo'  >> /tmp/formatsuper.sh
  echo "dd if=/dev/zero of=/dev/$DRVSEL bs=512 count=1" >> /tmp/formatsuper.sh
  echo 'sync'  >> /tmp/formatsuper.sh
  if [ "$DESTFS" = "ext3" ];then
   echo 'echo "'$(gettext 'Creating an ext3 filesystem on the drive...')'"'   >> /tmp/formatsuper.sh
   echo -e "echo \"y\" | mke2fs -j -m 0 /dev/$DRVSEL"   >> /tmp/formatsuper.sh
  else #vfat
   echo 'echo "'$(gettext 'Creating a vfat filesystem on the drive...')'"'   >> /tmp/formatsuper.sh
   echo "mkdosfs -I /dev/$DRVSEL" >> /tmp/formatsuper.sh
  fi
  echo 'sync'  >> /tmp/formatsuper.sh
  echo 'echo -n "'$(gettext 'Press ENTER key to continue:')' "'   >> /tmp/formatsuper.sh
  echo 'read moo'   >> /tmp/formatsuper.sh
  chmod 755 /tmp/formatsuper.sh
  xterm -bg "$BG" -fg "$FG" -title "$(gettext 'Puppy Universal Installer')" -geometry 80x10 -e /tmp/formatsuper.sh
  DESTSIZM="$DRVSIZM"
  IDESTSIZM="$IDRVSIZM"
  DESTVER="" #previous version, n.a.
  #w015 need to refresh desktop drive icons(s)...
  #/tmp/pup_event_frontend_block_request is used in /sbin/pup_event_frontend_d to refresh drv...
  sync
  #130722 ...this file was read by the old /sbin/pup_event_frontend_d big script.
  #now have a binary daemon, send this uevent to it, then /usr/local/pup_event/frontend_change will be called...
  echo change > /sys/block/${DRVSEL}/uevent
 else
  #want to partition with partition 1...
DLG3="
<window title=\"$(gettext 'Puppy Universal Installer')\">
 <vbox>
   <text><label>`eval_gettext \"You have chosen to format /dev/\\\${DRVSEL} with a MBR and a partition. You now need to choose whether to create a vfat or a ext3 filesystem on the partition. Ext3 is the most flexible (and recommended) choice, however if you need to be able to read the drive from Windows then choose vfat. Note, however, there are tools for accessing ext2/3 drives in Windows.\"`</label></text>
   <text><label>`eval_gettext \"A further clarification: if you choose ext3, then the entire partition will be immediately available for personal data storage, whereas if you choose vfat then Puppy will have to create a file with a ext2 filesystem inside it (\\\${DISTRO_FILE_PREFIX}save.2fs) -- that file will contain all your personal files.\"`</label></text>
   <text><label>$(gettext 'USB Flash drives: it is most common to create a vfat (fat16) partition on these, for Windows interchangeability and some older BIOSes boot only if see a fat16 f.s.')</label></text>
   <text><label>\" \"</label></text>
   <text><label>$(gettext 'Click button to run GParted...')</label></text>
  <hbox>
   <button>
    <label>$(gettext 'Run GParted')</label>
    <action>EXIT:gparted</action>
   </button>
   <button cancel>
   </button>
  </hbox>
 </vbox>
</window>
"
  RETTXT="`echo "$DLG3" | ${GTKDIALOGEXE} --stdin -c`"
  MYACTION="`echo "$RETTXT" | grep 'EXIT:' | cut -f 2 -d ':'`"
  if [ "$MYACTION" = "gparted" ];then
   #w015 if a superfloppy, first need to create a disk label, else gparted is upset...
   if [ "`echo "$DRVMSG" | cut -f 1 -d ':' | grep "[a-zA-Z]$"`" != "" ];then
    dd if=/dev/zero of=/dev/$DRVSEL bs=512 count=1
    parted --script /dev/sdb mklabel msdos
    sync
   fi
   #exec gparted /dev/$DRVSEL
   gpartedfixfunc $DRVSEL #w015
   continue #BIG LOOP
  fi
  #exit
  
 fi
else
  DESTPART="`echo "$RETTXT" | grep '^EXIT' | cut -f 2 -d '"'`" #'geany
 DESTFS="`echo "$DRVMSG" | grep "${DESTPART}:" | cut -f 1 -d ',' | tr -s " " | cut -f 2 -d " "`" #120310
 DESTSIZE="`echo "$DRVMSG" | grep "${DESTPART}:" | tr -s " " | cut -f 4-5 -d " "`" #120310
 DESTSIZM="`echo -n "$DESTSIZE" | cut -f 1 -d " "`"
 if [ ! "`echo "$DESTSIZE" | grep "GiB"`" = "" ];then
  DESTSIZM="`LANG=C; dc $DESTSIZM 1000 \* p`" #LANG=C; rodin.s for i18n
 fi
 IDESTSIZM="`echo -n "$DESTSIZM" | cut -f 1 -d '.'`" #truncate to integer.
 DESTVER="`echo "$DRVMSG" | grep "${DESTPART}:" | tr -s " " | grep "installed" | cut -f 8 -d " "`" #120310
fi

break
done #BIG LOOP
###########END BIG LOOP###############


#131213 shinobar: check if extlinux supports ext4
case $FINALPMEDIA in
 usbflash|usbhd|ataflash|atazip) #removable media, install with syslinux/extlinux
  EXTLINUX="extlinux"
  if [ "$DESTFS" = "ext4" ]; then
    EXTLINUX=""
    for D in $(echo $PATH| tr ':' ' '); do
      [ -x $D/extlinux ] || continue
      [ "$($D/extlinux --version 2>&1 | cut -s -d' ' -f2 | cut -d'.' -f1)" = "4" ] && EXTLINUX="$D/extlinux" && break
    done
    if [ -z "$EXTLINUX" ]; then
      MSG=$(gettext "You need 'Extlinux version 4.x' for frugal install on 'ext4' partitions.")
      which frugalinstaller &>/dev/null && MSG=$(gettext "'Puppy Universal Installer' cannot handle frugal install on 'ext4' partitions.
Use 'Frugal Installer' instead.")
      Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy Universal Installer: ERROR')" --msgbox "$MSG" 0 0
      exit 1
    fi
  fi
esac

if [ "`echo "$DESTPART" | grep "[0-9]$"`" = "" ];then
 #superfloppy
DLG4="
<window title=\"$(gettext 'Puppy Universal Installer')\">
 <vbox>
  <frame>
   <text><label>$(gettext 'You have chosen to install Puppy to the entire drive, which is what we call Superfloppy mode, in which there is no MBR (Master Boot Record) and no partitions. This may be a good choice for Flash memory, especially USB Flash, that the PC BIOS has trouble booting from.')</label></text>
   <text><label>`eval_gettext \"The target drive is \\\${DESTPART}, which has a \\\${DESTFS}, of size \\\${DESTSIZM}M. Press OK button to install Puppy to the drive...\"`</label></text>
  </frame>
  <hbox><button ok></button><button cancel></button></hbox>
 </vbox>
</window>
"
 RETTXT="`echo "$DLG4" | ${GTKDIALOGEXE} --stdin -c`"
else
DLG4="
<window title=\"$(gettext 'Puppy Universal Installer')\">

 <vbox>
  <hbox space-expand=\"true\" space-fill=\"false\">
   <frame>
    <text><label>`eval_gettext \"You have chosen to install Puppy to partition /dev/\\\${DESTPART}, which has a \\\${DESTFS} filesystem and the size is \\\${DESTSIZM}M.\"`</label></text>
    <text><label>$(gettext 'Press OK button to install Puppy (note, there will be one or two more dialog windows before you have to commit to the actual install)...')</label></text>
   </frame> 
  </hbox>
  <hbox><button ok></button><button cancel></button></hbox>
 </vbox>
</window>
"
 RETTXT="`echo "$DLG4" | ${GTKDIALOGEXE} --stdin -c`"
fi

[ ! $? -eq 0 ] && exit
[ "`echo "$RETTXT" | grep 'OK'`" = "" ] && exit



#get latest Puppy files....
locate_puppy_src_files


#find out if puppy already on dest...
#ispupfunc $DESTFS $DESTPART
#...sets PUPVEROLD. #deleted 140701

#now to do the actual install...
case $FINALPMEDIA in
 usbflash|usbhd|ataflash|atazip) #removable media, install with syslinux/extlinux

  #26mar06 JustGreg fix mbr...
  if [ "`echo "$DESTPART" | grep '[0-9]$'`" != "" ];then #superfloppy mode has no mbr.
   #DESTPART is a partition (not superfloppy).
   DESTDRV="`echo -n "$DESTPART" | sed -e 's/[0-9]*$//' -e 's/p$//'`" #110126 mmcblk0p1 becomes mmcblk0
   DESTNUM="`echo -n "$DESTPART" | cut -c 4-5`"
   [ "`echo -n "$DESTPART" | grep 'mmc'`" != "" ] && DESTNUM="`echo -n "$DESTPART" | rev | cut -c 1 | rev`" #110126 mmcblk0p1 becomes 1 -- note, left off the p.
   dd if=/dev/$DESTDRV of=/tmp/mbr446.bin bs=446 count=1
   sync
   
   #v3.95
   ALPHADUMP="`hexdump -C /tmp/mbr446.bin | cut -f 2 -d '|' | tr '\n' ' ' | tr -d ' '`"
   #this will be =1 if mbr has 'GRUB' in it, else =0...
   FLAGGRUB="`echo "$ALPHADUMP" | grep 'GRUB' | wc -l`"
   
   #v3.97 have a case where normal bootable flash vfat pen drive has the
   #string 'GRUB.Geom.HardDisk.Read.Error' in the mbr...
   [ "$FLAGGRUB" = "1" ] && [ "`echo "$ALPHADUMP" | grep 'GRUB.Geom.HardDisk.Read.Error'`" != "" ] && FLAGGRUB="0"
   
   #if [ "`hexdump -x /tmp/mbr446.bin | tr -s " " | cut -f 2-9 -d " " | grep '[1-9a-zA-Z]'`" = "" -o "$FLAGGRUB" = "1" ];then
   #131213 shinobar: bootable mbr (space charactors removed before in ALPHADUMP)
   echo "$ALPHADUMP" | grep -q 'operatingsystem' && BOOTABLE="y" || BOOTABLE=""
   if [ -z "$BOOTABLE" ]; then
    MBRCHOICE="`Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --no-cancel --stdout --menubox "$(gettext 'The bootstrap loader code is missing from the MBR (Master Boot Record),\nmeaning that the drive will not boot. Please choose one of the\nfollowing to fix this. Personally, I have had success with mbr.bin.')" 0 0 4 sys-nopart.mbr "$(gettext 'Bootstrap loader code, created by JustGreg')" mbr.bin "$(gettext 'From the Syslinux package')" mbrfat.bin "$(gettext 'From the makebootfat package')" spb2_mbr.bin "$(gettext 'From spblinux, created by Christian Ostheimer')" `"
   else
    MBRCHOICE="`Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --no-cancel --stdout --menubox "$(eval_gettext \"The MBR (Master Boot Record) in the USB drive is probably okay as-is, so choose\nthe first option. However, if you cannot get the drive to boot, and you are\nsure that the BIOS settings are not to blame and the PC is capable of booting\nfrom USB, then try one of these alternative MBRs.\nNote, if you choose to change the MBR, the old first 446 bytes (the\nbootstrap loader part) of the MBR will be backed up to /tmp/oldmbr.bin\nNote, you can install these MBRs directly, without running this\nInstaller program again: they are located in /usr/share/syslinux,\nand you can use the dd program to copy them.\nExample: dd if=spb2_mbr.bin of=/dev/\\\${DESTDRV}\nNote, personally I have had success with mbr.bin.\")" 0 0 5 default "$(gettext 'DO NOTHING, leave current MBR alone')" sys-nopart.mbr "$(gettext 'Bootstrap loader code, created by JustGreg')" mbr.bin "$(gettext 'From the Syslinux package')" mbrfat.bin "$(gettext 'From the makebootfat package')" spb2_mbr.bin "$(gettext 'From spblinux, created by Christian Ostheimer')" `"
   fi
   rm /tmp/mbr446.bin
   if [ ! "$MBRCHOICE" = "" ];then
    if [ ! "$MBRCHOICE" = "default" ];then
     dd if=/dev/$DESTDRV of=/tmp/oldmbr.bin bs=446 count=1
     sync
     #dd if=/usr/lib/syslinux/$MBRCHOICE of=/dev/$DESTDRV
     dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/$MBRCHOICE of=/dev/$DESTDRV
     sync
    fi
   fi
   
   #check that partition has 'boot' flag set...
   QPATTERN="^Partition ${DESTNUM}:"
   if [ "`disktype /dev/$DESTDRV | grep "$QPATTERN" | grep 'bootable'`" = "" ];then
    Xdialog --wmclass "gtkdialog2" --left \
    --title "$(gettext 'Puppy Universal Installer')" --no-cancel --stdout \
--ok-label "GParted" --cancel-label "$(gettext 'bypass')" \
--yesno "`eval_gettext "Slight problem. It seems that partition \
\\\${DESTPART} does not have it's 'boot'"`
$(gettext 'flag set, which is required to make it bootable. Recommended to click the')
$(gettext "'GParted' button now to run GParted, then right-click on the partition and")
$(gettext "choose 'Manage flags' then set 'boot' flag.")
$(gettext 'After exiting from GParted, this Puppy Universal Installer will continue.')

$(gettext "Click 'GParted' (recommended) to run GParted...")
$(gettext "Click 'bypass' to bypass GParted...")" 0 0
    [ $? -eq 0 ] && gparted /dev/$DESTDRV
   fi
  fi

  case $DESTFS in
   vfat)
     if [ ! "`echo "$DESTPART" | grep '[0-9]$'`" = "" ];then
      PARTCHOICE="`Xdialog --wmclass "gtkdialog2" --left \
--title "$(gettext 'Puppy Universal Installer')" --no-cancel --stdout \
--menubox "$(gettext \"Please just accept the default here, 'JUST KEEP GOING'.\
\nPLEASE MAKE SURE TOP ENTRY HIGHLIGHTED THEN CLICK OK BUTTON\n\nHowever, \
if you have tried this Puppy Universal Installer\nbut Puppy will not boot, \
you can try one of the other choices here.\n\nPersonally, I have had success \
with using GParted to wipe and\nrecreate the partition...\")" 0 0 4 default \
"$(gettext 'JUST KEEP GOING')" GParted "$(gettext 'Wipe and recreate \
USB/IDE-CF partition using GParted')" syslinux "$(gettext 'Make USB or \
IDE-CF msdos/vfat partition bootable')"`"
      [ "$PARTCHOICE" = "syslinux" ] && fixusbfunc $DESTPART $DESTFS
      [ "$PARTCHOICE" = "GParted" ] && gpartedfixfunc $DRVSEL
     fi
    #params passed in: $DESTPART "$SRCPATH" $FINALPMEDIA
    echo '#!/bin/sh
DESTPART="$1"
SRCPATH="$2"
FINALPMEDIA="$3"
DISTRO_FILE_PREFIX="$4"
DISTRO_PUPPYSFS="$5"
DISTRO_ZDRVSFS="$6"
#DISTRO_DEVXSFS="$7"
DISTRO_ADRVSFS="$7"
DISTRO_YDRVSFS="$8"
PREFIX1CHAR="`echo -n "$DISTRO_FILE_PREFIX" | cut -c 1`"
echo "'$(gettext 'ABSOLUTE FINAL SANITY CHECK!')'"
echo "'$(gettext 'You are about to install Puppy to /dev/')'$DESTPART, '$(gettext 'with boot parameter')'"
echo "pmedia=$FINALPMEDIA. '$(gettext 'The files vmlinuz, initrd.gz,')' ${DISTRO_PUPPYSFS}, syslinux.cfg"
echo "('$(gettext 'and maybe the')' ${DISTRO_ZDRVSFS} '$(gettext 'if it exists.')' ${DISTRO_PUPPYSFS} '$(gettext 'may be inside')' initrd.gz)"
echo "'$(gettext 'will be written to /dev/')'$DESTPART '$(gettext 'and Syslinux will be used to make it bootable.')'"
echo
echo -n "'$(gettext 'Press ENTER key to continue, CTRL-C to abort:')' "
read goforit

#v3.95...
mount -t vfat /dev/$DESTPART /mnt/data
echo
echo "'$(gettext 'Do you want to wipe all files in /dev/')'$DESTPART? '$(gettext 'This is currently mounted')'"
echo "'$(gettext 'on /mnt/data so you can look at what is there before answering. You would')'"
echo "'$(gettext 'normally answer no if upgrading Puppy, or yes for a fresh install and')'"
echo "'$(gettext 'you want to get rid of old files that are just taking up space.')'"
echo "'$(gettext 'Note, you can also manually prune files in /mnt/data then choose no here.')'"
echo "'$(gettext 'Note, if you choose no here, this script will still do some basic deleting')'"
echo "'$(gettext 'of any files of an earlier Puppy installation')' ('$(gettext 'not the')' ${DISTRO_FILE_PREFIX}save '$(gettext 'though!')')."
echo
echo "'$(gettext 'No, press ENTER only to not wipe all files, or')'"
echo -n "'$(gettext 'Yes, press any alpha or numeric char then ENTER to wipe all files:')' "
read WIPEALL
if [ "$WIPEALL" != "" ];then
 echo
 echo "'$(gettext 'Deleting everything in /mnt/data...')'"
 rm -rf /mnt/data/*
else
 #well, minimum to get rid of...
 rm -f /mnt/data/image.gz 2> /dev/null
 rm -f /mnt/data/usr_cram.fs 2> /dev/null #old name.
 rm -f /mnt/data/pup_[0-9]*.sfs 2>/dev/null #old name.
 rm -f /mnt/data/${DISTRO_FILE_PREFIX}-*.sfs 2>/dev/null
 rm -f /mnt/data/devx_*.sfs 2>/dev/null
 rm -f /mnt/data/${DISTRO_FILE_PREFIX}z*.sfs 2>/dev/null #old name.
 rm -f /mnt/data/z${PREFIX1CHAR}*.sfs 2>/dev/null
 rm -f /mnt/data/${DISTRO_PUPPYSFS} 2>/dev/null #100913
 rm -f /mnt/data/${DISTRO_ZDRVSFS} 2>/dev/null #100913
 rm -f /mnt/data/${DISTRO_ADRVSFS} 2>/dev/null #140630
 rm -f /mnt/data/${DISTRO_YDRVSFS} 2>/dev/null #140630
 #rm -f /mnt/data/${DISTRO_DEVXSFS} 2>/dev/null #100913
fi
sync
#umount /mnt/data

echo
echo "'$(gettext 'Making')' $DESTPART '$(gettext 'bootable...')'"
#syslinux  /dev/$DESTPART #140701 extlinux writes to mount point no device!
extlinux -i /mnt/data ###/dev/$DESTPART
sync

#mount -t vfat /dev/$DESTPART /mnt/data
echo
echo "'$(gettext 'Copying vmlinuz...')'"
cp -f $SRCPATH/vmlinuz /mnt/data/
sync
echo "'$(gettext 'Creating syslinux.cfg...')'"
FCOPY=""
echo "'$(gettext 'Would you like syslinux.cfg to have')' 'pfix=copy' '$(gettext 'boot parameter?')'"
echo "'$(gettext 'this will cause')' ${DISTRO_PUPPYSFS} '$(gettext 'to be copied into RAM at bootup, which slows')'"
echo "'$(gettext 'bootup but speeds application startup slightly.')'"
echo "'$(gettext 'Probably good to answer yes here if PC has 256MB or more RAM.')'"
echo "'$(gettext '(you can edit syslinux.cfg later to change the choice made here)')'"
echo "('$(gettext 'note, Puppy will override this and not copy if there is insufficient RAM')')"
echo -n "'$(gettext 'ENTER key only for no, else any char then ENTER for yes:')' "
read FCOPY
[ "$FCOPY" != "" ] && FCOPY=" pfix=copy"

#100321 big assumption here, that current running kernel matches one being installed...
if [ -e /proc/ide ];then
 NODMA="ide=nodma" #old kernel
else
 #old ide=nodma does not work, need libata.dma=3 this enables DMA for Disk and CD/DVD but NOT for CF.
 NODMA="libata.dma=3"
fi
if [ ! -f $SRCPATH/boot.msg ];then #100321
 if [ "$FINALPMEDIA" = "ataflash" ];then
  echo "default vmlinuz initrd=initrd.gz ${NODMA} pmedia=${FINALPMEDIA}${FCOPY}" > /mnt/data/syslinux.cfg
 else
  echo "default vmlinuz initrd=initrd.gz pmedia=$FINALPMEDIA$FCOPY" > /mnt/data/syslinux.cfg
 fi
else
 #100321 want a boot menu...
 if [ "$FINALPMEDIA" = "ataflash" ];then
  echo "default puppy
display boot.msg
prompt 1
timeout 50

F1 boot.msg
F2 help.msg
F3 help2.msg

label puppy
kernel vmlinuz
append initrd=initrd.gz ${NODMA} pmedia=${FINALPMEDIA}${FCOPY}" > /mnt/data/syslinux.cfg
 else
  echo "default puppy
display boot.msg
prompt 1
timeout 50

F1 boot.msg
F2 help.msg
F3 help2.msg

label puppy
kernel vmlinuz
append initrd=initrd.gz pmedia=${FINALPMEDIA}${FCOPY}" > /mnt/data/syslinux.cfg
 fi
 cp -f $SRCPATH/boot.msg /mnt/data/
 cp -f $SRCPATH/help*.msg /mnt/data/
 cp -f $SRCPATH/logo.16 /mnt/data/
 sync
fi

echo ""
echo "'$(gettext 'Creating marker file for wakepup boot floppy...')'"
[ "$FINALPMEDIA" = "usbflash" ] && touch /mnt/data/USBFLASH
[ "$FINALPMEDIA" = "ataflash" ] && touch /mnt/data/IDEFLASH
[ "$FINALPMEDIA" = "usbhd" ] && touch /mnt/data/USBHD
[ "$FINALPMEDIA" = "atazip" ] && touch /mnt/data/IDEZIP
sync
echo "'$(gettext 'Copying initrd.gz...')'"
cp -f $SRCPATH/initrd.gz /mnt/data/
sync
echo "'$(gettext 'Copying .sfs files...')'"
cp -f $SRCPATH/${DISTRO_PUPPYSFS} /mnt/data/ 2>/dev/null
sync
for xdrv in ${DISTRO_ZDRVSFS} ${DISTRO_ADRVSFS} ${DISTRO_YDRVSFS};do
 [ -f $SRCPATH/"${xdrv}" ] && cp -f $SRCPATH/${xdrv} /mnt/data/ 2>/dev/null
 sync
done
#cp -f $SRCPATH/${DISTRO_DEVXSFS} /mnt/data/ 2>/dev/null
#sync
umount /mnt/data
echo -n "'$(gettext 'Finished, press ENTER key to continue:')' "
read moo
' > /tmp/installpup.sh
    chmod 755 /tmp/installpup.sh
    xterm -bg "$BG" -fg "$FG" -title "$(gettext 'Puppy Universal Installer')" \
-geometry 80x10 -e /tmp/installpup.sh $DESTPART $SRCPATH $FINALPMEDIA \
$DISTRO_FILE_PREFIX $DISTRO_PUPPYSFS $DISTRO_ZDRVSFS $DISTRO_ADRVSFS $DISTRO_YDRVSFS #$DISTRO_DEVXSFS
    ;;
   ext2|ext3|ext4)
    #params passed in: $DESTPART "$SRCPATH" $FINALPMEDIA
    echo '#!/bin/sh
DESTPART="$1"
SRCPATH="$2"
FINALPMEDIA="$3"
DESTFS="$4"
DISTRO_FILE_PREFIX="$5"
DISTRO_PUPPYSFS="$6"
DISTRO_ZDRVSFS="$7"
#DISTRO_DEVXSFS="$8"
DISTRO_ADRVSFS="$8"
DISTRO_ADRVSFS="$9"
PREFIX1CHAR="`echo -n "$DISTRO_FILE_PREFIX" | cut -c 1`"
echo "'$(gettext 'ABSOLUTE FINAL SANITY CHECK!')'"
echo "'$(gettext 'You are about to install Puppy to /dev/')'$DESTPART, '$(gettext 'with boot parameter')'"
echo "pmedia=$FINALPMEDIA. '$(gettext 'The files vmlinuz, initrd.gz,')' ${DISTRO_PUPPYSFS}, extlinux.conf"
echo "('$(gettext 'and maybe')' ${DISTRO_ZDRVSFS} '$(gettext 'if it exists.')' ${DISTRO_PUPPYSFS} '$(gettext 'may be inside initrd.gz')')"
echo "'$(gettext 'will be written to /dev/')'$DESTPART '$(gettext 'and Extlinux will be used to make it bootable.')'"
echo
echo -n "'$(gettext 'Press ENTER key to continue, CTRL-C to abort:')' "
read goforit
mount -t $DESTFS /dev/$DESTPART /mnt/data
if [ ! $? -eq 0 ];then #v3.95
 dpPATTERN="^/dev/$DESTPART "
 if [ "`mount | grep "$dpPATTERN"`" = "" ];then
  echo
  echo "/dev/$DESTPART '$(gettext 'failed to mount on /mnt/data ...nothing else mounted on')'"
  echo "/mnt/data? '$(gettext 'One known case for this failure is when installing to the')'"
  echo "'$(gettext 'internal Flash drive of a Intel Classmate laptop. Well, you can attempt')'"
  echo "'$(gettext 'to fix this problem by creating a new')' $DESTFS '$(gettext 'filesystem in')' $DESTPART ('$(gettext 'which')'"
  echo "'$(gettext 'will of course wipe anything previously there -- which will not matter')'"
  echo "'$(gettext 'if you have already backed up the previous operating system')')."
  echo "'$(gettext 'Press ENTER only to abort installation, or')'"
  echo "'$(gettext 'press any char then ENTER to create a new f.s. then proceed with install:')' "
  read NEWFS
  [ "$NEWFS" = "" ] && exit
  DFPARAM=""
  [ "$DESTFS" = "ext3" ] && DFPARAM="-j"
  echo "y" | mke2fs $DFPARAM -m 0 /dev/$DESTPART
  sync
  mount -t $DESTFS /dev/$DESTPART /mnt/data
  if [ ! $? -eq 0 ];then
   echo -n "'$(gettext 'Sorry, still cannot mount')' $DESTPART '$(gettext 'partition. Press ENTER to quit:')' "
   read NOWQUIT
   exit
  fi
 fi
fi
echo
echo "'$(gettext 'Do you want to wipe all files in /dev/')'$DESTPART? '$(gettext 'This is currently mounted')'"
echo "'$(gettext 'on /mnt/data so you can look at what is there before answering. You would')'"
echo "'$(gettext 'normally answer no if upgrading Puppy, or yes for a fresh install and')'"
echo "'$(gettext 'you want to get rid of old files that are just taking up space.')'"
echo "'$(gettext 'Note, you can also manually prune files in /mnt/data then choose no here.')'"
echo "'$(gettext 'Note, if you choose no here, this script will still do some basic deleting')'"
echo "'$(gettext 'of any files of an earlier Puppy installation')' ('$(gettext 'not the')' ${DISTRO_FILE_PREFIX}save '$(gettext 'though!')')."
echo
echo "'$(gettext 'No, press ENTER only to not wipe all files, or')'"
echo -n "'$(gettext 'Yes, press any alpha or numeric char then ENTER to wipe all files:')' "
read WIPEALL
if [ "$WIPEALL" != "" ];then
 echo
 echo "'$(gettext 'Deleting everything in /mnt/data...')'"
 rm -rf /mnt/data/*
else
 #well, minimum to get rid of...
 rm -f /mnt/data/image.gz 2> /dev/null
 rm -f /mnt/data/usr_cram.fs 2> /dev/null
 rm -f /mnt/data/pup_[0-9]*.sfs 2>/dev/null
 rm -f /mnt/data/${DISTRO_FILE_PREFIX}-*.sfs 2>/dev/null
 rm -f /mnt/data/devx_*.sfs 2>/dev/null
 rm -f /mnt/data/z${PREFIX1CHAR}*.sfs 2>/dev/null
 rm -f /mnt/data/${DISTRO_PUPPYSFS} 2>/dev/null #100913
 rm -f /mnt/data/${DISTRO_ZDRVSFS} 2>/dev/null #100913
 rm -f /mnt/data/${DISTRO_ADRVSFS} 2>/dev/null #140630
 rm -f /mnt/data/${DISTRO_YDRVSFS} 2>/dev/null #140630
 #rm -f /mnt/data/${DISTRO_DEVXSFS} 2>/dev/null #100913
fi
sync
echo
echo "'$(gettext 'Making')' $DESTPART '$(gettext 'bootable...')'"
if [ ! "`echo "$DESTPART" | grep '[0-9]$'`" = "" ];then
 '$EXTLINUX' -i /mnt/data #i think only use -z for superfloppy.
else
 '$EXTLINUX' -i -z /mnt/data
fi
sync
echo "'$(gettext 'Copying vmlinuz...')'"
cp -f $SRCPATH/vmlinuz /mnt/data/
sync
echo "'$(gettext 'Creating extlinux.conf...')'"
FCOPY=""
echo "'$(gettext 'Would you like extlinux.cfg to have')' 'pfix=copy' '$(gettext 'boot parameter?')'"
echo "'$(gettext 'this will cause')' ${DISTRO_PUPPYSFS} '$(gettext 'to be copied into RAM at bootup, which slows')'"
echo "'$(gettext 'bootup but speeds application startup slightly.')'"
echo "'$(gettext 'Probably good to answer yes here if PC has 256MB or more RAM.')'"
echo "('$(gettext 'you can edit extlinux.conf later to change the choice made here')')"
echo "('$(gettext 'note, Puppy will override this and not copy if there is insufficient RAM')')"
echo -n "'$(gettext 'ENTER key only for no, else any char then ENTER for yes:')' "
read FCOPY
[ "$FCOPY" != "" ] && FCOPY=" pfix=copy"

#100321 big assumption here, that current running kernel matches one being installed...
if [ -e /proc/ide ];then
 NODMA="ide=nodma" #old kernel
else
 #old ide=nodma does not work, need libata.dma=3 this enables DMA for Disk and CD/DVD but NOT for CF.
 NODMA="libata.dma=3"
fi
if [ ! -f $SRCPATH/boot.msg ];then #100321
 if [ "$FINALPMEDIA" = "ataflash" ];then
  echo "default vmlinuz initrd=initrd.gz ${NODMA} pmedia=${FINALPMEDIA}${FCOPY}" > /mnt/data/extlinux.conf
 else
  echo "default vmlinuz initrd=initrd.gz pmedia=$FINALPMEDIA$FCOPY" > /mnt/data/extlinux.conf
 fi
else
 #100321 want a boot menu...
 if [ "$FINALPMEDIA" = "ataflash" ];then
  echo "default puppy
display boot.msg
prompt 1
timeout 50

F1 boot.msg
F2 help.msg
F3 help2.msg

label puppy
kernel vmlinuz
append initrd=initrd.gz ${NODMA} pmedia=${FINALPMEDIA}${FCOPY}" > /mnt/data/extlinux.conf
 else
  echo "default puppy
display boot.msg
prompt 1
timeout 50

F1 boot.msg
F2 help.msg
F3 help2.msg

label puppy
kernel vmlinuz
append initrd=initrd.gz pmedia=${FINALPMEDIA}${FCOPY}" > /mnt/data/extlinux.conf
 fi
 cp -f $SRCPATH/boot.msg /mnt/data/
 cp -f $SRCPATH/help*.msg /mnt/data/
 cp -f $SRCPATH/logo.16 /mnt/data/
 sync
fi

#note do not create wakepup marker file as it only boots msdsos/vfat.
echo "Copying initrd.gz..."
cp -f $SRCPATH/initrd.gz /mnt/data/
sync
echo "Copying .sfs files..."
cp -f $SRCPATH/${DISTRO_PUPPYSFS} /mnt/data/ 2>/dev/null
sync
for xdrv in ${DISTRO_ZDRVSFS} ${DISTRO_ADRVSFS} ${DISTRO_YDRVSFS};do
 [ -f $SRCPATH/"${xdrv}" ] && cp -f $SRCPATH/${xdrv} /mnt/data/ 2>/dev/null
 sync
done
sync
#cp -f $SRCPATH/${DISTRO_DEVXSFS} /mnt/data/ 2>/dev/null
#sync
umount /mnt/data
echo -n "'$(gettext 'Finished, press ENTER key to continue:')' "
read moo
' > /tmp/installpup.sh
    chmod 755 /tmp/installpup.sh
    xterm -bg "$BG" -fg "$FG" -title "$(gettext 'Puppy Universal Installer')" \
    -geometry 80x10 -e /tmp/installpup.sh $DESTPART $SRCPATH $FINALPMEDIA $DESTFS \
    $DISTRO_FILE_PREFIX $DISTRO_PUPPYSFS $DISTRO_ZDRVSFS $DISTRO_ADRVSFS $DISTRO_YDRVSFS #$DISTRO_DEVXSFS
    ;;
   *)
    pupmessage -bg '#FF8080' "`eval_gettext \"Your \\\$DESTPART has a \\\$DESTFS filesystem. Currently only install
to external vfat (FAT16/32) or ext2/ext3 filesystems is supported.
Click OK to quit...\"`"
    exit
    ;;
  esac
  ;;
 scsihd|atahd|idehd|satahd) #internal media, boot with boot-disk or grub. #v3.98
  #arrive here with DESTPART & DESTFS where installing to, PMEDIA boot param FINALPMEDIA,
  #SRCPATH where to get vmlinuz, initrd, ${DISTRO_PUPPYSFS}, PUPVEROLD is old puppy version in
  #format 200 (without dots) =0 if no prior installed puppy,
  #NEWVER is new puppy version, DESTSIZM is size of dest partition in Mb (real number),
  DESTMNTPT="`mount | grep "/dev/${DESTPART} " | tr -s " " | cut -f 3 -d " "`" #120310 
  #120310 er, i think this only needed for older version of ntfs-3g. harmless with latest ntfs-3g...
  [ "$DESTMNTPT" = "" ] && DESTMNTPT="`ps -e | grep -o 'ntfs\-3g.*' | grep "/dev/${DESTPART} " | tr '\t' ' ' | tr -s ' ' | tr ' ' "\n" | grep '^/mnt/'`" #120310

  if [ "$DESTMNTPT" = "" ];then
   mkdir /mnt/$DESTPART 2> /dev/null
   mount -t $DESTFS /dev/$DESTPART /mnt/$DESTPART
   DESTMNTPT="/mnt/$DESTPART"
  fi

  #choose option1 or option2 install...
  #option1: files vmlinuz, initrd.gz, ${DISTRO_PUPPYSFS}, to dest partition.
  #option2: file vmlinuz to partition, contents of ${DISTRO_PUPPYSFS} to partition.
  # deleted PREVPUPMESSAGE, depends PUPVEROLD, also deleted 140701
  INSTOPTION="1"
  if [ "$DESTFS" != "vfat" -a "$DESTFS" != "ntfs" ];then #120126 jemimah: add ntfs exclusion.
   Xdialog --wmclass "gtkdialog2" --left --buttons-style text --title "$(gettext 'Puppy Universal Installer')" --ok-label "$(gettext 'FRUGAL')" --cancel-label "$(gettext 'FULL')" --yesno "`eval_gettext \"You have chosen to install to /dev/\\\$DESTPART, which appears to be an internal
hard drive\"` ($FINALPMEDIA).

$(gettext 'There are two ways to install Puppy to the partition:')

$(gettext '1. FRUGAL (recommended)')
`eval_gettext \"Copies the files vmlinuz, initrd.gz, \\\${DISTRO_PUPPYSFS} and \\\${DISTRO_ZDRVSFS} to the\"`
`eval_gettext \"partition, saving your personal settings inside a \\\${DISTRO_FILE_PREFIX}save file (like when\"`
$(gettext 'running from CD) and leaving the previous contents of the partition')
$(gettext 'untouched. PROS:')
- $(gettext 'Can coexist with another distro installed to the same partition.')
- $(gettext 'Can install to a non-Linux FAT or NTFS partition (former preferred).')
- $(gettext 'Easier to upgrade: just download the new .iso file and replace the above')
  $(gettext 'files with their newer versions (mount the .iso to access the files).')
  $(gettext '(Note: A .iso can easily be mounted in puppy just by clicking on it)')
- $(gettext 'Enables the use of .sfs addons (application combo-packs)').
- $(gettext 'Can save to entire partition if Linux f.s. and no need to coexist.')

$(gettext '2. FULL')
$(gettext 'A "normal" Linux installation, requiring usage of the entire partition by Puppy.')

$(gettext 'Note: FRUGAL option, if install to a FAT or NTFS partition, there is an')
$(gettext 'extra boot option: "WakePup2", using a floppy disk. Otherwise, GRUB is')
$(gettext 'the normal method, booting from floppy, USB or internal hard drive.')

$(gettext 'Click FRUGAL for coexist install (recommended)...')
$(gettext 'Click FULL for conventional install...')
$(gettext 'Click window close box to quit...')" 0 0
   RET_VAL=$?
   [ $RET_VAL -eq 255 ] && exit
   [ $RET_VAL -eq 1 ] && INSTOPTION="2"
  fi

  #info for creating a grub entry...
  DRV_TYPE="`echo -n "$DESTPART" | cut -b 1-3`" #ex: hda
  GRUB_DRVN=0
  for ONE_TYPE in `fdisk -l | grep "^Disk /dev/" | cut -d' ' -f2 | cut -d':' -f1 | tr '\n' ' '`
  do
   [ "/dev/$DRV_TYPE" = "$ONE_TYPE" ] && break
   GRUB_DRVN=`expr $GRUB_DRVN + 1`
  done
  GRUB_PARTN=`echo -n "$DESTPART" | cut -b 4-5`
  if [ $GRUB_PARTN ];then
   GRUB_PARTN=`expr $GRUB_PARTN - 1`
   GRUB_PARTN=",$GRUB_PARTN"
  fi
   
  #INSTOPTION, install puppy to DESTPART...
  if [ "$INSTOPTION" = "1" ];then #frugal.
  
   case $DESTFS in #100719
    ext2|ext3|ext4|reiserfs|minix|btrfs|f2fs) #130216 add f2fs.
     Xdialog --stdout --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --msgbox "$(gettext 'You have chosen a frugal installation. As you are installing Puppy to a')
`eval_gettext \"partition with a Linux filesystem (\\\${DESTFS}), the first time that you run\"`
$(gettext 'Puppy and shutdown you will be offered to save the session to either a single')
$(gettext 'file or to the entire partition.')

$(gettext 'A single file has convenience for backup and there can be multiple session')
$(gettext 'files, both for this installation of Puppy and for other installations of')
$(gettext 'Puppy. Saving the session to the entire partition has the advantage of all')
$(gettext 'the space available in that partition, however this is a warning for the')
$(gettext 'next dialog in this Puppy Universal Installer...')

$(gettext 'The next dialog will ask if you want to install Puppy into a sub-directory.')
$(gettext 'If you intend to, or might want to, save the session to entire partition, do')
$(gettext 'NOT install to a sub-directory, leave the folder field empty...')" 0 0
    ;;
   esac
   
   #101107 fix msg, puppy now searches 2-deep...
   Xdialog --stdout --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --fixed-font --no-cancel --inputbox "`eval_gettext \"Puppy is going to be installed to partition \\\${DESTPART}, which is currently\"`
`eval_gettext \"mounted on path \\\${DESTMNTPT}. As this will be a frugal install, you can\"`
$(gettext 'choose to place the Puppy files in a folder. This is very convenient if you')
$(gettext 'want to have more than one installation of Puppy. Even if you only have one')
$(gettext 'installation, it may be good to have all the Puppy files in their own folder')
$(gettext 'and so not causing any potential conflicts with other files at the top') '/'
`gettext \"level (ex: a pre-existing full Linux install may have 'initrd.gz' at '/')\"`.

$(gettext 'Enter a unique name for the folder. Note, Puppy will search 2-deep, so this')
`eval_gettext \"(for example) 'puppyfiles/pup\\\${NEWVER}' is okay. Or, a folder at the top\"`
`eval_gettext \"level, 'pup\\\${NEWVER}' is okay (of course, do not type the '). Type a string\"`
`gettext \"without any spaces, or leave blank if you want to install Puppy at '/'.\"`
`gettext \"The folders will be created if they don't exist...\"`" 0 0 "${DISTRO_FILE_PREFIX}${NEWVER}frugal" >/tmp/NEWPSUBDIR #111011
   [ $? -ne 0 ] && exit
   NEWPSUBDIR="`cat /tmp/NEWPSUBDIR | sed -e 's%^/%%' -e 's%/$%%' -e 's%[^/a-zA-Z0-9.]%%g'`" #101107 111011
   NEWPSUBCNT=`echo -n "$NEWPSUBDIR" | sed -e 's%[^/]%%g' | wc -c` #101107
   [ $NEWPSUBCNT -gt 2 ] && NEWPSUBDIR="`echo -n "$NEWPSUBDIR" | sed -e 's%/%%g'`" #101107 too many '/' chars.
   
   yaf-splash -bg orange -text "`eval_gettext \"Please wait, copying files to \\\$DESTPART, in folder \\\$NEWPSUBDIR...\"`" &
   XPID=$!
   mkdir -p "$DESTMNTPT"/"$NEWPSUBDIR"
   for pupfiles in vmlinuz initrd.gz ${DISTRO_PUPPYSFS} ${DISTRO_ZDRVSFS} ${DISTRO_ADRVSFS} ${DISTRO_YDRVSFS}; do
     [ -f "$SRCPATH"/"$pupfiles" ] && cp -f "$SRCPATH"/"$pupfiles" "$DESTMNTPT"/"$NEWPSUBDIR"/ || continue
     sync
   done #140630 made loop
   FPCAPS="`echo -n "$FINALPMEDIA" | tr [a-z] [A-Z]`"
   touch $DESTMNTPT/$NEWPSUBDIR/$FPCAPS #needed by WakePup
   sync
   kill $XPID
   umount /dev/$DESTPART
   unmountcdfunc
   type grub4dosconfig
   if [ "$?" = 0 ];then
     grub4dos_func
   fi
   
   if [ "`echo -n "$DESTFS" | grep -E 'msdos|vfat'`" != "" ];then
   
    Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --ok-label "$(gettext 'CREATE_BOOT_DISK')" --cancel-label "$(gettext 'NO_BOOT_DISK')" --yesno "
$(gettext 'A floppy disk can be used to boot Puppy.')
$(gettext 'Currently we are offering WakePup2, a generic boot floppy')
$(gettext 'that scans the PC and finds where Puppy is installed.')

$(gettext 'Note, after this, you will be informed how to configure the')
$(gettext 'Grub bootloader.')" 0 0
    if [ $? -eq 0 ];then
     /usr/sbin/wakepup2
    fi
    
   fi
   xNEWPSUBDIR=""
   OPTIONPSUBDIR=""
   if [ "$NEWPSUBDIR" != "" ];then
    xNEWPSUBDIR="/$NEWPSUBDIR"
    OPTIONPSUBDIR=" psubdir=${NEWPSUBDIR}"
   fi
   if [ -e /proc/ide ];then #v3.97
    grubEXTRAMSG=""
   else
    grubEXTRAMSG="
`eval_gettext \"WARNING: If your PC has a mix of IDE and SATA hard drives, then (hd\\\${GRUB_DRVN}\\\${GRUB_PARTN})\"`
$(gettext 'may be wrong. GRUB numbers drives as hd<drive>,<partition> where')
$(gettext '<drive> and <partition> numbering starts from zero. The problem is')
$(gettext 'that GRUB sees IDE drives first (hd0,), whereas if you look at Pmount')
$(gettext 'you may see it listed second (hd1,). The Uni. Installer uses the')
$(gettext 'ordering as reported by Pmount, which may be wrong for GRUB!')
$(gettext '(EX: if an IDE drive is hd1,0 (sdb1), may need to be changed to hd0,0)')"
   fi

   #091211...
   echo "`gettext \"If you have GRUB installed, find the 'menu.lst' file (usually in /boot
in the partition in which GRUB is installed) and insert this:\"`
" > /tmp/NEWGRUBTEXT
   echo "title Puppy Linux ${NEWVER} frugal in ${DESTPART} dir ${NEWPSUBDIR}
rootnoverify (hd${GRUB_DRVN}${GRUB_PARTN})
kernel ${xNEWPSUBDIR}/vmlinuz pmedia=${FINALPMEDIA}${OPTIONPSUBDIR}
initrd ${xNEWPSUBDIR}/initrd.gz
" >> /tmp/NEWGRUBTEXT
   echo "`gettext \"If you have GRUB4DOS installed, find the 'menu.lst' file (usually in /
in the partition in which GRUB4DOS is installed) and insert this:\"`
" >> /tmp/NEWGRUBTEXT
   echo "title Puppy Linux ${NEWVER} frugal in ${DESTPART} dir ${NEWPSUBDIR}
  find --set-root --ignore-floppies ${xNEWPSUBDIR}/initrd.gz
  kernel ${xNEWPSUBDIR}/vmlinuz pmedia=${FINALPMEDIA}${OPTIONPSUBDIR}
  initrd ${xNEWPSUBDIR}/initrd.gz" >> /tmp/NEWGRUBTEXT

   nohup defaulttextviewer /tmp/NEWGRUBTEXT &
   sleep 3

   Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --msgbox "$(gettext 'Now you can manually setup your GRUB, GRUB4DOS, LILO or other boot system.')

`eval_gettext \"Partition \\\$DESTPART has the files vmlinuz, initrd.gz and \\\${DISTRO_PUPPYSFS}\"`
$(gettext 'on it, where vmlinuz is the kernel, initrd.gz is the initial ramdisk and')
${DISTRO_PUPPYSFS} $(gettext 'is all the Puppy-files.')

$(gettext 'If you already have GRUB or GRUB4DOS installed, see the text editor')
$(gettext 'window for text that can be inserted into') 'menu.lst'.

$(gettext 'If you do NOT have GRUB or GRUB4DOS (or any other boot manager) installed,')
`gettext \"just click the 'OK' button to finish the installation, then go to the\"`
`gettext \"'System' menu and run the GRUB or GRUB4DOS installer (you probably will\"`
$(gettext 'not need the text in the text editor window, as the installers will probe')
$(gettext 'your PC for all installations of Puppy and other operating systems)').
${grubEXTRAMSG}" 0 0
   
   #finished. could put up a final dlg here, then quit script.
   Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy Universal Installer')" --msgbox "$(gettext 'Okay, all done!')" 0 0
   exit
  else #h.d. install option2
   #upgrade, ask if want delete partition...
   #add a simple test.. 
   if [ -f "${DESTMNTPT}/etc/DISTRO_SPECSh" ];then #I hate Xdialog for complex GUI, use gtkdialog
     UPGRADE=$(gettext "UPGRADE")
     WIPE=$(gettext " WIPE ")
     BTNS='<hbox homogeneous="true">
        <button image-position="Bottom" width-request="120">
			<label>'"$UPGRADE"'</label>
			<input file stock="gtk-go-up"></input>
			<action>EXIT:42</action>
		</button>
		<button image-position="Bottom" width-request="120">
			<label>'"$WIPE"'</label>
			<input file stock="gtk-delete"></input>
			<action>EXIT:1</action>
		</button>
	  </hbox>'
	  OLDNAME=`grep DISTRO_FILE_PREFIX "${DESTMNTPT}/etc/DISTRO_SPECS"|cut -d '=' -f2|sed -e "s%'%%g" -e 's%"%%g'`
	  OLDVER=`grep DISTRO_VERSION "${DESTMNTPT}/etc/DISTRO_SPECS"|cut -d '=' -f2|sed -e "s%'%%g" -e 's%"%%g'`
	  OLDPUP="${OLDNAME}-${OLDVER}"
	  S1=$(eval_gettext 'There is already a version of Puppy <b>'"$OLDPUP"'</b> installed in '"${DESTPART}"'. ')
	  S2=$(gettext 'Do you want to upgrade, or totally wipe the partition? ')
	  S3=$(gettext 'Select UPGRADE button to upgrade... ')
	  S4=$(gettext 'Select WIPE button to wipe partition...')
	  
	  TOPTEXT='<text use-markup="true"><label>"'"$S1 $S2"'"</label></text>'
	  BTMTEXT='<text><label>'"$S3 $S4"'</label></text>'
	else
	  BTNS='<hbox homogeneous="true">
	     <button ok></button><button cancel></button>
	   </hbox>'
	  S5=$(gettext 'Click "OK" to install.')
	  BTMTEXT='<text><label>'"$S5"'</label></text>'
    fi
	M1=$(gettext 'Puppy Universal Installer')
	M2=$(eval_gettext 'FINAL SANITY CHECK : ARE YOU SURE '"${DESTPART}"' IS THE RIGHT PARTITION?')
	M3=$(eval_gettext ''"${DESTPART}"' is currently mounted on '"${DESTMNTPT}"'')
	# gui
    export maingui='<window title="'"$M1"'" window-position="1">
        <vbox>
          <frame>
           '"$TOPTEXT"'
           <text><label>""</label></text>
           <text use-markup="true"><label>"<b><big>'"$M2"'</big></b>"</label></text>
           <text><label>""</label></text>
           <text><label>'"$M3"'</label></text>
           '"$BTMTEXT"'
           <text><label>""</label></text>
          </frame>
         '"$BTNS"'
        </vbox>
      </window>'

    eval $(gtkdialog -p maingui)
    [ "$EXIT" = "abort" ] && EXIT=255    
    [ "$EXIT" = "Cancel" ] && EXIT=255
    [ "$EXIT" = "OK" ] && EXIT=1
    RET_VAL="$EXIT"
    if [ $RET_VAL -eq 255 ];then
     echo "bailing"
     rox -D "$DESTMNTPT"
     sync
     umount $DESTMNTPT 2>/dev/null
     exit
    fi
    if [ $RET_VAL -eq 1 ];then #no
     echo "wiping"
     yaf-splash -close never -bg orange -text "$(gettext 'Please wait, wiping all files in') $DESTPART..." &
     XPID=$!
     sleep 3
     rm -rf $DESTMNTPT/*
     sync
     kill $XPID
     #PUPVEROLD=`expr $DISTRO_VERSION - 1` #v430
     #PUPVEROLD='0.0' #110422
    fi
   #fi
   echo "installing"
   yaf-splash -close never -bg orange -text "$(gettext 'Please wait, copying Puppy files to') $DESTPART..." &
   XPID=$!
   sleep 1
   cp -f /etc/profile /tmp/profile-before_upgrade #120823 rerwin: preserve user-modified files in full-install upgrade.
   #loop-device mount ${DISTRO_PUPPYSFS} in DESTPART, copy contents of ${DISTRO_PUPPYSFS} to DESTPART...
   losetup-FULL /dev/loop2 $SRCPATH/$DISTRO_PUPPYSFS #100913
   mkdir "$DESTMNTPT"/srcmntpt
   mount -r -t squashfs /dev/loop2 $DESTMNTPT/srcmntpt
   for dir in bin dev etc lib lib64 mnt opt proc root sbin tmp usr var ; do
     [ -d "$DESTMNTPT"/srcmntpt/"$dir" ] || continue
     cp -a -u --remove-destination "$DESTMNTPT"/srcmntpt/"$dir" "$DESTMNTPT"/ \
     || fatal
   done #140630
   sync
   #130613 puppy currently has /run a symlink to /tmp, but don't assume this always. create as-is...
   #cp -a -f --remove-destination $DESTMNTPT/srcmntpt/run $DESTMNTPT/
   (cd "$DESTMNTPT"/; ln -sf tmp run) #140630
   
   #v2.12 external modules file...
   #w482 fix handling if separate zdrv file... 140630 a + y too
   for drv in ${DISTRO_ZDRVSFS} ${DISTRO_ADRVSFS} ${DISTRO_YDRVSFS};do
     [ -f $SRCPATH/"${drv}" ] && cp -af "$SRCPATH"/"${drv}" "$DESTMNTPT"/
     [ -f /"${drv}" ] && cp -af /"${drv}" "$DESTMNTPT"/
   
     if [ -f "$DESTMNTPT"/"${drv}" ];then #w482
       mkdir -p "$DESTMNTPT"/data
       mount -t squashfs -o loop "$DESTMNTPT"/"${drv}" "$DESTMNTPT"/data
       cp -a -f --remove-destination "$DESTMNTPT"/data/* "$DESTMNTPT"/
       
       sync
       umount "$DESTMNTPT"/data
       rm -f "$DESTMNTPT"/"${drv}"
     fi
   done
   rm -r "$DESTMNTPT"/data
   #w482 put these modules back into the normal places...
   if [ -d /lib/modules/"$KERNELVER"/initrd ];then
    #note, important to not have 'initrd' folder in full hd installation, messes up rc.sysinit.
    cp -af /lib/modules/"$KERNELVER/initrd"/* "$DESTMNTPT"/lib/modules/$KERNELVER/
    depmod -b "$DESTMNTPT"
   fi

   #100113 /bin/TARGETEXES has list of executables that were copied from initrd (and not in main f.s.)...
   if [ -f /bin/TARGETEXES ];then
    for ONEEXE in `cat /bin/TARGETEXES` #ex: sbin/e2fsck
    do
     DIREXE="`dirname $ONEEXE`"
     cp -a -f --remove-destination /${ONEEXE} ${DESTMNTPT}/${DIREXE}/
    done
   fi
   #same thing here...
   if [ -d /lib/keymaps ];then
    mkdir -p ${DESTMNTPT}/lib/keymaps
    cp -a -f /lib/keymaps/* ${DESTMNTPT}/lib/keymaps/
    mkdir -p ${DESTMNTPT}/lib/consolefonts
    cp -a -f /lib/consolefonts/* ${DESTMNTPT}/lib/consolefonts/
   fi

   
   mkdir $DESTMNTPT/sys 2>/dev/null
   rm -f $DESTMNTPT/var/log/modprobes.log 2>/dev/null #v2.13 see /sbin/modprobe script.
   #...not necessary, not using this any more.
    echo '#ATADRIVES is all internal ide/pata/sata drives...' >> $DESTMNTPT/etc/rc.d/PUPSTATE
    echo "ATADRIVES='$ATADRIVES'" >> $DESTMNTPT/etc/rc.d/PUPSTATE
   #fi
   sync
   kill $XPID
   #fix space optimisation links into initrd...
    #TODO
   #chroot to DESTPART, set DISTRO_VERSION back to $PUPVEROLD, run rc.update
   yaf-splash -bg orange -text "$(gettext 'Please wait, executing rc.update script...')" &
   XPID=$!
   sleep 2
   
   if [ -f "$DESTMNTPT"/etc/puppyversion ];then #historical file, old installations.
    echo -n "0.0" > $DESTMNTPT/etc/puppyversion #set it back to what it was.
   fi
   if [ -f "$DESTMNTPT"/etc/DISTRO_SPECS ];then
    vPATTERN="s%^DISTRO_VERSION=.*%DISTRO_VERSION=0.0'%" #110422
    sed -e "$vPATTERN" $DESTMNTPT/etc/DISTRO_SPECS > /tmp/distro_specs_old
    cp -f /tmp/distro_specs_old $DESTMNTPT/etc/DISTRO_SPECS #set DISTRO_VERSION back to what it was.
   fi
   
   #busybox chroot does not have -c ...
   FLAGDEVMISS=0
   if [ ! -e "$DESTMNTPT"/dev/null ];then #130613 recent pups have empty /dev. rc.update may need these...
    cp -a /dev/console $DESTMNTPT/dev/
    cp -a /dev/null $DESTMNTPT/dev/
    FLAGDEVMISS=1
   fi
   chroot $DESTMNTPT /etc/rc.d/rc.update option2hdinstall
   if [ "$FLAGDEVMISS" -eq 1 ];then #130613
    rm -f $DESTMNTPT/dev/console
    rm -f $DESTMNTPT/dev/null
   fi
   #(need to tell rc.update where src files are, as passed param)
   sync
    mv -f $DESTMNTPT/etc/fstab $DESTMNTPT/etc/fstab.bak
    echo "/dev/$DESTPART     /            $DESTFS     defaults               0 1" > $DESTMNTPT/etc/fstab
    echo "none          /proc        proc     defaults               0 0" >> $DESTMNTPT/etc/fstab
    echo "none          /sys         sysfs    defaults               0 0" >> $DESTMNTPT/etc/fstab
    echo "none          /dev/pts     devpts   gid=2,mode=620         0 0" >> $DESTMNTPT/etc/fstab
    echo "/dev/fd0      /mnt/floppy  auto     noauto,rw              0 0" >> $DESTMNTPT/etc/fstab
    sync
   kill $XPID
   umount $DESTMNTPT/srcmntpt
   rmdir $DESTMNTPT/srcmntpt
  fi
  #140707
  type grub4dosconfig
  if [ "$?" = 0 ];then
    mkdir -p /mnt/$DESTPART/boot
    cp -p --remove-destination $SRCPATH/vmlinuz /mnt/$DESTPART/boot/
    sync
    umount /mnt/$DESTPART
    unmountcdfunc
    grub4dos_func
  fi
  #make bootable...
  #usb/floppy boot disk, or grub.

  while [ "$INSTOPTION" = "2" ];do

   #091210 simpler stuff for grub4dos...
   if [ -f /usr/sbin/grub4dosconfig ];then
    echo "
`gettext \"If you have GRUB installed, find the 'menu.lst' file (usually in /boot
in the partition in which GRUB is installed) and insert the text,
or, if you have GRUB4DOS installed, find the 'menu.lst' file (usually
in / in the partition in which GRUB4DOS is installed) and insert this:\"`
" > /tmp/NEWGRUBTEXT
    echo "title Puppy Linux ${NEWVER} full install in ${DESTPART}
root (hd${GRUB_DRVN}${GRUB_PARTN})
kernel /boot/vmlinuz root=/dev/${DESTPART} pmedia=${FINALPMEDIA}

`eval_gettext \"WARNING: If your PC has a mix of IDE and SATA hard drives, then (hd\\\${GRUB_DRVN}\\\${GRUB_PARTN})
may be wrong. GRUB numbers drives as hd<drive>,<partition> where
<drive> and <partition> numbering starts from zero. The problem is
that GRUB sees IDE drives first (hd0,), whereas if you look at Pmount
you may see it listed second (hd1,). The Uni. Installer uses the
ordering as reported by Pmount, which may be wrong for GRUB!
(EX: if an IDE drive is hd1,0 (sdb1), may need to be changed to hd0,0)\"`" >> /tmp/NEWGRUBTEXT

    nohup defaulttextviewer /tmp/NEWGRUBTEXT &
    sleep 3

    Xdialog --wmclass "gtkdialog2" --left --buttons-style text --title "$(gettext 'Puppy Universal Installer')" --ok-label "OK" --msgbox "$(gettext 'If all went well, Puppy has been installed to') $DESTPART.\n\
$(gettext 'The text-editor window shows text that you can insert into the menu.lst')
$(gettext 'file if you have GRUB or GRUB4DOS inststalled.')

$(gettext 'If your PC does not have GRUB, GRUB4DOS, LILO or any other boot manager')
$(gettext 'installed, or you want to do a reinstallation, just click the ')'OK'
$(gettext 'button to finish the Universal Installer, then go to the "System" menu')
$(gettext 'and run the GRUB or GRUB4DOS installer -- this will probe your PC, so')
$(gettext 'you probably wont need the contents of the text-editor...')" 0 0

    break
   fi

#TODO needs work, as could have usb hd, that cannot unplug and do not want it to be turned
#     into a boot disk!...
   Xdialog --wmclass "gtkdialog2" \
   --left --buttons-style text --title "$(gettext 'Puppy Universal Installer')" \
   --ok-label "$(gettext 'Boot from USB')" \
   --cancel-label "$(gettext 'Install/update GRUB')" \
   --yesno "$(gettext 'If all went well, Puppy has been installed to') $DESTPART.\n\
$(gettext 'But, now we have the sometimes-difficult part, how to make Puppy bootable.')\n\
$(gettext 'You can do one, or both, of these:')\n\
\n\
$(gettext 'Boot disk: Use a floppy disk or USB pen drive to boot Puppy.')\n\
GRUB:      $(gettext 'Install GRUB in the hard drive (or update existing GRUB)').\n\
\n\
`gettext \"Click 'Boot from USB' if you want to sacrifice a usb Flash pen drive as a\"`\n\
`gettext \"'boot disk'. This Universal Installer script will return to this dialog\"`\n\
`gettext \"window after creating the USB 'boot disk'.\"`\n\
$(gettext 'Note, you would probably only choose this if your PC does not have a floppy')\n\
$(gettext 'drive and you do not want to install GRUB to the hard drive.')\n\
\n\
`gettext \"Click 'Install/update GRUB' to install (or update) the GRUB boot manager.\"`\n\
`gettext \"You will be given the opportunity to create a floppy 'boot disk' as well\"`\n\
`gettext \"as install GRUB to the hard drive.\"`\n\
\n\
$(gettext 'FOR USB BOOT DISK CHOICE, PLEASE INSERT IT RIGHT NOW BEFORE PROCEEDING!')\n\
 ($(gettext 'must have FAT16 f.s., and any files in it will be deleted'))\n\
 $(gettext 'IT MUST ALSO BE UNMOUNTED -- BE SURE BEFORE PROCEEDING')" 0 0
   if [ $? -eq 0 ];then #BOOT DISK
    BOOTPART=""
    #v3.96...
    USBDRIVES="`echo "$PROBEDISK" | grep '|usbdrv|' | cut -f 3 -d '/' | cut -f 1 -d '|' | tr "\n" " "`"
    for ONEUSB in $USBDRIVES
    do
     [ "$ATADRIVES" != "" ] && [ "`echo "$ATADRIVES" | grep "$ONEUSB"`" != "" ] && continue #eliminate ide/sata drives. v3.97
     if [ "`disktype /dev/$ONEUSB | grep "file system" | grep "^FAT"`" != "" ];then
      BOOTPART="$ONEUSB"
      break
     fi
     if [ "`disktype /dev/${ONEUSB}1 | grep "file system" | grep "^FAT"`" != "" ];then
      BOOTPART="${ONEUSB}1"
      break
     fi
    done
    if [ ! "$BOOTPART" = "" ];then
     Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "`eval_gettext \"Please confirm that you want to make '\\\$BOOTPART' into a boot disk for Puppy.\nIf Puppy has mis-identified the device, then answer 'No' here...\n\nClick 'Yes' button if correct device...\"`" 0 0
     [ ! $? -eq 0 ] && continue
    fi
    if [ ! "$BOOTPART" = "" ];then
     cp -af $SRCPATH/vmlinuz /mnt/$DESTPART/
     sync
     Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy Universal Installer')" --msgbox "`eval_gettext \"Creating \\\$BOOTPART boot disk...\"`" 0 0 &
     XPID=$!
     #passed params $BOOTPART $DESTPART $SRCPATH $FINALPMEDIA
     echo '#!/bin/sh
     BOOTPART="$1"
     DESTPART="$2"
     SRCPATH="$3"
     FINALPMEDIA="$4"
      mkdir /tmp/bootdiskmntpt 2> /dev/null
      mount -t msdos /dev/$BOOTPART /tmp/bootdiskmntpt
      rm -rf /tmp/bootdiskmntpt/*
      echo "'$(gettext 'Creating syslinux.cfg on /dev/')'$BOOTPART..."
      echo "default vmlinuz root=/dev/$DESTPART pmedia=$FINALPMEDIA" > /tmp/bootdiskmntpt/syslinux.cfg
      echo "'$(gettext 'Writing the Linux kernel, vmlinuz, to')' /dev/$BOOTPART..."
      cp -f $SRCPATH/vmlinuz /tmp/bootdiskmntpt/
      sync
      umount /dev/$BOOTPART
      rmdir /tmp/bootdiskmntpt
      echo "'$(gettext 'Making the')' /dev/$BOOTPART '$(gettext 'bootable using Syslinux...')'"
      #syslinux /dev/$BOOTPART ### same bug #140701
      extlinux -i /tmp/bootdiskmntpt ###/dev/$BOOTPART
     sync
     echo -n "'$(gettext 'Done. Press ENTER key:')' "
     read allisdone
' > /tmp/installpup.sh
     chmod 755 /tmp/installpup.sh
     xterm -bg "$BG" -fg "$FG" -title "$(gettext 'Puppy Universal Installer')" \
     -geometry 80x10 -e /tmp/installpup.sh $BOOTPART $DESTPART $SRCPATH $FINALPMEDIA

     kill $XPID
    fi
   else #Grub
    mkdir -p /mnt/$DESTPART/boot/grub
    cp -af $SRCPATH/vmlinuz /mnt/$DESTPART/boot/
    sync
    umount /mnt/$DESTPART
    unmountcdfunc
    if [ -e /proc/ide ];then #v3.97
     grubEXTRAMSG=""
    else
     grubEXTRAMSG="
$(gettext 'WARNING: If your PC has a mix of IDE and SATA hard drives, then')
$(gettext 'GRUB may get the drive numbering wrong. In which case, you may')
$(gettext 'have to manually edit the entry or entries in /boot/grub/menu.lst')
$(gettext 'in the partition where GRUB is installed. This problem has come')
$(gettext 'with recent Linux kernels where IDE drives are now /dev/sd*, the')
$(gettext 'same letters as SATA drives, whereas before they were /dev/hd*.')
$(gettext 'GRUB uses "hd" notation for both IDE and SATA drives, and numbers')
$(gettext 'drives as hd<drive>,<partition> where <drive> and <partition> numbering')
$(gettext 'starts from zero (again different, as Linux numbers partitions from 1).')
$(gettext 'The problem is that GRUB sees IDE drives first (hd0,), whereas if you')
$(gettext 'look at Pmount you may see it listed second (hd1,). The Uni. Installer')
$(gettext 'uses the ordering as reported by Pmount, which may be wrong for GRUB!')
($(gettext 'EX: if an IDE drive is hd1,0 (sdb1), may need to be changed to hd0,0'))"
    fi
    Xdialog --buttons-style "text" --wmclass "gtkdialog2" --left \
    --title "$(gettext 'Universal Installer')" --ok-label "$(gettext 'UPDATE')" \
    --cancel-label "$(gettext 'INSTALL')" \
    --yesno \
"$(gettext 'Click UPDATE button if you want to update an existing installation')
$(gettext 'of GRUB, or click INSTALL to install or reinstall GRUB...')
${grubEXTRAMSG}" 0 0
    if [ $? -eq 0 ];then #v3.96 update grub.
     echo "title Puppy Linux $NEWVER full install
root (hd${GRUB_DRVN}${GRUB_PARTN})
kernel /boot/vmlinuz root=/dev/$DESTPART pmedia=${FINALPMEDIA}" > /tmp/NEWGRUBTEXT
     if [ -e /proc/ide ];then #v3.97
      grubEXTRAMSG="$(gettext '...for your convenience, this text has been written to') /tmp/NEWGRUBTEXT"
     else
      grubEXTRAMSG="$(gettext '...for your convenience, this text has been written to') /tmp/NEWGRUBTEXT

$(gettext 'WARNING: If your PC has a mix of IDE and SATA hard drives, then') (hd${GRUB_DRVN}${GRUB_PARTN})
$(gettext 'may be wrong. GRUB numbers drives as hd<drive>,<partition> where')
$(gettext '<drive> and <partition> numbering starts from zero. The problem is')
$(gettext 'that GRUB sees IDE drives first (hd0,), whereas if you look at Pmount')
$(gettext 'you may see it listed second (hd1,). The Uni. Installer uses the')
$(gettext 'ordering as reported by Pmount, which may be wrong for GRUB!')
($(gettext 'EX: if an IDE drive is hd1,0 (sdb1), may need to be changed to hd0,0'))"
     fi

     Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Universal Installer')" \
     --msgbox "$(gettext 'You have chosen to update an existing GRUB installation.')
$(gettext 'This requires a final manual step...')
$(gettext 'Find the "menu.lst" file (usually in /boot/grub in the') 
$(gettext 'partition in which GRUB is installed) and insert this:')

title Puppy Linux $NEWVER full install in $DESTPART
root (hd${GRUB_DRVN}${GRUB_PARTN})
kernel /boot/vmlinuz root=/dev/$DESTPART pmedia=${FINALPMEDIA}

$grubEXTRAMSG

$(gettext 'After you click the OK button, the Uni. Installer will exit.')" 0 0
     exit
    else
     Xdialog --wmclass "gtkdialog2" \
     --left --title "$(gettext 'Puppy Universal Installer')" \
     --msgbox "$(gettext 'The "grubconfig" script is about to be executed.\
\nJust accept the defaults. The very first question will ask if you\nwant to \
do a simple installation, and you accept that option, which\nis the default. \
The only place where I suggest you deviate from the\ndefault, is when it asks \
where to install Grub, choose "MBR"')." 0 0
     while [ 1 ];do
      grubconfig /dev/$DESTPART
      #...if choose to create a boot floppy, writes 'no' to /tmp/loopbrk.txt.
      Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Universal Installer')" \
      --ok-label "$Yes_lbl" --cancel-label "$No_lbl" \
      --yesno "$(gettext 'If you have already run grubconfig to create a boot floppy or installed')\n\
`gettext \"to h.d., click 'Yes' button to rerun grubconfig script to either create\"`\n\
`gettext \"another boot floppy or install or reinstall GRUB to the hard drive.\"`\n\
`gettext \"If choose 'Yes', just accept all the defaults as before.\"`\n\
\n\
`gettext \"Click 'Yes' to rerun grubconfig...\"`\n\
`gettext \"Click 'No' if installation finished...\"`" 0 0
      [ ! $? -eq 0 ] && break
     done
     rm -f /tmp/loopbrk.txt
     exit
    fi
   fi
   Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy Universal Installer')" \
   --ok-label "$Yes_lbl" --cancel-label "$No_lbl" \
   --yesno "$(gettext 'Do you want to go back and create another boot disk or Grub boot?...')" 0 0
   [ ! $? -eq 0 ] && break
  done

  umount /mnt/$DESTPART 2> /dev/null
  ;;
 atacd|scsicd|usbcd)
  pupmessage -bg '#FF8080' "$(gettext 'not yet implemented')"
  #TO DO
  ;;
esac

unmountcdfunc

###END###
