#!/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.

[ "`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')"

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

#export LANG=C #110510 comment out for i18n
#NEWVER="`cat /etc/puppyversion`"
KERNELVER="`uname -r`"

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

#PREFIX1CHAR="`echo -n "$DISTRO_FILE_PREFIX" | cut -c 1`"
#KERNEL3CHARS="`echo -n "$KERNELVER" | tr -d '.' | tr -d '\-' | tr -d '[a-z]' | rev | cut -c 1,2,3 | rev`"
#ZDRVSFS="z${PREFIX1CHAR}${DISTRO_VERSION}${KERNEL3CHARS}.sfs"

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
 *)        FSTYPE="" ;; #100412
 esac 
}

ispupfunc() { #two params: fstype partition
 #returns version of already-installed Puppy, if any.
 PUPSFS=""
 PUPVEROLD='0.0' #110422
 [ ! $2 ] && return 0
 mkdir -p /mnt/$2
 ISPUPMNTPT="`mount | grep "^/dev/$2" | tr -s " " | cut -f 3 -d " "`"
 if [ "$ISPUPMNTPT" = "" ];then
  ISPUPMNTPT="/mnt/$2"
  mount -t $1 /dev/$2 /mnt/$2
  [ $? -ne 0 ] && return 0
 fi

 #check for ancient puppy...
 [ -f $ISPUPMNTPT/usr_cram.fs ] && PUPVEROLD='1.0.8' #110422
 #puppy 2,3,4 has format pup_412.sfs ...
 ISFILE="`ls -1 ${ISPUPMNTPT}/${DISTRO_FILE_PREFIX}_[0-9][0-9][0-9].sfs 2>/dev/null | head -n 1`"
 [ "$ISFILE" != "" ] && PUPVEROLD=`basename $ISFILE | rev | cut -c 5,6,7 | rev`
 #puppy built by earlier woof has format distroprefix-500.sfs (ex: upup-500.sfs) ...
 ISFILE="`ls -1 ${ISPUPMNTPT}/${DISTRO_FILE_PREFIX}-[0-9][0-9][0-9].sfs 2>/dev/null | head -n 1`"
 [ "$ISFILE" != "" ] && PUPVEROLD=`basename $ISFILE | rev | cut -c 5,6,7 | rev`
 if [ "$ISFILE" != "" ];then #110422
  PUPVEROLD="` echo -n "$PUPVEROLD" | sed -e 's%[0-9]%&.%g' -e 's%\.$%%'`" #ex 512 becomes 5.1.2
 fi

 if [ "$ISFILE" != "" ];then #110422 new naming is puppy_wary_5.1.2.sfs
  ISFILE="`ls -1 ${ISPUPMNTPT}/${DISTRO_PUPPYSFS} 2>/dev/null | head -n 1`"
  [ "$ISFILE" != "" ] && PUPVEROLD="`basename $ISFILE | rev | cut -f 1 -d '_' | cut -f 1 -d '-' | rev`"
  [ "$PUPVEROLD" = "" ] && PUPVEROLD='0.0'
 fi

 #100913 new naming scheme...
 if [ "$PUPVEROLD" = "0.0" ];then
  #should really search subdirs also...
  #ISFILE="`ls -1 ${ISPUPMNTPT}/${DISTRO_PUPPYSFS} 2>/dev/null | head -n 1`"
  ISFILE="`find ${ISPUPMNTPT} -maxdepth 2 -type f -name ${DISTRO_PUPPYSFS} | head -n 1`"
  if [ "$ISFILE" != "" ];then
   #version number is contained in the id-string appended to puppy.sfs file...
   FULLSIZE=`stat -c %s ${ISFILE}`
   #ORIGSIZE=`expr $FULLSIZE - 16`
   #PUPVEROLD="`dd if=${ISFILE} bs=1 skip=${ORIGSIZE} 2>/dev/null | cut -b 2,3,4 | sed -e 's%[^0-9]%%g'`"
   #110422 format of idstring, see remasterpup2 line 249 (ex: w110423092024ZZZZ5.1.2XXXXXXXXXX)...
   ORIGSIZE=`expr $FULLSIZE - 32`
   PUPVEROLD="`dd if=${ISFILE} bs=1 skip=${ORIGSIZE} 2>/dev/null | sed -e 's%[^a-zA-Z0-9\.]%%g' | cut -b 18-32 | tr -s 'X' | sed -e 's%X$%%'`"
  fi
 fi

 #check for a full install to the partition...
 [ -f $ISPUPMNTPT/etc/puppyversion ] && PUPVEROLD="`cat $ISPUPMNTPT/etc/puppyversion`" #old pre-w464 install. 110422
 [ -f $ISPUPMNTPT/etc/DISTRO_SPECS ] && PUPVEROLD="`grep '^DISTRO_VERSION' ${ISPUPMNTPT}/etc/DISTRO_SPECS | cut -f 2 -d '=' | cut -f 2 -d '"' | cut -f 2 -d "'" | cut -f 1 -d ' '`" #w464 w478 110422
 umount /dev/$2 2>/dev/null
 #return $PUPVEROLD #110422
}

