#!/bin/sh
#(c) Copyright Barry Kauler 2009, puppylinux.com
#2009 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html).
#a gui for makebootfat and syslinux, to make a usb drive bootable, then install puppy.
#optional drive name passed in, ex: 'sdc'.
#100913 new simplified puppy filenames.
#101026 .sfs was not getting copied off cd. various improvements.
#110130 fix for ldlinux.sys deleted, see jinx100 http://murga-linux.com/puppy/viewtopic.php?t=63719&start=165
#110505 support sudo for non-root user.
#120201 rodin.s: internationalized.
#120311 ComboFormat, 128M first partition not big enough for some puppies, change to 256M.
#120323 replace 'xmessage' with 'pupmessage'.
#130203 change probedisk2 to probedisk.
#130722 new method to update desktop drive icons. see also /usr/sbin/gparted_shell, puppyinstaller.

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

export TEXTDOMAIN=bootflash
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
export LANGORG=$LANG

PARAM1=""
[ $1 ] && PARAM1="$1"
USBDRV="$PARAM1"

#export LANG=C # turn off for gettext rodin.s
. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS #100913

# <text><label>I suggest, if you haven't looked at the BIOS setup boot choices, quit this program and do that first. It is usually entered by pressing F1 or DEL keys when the computer is just powered on -- plug in the USB drive beforehand so that you can see if the BIOS auto-detects it.</label></text>
# Please be aware that running this program will erase anything currently on the USB drive.
# <text><label>The successive fallbacks are to try 'USB-ZIP' then 'USB-FDD'. Ticking one of these radiobuttons will format the USB drive such that it seems like a zip or floppy drive to the PC, then hopefully the PC will boot from it. 'All of the above' makes your USB drive seem like a hard/zip/floppy all together, but note that it creates a strange formatting of the USB drive that some partitioning tools such as Gparted cannot recognise -- so treat that as the last resort.</label></text>
#  <text><label>Click 'OK' to continue, 'Cancel' to quit:</label></text>

echo '#!/bin/sh' > /tmp/bootme-morehelp
echo 'export HELP_DIALOG="<window title=\"'$(gettext 'BootFlash: Help')'\" icon-name=\"gtk-info\">
<hbox>
<vbox>
<text use-markup=\"true\"><label>\"<b>'$(gettext 'BIOS-setup</b>')'
'$(gettext 'When the computer is just starting to power up, pressing the DEL or F1 key usually enters the BIOS-setup. If you had plugged in the USB drive beforehand, you would be able to see whether the BIOS has auto-detected it. Or, you will be able to see whether the BIOS has manual choices of USB-HDD, USB-ZIP or USB-FDD. It is recommended that you do this before running BootFlash.')'

'$(gettext '<b>Windows compatibility</b>')'
'$(gettext 'BootFlash creates one or two partitions on the USB drive, or in the case of USB-FDD formats the drive without any partitions (what is called <i>SuperFloppy</i> mode). Whatever, the filesystem created on the drive is FAT16 or FAT32, which is compatible with Microsoft Windows. This means that you can read and write files from Windows.')'

<b>ISO9660</b>
'$(gettext 'This one has had less testing, and I got it to work on 2 out of 4 PCs that I tested. For a BIOS with manual choices, choose USB-HDD. However, unlike the USB-HDD choice in BootFlash which formats the drive with one FAT partition, this creates two partitons, the first one ISO9660 and the second FAT -- the first is the filesystem used in CDs. It is not normal to have a ISO9660 filesystem in a Flash memory or hard drive, so I am not surprised that some BIOSes do not like it! -- but on the otherhand it may work where other choices do not. However, there is one big caveat: it has the disadvantage that to upgrade Puppy the drive has to be reformatted using BootFlash, so you would have to copy-off the pupsave.2fs file (the file that has all your personal stuff) then copy it back afterward -- a bit more involved than the usual super-simple upgrade process.')'\"</label></text>
</vbox>