gpartedfixfunc() { #params: $DRVSEL
 GPDRV="$1"
 gparted /dev/$GPDRV &
 sleep 2
 pupmessage -bg '#ba55d3' -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
 ##/tmp/pup_event_frontend_block_request is used in /sbin/pup_event_frontend_d to refresh drv...
 #echo "$GPDRV" > /tmp/pup_event_frontend_block_request
 #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
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
 rxvt -bg "pink" -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
#sanity check...
if [ ! -f ${SRCPATH}/${DISTRO_PUPPYSFS} ];then #v3.01 v424 100913
 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.


PMEDIA="`Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --no-tags --stdout --menubox "$(gettext 'Welcome to the Puppy Universal Installer!\nIf you wish to install Puppy to a removable media, such as a USB Flash\nor hard drive, CD/DVD disc, Zip disk or LS-120 disk, please insert it\nright now, before proceeding.\n\nINSERT MEDIA NOW\nThen, choose the media that you want to install Puppy to:')" 0 0 10 usbflash "$(gettext 'USB Flash drive')" usbhd "$(gettext 'USB hard drive')" usb2ataflash "$(gettext 'USB CF Flash drive, later move CF card to IDE/SATA internal adaptor')" iusbflash "$(gettext 'Internal USB Flash drive (ex: uDiskOnChip)')" ataflash "$(gettext 'Internal IDE/SATA Flash drive (exs: CF card, SD memory card)')" atazip "$(gettext 'Internal ZIP or LS120 drive')" atahd "$(gettext 'Internal (IDE or SATA) hard drive')" scsihd "$(gettext 'Ancient true-SCSI hard drive')" cd "$(gettext 'CD drive')"`"
[ ! $? -eq 0 ] && exit
[ "$PMEDIA" = "" ] && exit

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

############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 #eliminate ide/sata drives.
   #[ "$SATADRIVES" != "" ] && [ "`echo "$SATADRIVES" | grep "$ONEUSB"`" != "" ] && continue #for old kernel. 110126
   [ "$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="${SELECTIONS} $ONEUSB \"${USBINFO}, ${USBSIZE}\""
  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 #eliminate ide/sata drives.
   #[ "$SATADRIVES" != "" ] && [ "`echo "$SATADRIVES" | grep "$ONEUSB"`" != "" ] && continue #for old kernel. 110126
   [ "`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="${SELECTIONS} $ONEUSB \"${USBINFO}, ${USBSIZE}\""
  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 " "`"
   SELECTIONS="${SELECTIONS} $ONEHD \"${HDINFO}, ${HDSIZE}\""
  done
  Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --ok-label "$(gettext 'QUIT')" --msgbox "`eval_gettext \"Your computer has these CD/DVD drives:\n\\\$SELECTIONS \n\nHowever, you cannot install Puppy to a CD/DVD in the same way as a\nhard drive, USB Flash drive or Zip drive, as the CD/DVD is not\ndirectly writable. For the CD/DVD, you need to do what is called\n'remastering' and Puppy has a program for this, called 'Remaster\nPuppy live-CD', found in the Setup menu. This program enables\nyou to create your own custom live-CD/DVD.\nNote, you will need a new blank CD-R or DVD-R (not +R).\n\nClick button to quit...\"`" 0 0
  exit
 ;;
 atazip)
 
  #110126 remove...
  Xdialog --wmclass "gtkdialog2" --left --title "$(gettext 'Puppy Universal Installer')" --ok-label "$(gettext 'QUIT')" --msgbox "$(gettext 'Booting Puppy off a LS120 or Zip drive is such an incredibly slow experience,\nyou are being saved the pain of finding this out for yourself. For example,\nbooting from a Zip disk is typically 20 minutes!\nThis option has been removed from the Universal Installer. If you want to boot\nfrom a plug-in media, it is far far better to boot from CD/DVD, USB Flash or\nhard drive, or other solid-state memory card media.\n\n\nClick button to quit...')" 0 0
  exit
  
  HDDRIVES="`echo "$PROBEDISK" | grep '|floppy|' | cut -f 3 -d '/' | cut -f 1 -d '|' | tr "\n" " "`"
  SELECTIONS=""
  for ONEHD in $HDDRIVES
  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="${SELECTIONS} $ONEHD \"${HDINFO}, ${HDSIZE}\""
  done
 ;;
 atahd|ataflash|scsihd) #v425 added scsihd
  #at bootup, init script creates ATADRIVES='' in /etc/rc.d/PUPSTATE...
  HDDRIVES="$ATADRIVES"
  #110126 ATADRIVES now has all ide/pata/sata drives.
  #if [ -e /proc/ide ];then #old kernel.
  #  IDEDRIVES="`echo "$PROBEDISK" | grep '|drive|' | grep '/dev/hd' | cut -f 3 -d '/' | cut -f 1 -d '|' | tr "\n" " "`"
  #  HDDRIVES="$IDEDRIVES $SATADRIVES"
  #fi
  #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="${SELECTIONS} $ONEHD \"${HDINFO}, ${HDSIZE}\""
  done
 ;;
 *)
  pupmessage -bg '#FF8080' "$(gettext 'Not yet implemented')"
  exit
 ;;