<vbox>
<text use-markup=\"true\"><label>\"<b>'$(gettext 'Installing Puppy</b>')'
'$(gettext 'BootFlash will format the USB drive so that it is bootable, then will offer to install Puppy Linux. Obviously, as this erases whatever was on the drive, it is for doing a new installation of Puppy.')'

'$(gettext '<b>Upgrading Puppy</b>')'
'$(gettext 'You could use the Puppy Universal Installer, however it is a piece of cake to do it manually. When a new version of Puppy is released, download the live-CD iso file, then just click on it to open it. Drag the files vmlinuz, initrd.gz and puppy.sfs (and zdrv.sfs if it exists) (some builds of Puppy have different names for these files, for example: wary_092.sfs and zw092345.sfs) to the USB drive, and that is it, you have upgraded!
It is super-simple, but just a warning: if there are files of an old installation, for example wary_091.2fs and zw091346.sfs, just delete those.')'

'$(gettext 'WARNING: There is one caveat to the above. Be careful before deleting the old puppy.sfs that it is not in use by Puppy. If the PC has 256MB or more of RAM, Puppy will normally copy puppy.sfs into RAM, so the original is not being used and can be deleted. To find out, open a terminal and type <i>losetup</i> -- if you see something like /mnt/tmpfs/puppy.sfs then it is in RAM. If not, or uncertain, boot another Puppy from a different drive/CD.')'

'$(gettext '<b>Puppy Universal Installer</b>')'
'$(gettext 'This is a tool for installing Puppy to any media, including USB. BootFlash is an alternative tool specific to USB and for new installations only. You might want to try the Universal Installer first, and if you cannot get the drive to boot then try BootFlash.')'\"</label></text>
</vbox>

</hbox>
</window>"
gtkdialog3 --center --program=HELP_DIALOG' >> /tmp/bootme-morehelp
chmod 777 /tmp/bootme-morehelp

#About dlg...
echo '#!/bin/sh' > /tmp/bootme-about
echo 'export ABOUT_DIALOG="<window title=\"'$(gettext 'BootFlash: About')'\" icon-name=\"gtk-info\">
<vbox>
<text use-markup=\"true\"><label>\"<b>'$(gettext 'BootFlash</b>
Is a GUI application written by Barry Kauler, March 2009. (c) copyright Barry Kauler 2009, licenced under the Lesser GPL licence v2, http://www.fsf.org/licensing/licenses/lgpl.html.

BootFlash uses excellent applications under-the-hood, in particular I would like to acknowledge Makebootfat created by Andrea Mazzoleni, and Syslinux created by Peter Anvin.

Note, Makebootfat has been patched by me to optimise the number of heads and sectors/track to suit BootFlash. Note also, Syslinux version 3.72 or greater is required for the ISO9660 mode of BootFlash.')'\"</label></text>
</vbox>
</window>"
gtkdialog3 --center --program=ABOUT_DIALOG' >> /tmp/bootme-about
chmod 777 /tmp/bootme-about


export MAIN_DIALOG="<window title=\"$(gettext 'BootFlash: make USB drive bootable')\" icon-name=\"gtk-preferences\">
<vbox>

<hbox>
<vbox>
 <text><label>\"  \"</label></text>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'Welcome! The intention of BootFlash is to get a USB memory stick (Flash drive) or USB hard drive to boot off any computer that has options in its BIOS setup for booting from USB ...then to install Puppy on it')</b>\"</label></text>
 
 <text><label>`gettext \"Your PC's BIOS setup has choices for booting from various types of drives. Modern BIOSes will autodetect whatever USB drives are plugged in, but older BIOSes have fixed choices such as 'USB-HDD' (hard drive), 'USB-ZIP' (zip drive) or 'USB-FDD' (floppy drive). For modern auto-detecting BIOSes or an older one that offers 'USB-HDD', I suggest you make that your preferred choice and tick the 'USB-HDD' radiobutton below. However, it may not work, even on modern computers.\"`</label></text>
 
 <text use-markup=\"true\"><label>\"<b>$(gettext 'Please be aware that running this program will erase anything currently on the USB drive')</b>\"</label></text>
 
 <frame>
 <radiobutton><label>$(gettext 'USB-HDD (hard drive) PREFERRED CHOICE')</label><variable>RADIO_HDD</variable></radiobutton>
 <radiobutton><label>$(gettext 'USB-ZIP (zip drive)')</label><variable>RADIO_ZIP</variable></radiobutton>
 <radiobutton><label>$(gettext "USB-FDD ('SuperFloppy' drive)")</label><variable>RADIO_FDD</variable></radiobutton>
 <radiobutton><label>$(gettext 'ComboFormat (hard/zip/floppy chameleon)')</label><variable>RADIO_ALL</variable></radiobutton>
 <radiobutton><label>$(gettext 'ISO9660 (CD filesystem)')</label><variable>RADIO_ISO</variable></radiobutton>
 </frame>
 
 <hbox>
  <button><label>$(gettext 'More help')</label><action>/tmp/bootme-morehelp & </action></button>
  <button><label>$(gettext 'About')</label><action>/tmp/bootme-about & </action></button>
  <text><label>\"                                    \"</label></text>
  <button ok></button>
  <button cancel></button>
 </hbox>

 </vbox>
 <vbox>
  <frame $(gettext 'Help')>
   <text use-markup=\"true\"><label>\"<b>USB-HDD</b>
`gettext \"This is the preferred choice for modern PCs, whether the USB drive is an actual hard drive or a Flash 'memory stick'. The BIOS in modern PCs will auto-detect whatever is plugged in, and slightly older BIOSes may have a 'USB-HDD' choice in the BIOS-setup -- if so, choose that. Note though, even modern PCs may not boot the USB drive, and you may need to look at one of the following choices.

<b>USB-ZIP</b>
This will make the USB drive seem like a Zip drive. A slightly older BIOS may offer a 'USB-ZIP' boot option, so if you set that in the BIOS then format the USB drive to seem like a Zip drive, it may succeed. You can even do this with a USB hard drive.

<b>USB-FDD</b>
This is also known as <b>SuperFloppy</b> mode and will make the USB drive seem like a floppy drive. The drive is formatted without any partitions -- just like a floppy, the entire disk is used for the filesystem.

<b>ComboFormat</b>
This one is very cunning. It makes the drive seem like a HDD, ZIP <i>and</i> FDD. This is the one to choose if you want to be able to take your USB memory stick to just about any PC and it will boot.\"`\"</label></text>   
  </frame>
 </vbox>
 </hbox>
 
 
</vbox>
</window>"

RETPARAMS="`gtkdialog3 --center --program=MAIN_DIALOG`"
#ex:
#RADIO_ALL="false"
#RADIO_FDD="false"
#RADIO_HDD="true"
#RADIO_ZIP="false"
#EXIT="OK"

eval "$RETPARAMS"

[ "$EXIT" != "OK" ] && exit

#decide what drive...
if [ "$USBDRV" = "" ];then
 export PLUGIN_DIALOG="<window title=\"BootFlash\" icon-name=\"gtk-preferences\">