esac

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

echo '#!/bin/sh' > /tmp/dlg1
echo -n 'Xdialog --wmclass "gtkdialog2" --title "'$(gettext 'Puppy Universal Installer')'" --no-cancel --stdout --menubox "'$(gettext 'Choose which drive to install to:')'" 10 60 0' >> /tmp/dlg1
echo -n "$SELECTIONS" >> /tmp/dlg1
echo ' > /tmp/retval'  >> /tmp/dlg1
echo 'exit $?' >> /tmp/dlg1

chmod 755 /tmp/dlg1

/tmp/dlg1
[ $? -ne 0 ] && exit
 
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.
 
DRVSEL="`cat /tmp/retval`"
[ "$DRVSEL" = "" ] && exit

#110126 remove...
##old kernel, distinguish between ide and sata...
#if [ -e /proc/ide ];then
# substPATTERN='s/^ata/ide/'
# if [ "$SATADRIVES" != "" ];then
#  [ "`echo "$SATADRIVES" | grep "$DRVSEL"`" != "" ] && substPATTERN='s/^ata/sata/'
# fi
# PMEDIA="`echo -n "$PMEDIA" | sed -e "$substPATTERN"`"
# FINALPMEDIA="`echo -n "$FINALPMEDIA" | sed -e "$substPATTERN"`"
#fi

DRVINFO="`disktype /dev/$DRVSEL`"
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.
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 " "`"
[ "`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
 ispupfunc $FSTYPE $DRVSEL #sets PUPVEROLD
 #[ $? -ne 0 ] && PUPINST=", Puppy version $PUPVEROLD installed"
 [ "$PUPVEROLD" != '0.0' ] && PUPINST=", Puppy version $PUPVEROLD installed"
 DRVMSG="${DRVSEL}:  ${FSTYPE}, ${DRVSIZE}${PUPINST}
"
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
  #xPARTNUM="`echo "$ONELINE" | grep '^Partition ' | tr -s " " | cut -f 1 -d ':' | cut -f 2 -d " "`"
  #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
   fsfunc $PARTITIONFS #sets FSTYPE.
   #find out if partition has puppy, or has ${DISTRO_PUPPYSFS} file...
   if [ "$FSTYPE" ];then #100412 fix from EdV.
    PUPINST=""
    ispupfunc $FSTYPE ${DRVSEL}${PARTNUM} #sets PUPVEROLD
    #[ $? -ne 0 ] && PUPINST=", Puppy version $PUPVEROLD installed"
    [ "$PUPVEROLD" != '0.0' ] && PUPINST=", Puppy version $PUPVEROLD installed" #110422
    echo "${DRVSEL}${PARTNUM}: ${FSTYPE}, size ${PARTSIZE}${PUPINST}" >> /tmp/pidrvmsg.txt
   fi
  fi
 done
 DRVMSG="`cat /tmp/pidrvmsg.txt`"
fi

DLGTXT=""
CHOICES="`echo "$DRVMSG" | grep ':' | cut -f 1 -d ':' | tr "\n" " "`"
for ONECHOICE in $CHOICES
do
 DLGTXT="$DLGTXT
   <hbox>
    <text><label>$(gettext 'Install Puppy to') ${ONECHOICE}:</label></text>
    <button>
     <input file>/usr/local/lib/X11/mini-icons/mini-dog.xpm</input>
     <action>Exit:${ONECHOICE}</action>
    </button>
   </hbox>
"
done
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="<hbox>
 <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/local/lib/X11/mini-icons/mini-hdisk.xpm</input>
     <label>GParted</label>
     <action>EXIT:RUNGPARTED</action>
    </button>
</hbox>
$MSGex1
<text><label>\" \"</label></text>
"
fi

DLGEXT=""

if [ "`echo "$DRVMSG" | cut -f 1 -d ':' | grep "[a-zA-Z]$"`" = "" ];then
 #ask if want superfloppy install...
 DLGEXT="$DLGEXT
   <frame $(gettext 'Filesystem in partition')>
    <hbox>
     <text><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. Ntfs 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>
       <input file>/usr/local/lib/X11/mini-icons/gparted.xpm</input>
       <action>EXIT:RUNGPARTED</action>
      </button>
     </vbox>
    </hbox>
   </frame>
   <frame Superfloppy>
   <hbox>
    <text><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>
      <input file>/usr/local/lib/X11/mini-icons/mini-dog.xpm</input>
      <action>EXIT:REFORMAT_${DRVSEL}</action>
     </button>
    </vbox>
   </hbox>
   </frame>
"
else #drive is a superfloppy.
 DLGEXT="$DLGEXT
   <frame Superfloppy>
   <hbox>
    <text><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>
      <input file>/usr/local/lib/X11/mini-icons/mini-dog.xpm</input>
      <action>EXIT:REFORMAT_${DRVSEL}1</action>
     </button>
    </vbox>
   </hbox>
   </frame>
"
fi

DLG2="
<window title=\"$(gettext 'Puppy Universal Installer')\">
 <vbox>
  <text><label>$(gettext 'Here is what Puppy has found out about the chosen drive:')</label></text>
  <text><label>\"${DRVMSG}\"</label></text>