<vbox>
 <pixmap><input file>/usr/local/lib/X11/pixmaps/question.xpm</input></pixmap>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'Please plugin the USB drive right now. If it is already plugged in, make absolutely sure that it is unmounted before continuing')</b>\"</label></text>
 <text><label>$(gettext 'Note, it does not matter if you have other USB drives or devices plugged in, as the next dialog window will present them all and ask you to choose one.')</label></text>
 <hbox>
  <text><label>`gettext \"Click 'OK' to continue, 'Cancel' to quit:\"`</label></text>
  <button ok></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>"
 RETPARAMS="`gtkdialog3 --center --program=PLUGIN_DIALOG`"
 eval "$RETPARAMS"
 [ "$EXIT" != "OK" ] && exit
 sleep 2
 PROBEDISK="`probedisk 2> /tmp/probediskerr.txt`"
 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.
  USBMEMTYPE="Flash memory"
  [ "`cat /sys/block/$ONEUSB/removable`" != "1" ] && USBMEMTYPE="hard drive"
   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 \"${USBMEMTYPE}, ${USBINFO}, ${USBSIZE}\""
 done
 xSELECTIONS="`echo "$SELECTIONS" | tr '\n' ' ' | tr -s ' '`"
 if [ "$SELECTIONS" = "" ];then
  pupmessage -bg red -center -title "$(gettext 'ERROR')" "$(gettext 'No USB drive plugged in!')"
  exit
 fi
 CHOOSEDLG="Xdialog --wmclass \"gtkdialog2\" --title \"$(gettext 'BootFlash: choose drive')\" --no-cancel --stdout --menubox \"$(gettext 'Choose which drive to format:')\" 10 60 0 ${xSELECTIONS}"
 DRVSEL="`eval "$CHOOSEDLG"`"
 [ $? -ne 0 ] && exit
 [ "$DRVSEL" = "" ] && exit #precaution.
 [ "`echo -n "$DRVSEL" | cut -c 1,2`" != "sd" ] && exit #precaution.
 USBDRV="$DRVSEL"
 #reconfirm correct drv choice...
 MYSELECTION="`echo "$SELECTIONS" | grep "^${USBDRV} " | tr '"' ' ' | tr -s ' '`" #'geany
 export RECONFIRM_DIALOG="<window title=\"BootFlash\" icon-name=\"gtk-preferences\">
<vbox>
 <pixmap><input file>/usr/local/lib/X11/pixmaps/question.xpm</input></pixmap>
 <text><label>$(gettext 'Please reconfirm that this is the USB drive that you want to format:')</label></text>
 <text use-markup=\"true\"><label>\"<b>${MYSELECTION}</b>\"</label></text>
 <text><label>`gettext \"Please also reconfirm that you want to use BootFlash to reformat the drive (and install Puppy). If you have a new USB drive, it is normally recommended that you use the Puppy Universal Installer first, as that will attempt to make the drive bootable as-is, that is, without reformatting it. If you haven't tried the Universal Installer yet, recommend that you hit Cancel here, and run the Universal Installer from the Setup menu.\"`</label></text>
 <hbox>
  <text><label>`gettext \"Click 'OK' to continue, 'Cancel' to quit:\"`</label></text>
  <button ok></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>"
 RETPARAMS="`gtkdialog3 --center --program=RECONFIRM_DIALOG`"
 eval "$RETPARAMS"
 [ "$EXIT" != "OK" ] && exit
fi

#make sure not mounted...
while [ "`mount | grep "^/dev/$DRVSEL"`" != "" ];do
 export PLUGIN_DIALOG="<window title=\"BootFlash\" icon-name=\"gtk-preferences\">
<vbox>
 <pixmap><input file>/usr/local/lib/X11/pixmaps/error.xpm</input></pixmap>
 <text use-markup=\"true\"><label>\"<b>`eval_gettext \"Drive \\\${DRVSEL} is still mounted. Please unmount it before continuing. If the drive has multiple partitions, are they all unmounted?\"`</b>\"</label></text>
 <text><label>$(gettext 'Note: click on the appropriate mounted desktop drive icon, and you will receive an offer to unmount it.')</label></text>
 <hbox>
  <text><label>`gettext \"Click 'OK' to continue, 'Cancel' to quit:\"`</label></text>
  <button ok></button>
  <button cancel></button>
 </hbox>
</vbox>
</window>"
 RETPARAMS="`gtkdialog3 --center --program=PLUGIN_DIALOG`"
 eval "$RETPARAMS"
 [ "$EXIT" != "OK" ] && exit
 sleep 1
done