$DLGTXT
$DLGEXT
  <hbox>
   <button>
    <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`"

#returns EXIT="Cancel" or EXIT="abort"...
[ "`echo "$RETTXT" | grep 'EXIT' | grep -E 'Cancel|abort'`" != "" ] && exit

if [ "`echo "$RETTXT" | grep 'RUNGPARTED'`" != "" ];then
 gpartedfixfunc $DRVSEL #w015
 continue #BIG LOOP
fi

[ "`echo "$RETTXT" | grep 'EXIT:GOBACK'`" != "" ] && continue

#when arrive here, have
#RETTXT="EXIT:sda1", DRVMSG="sda1: vfat, size 122.1 MiB, Puppy version 108 installed" (has all parts fnd)

#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`"

  [ ! "`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 'echo "Zeroising the entire drive..."'  >> /tmp/formatsuper.sh
  #echo 'echo "(please be very patient, this could take awhile)"'  >> /tmp/formatsuper.sh
  ##echo "dd if=/dev/zero bs=1024 | $BAR -of /dev/$DRVSEL -s ${IDRVSIZM}m" >> /tmp/formatsuper.sh
  #echo "dd if=/dev/zero of=/dev/$DRVSEL" >> /tmp/formatsuper.sh
  #no, will just zeroise the mbr section...
  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
  rxvt -bg "orange" -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
  #echo "$DRVSEL" > /tmp/pup_event_frontend_block_request
  #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`"
  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###############



if [ "`echo "$DESTPART" | grep "[0-9]$"`" = "" ];then
 #superfloppy
DLG4="
<window title=\"$(gettext 'Puppy Universal Installer')\">
 <vbox>
  <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>
  <hbox><button ok></button><button cancel></button></hbox>
 </vbox>
</window>
"
 RETTXT="`echo "$DLG4" | ${GTKDIALOGEXE} --stdin`"
else
DLG4="
<window title=\"$(gettext 'Puppy Universal Installer')\">

 <vbox>
  <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>
  <hbox><button ok></button><button cancel></button></hbox>
 </vbox>
</window>
"
 RETTXT="`echo "$DLG4" | ${GTKDIALOGEXE} --stdin`"
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.

#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" | cut -b 1-3`"
   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
    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/lib/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
     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"
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_DEVXSFS} 2>/dev/null #100913
fi
sync
umount /mnt/data

echo
echo "'$(gettext 'Making')' $DESTPART '$(gettext 'bootable...')'"
syslinux  /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
cp -f $SRCPATH/${DISTRO_ZDRVSFS} /mnt/data/ 2>/dev/null
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
    rxvt -bg "orange" -title "$(gettext 'Puppy Universal Installer')" -geometry 80x10 -e /tmp/installpup.sh $DESTPART $SRCPATH $FINALPMEDIA $DISTRO_FILE_PREFIX $DISTRO_PUPPYSFS $DISTRO_ZDRVSFS #$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"
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_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
cp -f $SRCPATH/${DISTRO_ZDRVSFS} /mnt/data/ 2>/dev/null
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
    rxvt -bg "orange" -title "$(gettext 'Puppy Universal Installer')" -geometry 80x10 -e /tmp/installpup.sh $DESTPART $SRCPATH $FINALPMEDIA $DESTFS $DISTRO_FILE_PREFIX $DISTRO_PUPPYSFS $DISTRO_ZDRVSFS #$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
  #v2.02 ntfs-3g needs this...
  #[ "$DESTMNTPT" = "" ] && DESTMNTPT="`ps -e | grep -o ntfs\-3g.* | grep "/dev/$DESTPART" | sed -e 's/[^a-zA-Z0-9/._\-]/ /g' | tr -s ' ' | tr ' ' "\n" | grep '^/mnt/'`"
  
  #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.
  #if [ $PUPVEROLD -eq 0 ];then
  if [ "$PUPVEROLD" = "0.0" ];then #110422
   PREVPUPMSG="$(gettext 'It appears that this is a new installation.')"
  else
   PREVPUPMSG="`eval_gettext \"There appears to be an existing installation of Puppy, version \\\$PUPVEROLD,
and you may choose to upgrade or totally replace.\"`"
  fi
  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).
$PREVPUPMSG 