#gather the files together...
rm -rf /tmp/makebootfat 2>/dev/null
mkdir /tmp/makebootfat
cd /tmp/makebootfat
cp -a /usr/lib/syslinux/ldlinux.bss ./
cp -a /usr/lib/syslinux/mbrfat.bin ./
cp -a /usr/lib/syslinux/mbr.bin ./
cp -a /usr/lib/syslinux/ldlinux.sys ./
mkdir tempdir

#yaf-splash -font "8x16" -outline 0 -margin 4 -bg orange -text "Please wait, formatting ${USBDRV} drive..." &
yaf-splash -close never -bg orange -text "`eval_gettext \"Please wait, formatting \\\${USBDRV} drive...\"`" &
X1PID=$!

if [ "$RADIO_HDD" = "true" ];then #hard drive.
 #weird code here...
 dd if=/dev/zero of=/dev/$USBDRV bs=512 count=1
 sync
 #fdisk cannot be used to create a partition table, it won't boot, use makebootfat...
 makebootfat --output /dev/$USBDRV --syslinux3 --boot ldlinux.bss --mbr mbr.bin --copy ldlinux.sys tempdir
 #...but drive broken, fix as follows...

 #update: have patched 'makebootfat', no longer need this...
 ##optional...
 ##maybe should use 'parted'. with fdisk, to change heads/sectors, have to create a broken partition...
 ##fdisk weird, delete partition, change to 255 heads, 63 sectors/track, create a (wrong) partition...
 #echo -e 'd\nx\nh\n255\ns\n63\nr\nn\np\n1\n\n\nw' | fdisk /dev/$USBDRV
 #sync
 ##delete wrong partition, create correct partition...
 #echo -e 'd\nn\np\n1\n\n\nw' | fdisk /dev/$USBDRV
 #sync
 
 #also optional...
 dd if=/dev/zero of=/dev/${USBDRV}1 bs=512 count=1 #zeroise first 512 bytes of partition. some apps need this.

 DRVSIZEB=`fdisk -l /dev/${USBDRV} | grep '^Disk /dev/' | cut -f 5 -d ' '` #in bytes.
 DRVSIZEK=`expr $DRVSIZEB \/ 1024`
 DRVSIZEM=`expr $DRVSIZEK \/ 1024`
 sync
 if [ $DRVSIZEM -gt 1024 ];then #1GB
  #set partition-id to fat32 (0b=fat32, oc=fat32-lba)...
  echo -e 't\n0b\nw' | fdisk /dev/$USBDRV
  mkdosfs -F 32 /dev/${USBDRV}1 #FAT32.  -n puppysave
 else
  #set partition-id to fat16 (06=fat16, 0e=fat16-lba)...
  echo -e 't\n06\nw' | fdisk /dev/$USBDRV
  mkdosfs -F 16 /dev/${USBDRV}1 #FAT16.  -n puppysave
 fi
 sync
 
 #110130 fix for ldlinux.sys wiped... note, i am uncertain how this hack makes any difference...
 makebootfat --output /dev/$USBDRV --syslinux3 --boot ldlinux.bss --mbr mbr.bin --copy ldlinux.sys tempdir
 sync
 
 syslinux /dev/${USBDRV}1
 #um, it may still need to have the boot flag set...
 [ "`fdisk -l /dev/$USBDRV | grep '/dev/.* \\* '`" = "" ] && echo -e 'a\n1\nw' | fdisk /dev/$USBDRV
 #one big fat partition, pup_save will be in it too...
 PUPSAVEPART="${USBDRV}1"
 PUPBOOTPART="${USBDRV}1"
else

 if [ "$RADIO_FDD" = "true" ];then #only choose this for drives no greater than 1GB.
  #take out '--mbrfat' has partition but makes look like floppy drive.
  #take out '--mbr mbrfat.com' as there are no partitions.
  #take out '--zip' as that forces 32 sector 64 head zip disk architecture.
  #add '--disk' to ensure operate on disk not a partition.
  makebootfat --output /dev/$USBDRV --syslinux3 --disk --boot ldlinux.bss --copy ldlinux.sys tempdir
  PUPSAVEPART="${USBDRV}"
  PUPBOOTPART="${USBDRV}"
 else

  #need space for 256MB image...
  WKGPATH="/tmp"
  FREEM=`df -m | grep ' /tmp$' | tr -s ' ' | cut -f 4 -d ' '`
  [ ! $FREEM ] && FREEM=0
  if [ $FREEM -lt 300 ];then
   WKGPATH="/root"
   FREEM=`df -m | grep ' /$' | tr -s ' ' | cut -f 4 -d ' '`
   if [ $FREEM -lt 300 ];then
    WKGPATH="`readlink /mnt/home`"
    mhPATTERN=' '"$WKGPATH"'$'
    FREEM=`df -m | grep "$mhPATTERN" | tr -s ' ' | cut -f 4 -d ' '`
   fi
  fi
  if [ $FREEM -lt 300 ];then
   kill $X1PID
   pupmessage -bg red -title "$(gettext 'ERROR')" "$(gettext 'SORRY, cannot find 256MB free working space!')"
   exit
  fi

  if [ "$RADIO_ISO" = "true" ];then
   #need puppy.iso to copy to usb drive...
   kill $X1PID
   SRCFPATH="`Xdialog --wrap --wmclass "gtkdialog2" --left --backtitle "$(gettext "The 'ISO9660' choice requires the Puppy .iso file to be written directly into the USB drive. Please select it then click 'OK':")" --title "BootFlash: ISO9660 format" --stdout --no-buttons --no-cancel --fselect "*.iso" 0 0`"
   [ $? -ne 0 ] && exit
   #yaf-splash -font "8x16" -outline 0 -margin 4 -bg orange -text "Please wait, copying .iso file..." &
   yaf-splash -close never -bg orange -text "$(gettext 'Please wait, copying .iso file...')" &
   X1PID=$!
   rm -f $WKGPATH/image256m 2>/dev/null
   cp -f "$SRCFPATH" $WKGPATH/image256m
   sync
   isohybrid $WKGPATH/image256m
   sync
   PUPSAVEPART="${USBDRV}2"
   PUPBOOTPART="${USBDRV}1"
  else
   #create 256M empty file...
   dd if=/dev/zero of=${WKGPATH}/image256m bs=1024 count=262144 #256MB 131072=128MB
   sync
   LOOPDEV="`losetup -f`"
   losetup $LOOPDEV ${WKGPATH}/image256m
   if [ "$RADIO_ALL" = "true" ];then #ComboFormat
    makebootfat --output $LOOPDEV --syslinux3 --zip --boot ldlinux.bss --mbr mbrfat.bin --mbrfat --copy ldlinux.sys tempdir
    #'--zip' sets partition-4 as bootable, so pup_save will go into partition-1...
    PUPSAVEPART="${USBDRV}1"
    PUPBOOTPART="${USBDRV}4"
   fi
   if [ "$RADIO_ZIP" = "true" ];then
    #take out '--mbrfat' has partition but makes look like floppy drive.
    makebootfat --output $LOOPDEV --syslinux3 --zip --boot ldlinux.bss --mbr mbrfat.bin --copy ldlinux.sys tempdir
    #'--zip' sets partition-4 as bootable, so pup_save will go into partition-1...
    PUPSAVEPART="${USBDRV}1"
    PUPBOOTPART="${USBDRV}4"
   fi
   sync
   losetup -d $LOOPDEV
  fi

  #write bootable f.s. to drive...
  dd if=${WKGPATH}/image256m of=/dev/$USBDRV
  sync
  rm -f ${WKGPATH}/image256m

  #now create partition to fill remaining space on usb drive...
  if [ "$RADIO_ISO" = "true" ];then
   echo -e 'n\np\n2\n\n\nw' | fdisk /dev/$USBDRV #create partition-2
   PARTNUM="2"
  else
   echo -e 'n\np\n1\n\n\nw' | fdisk /dev/$USBDRV #create partition-1
   PARTNUM="1"
  fi
  DRVSIZEB=`fdisk -l /dev/${USBDRV} | grep '^Disk /dev/' | cut -f 5 -d ' '` #in bytes.
  DRVSIZEK=`expr $DRVSIZEB \/ 1024`
  DRVSIZEM=`expr $DRVSIZEK \/ 1024`
  if [ $DRVSIZEM -gt 1024 ];then #1GB
   #set partition-id to fat32 (0b=fat32, oc=fat32-lba)...
   echo -e "t\n${PARTNUM}\n0b\nw" | fdisk /dev/${USBDRV}
   sync
   mkdosfs -F 32 -n puppysave /dev/${PUPSAVEPART} #FAT32.
  else
   #set partition-id to fat16 (06=fat16, 0e=fat16-lba)...
   echo -e "t\n${PARTNUM}\n06\nw" | fdisk /dev/${USBDRV}
   mkdosfs -n puppysave /dev/${PUPSAVEPART} #FAT16.
  fi

 fi