$(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 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 -font "8x16" -outline 0 -margin 4 -bg orange -text "Please wait, copying files to $DESTPART, in folder $NEWPSUBDIR..." &
#   /usr/X11R7/bin/yaf-splash -font "8x16" -outline 0 -margin 4 -bg orange -text "Please wait, copying files to $DESTPART, in folder $NEWPSUBDIR..." &
   yaf-splash -bg orange -text "`eval_gettext \"Please wait, copying files to \\\$DESTPART, in folder \\\$NEWPSUBDIR...\"`" &
   XPID=$!
   mkdir -p $DESTMNTPT/$NEWPSUBDIR
   cp -f $SRCPATH/vmlinuz $DESTMNTPT/$NEWPSUBDIR/
   sync
   cp -f $SRCPATH/initrd.gz $DESTMNTPT/$NEWPSUBDIR/
   sync
   cp -f ${SRCPATH}/${DISTRO_PUPPYSFS} $DESTMNTPT/$NEWPSUBDIR/ #v424
   sync
   [ -f $SRCPATH/${DISTRO_ZDRVSFS} ] && cp -f $SRCPATH/${DISTRO_ZDRVSFS} $DESTMNTPT/$NEWPSUBDIR/
   sync
   #[ -f $SRCPATH/${DISTRO_DEVXSFS} ] && cp -f $SRCPATH/${DISTRO_DEVXSFS} $DESTMNTPT/$NEWPSUBDIR/ #100913
   sync
   FPCAPS="`echo -n "$FINALPMEDIA" | tr [a-z] [A-Z]`"
   touch $DESTMNTPT/$NEWPSUBDIR/$FPCAPS #needed by WakePup
   sync
   kill $XPID
   umount /dev/$DESTPART
   
   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...
   #if [ ! $PUPVEROLD -eq 0 ];then
   #if [ "$PUPVEROLD" != "0.0" ];then #110422  130613 remove if.
    Xdialog --wmclass "gtkdialog2" --left --buttons-style text --title "$(gettext 'Puppy Universal Installer')" --ok-label "$(gettext 'UPGRADE')" --cancel-label "$(gettext 'WIPE')" --yesno "$(eval_gettext "There is already a version of Puppy installed in \${DESTPART}.")\n$(gettext 'Do you want to upgrade, or totally wipe the partition?')\n\n$(eval_gettext "FINAL SANITY CHECK: ARE YOU SURE \${DESTPART} IS THE RIGHT PARTITION?")\n$(eval_gettext "(note, \${DESTPART} is currently mounted on \${DESTMNTPT} -- okay?)")\n\n$(gettext 'Select UPGRADE button to upgrade...')\n$(gettext 'Select WIPE button to wipe partition...')\n$(gettext 'Click window Close box to quit...')" 0 0
    RET_VAL=$?
    if [ $RET_VAL -eq 255 ];then
     rox -D "$DESTMNTPT"
     sync
     umount $DESTMNTPT 2>/dev/null
     exit
    fi
    if [ $RET_VAL -eq 1 ];then #no
     Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy Universal Installer')" --no-buttons --infobox "$(gettext 'Please wait, wiping all files in') $DESTPART..." 0 0 200000 &
     XPID=$!
     sleep 3
     rm -rf $DESTMNTPT/*
     sync
     kill $XPID
     #PUPVEROLD=`expr $DISTRO_VERSION - 1` #v430
     PUPVEROLD='0.0' #110422
    fi
   #fi
   Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy Universal Installer')" --no-buttons --infobox "$(gettext 'Please wait, copying Puppy files to') $DESTPART..." 0 0 120000 &
   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...
   #SFSPATTERN="^${DISTRO_PUPPYSFS}" #100913
   #xSFSNAME="`ls -1 $SRCPATH/ | grep "$SFSPATTERN" | head -n 1`" #v3.01
   ##losetup-FULL /dev/loop2 $SRCPATH/${DISTRO_FILE_PREFIX}-$NEWVER.sfs
   #losetup-FULL /dev/loop2 $SRCPATH/$xSFSNAME #v3.01
   losetup-FULL /dev/loop2 $SRCPATH/$DISTRO_PUPPYSFS #100913
   mkdir $DESTMNTPT/srcmntpt
   mount -r -t squashfs /dev/loop2 $DESTMNTPT/srcmntpt
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/bin $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/dev $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/etc $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/lib $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/mnt $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/proc $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/root $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/sbin $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/tmp $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/usr $DESTMNTPT/
   cp -a -u --remove-destination $DESTMNTPT/srcmntpt/var $DESTMNTPT/
   [ -d $DESTMNTPT/srcmntpt/opt ] && cp -a -u --remove-destination $DESTMNTPT/srcmntpt/opt $DESTMNTPT/ #v2.11
   
   #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/
   
   #v2.12 external modules file...
   #w482 fix handling if separate zdrv file...
   [ -f $SRCPATH/${DISTRO_ZDRVSFS} ] && cp -af $SRCPATH/${DISTRO_ZDRVSFS} $DESTMNTPT/
   [ -f /${DISTRO_ZDRVSFS} ] && cp -af /${DISTRO_ZDRVSFS} $DESTMNTPT/
   if [ -f $DESTMNTPT/${DISTRO_ZDRVSFS} ];then #w482
    mkdir -p $DESTMNTPT/data
    mount -t squashfs -o loop $DESTMNTPT/${DISTRO_ZDRVSFS} $DESTMNTPT/data
    cp -a -f --remove-destination $DESTMNTPT/data/* $DESTMNTPT/
    depmod -b $DESTMNTPT
    sync
    umount $DESTMNTPT/data
    rm -f $DESTMNTPT/${DISTRO_ZDRVSFS}
   fi
   #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

   ##100913 do same for devx...
   #[ -f $SRCPATH/${DISTRO_DEVXSFS} ] && cp -af $SRCPATH/${DISTRO_DEVXSFS} $DESTMNTPT/
   #[ -f /${DISTRO_DEVXSFS} ] && cp -af /${DISTRO_DEVXSFS} $DESTMNTPT/
   #if [ -f $DESTMNTPT/${DISTRO_DEVXSFS} ];then #w482
   # mkdir -p $DESTMNTPT/data
   # mount -t squashfs -o loop $DESTMNTPT/${DISTRO_DEVXSFS} $DESTMNTPT/data
   # cp -a -f --remove-destination $DESTMNTPT/data/* $DESTMNTPT/
   # sync
   # umount $DESTMNTPT/data
   # rm -f $DESTMNTPT/${DISTRO_DEVXSFS}
   #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.
   #v3.96 /etc/rc.d/PUPSTATE only has 'PUPMODE=2', add this... 110126 remove...
   #if [ -e /proc/ide ];then
   # echo "SATADRIVES='$SATADRIVES'" >> $DESTMNTPT/etc/rc.d/PUPSTATE
   #else
    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
   Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy Universal Installer')" --no-buttons --infobox "$(gettext 'Please wait, executing rc.update script...')" 0 0 60000 &
   XPID=$!
   sleep 2
   
   if [ -f $DESTMNTPT/etc/puppyversion ];then #historical file, old installations.
    echo -n "$PUPVEROLD" > $DESTMNTPT/etc/puppyversion #set it back to what it was.
   fi
   if [ -f $DESTMNTPT/etc/DISTRO_SPECS ];then
    vPATTERN="s%^DISTRO_VERSION=.*%DISTRO_VERSION='${PUPVEROLD}'%" #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
   
   #chroot $DESTMNTPT -c /etc/rc.d/rc.update option2hdinstall
   #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
    #echo "/dev/cdrom    /mnt/cdrom   iso9660  noauto,ro              0 0" >> $DESTMNTPT/etc/fstab
    sync
   kill $XPID
   umount $DESTMNTPT/srcmntpt
   rmdir $DESTMNTPT/srcmntpt
  fi

  #make bootable...
  #usb/floppy boot disk, or grub.

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

   #091210 simpler stuff for grub4dos...
   if [ -f /usr/sbin/grub4dosconfig ];then
    mkdir -p /mnt/$DESTPART/boot/grub
    cp -af $SRCPATH/vmlinuz /mnt/$DESTPART/boot/
    sync
    umount /mnt/$DESTPART
    unmountcdfunc
    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 won't 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
     #[ "$SATADRIVES" != "" ] && [ "`echo "$SATADRIVES" | grep "$ONEUSB"`" != "" ] && continue #eliminate sata drives, old kernel v3.97 110126
     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
     sync
     echo -n "'$(gettext 'Done. Press ENTER key:')' "
     read allisdone
' > /tmp/installpup.sh
     chmod 755 /tmp/installpup.sh
     rxvt -bg "orange" -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###