fi

#need to refresh desktop drive icons(s)...
##/tmp/pup_event_frontend_block_request is used in /sbin/pup_event_frontend_d to refresh drv...
#echo "$USBDRV" > /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/${USBDRV}/uevent

kill $X1PID


#announce finished, offer to install Puppy...
if [ "$RADIO_ISO" = "true" ];then
 export FINISHED_DIALOG="<window title=\"$(gettext 'BootFlash: ISO9660 format finished')\" icon-name=\"gtk-preferences\">
<vbox>
 <pixmap><input file>/usr/local/lib/X11/pixmaps/ok.xpm</input></pixmap>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'All done!')</b>\"</label></text>
 <text><label>$(gettext 'The drive now has two partitions, ISO9660 and the rest of the drive a FAT partition. Puppy will offer to create a pup-save file in the latter, at the first shutdown after successfully booting from the USB drive.')</label></text>
 <hbox>
  <button ok></button>
 </hbox>
</vbox>
</window>"
 gtkdialog3 --center --program=FINISHED_DIALOG
 exit
fi

export FINISHED_DIALOG="<window title=\"$(gettext 'BootFlash: install Puppy')\" icon-name=\"gtk-preferences\">
<vbox>
 <pixmap><input file>/usr/local/lib/X11/pixmaps/question.xpm</input></pixmap>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'USB drive now (hopefully) bootable!')</b>\"</label></text>
 <text><label>$(gettext 'Now, if you wish, you may install Puppy Linux on the drive. If you decline, it can be done later using the Puppy Universal Installer. If you choose to install Puppy now, you must have a Puppy live-CD .iso file available locally (that you had previously downloaded)').</label></text>
 <hbox>
  <button type=\"exit\">
   <label>$(gettext 'Install Puppy')</label>
  </button>
  <button type=\"exit\">
   <input file icon=\"gtk-close\"></input>
   <label>$(gettext 'Exit, finished')</label>
  </button>
 </hbox>
</vbox>
</window>"
RETPARAMS="`gtkdialog3 --center --program=FINISHED_DIALOG`"

eval "$RETPARAMS"
[ "$EXIT" != "$(gettext 'Install Puppy')" ] && exit

cd /root
SRCFPATH="`Xdialog --wrap --wmclass "gtkdialog2" --left --backtitle "$(gettext "Please select a Puppy live-CD .iso file, then click 'OK':")" --title "$(gettext 'BootFlash: Choose .iso')" --stdout --no-buttons --no-cancel --fselect "*.iso" 0 0`"
[ $? -ne 0 ] && exit
#yaf-splash -font "8x16" -outline 0 -margin 4 -bg orange -text "Please wait, copying files from .iso file..." &
yaf-splash -close never -bg orange -text "$(gettext 'Please wait, copying files from .iso file...')" &
X1PID=$!
mkdir -p /tmp/bootflash_iso
mount -t iso9660 -o loop ${SRCFPATH} /tmp/bootflash_iso
if [ $? -ne 0 -o ! -f /tmp/bootflash_iso/initrd.gz ];then
 pupmessage -bg red -center -title "$(gettext 'ERROR')" "$(gettext 'Sorry error accessing .iso file')"
 exit
fi

mkdir -p /mnt/${PUPBOOTPART}
mount -t vfat /dev/${PUPBOOTPART} /mnt/${PUPBOOTPART}
if [ "`mount | grep "/dev/${PUPBOOTPART}"`" = "" ];then
 pupmessage -bg red -center -title "$(gettext 'ERROR')" "$(gettext 'Sorry error mounting USB drive')"
 exit
fi

rm -f /mnt/${PUPBOOTPART}/*-[0-9][0-9][0-9].sfs 2>/dev/null
rm -f /mnt/${PUPBOOTPART}/*_[0-9][0-9][0-9].sfs 2>/dev/null #101026
[ -f /mnt/${PUPBOOTPART}/${DISTRO_PUPPYSFS} ] && rm -f /mnt/${PUPBOOTPART}/${DISTRO_PUPPYSFS} #100913
[ -f /mnt/${PUPBOOTPART}/${DISTRO_ZDRVSFS} ] && rm -f /mnt/${PUPBOOTPART}/${DISTRO_ZDRVSFS} #100913
#[ -f /mnt/${PUPBOOTPART}/${DISTRO_DEVXSFS} ] && rm -f /mnt/${PUPBOOTPART}/${DISTRO_DEVXSFS} #100913
#create a syslinux.cfg file...
PMEDIA="usbflash"
[ "`cat /sys/block/$USBDRV/removable`" != "1" ] && PMEDIA="usbhd" #101026 bugfix.
echo "default vmlinuz initrd=initrd.gz pmedia=${PMEDIA}" > /mnt/${PUPBOOTPART}/syslinux.cfg
cp -f /tmp/bootflash_iso/vmlinuz /mnt/${PUPBOOTPART}/
cp -f /tmp/bootflash_iso/initrd.gz /mnt/${PUPBOOTPART}/
#cp -f /tmp/bootflash_iso/*-[0-9][0-9][0-9].sfs /mnt/${PUPBOOTPART}/
#cp -f /tmp/bootflash_iso/${DISTRO_PUPPYSFS} /mnt/${PUPBOOTPART}/ 2>/dev/null #100913
#cp -f /tmp/bootflash_iso/${DISTRO_ZDRVSFS} /mnt/${PUPBOOTPART}/ 2>/dev/null #100913
#cp -f /tmp/bootflash_iso/${DISTRO_DEVXSFS} /mnt/${PUPBOOTPART}/ 2>/dev/null #100913
#cp -f /tmp/bootflash_iso/*_[0-9][0-9][0-9].sfs /mnt/${PUPBOOTPART}/ 2>/dev/null #101026
#cp -f /tmp/bootflash_iso/puppy.sfs /mnt/${PUPBOOTPART}/ 2>/dev/null #101026
#cp -f /tmp/bootflash_iso/z*.sfs /mnt/${PUPBOOTPART}/ 2>/dev/null #101026
#101026 nah, just do this...
cp -f /tmp/bootflash_iso/*.sfs /mnt/${PUPBOOTPART}/ 2>/dev/null #101026

sync

umount /mnt/${PUPBOOTPART}
umount /tmp/bootflash_iso
kill $X1PID

export DONE_DIALOG="<window title=\"$(gettext 'BootFlash: Puppy installed')\" icon-name=\"gtk-preferences\">
<vbox>
 <pixmap><input file>/usr/local/lib/X11/pixmaps/ok.xpm</input></pixmap>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'Puppy installed!')</b>\"</label></text>
 <text><label>$(gettext 'The desktop drive icons should have been updated, so you can click on them to check that everything looks right. Then reboot and find out for sure...')</label></text>
 <hbox>
  <button ok></button>
 </hbox>
</vbox>
</window>"
gtkdialog3 --center --program=DONE_DIALOG

###END###
