#!/bin/sh
#simple script to remaster the puppy live-cd.
#(c) Copyright 2006 Barry Kauler, www.puppyos.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#BK 9 oct 2007: updated for puppy v3.01.
#RE 9 apr 2008: v3.99 copy active post-install scripts
#v4.02 /lib/modules/firmware.dep.<kern>,DOTconfig-K<kern> moved to /etc/modules. ...um no mods this script.
#v411 update what gets copied from /root and /etc.
#v412 /etc/DISTRO_SPECS, renamed pup_xxx.sfs, pup_save.2fs etc.
#w001 pup files renamed to woof-555.sfs, woofsave.2fs (or similar).
#w018 april 2009: Woof-related fixes, new standardised package database format.
#w460 zdrv has new name format, ex zu500269.sfs.
# 22jun09 by Masaki Shinomiya <shino@pos.to> bugfix in cd drive selection
# 23jun09 I18N
# 27jun09 copy all *.msg and *.16 from Live CD
# 01jul09 needed space calculation
# 02jul09 I18N bugfix to burn CD
# 08jul09 remove kernel modules from initrd
# 08jul09 loop mounted iso file usable
# 7aug09 grub4dos support
#w482 shinobar: exclude /lib/modules/${KERNELVER}/initrd.
#v423 bugfix.
# 2oct09 v430JPRC shinobar: make ZDRVSFS
#v431 bugfixes.
#v431JP add application ID on iso image.(removed)
#v431JP removed app ID code, permitions of sfs files, forground collor of rxvt
#v431JP mkisofs with Joliet option, volume id, what copied from cd, remove slmodems
#v431JP error message for full install
#091212 weird bug, no processes but when run this, x restarts...
# 28dec09 remove /modules/${KERNELVER}/modules.*
#100117 save user-modified rox globicons file.
# 20jan10 Puppy v4.x(before woof) backward compatibility
#100913 simplified puppy filenames. see variables in /etc/DISTRO_SPECS.
#100916 no longer have DISTRO_DEVXSFS in /etc/DISTRO_SPECS.(it was only there briefly)
#101020 no longer have /etc/modprobe.conf.
#101107 bug fix from zygo.
#110505 support sudo for non-root user.
#110722 tweak sort.
#110808 PANZERKOPF: bug fix. 110822 reverted.
#120323 replace 'xmessage' with 'pupmessage'.
#120329 Xdialog bug --yesno supposed to "yes" "No" buttons, but they are "OK" "Cancel".
#120512 01micko: support xz compression.
#120605 rerwin: omit /dev/snd content, /dev/.udev subdirectory and modem daemons from new master (because daemons reinstalled from firmware if needed).
#120606 rerwin: support users' replacement of stripped /root & /etc with complete directories (for "boot disk").
#120607 rerwin: Remove indicators/files for integrated user-installed packages.
#120628 shinobar: no change id-string for reuse
#120721 revert 'wildcards' option (from 120605); improve /dev/snd file exclusion; exclude /usr/share/icons/hicolor/icon-theme.cache (shinobar).
#121021 rerwin: revert 120605 modem daemon removals (because daemons no longer copied from firmware directory).
#130203 change probedisk2 to probedisk.
#130216 partial support f2fs.
#130222 convert internationalization from "technosaurus method" to gettext method.
#130222 virtual-CD detection was broken.
#130223 various fixes. note, also fixed /usr/sbin/filemnt. now can retain files between remasters.
#130301 o/p of "losetup" (BB), shows all in use, but "losetup-FULL -a" needs param, also o/p format different.
#130302 L18L: moved m_09 m_10 m_11 top and made another use of it.
#130304 cp -n not supported, use -u
#130306 gettext translation fixed, line 354. 130307 again.
#130308 npierce: filter out wrong CDs from choices offered to user; remove CD drive from VIRTUALCD list.
#130527 /etc/.XLOADED has been moved to /root, refer /usr/bin/xwin.

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

#NOTE: rename to avoid clash with 'remasterpup2.mo' used by previous i18n method.
export TEXTDOMAIN=remasterpup2x
export OUTPUT_CHARSET=UTF-8
. gettext.sh

Yes_lbl="$(gettext 'Yes')"
No_lbl="$(gettext 'No')"
m_01="$(gettext 'Puppy simple CD remaster')" #window title.
m_02="$(gettext 'ERROR')"
m_07="$(gettext 'currently mounted')"
m_08="$(gettext 'not mounted')"
m_09="$(gettext 'Filesystem')" #130302
m_10="$(gettext 'Size')" #130302
m_11="$(gettext 'Free')" #130302

choice_cdd () {
 #copy files off live-cd.... w018 use probedisk2...
 SELECTIONS="`probedisk 2>&1 | grep '^/dev/' | grep "|optical|" | cut -f 1,3 -d "|" | tr " " "_" | tr "|" " " | tr '$' "_"`"
 SELECTIONS="$SELECTIONS $VIRTUALCD"
 if [ "$(echo "$SELECTIONS"|tr -d ' ')" = "" ]; then
  m_14_2="$(gettext 'NO CD/DVD drives found.')"
  Xdialog --wrap --left --title "$m_01: $m_02" --msgbox "$m_14_2
$MSG" 0 80
  exit
 fi
 if [ "$1" = "filter" ];then #130308 Filter out devices with a missing file.
  FILT_SELECTIONS=""
  i="0"
  for ASELECTION in $SELECTIONS
  do
   if [ "$((i++&1))" -eq "0"  ];then
    BLKDEV="$ASELECTION"
   else
    DEVNAME="$ASELECTION"
    MNTPNT="`mount | grep "$BLKDEV" | tr -s " " | cut -f 3 -d " "`"
    [ -z "$MNTPNT" -o -f "$MNTPNT/initrd.gz" -a -f "$MNTPNT/$PUPPYSFS" ] && FILT_SELECTIONS="$FILT_SELECTIONS $BLKDEV $DEVNAME"
   fi
  done
  SELECTIONS="$FILT_SELECTIONS"
  if [ -z "$SELECTIONS" ];then
   m_14_3="$(gettext 'Puppy live-CD or virtual CD was not found.')
$(eval_gettext "(No CD or .iso file has both 'initrd.gz' and '${PUPPYSFS}'.)")"
   Xdialog --wrap --left  --title "$m_01: $m_02" --msgbox "$m_14_3" 0 0
   exit
  fi
 fi #130308 end of filter
 m_14_1="$(gettext 'Choose the CD/DVD drive...')"
 DIALOG="Xdialog --wrap --left --title \"$m_01\" --stdout --menubox \"$MSG\n\n$m_14_1\" 0 56 4 $SELECTIONS 2> /dev/null"
 BURNERDRV=""
 RESULT=$(eval $DIALOG) #w018	# 22jun09
 [ $? -ne 0 ] && exit
 BURNERDRV=$RESULT
 [ "$BURNERDRV" = "" ] && exit
 BURNERDRV="`echo -n "$BURNERDRV" | cut -f 3 -d '/'`"
}

#LANG=C #faster, plus Xdialog happier.
KERNELVER="`uname -r`"

#120512 01micko: set compression
COMP=""
mksquashfs -help &>/tmp/mk #refuses to grep from stdout
grep 'xz' /tmp/mk &>/dev/null
[ "$?" = "0" ] && COMP="-comp xz"
ARCH="`uname -m`"
[ "$(uname -m|grep ^i)" ] && ARCH="`uname -m|cut -c3,4`"
case "$ARCH" in
86)AOPT="-Xbcj x86" ;;
*)AOPT="" ;; #todo add other arches
esac
rm /tmp/mk
COPTIONS=""${COMP}" "${AOPT}""
#end set compression

#variables created at bootup by /initrd/usr/sbin/init...
. /etc/rc.d/PUPSTATE
[ "$PUP_LAYER" = "" ] && PUP_LAYER="/pup_ro2"

. /etc/DISTRO_SPECS
if [ ! "$DISTRO_IDSTRING" ];then #101107
  #need some stuff in here if want to support older puppies. maybe also more fixes needed further down.
 pupmessage -bg red -title "RemasterPup" -center "Sorry, your Puppy is too old for this script"
 exit
else
 PUPPYSFS="$DISTRO_PUPPYSFS" #ex: puppy.sfs
 ZDRVSFS="$DISTRO_ZDRVSFS"   #ex: zdrv.sfs
 SFSBASE="`basename $PUPPYSFS .sfs`" #ex: puppy
fi

#100913 generate a new id-string...
IDDATE="`date +%y%m%d%H%M%S`" #12 bytes, ex: 100911153319
IDPREFIX="`echo -n "$DISTRO_FILE_PREFIX" | cut -b 1`"
#...needs to be appended to vmlinuz, puppy.sfs, zdrv.sfs and devx.sfs
#110422 increase size of idstring to 32 bytes...
IDSTRING="${IDPREFIX}${IDDATE}ZZZZ${DISTRO_VERSION}"
#...ZZZZ reserved space for future use.
LEN1=`echo -n "$IDSTRING" | wc -c`
LEN2=`expr 32 - $LEN1`
EXTX=''
while [ $LEN2 -gt 0 ];do
 LEN2=`expr $LEN2 - 1`
 EXTX="${EXTX}X"
done
IDSTRING="${IDSTRING}${EXTX}" #32 bytes ex: w110423092024ZZZZ5.1.2XXXXXXXXXX

PPATTERN="/initrd${PUP_LAYER}"
if [ "`mount | grep "$PPATTERN"`" = "" ];then
 #no puppy.sfs file mounted on pup_ro2, probably h.d. install...
 m_03="$(eval_gettext 'This program requires a ${PUPPYSFS} file mounted on /initrd${PUP_LAYER}.')
$(gettext 'Which is not the case here, probably because this is a full hard drive installation?')
$(gettext 'Whatever, boot Puppy from live-CD and then you will be able to use this script.')

$(eval_gettext 'Note, I have not tried it, but if you setup the situation of ${PUPPYSFS} mounted on directory /initrd${PUP_LAYER}, that will be acceptable for this program.')
$(gettext 'You will also need a current live-CD, as the program gets files off it.')
$(gettext 'Anyone want to try it?')

$(gettext "Click 'Ok' button to quit...")"
 Xdialog --wrap --left --title "m_01: m_02"  --msgbox "$m_03" 0 80
 exit
fi

SAVEPART="$PDEV1" #from PUPSTATE.
CDR="/dev/$SAVEPART"

#choose where to create isolinux-builds/ directory...
m_05="$(gettext 'Welcome! This little program takes a snapshot of your current system and burns it to CD.')

$(eval_gettext 'A Puppy live-CD has 4 main files: vmlinuz, isolinux.cfg, initrd.gz and ${PUPPYSFS}. Note, in some builds of Puppy there may also be a 5th file, ${ZDRVSFS}.')

$(eval_gettext "It is \${PUPPYSFS} that mainly interests us here: it has the entire Puppy filesystem, everything from '/' down. What this script does is rebuild this file \${PUPPYSFS}, with everything currently under '/' -- that is, all user-installed packages, all mounted .sfs extension files, everything, gets combined into one file, \${PUPPYSFS} and burnt to CD.")
$(eval_gettext "Note, if you have an 'underdog Linux' mounted, that too will get combined into the \${PUPPYSFS} file, so beware, it could get big!")

$(gettext 'Click OK button to continue (or close window to quit)...')"
Xdialog --wrap --left --title "$m_01" --msgbox "$m_05" 0 80

[ $? -ne 0 ] && exit

#calc size needed...
m_06="$(gettext 'Calculating needed working space.')
$(gettext 'Please wait, this may take awhile...')"
Xdialog --wrap --left  --title "$m_01" --no-buttons --ignore-eof  --infobox "$m_06" 0 0 0 &
XPID=$!
SIZEOPT=0
SIZEBIN=`du -sk /bin | cut -f 1`
SIZESBIN=`du -sk /sbin | cut -f 1`
SIZELIB=`du -sk /lib | cut -f 1`
SIZEUSR=`du -sk /usr | cut -f 1`
SIZEOPT=0	# 01jul09
[ -d /opt ] && SIZEOPT=`du -sk /opt | cut -f 1`
sync
SIZETOTALK=`LANG=C dc $SIZEBIN $SIZESBIN + $SIZELIB + $SIZEUSR + $SIZEOPT + p`
SIZETOTALM=`LANG=C dc $SIZETOTALK 1024 \/ p| cut -d'.' -f1`	# 01jul09
[ "$SIZETOTALM" ] || SIZETOTALM=1
#estimate a compressed size...
SIZENEEDEDM=`expr $SIZETOTALM \/ 3`
SIZESLACKM=`expr $SIZENEEDEDM \/ 3` #guess
SIZENEEDEDM=`expr $SIZENEEDEDM + $SIZESLACKM`
SIZENEEDEDM=`expr $SIZENEEDEDM + 25` #space for vmlinuz, initrd.gz, zdrv, etc
#now choose working partition... v431 add ext4...  130216 add f2fs...
PARTSLIST="`probepart -m 2> /dev/null | grep '^/dev/' | grep -E 'f2fs|ext2|ext3|ext4|reiserfs|msdos|vfat|ntfs' | cut -f 1-3 -d '|'`"

 echo -n "" > /tmp/schoices.txt
 echo "$PARTSLIST" |
 while read APART
 do
  [ "$APART" = "" ] && continue #v3.01 preaution
  MNTSTATUS="($m_07)" #"currently mounted"
  ATAG="`echo -n "$APART" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
  ASIZE="`echo -n "$APART" | cut -f 3 -d '|'`"
  AFS="`echo -n "$APART" | cut -f 2 -d '|'`"
  AFPATTERN="^/dev/$ATAG " #v3.01
  AFREE="`df -m | grep "$AFPATTERN" | tr -s " " | cut -f 4 -d " "`"
  FLAG_RO='no' #v431
  if [ ! "$AFREE" ];then
   MNTSTATUS="($m_08)" #"not mounted"
   mkdir -p /mnt/$ATAG
   mount -t $AFS /dev/$ATAG /mnt/$ATAG > /dev/null 2>&1
   if [ $? -eq 0 ];then
    if [ "$ATAG" = "fd0" ];then
     FFREE=`df -k | grep "$AFPATTERN" | tr -s " " | cut -f 4 -d " "`
     AFREE="`LANG=C dc $FFREE 1024 \/ p`"
    else
     AFREE="`df -m | grep "$AFPATTERN" | tr -s " " | cut -f 4 -d " "`"
    fi
    [ "`mount | grep "$AFPATTERN" | grep ' (rw'`" = "" ] && FLAG_RO='yes' #v431 make sure writable.
    umount /dev/$ATAG
   else #v431
    continue
   fi
  else
   [ "`mount | grep "$AFPATTERN" | grep ' (rw'`" = "" ] && FLAG_RO='yes' #v431 make sure writable.
  fi
  [ "$FLAG_RO" = "yes" ] && continue #v431
  [ "$AFS" ] && echo "${ATAG} \"${m_09}: $AFS  ${m_10}: ${ASIZE}M  ${m_11}: ${AFREE}M ${MNTSTATUS}\" \\" >> /tmp/schoices.txt #v3.01 added precaution. #130302 L18L
 done
 SCHOICES="`cat /tmp/schoices.txt`"

#add tmpfs ramdisk choice...
SIZETMPFSM="`df -m | grep '^tmpfs' | grep '/initrd/pup_rw' | tr -s " " | cut -f 4 -d " "`"
TMPFSMSG=''

if [ "$SIZETMPFSM" != "" ];then
 TOTALTMPFSM="`df -m | grep '^tmpfs' | grep '/initrd/pup_rw' | tr -s " " | cut -f 2 -d " "`"
 if [ "$SCHOICES" = "" ];then #v3.01
  SCHOICES="ramdisk \"$m_09: tmpfs  $m_10: ${TOTALTMPFSM}M  $m_11: ${SIZETMPFSM}M ($m_07)\" \\"
 else
  SCHOICES="$SCHOICES
ramdisk \"$m_09: tmpfs  $m_10: ${TOTALTMPFSM}M  $m_11: ${SIZETMPFSM}M ($m_07)\" \\"
 fi
 m_12="$(gettext 'Note 2: you can use the tmpfs ramdisk, which is in RAM, if it has enough space.')"
 TMPFSMSG="$m_12
 "
fi

[ "$SCHOICES" = "" ] && exit #precaution.
m_13="$(gettext 'A working area is required in which to create the new live-CD iso file.')
$(eval_gettext 'Here are the available partitions. You must choose one that has at least ${SIZENEEDEDM}M free space on it.') $(gettext 'Also, if you choose to create an ISO file rather than burn direct to CD/DVD, then you will need an extra 100 - 300MB space (whatever the size of ISO file is going to be).')

$(eval_gettext 'If the partition that you would like to use has less than ${SIZENEEDEDM}M free space on it, you will need to quit this script and delete some files.')

$(gettext 'Note 1: you can use a usb drive, but it needs to have been plugged in before running this script, so that it will get detected.')
${TMPFSMSG}
$(gettext 'Highlight desired choice then click OK button...')"
 echo '#!/bin/sh' > /tmp/savedlg
 echo -n "Xdialog --wrap --left --stdout --title \"$m_01\" --menubox \"$m_13\" 0 80 5 " >> /tmp/savedlg
 echo "$SCHOICES"  >> /tmp/savedlg
 echo ' > /tmp/tag.txt' >> /tmp/savedlg
 chmod 755 /tmp/savedlg

kill $XPID
/tmp/savedlg
[ $? -ne 0 ] && exit #130223
WKGPART="`cat /tmp/tag.txt`"
echo "WKGPART=$WKGPART"
[ "$WKGPART" = "" ] && exit

#now get the files off it...
#firstly need working place to write to...
[ "$WKGPART" = "ramdisk" ] && WKGMNTPT="/"
WPATTERN="/dev/$WKGPART "
[  "$WKGMNTPT" = "" ] && WKGMNTPT="`mount | grep "$WPATTERN" | head -n 1 |tr -s " " | cut -f 3 -d " "`" 
if [ "$WKGMNTPT" = "" ];then
 PPATTERN="^$WKGPART "
 WKGFS="`echo "$SCHOICES" | grep "$PPATTERN" | cut -f 2 -d ':' | cut -f 2 -d " "`"
 [ "$WKGFS" = "msdos" ] && WKGFS="vfat"
 mkdir -p /mnt/$WKGPART
 mount -t $WKGFS /dev/$WKGPART /mnt/$WKGPART
 [ $? -ne 0 ] && exit #precaution.
 WKGMNTPT="/mnt/$WKGPART"
fi
[ "$WKGMNTPT" = "/" ] && WKGMNTPT=""

# reuse if sfs file already exists
NEW="yes"
SFSPATH=$(ls -1 $WKGMNTPT/puppylivecdbuild/$PUPPYSFS 2>/dev/null)
if [ "$SFSPATH" != "" -a $(echo "$SFSPATH" | wc -l) -eq 1 ]; then
 m_35="$(eval_gettext '${SFSPATH} found.')
$(gettext 'Do you want to reuse it without any change?')
$(gettext "If uncertain, click the 'No' button")"
 Xdialog --wrap --left --title "$m_01" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$m_35" 0 0 && NEW="no"
fi
SFSPATH=""

if [ "$NEW" = "yes" ]; then  ###### long skip if, cleating new sfs

#130223 i think should wipe contents of $WKGMNTPT/puppylivecdbuild?
if [ -d $WKGMNTPT/puppylivecdbuild ];then
 [ -f $WKGMNTPT/puppylivecdbuild/$PUPPYSFS ] && rm -f $WKGMNTPT/puppylivecdbuild/$PUPPYSFS
 [ -f $WKGMNTPT/puppylivecdbuild/$ZDRVSFS ] && rm -f $WKGMNTPT/puppylivecdbuild/$ZDRVSFS
 m_41="$(eval_gettext 'Directory ${WKGMNTPT}/puppylivecdbuild already exists.')

$(gettext "Normally, this would be created fresh, empty. However, it exists from a previous execution of 'remasterpup2' script (this program), with files in it. You may choose to leave these files in-place, for the current remaster. If uncertain, click the 'Erase' button.")

$(gettext 'Note, if you choose to retain the files: The next operation is this script will will read files from a CD, however they will not over-write existing files. Therefore, any customised files will be retained.')

$(gettext "WARNING: If the files in 'puppylivecdbuild' are for a different version of Puppy, click the 'Erase' button.")
$(gettext "Again, warning, if at all uncertain, click the 'Erase' button.")

$(gettext "Click 'Keep' button to retain the files...")
$(gettext "Click 'Erase' button to delete the files...")"
 lbl_41="$(gettext 'Keep')"
 lbl_42="$(gettext 'Erase')"
 Xdialog --wrap --left --title "$m_01" --ok-label "$lbl_41" --cancel-label "$lbl_42" --yesno "$m_41" 0 80
 [ $? -ne 0 ] && rm -rf $WKGMNTPT/puppylivecdbuild
fi

#130222
#130307 notice, line 356 "WARNING:....", the $ is not backslashed, which is confusing...
m_42=""
if [ -d $WKGMNTPT/puppylivecdbuild ];then
 m_42="

$(gettext 'NOTICE:')
$(eval_gettext 'Directory ${WKGMNTPT}/puppylivecdbuild already exists.')
$(gettext 'If you want to re-use the files in it, and not re-read files from the CD (or virtual-CD), just click the window close-box to bypass this operation.')
$(gettext '(You may view the directory contents with a file-manager to satisfy yourself whether these files should be re-used)')"
fi
m_40="$(gettext 'This remaster program needs to read some files off the current live-CD.')

$(gettext "However, if a CD-image file (which has filename extension .iso) is mounted, the files may be read from that. We refer to this as a 'virtual CD'. To mount a virtual CD, just click on a .iso file.")

$(eval_gettext "WARNING: Be sure that the live-CD or .iso file is the correct one for the currently running Puppy, which is '\${DISTRO_NAME}', version \${DISTRO_VERSION}. As a check, it must have in it the file '\${DISTRO_PUPPYSFS}'")

$(gettext "If you want to read the files off a .iso file rather than a CD/DVD, please click on the .iso file right now (before clicking the 'OK' button below)")
$(gettext "Alternatively, if you want to read the files off a Puppy live-CD, insert it in drive, then click 'OK' button.")${m_42}"
Xdialog --wrap --left  --title "$m_01" --msgbox "$m_40" 0 80

if [ $? -eq 0 ];then #130223 big if "BIGIF2"

#130222 fix this...
VIRTUALCD=""
#130301 crap, earlier was using losetup-FULL from util-linux git, now using older 2.21.1, it needs -a param,
#but also truncates long lines (same as bb losetup) -- what a nuisance...
for ONELOOP in `cat /proc/mounts | grep -E '(^/dev/loop.*)( udf | iso9660 )' | cut -f 1 -d ' ' | tr '\n' ' '` #130308 /dev/loop only
do
 oPTN="^${ONELOOP} "
 ONEISO="$(cat /proc/mounts | grep "$oPTN" | cut -f 2 -d ' ' | rev | cut -f 1 -d '+' | cut -f 1 -d '/' | rev)" #130301
 [ "$ONEISO" = "" ] && continue #130301 precaution.
 VIRTUALCD="${VIRTUALCD} ${ONELOOP} ${ONEISO}"
done
m_14="$(gettext 'This remaster program needs to read some files off the current live-CD.')
$(gettext 'You can also use a .iso file if it is already mounted as a virtual CD (you would need to have previously clicked on a .iso file to mount it).')"
MSG="$m_14"
choice_cdd filter #130308 Use filtering.

CDPATTERN="/dev/$BURNERDRV "
CDMNTPT="`mount | grep "$CDPATTERN" | tr -s " " | cut -f 3 -d " "`"
if [ "$CDMNTPT" != "" ];then
 if [ ! -f $CDMNTPT/initrd.gz ];then #130308 In theory, this will now never be true.
  #091212 weird bug, no processes but when run this, x restarts...
  xFUSER="`fuser -m /dev/$BURNERDRV 2>/dev/null`" #do this first, seems to fix it.
  [ "$xFUSER" != "" ] && fuser -k -m /dev/$BURNERDRV
  sync
  umount /dev/$BURNERDRV 2> /dev/null
  if [ $? -ne 0 ];then
   m_15="$(gettext 'Mounted CD, get rid of it before running this program!')"
   Xdialog --wrap --left  --title "$m_02" --msgbox "$m_15" 0 0
   exit
  fi
  CDMNTPT=""
 fi
fi

if [ "$CDMNTPT" = "" ];then
 m_16="$(eval_gettext 'Please insert the current Puppy live-CD into drive ${BURNERDRV}.')
$(gettext 'Then click OK...')"
 while [ 1 ];do
 Xdialog --wrap --left  --title "$m_01" --msgbox "$m_16" 0 0
 [ $? -eq 0  ] || exit	# 22jun09
 #now mount it...
 CDMNTPT="/mnt/$BURNERDRV"
 mkdir -p /mnt/$BURNERDRV
 mount -t iso9660 /dev/$BURNERDRV /mnt/$BURNERDRV
 [ $? -eq 0 ] && break
 done
fi

#now get the files off it...
m_17="$(eval_gettext 'Copying files from CD to ${WKGMNTPT}/puppylivecdbuild/, please wait...')"
Xdialog --wrap --left  --title "$m_01" --no-buttons --ignore-eof   --infobox "$m_17" 0 0 0 &
XPID=$!
mkdir -p $WKGMNTPT/puppylivecdbuild
#now copy the files... # v431JP HTM, ICO, INF
   sync
   for F in vmlinuz initrd.gz ${ZDRVSFS} grldr menu.lst '*.xpm' isolinux.bin isolinux.cfg '*.msg' '*.16' '*.HTM*' '*.ICO' '*.INF'
   do
    #130223 -n means do not override an existing file... 130304 hmmm, -n not supported, use -u ...
    cp -a -u $CDMNTPT/$F $WKGMNTPT/puppylivecdbuild/ 2> /dev/null
    sync
   done
umount /dev/$BURNERDRV 2> /dev/null
rox -D $CDMNTPT #130223
kill $XPID

fi #end "BIGIF2"

m_16_1="$(eval_gettext 'Do you like to make ${ZDRVSFS}, the separate driver file?')"
Xdialog --wrap --left  --title "$m_01" --default-no --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$m_16_1" 0 0
[ $? -eq 0 ] && MKZDRV="yes" || MKZDRV=""
[ $? -eq 255 ] && exit #130223

m_18="$(eval_gettext 'Creating the ${PUPPYSFS} file in ${WKGMNTPT}/puppylivecdbuild/.')
$(gettext 'This can take quite a long time, so please wait.... and wait...')
$(eval_gettext 'This window will disappear when ${PUPPYSFS} is finally created.')
$(gettext 'Please wait...')"
Xdialog --wrap --left  --title "$m_01" --no-buttons --ignore-eof   --infobox "$m_18" 0 0 0 &
XPID=$!

#create new puppy.sfs file...
squash() {
    echo $0 $@
    rxvt -bg orange -fg black -title "$m_01" -geometry 80x6 -e mksquashfs $@ 2> /dev/null
}
DIRHOME=""
[ -d /home ] && DIRHOME="/home"
[ -d /sys ] && DIRSYS="/sys"
[ -d /lost+found ] && DIRLOST="/lost+found"
rm -f ${WKGMNTPT}/puppylivecdbuild/$PUPPYSFS 2> /dev/null
sync
#note, /puppy.sfs is not normally there, i relocated it to a separate tmpfs,
#however have not yet done that for multisession-cd/dvd (PUPMODE=77).
#note, /home could be in underdog linux...

# modules copied from initrd
ANOTHER_REMOVE="/lib/modules/$KERNELVER/initrd /lib/modules/*/modules.*"	# 28dec09 modules.*
if [ "$MKZDRV" = "yes" ]; then
  rm -f $WKGMNTPT/puppylivecdbuild/$ZDRVSFS 2> /dev/null
  mksquashfs /lib $WKGMNTPT/puppylivecdbuild/$ZDRVSFS -keep-as-directory -e /lib/[^m]* $ANOTHER_REMOVE
  ANOTHER_REMOVE="/lib/modules"
fi
#120605 Omit certain /dev subdir content and modem components loaded from firmware tarballs...
[ -d /dev/snd ] && [ "`ls /dev/snd`" != "" ] && DIRDEVSNDFILES="/dev/snd/*" #120721
[ -d /dev/.udev ] && DIRDEVUDEV="/dev/.udev"
#121021 modem daemons now left in place.
[ -f /usr/share/icons/hicolor/icon-theme.cache ] && ICONCACHE=/usr/share/icons/hicolor/icon-theme.cache #120721
TOPPLCDB=''
[ -e /puppylivecdbuild ] && TOPPLCDB='/puppylivecdbuild'
TOPPUPSFS=''
[ -e /${PUPPYSFS} ] && TOPPUPSFS="/${PUPPYSFS}"
# display terminal only for the first stage because it takes the most of time. 'squash' is a function, see above. 120512 $COPTIONS added...
squash / $WKGMNTPT/puppylivecdbuild/$PUPPYSFS ${COPTIONS} -e /etc /proc /initrd /var /tmp /archive /mnt /root $TOPPLCDB $ANOTHER_REMOVE $DIRHOME $DIRSYS $DIRLOST $TOPPUPSFS $DIRDEVSNDFILES $DIRDEVUDEV $ICONCACHE  #120605 end #120721 avoid wildecards option, icon-theme.cache 121021
sync

#add pristine folders (out of current puppy.sfs)...
mksquashfs /initrd${PUP_LAYER}/proc $WKGMNTPT/puppylivecdbuild/$PUPPYSFS -keep-as-directory
sync
mksquashfs /initrd${PUP_LAYER}/tmp $WKGMNTPT/puppylivecdbuild/$PUPPYSFS -keep-as-directory
sync
mksquashfs /initrd${PUP_LAYER}/mnt $WKGMNTPT/puppylivecdbuild/$PUPPYSFS -keep-as-directory
sync

kill $XPID

#######START WORKING ON /root#######
rm -rf /tmp/root 2> /dev/null
#do some work on /root before add it to the squashfs...
cp -a /initrd${PUP_LAYER}/root /tmp/root #pristine /root
#copy all of my-applications/
cp -af /root/my-applications/* /tmp/root/my-applications/
#some dotpups get installed here...
cp -af /root/my-roxapps /tmp/root/ 2>/dev/null
#window manager config files...
cp -af /root/.jwmrc /tmp/root/
cp -af /root/.jwmrc-tray /tmp/root/ #v411
cp -af /root/.fvwm95rc /tmp/root/
cp -af /root/.icewm/menu /tmp/root/.icewm/
#rox desktop settings...
cp -af /root/Choices/ROX-Filer/PuppyPin /tmp/root/Choices/ROX-Filer/
cp -af /root/Choices/ROX-Filer/globicons /tmp/root/Choices/ROX-Filer/
#100117 user changes get saved here, which overrides original...
if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
 mkdir -p /tmp/root/.config/rox.sourceforge.net/ROX-Filer/
 cp -af /root/.config/rox.sourceforge.net/ROX-Filer/globicons /tmp/root/.config/rox.sourceforge.net/ROX-Filer/
fi
#100117 i think also save user settings...
if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/Options ];then
 mkdir -p /tmp/root/.config/rox.sourceforge.net/ROX-Filer/
 cp -af /root/.config/rox.sourceforge.net/ROX-Filer/Options /tmp/root/.config/rox.sourceforge.net/ROX-Filer/
fi
#v411 tidy up, remove desktop drive icons...
grep -v '/root/.pup_event/drive_' /tmp/root/Choices/ROX-Filer/PuppyPin > /tmp/remaster-PuppyPin
mv -f /tmp/remaster-PuppyPin /tmp/root/Choices/ROX-Filer/PuppyPin
#this may have been modified for a different w.m...
cp -af /root/.xinitrc /tmp/root/
#also this...
[ -f /root/.xset.sh ] && cp -af /root/.xset.sh /tmp/root/

#v411 gtk theme may have been changed...
cp -af /root/.gtkrc-2.0 /tmp/root/

#v411 jwm theme may have changed...
cp -af /root/.jwm/jwm_colors /tmp/root/.jwm/
cp -af /root/.jwm/jwmrc-personal /tmp/root/.jwm/
cp -af /root/.jwm/jwmrc-personal2 /tmp/root/.jwm/
cp -af /root/.jwm/jwmrc-theme /tmp/root/.jwm/

#v411 pmount preferences...
cp -af /root/.pmountauto /tmp/root/
cp -af /root/.pmountengine /tmp/root/
cp -af /root/.pmountquit /tmp/root/
cp -af /root/.pmountsingle /tmp/root/

#TODO think need to do this for /var also...

#.packages/ .files, copy any files installed to /root...
echo -n "" > /tmp/allpkgs.files
for ONEPKG in `ls -1 /root/.packages/*.files 2>/dev/null | tr "\n" " "`
do
 for ONEFILE in `cat $ONEPKG | grep '^/root/' | tr "\n" " "`
 do
  if [ -d $ONEFILE ];then #w018
   mkdir -p /tmp$ONEFILE
  fi
  if [ -f $ONEFILE ];then #w018
   DIRNAME="`dirname $ONEFILE`"
   mkdir -p /tmp$DIRNAME
   cp -a -f $ONEFILE /tmp$DIRNAME/
  fi
 done
done

#w018...
#modify /root/.packages/ files, as all installed packages are now part of the new cd...
cp -a -f /root/.packages /tmp/root/
touch /root/.packages/user-installed-packages
cat /root/.packages/user-installed-packages >> /tmp/root/.packages/woof-installed-packages
sort -u --key=1 --field-separator="|" /tmp/root/.packages/woof-installed-packages > /tmp/woof-installed-packages-tmp #110722
mv -f /tmp/woof-installed-packages-tmp /tmp/root/.packages/woof-installed-packages
echo -n "" > /tmp/root/.packages/user-installed-packages #v431
sync #120607
rm -f /tmp/root/.packages/*.files #120607
rm -f /tmp/root/.packages/*.remove #120607

[ -f /tmp/root/.XLOADED ] && rm -f /tmp/root/.XLOADED #130527

m_19="$(eval_gettext 'This program has created folder /tmp/root, which has everything that is now going to be added as /root in the ${PUPPYSFS} file.')
$(gettext "This is mostly 'pristine', as obviously you do not want all your cache files, temp files, email files, and other working/temporary files to be burnt onto the CD. However, if you are familiar with the workings of Puppy, you might like to take a look at /tmp/root right now, and possibly add anything that you want from /root (or remove something!)")
$(gettext '(if you think that this program has missed out something important that  should be burnt into /root on the CD, please let me know -- Barry Kauler)')

$(eval_gettext "After examining /tmp/root, click 'Ok' to add /root in \${PUPPYSFS} file...")"
Xdialog --wrap --left --title "$m_01" --msgbox "$m_19" 0 80
sync
[ "`ls /tmp/root/.packages/*.files`" = "" ] && CLEARPKGLOG="yes" || CLEARPKGLOG="no" #120607 in case user copied entire /root to /tmp for boot disk.
mksquashfs /tmp/root $WKGMNTPT/puppylivecdbuild/$PUPPYSFS -keep-as-directory
sync
rm -rf /tmp/root
#######END WORKING ON /root#######

#######START WORKING ON /etc#######
rm -rf /tmp/etc 2> /dev/null
#do some work on /etc before add it to the .sfs...
cp -a /initrd${PUP_LAYER}/etc /tmp/etc #pristine /etc.
#maybe this has been modified...
cp -af /etc/ld.so.conf /tmp/etc/

#.packages/ .files, copy any files installed to /etc...
echo -n "" > /tmp/allpkgs.files
for ONEPKG in `ls -1 /root/.packages/*.files 2>/dev/null | tr "\n" " "`
do
 for ONEFILE in `cat $ONEPKG | grep '^/etc/' | tr "\n" " "`
 do
  if [ -d $ONEFILE ];then #w018
   mkdir -p /tmp$ONEFILE
  fi
  if [ -f $ONEFILE ];then #w018
   DIRNAME="`dirname $ONEFILE`"
   mkdir -p /tmp$DIRNAME
   cp -a -f $ONEFILE /tmp$DIRNAME/
  fi
 done
done
sync

cp -af /etc/eventmanager /tmp/etc/ #v411

#what about settings for a particular pc?...
m_20="$(gettext 'Hardware customisation was not chosen.')"
MSG1="$m_20"
m_21="$(eval_gettext 'Have created the /etc directory for the new ${PUPPYSFS} file.')
$(gettext "It is in a 'pristine' state, but you can choose to customise it.")
$(gettext "Do you want to add customisations for your hardware? Examples are mouse, keyboard, video. But, if you answer 'Yes' here, the live-CD will be preconfigured for your PC only. Therefore, it is best to answer 'No' if you want to boot the new live-CD on different PCs.")

$(gettext "Click 'Yes' button to customise /etc...")
$(gettext "Click 'No' button not to customise (recommended)...")"
Xdialog --wrap --left --title "$m_01" --default-no --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$m_21" 0 80
if [ $? -eq 0 ];then
 cp -af /etc/codepage /tmp/etc/
 cp -af /etc/fontmap /tmp/etc/
 cp -af /etc/keyboardtype /tmp/etc/
 cp -af /etc/keymap /tmp/etc/
 cp -af /etc/mousebuttons /tmp/etc/
 cp -af /etc/mousedevice /tmp/etc/
 cp -af /etc/videomode /tmp/etc/
 cp -af /etc/eth0mode /tmp/etc/
 cp -af /etc/eth1mode /tmp/etc/
 cp -af /etc/resolv.conf /tmp/etc/
 rm -f /tmp/etc/localtime #a symlink
 cp -af /etc/localtime /tmp/etc/
 cp -af /etc/TZ /tmp/etc/
 cp -af /etc/cdburnerdevice /tmp/etc/
 cp -af /etc/dvddevice /tmp/etc/
 cp -af /etc/modemdevice /tmp/etc/
 cp -af /etc/securetelnetrc /tmp/etc/
 cp -af /etc/modules.conf /tmp/etc/
 cp -af /etc/modprobe.d /tmp/etc/ #101020
 cp -af /etc/rdesktoprc /tmp/etc/
 cp -af /etc/windowmanager /tmp/etc/
 cp -af /etc/xextraoptions /tmp/etc/
 cp -af /etc/X11/xorg.conf /tmp/etc/X11
 cp -af /etc/network-wizard /tmp/etc/
 cp -af /etc/simple_network_setup/* /tmp/etc/simple_network_setup/
m_22="$(gettext 'These files were customised in /etc:')
codepage fontmap keyboardtype keymap mousebuttons mousedevice videomode eth0mode eth1mode resolv.conf localtime TZ cdburnerdevice dvddevice modemdevice securetelnetrc modprobe.d/* rdesktoprc windowmanager xextraoptions X11/xorg.conf
$(gettext 'Also these directories:')
network-wizard."
 MSG1="$m_22"
fi

#some files to always copy...
cp -af /etc/Puppybackgroundpicture /tmp/etc/
#TODO: other gtk and jwm customisations.

#100913 fix id-string variable...
idPATTERN="s%^DISTRO_IDSTRING.*%DISTRO_IDSTRING='${IDSTRING}'%"
sed -i -e "$idPATTERN" /tmp/etc/DISTRO_SPECS
cp -f /tmp/etc/DISTRO_SPECS /tmp/DISTRO_SPECSupdated #keep this to put into initrd.gz.

m_23="${MSG1}

$(gettext 'If you know what you are doing, you can now modify any files in /tmp/etc folder. This is just about to be added to /etc in the .sfs file.')
$(gettext "Do anything you want before clicking 'Ok'.")
$(gettext '(If this program has missed something important, let me know -- Barry Kauler)')

$(eval_gettext "Click 'Ok' to add /etc in \${PUPPYSFS} file...")"
Xdialog --wrap --left  --title "$m_01" --msgbox "$m_23" 0 80

#120606 in case user just now replaced the /etc directory...
sync
#130527 .XLOADED moved to /root (see /usr/bin/xwin), change test...
MODIFETC="$(find /tmp/etc/modules -mindepth 1 -maxdepth 1 -name 'firmware.dep.inst.*')"
if [ "$MODIFETC" != "" ];then
 #rm -f /tmp/etc/.XLOADED
 rm -f /tmp/etc/modules/firmware.dep.inst.*
 touch /tmp/etc/personal_settings_popup_disabled
 touch /tmp/etc/personal_data_save_disabled
fi
[ -f /tmp/etc/.XLOADED ] && rm -f /tmp/etc/.XLOADED #130527 just in case old file still there.

sync


mksquashfs /tmp/etc $WKGMNTPT/puppylivecdbuild/$PUPPYSFS -keep-as-directory
sync
rm -rf /tmp/etc
#######END WORKING ON /etc#######

#######START WORKING ON /var#######
rm -rf /tmp/var 2> /dev/null
cp -a /initrd${PUP_LAYER}/var /tmp/var #pristine var

#.packages/ .files, copy any files installed to /var...
echo -n "" > /tmp/allpkgs.files
for ONEPKG in `ls -1 /root/.packages/*.files 2>/dev/null | tr "\n" " "`
do
 for ONEFILE in `cat $ONEPKG | grep '^/var/' | tr "\n" " "`
 do
  if [ -d $ONEFILE ];then #w018
   mkdir -p /tmp$ONEFILE
  fi
  if [ -f $ONEFILE ];then #w018
   DIRNAME="`dirname $ONEFILE`"
   mkdir -p /tmp$DIRNAME
   cp -a -f $ONEFILE /tmp$DIRNAME/
  fi
 done
done
sync
[ "$CLEARPKGLOG" = "yes" ] && rm -f /tmp/var/log/packages/* #120607

sync
mksquashfs /tmp/var $WKGMNTPT/puppylivecdbuild/$PUPPYSFS -keep-as-directory
sync
rm -rf /tmp/var
#######END WORKING ON /var#######
chmod a+r $WKGMNTPT/puppylivecdbuild/* &>/dev/null
chmod a-x $WKGMNTPT/puppylivecdbuild/*.sfs &>/dev/null
fi ###### end of long skip if, cleating new sfs

if [ -f $WKGMNTPT/puppylivecdbuild/isolinux.cfg ]; then
 #edit isolinux.cfg...
 ISOLINUXCFG="`cat $WKGMNTPT/puppylivecdbuild/isolinux.cfg`"
 #small tweaks, like acpi=on, ide=nodma...
 m_24="$(gettext 'Almost ready to burn the CD!')
$(eval_gettext 'All the files that will be burnt to CD are in ${WKGMNTPT}/puppylivecdbuild/.')

$(gettext 'The thing that you may want to do is edit isolinux.cfg. It has this:')
${ISOLINUXCFG}

$(gettext "'pmedia' parameter is a hint to Puppy what media you are booting Puppy from.")
$(gettext "'pmedia=cd' means any CD/DVD drive. Other options are:")
 usbflash usbhd usbcd ataflash atahd atacd atazip scsihd scsicd
 $(gettext "(where 'ata' means either IDE or SATA interface, i.e. an internal drive)")

$(gettext "'pkeys' is the default keyboard layout. Example: 'pkeys=us'  Choices are:")
 azerty be-latin1 br-abnt2 br-abnt br-latin1-abnt2 br-latin1-us by cf croat cz de de-latin1 dk dvorak dvorak-l dvorak-r es et fi fr gr hu101 hu il it jp106 lt mk nl no pl pt-latin1 ro ru se sg sk-qwerty sk-qwertz slovene sv-latin1 uk us wangbe

$(gettext "You may also add any kernel parameters. Some PCs require 'acpi=off' to boot properly.")

$(gettext "(If answer 'Yes', an editor will appear, must exit editor before this program will continue)")
$(gettext "Click 'Yes' button to edit isolinux.cfg...")"
 Xdialog --wrap --left  --title "$m_01" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$m_24" 0 80
 if [ $? -eq 0 ];then
  defaulttexteditor $WKGMNTPT/puppylivecdbuild/isolinux.cfg
 fi
 sync
fi

m_25="$(gettext 'Almost ready to burn the CD!')
$(eval_gettext 'All the files that will be burnt to CD are in ${WKGMNTPT}/puppylivecdbuild/.')

$(gettext "If you want to add any more files, say extra SFS files, or to edit or modify the files in any way, do it now. Note, if you add an extra SFS file, say 'devx.sfs' then it will be available for use when you boot the new live-CD.")

$(eval_gettext "If you want to make any changes, use ROX-Filer to open \${WKGMNTPT}/puppylivecdbuild/ and do so now, before clicking the 'OK' button.")"
Xdialog --wrap --left  --title "$m_01" --msgbox "$m_25" 0 80
sync

BOOTPARM=""
[ -f $WKGMNTPT/puppylivecdbuild/grldr ] && BOOTPARM="-b grldr"
[ -f $WKGMNTPT/puppylivecdbuild/isolinux.bin ] && BOOTPARM="-b isolinux.bin -c boot.cat"
if [ "$BOOTPARM" = "" ]; then
 m_25_1="$(gettext 'No boot loaders found. You need isolinux.bin or grldr to make bootable CD.')
$(gettext "Click 'OK' to quit.")
$(eval_gettext '(leaving ${WKGMNTPT}/puppylivecdbuild/ files in existence)')"
 Xdialog --wrap --left  --title "$m_01" --msgbox "$m_25_1" 0 0
 exit
fi

if [ "$NEW" = "yes" ]; then	#120628: no change id-string for reuse
#100913 append id-string. doesn't matter if already have an id-string appended...
echo -n "$IDSTRING" >> $WKGMNTPT/puppylivecdbuild/vmlinuz
[ -f $WKGMNTPT/puppylivecdbuild/$PUPPYSFS ] && echo -n "$IDSTRING" >> $WKGMNTPT/puppylivecdbuild/$PUPPYSFS
[ -f $WKGMNTPT/puppylivecdbuild/$ZDRVSFS ] && echo -n "$IDSTRING" >> $WKGMNTPT/puppylivecdbuild/$ZDRVSFS
sync

#100913 need to update file DISTRO_SPECS in initrd.gz, so init script can find puppy files...
mv -f $WKGMNTPT/puppylivecdbuild/initrd.gz /tmp #note $WKGMNTPT may be non-linux fs.
cd /tmp
gunzip initrd.gz
mkdir initrd-tree-tmp1
cd initrd-tree-tmp1
cat ../initrd | cpio -i -d -m
sync
rm -f ../initrd
cp -a -f /tmp/DISTRO_SPECSupdated ./DISTRO_SPECS #see earlier.
find . | cpio -o -H newc | gzip -9 > $WKGMNTPT/puppylivecdbuild/initrd.gz
sync
cd ..
rm -rf initrd-tree-tmp1
cd /root
fi #120628 end

#build new iso file, or direct burn to cd...
#to save space, burn direct from $WKGMNTPT/puppylivecdbuild/ to cd...
m_26="$(eval_gettext 'All the files that will be burnt to CD/DVD are in ${WKGMNTPT}/puppylivecdbuild/.')

$(gettext 'As the working partition may have limited space (for example, you are using a USB pen drive), these files are burnt direct to CD/DVD, rather than create an intermediary iso file. If you do need an iso file, you can copy it from the CD afterward:')
$(eval_gettext '(dd if=/dev/${BURNERDRV} of=puppy.iso seek=0 bs=32k)')

$(gettext "However, if you just want to create an iso file now and not burn to CD/DVD, click the 'No' button.")

$(eval_gettext "Click 'No' button (or close-box on window) to quit program at this point (leaving \${WKGMNTPT}/puppylivecdbuild/ files in existence), and further option to create an iso file...")
$(gettext "Click 'Yes' button to burn to CD/DVD...")"
Xdialog --wrap --left --title "$m_01" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$m_26" 0 80

if [ ! $? -eq 0 ];then
 MSG="$(gettext 'ISO file not created.')"
 m_27="$(eval_gettext "Click 'Yes' button to create a 'custom-puppy.iso' in \${WKGMNTPT}/ (assuming that there is 100MB-300MB free space)...")
$(eval_gettext "Click 'No' button (or close-box on window) to quit program at this point (leaving  \${WKGMNTPT}/puppylivecdbuild/ files in existence)...")"
 Xdialog --wrap --left  --title "$m_01" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$m_27" 0 80
 if [ $? -eq 0 ];then
  VOLI=""
  m_27_1="$(gettext 'You can add the CD volume ID, it can be used on Windows and on some Linux system.')"
  VOLI=$(Xdialog --title "$m_01" --stdout --inputbox "$m_27_1" 0 60 "$SFSBASE")
  [ "$VOLI" != "" ] && VOLI="-V $(echo "$VOLI"|tr ' ' '_')"
  [ -f $WKGMNTPT/custom-puppy.iso ] && rm -f $WKGMNTPT/custom-puppy.iso #130223
  # Joliet option
  rxvt -bg orange -fg black -title "$m_01" -e mkisofs -J -D -R $VOLI -o $WKGMNTPT/custom-puppy.iso $BOOTPARM -no-emul-boot -boot-load-size 4 -boot-info-table $WKGMNTPT/puppylivecdbuild/
  MSG="$(eval_gettext '${WKGMNTPT}/custom-puppy.iso created.')"
 fi
 sync
 m_28="$(eval_gettext '${WKGMNTPT}/puppylivecdbuild/ files left in existence.')
$(gettext "Click 'Ok' button to quit...")"
 Xdialog --wrap --left  --title "$m_01" --msgbox "$MSG
 $m_28" 0 0
 exit
fi

VIRTUALCD=""
MSG="$(gettext 'You need to choose a burner drive so that the files created by this program can be burnt to a new CD/DVD.')"
if [ "$BURNERDRV" = "" -o  "$(echo "$BURNERDRV" | grep 'loop')" != "" ]; then
 choice_cdd
fi

if [ "`echo "$BURNERDRV" | grep 'hd'`" != "" ];then
 DEVDEV="ATAPI:/dev/$BURNERDRV"
else
 DEVDEV="/dev/$BURNERDRV"
fi

#now for the actual burn...
m_29="$(gettext 'Please choose the media to burn to.')
$(eval_gettext 'The burner drive is /dev/${BURNERDRV}, and obviously it must be able to burn a DVD if you want to burn a DVD!')

$(gettext "Note, you can later use the 'dd' program to create an iso file:")
 # dd if=/dev/${BURNERDRV} of=puppy.iso seek=0 bs=32k

$(gettext 'PLEASE INSERT THE NEW CD OR DVD RIGHT NOW, THEN CHOOSE FROM MENU...')"
m_30="$(gettext 'New blank recordable CD')"
m_31="$(gettext 'New blank DVD-R (not +R, -RW, +RW)')"
CDCHOICE="`Xdialog --wrap --left --stdout --title \"$m_01\" --menubox \"$m_29\" 0 0 4 CD-R \"$m_30\" DVD-R \"$m_31\"`"

echo '#!/bin/sh' > /tmp/new2cd.sh
if [ "`echo "$CDCHOICE" | grep 'DVD'`" = "" ];then
 #burn to CD...
 echo "mkisofs -D -R -quiet $BOOTPARM  -no-emul-boot -boot-load-size 4 -boot-info-table $WKGMNTPT/puppylivecdbuild/ | cdrecord -multi -tao -pad -data -v speed=4 gracetime=2 dev=$DEVDEV  -" >> /tmp/new2cd.sh
else
 echo "growisofs -speed=4 -Z /dev/$BURNERDRV -R -D -quiet $BOOTPARM -no-emul-boot -boot-load-size 4 -boot-info-table $WKGMNTPT/puppylivecdbuild/" >> /tmp/new2cd.sh
fi
m_32="$(gettext 'Finished. Press ENTER key to continue')"
echo "echo -n \"$m_32\": " >> /tmp/new2cd.sh
echo 'read nippi'  >> /tmp/new2cd.sh
chmod 755 /tmp/new2cd.sh
rxvt -bg orange -fg black -title "$m_01" -e /tmp/new2cd.sh

sync
rm -rf /puppylivecdbuild
rm -rf $WKGMNTPT/puppylivecdbuild
sync
umount /dev/$WKGPART

eject /dev/$BURNERDRV
m_33="$(gettext 'You should now have a new custom Puppy live-CD!')

$(gettext 'WARNING:')
$(gettext 'There is a tricky situation here, as all the installed packages (plus anything else that you may have installed, such as loaded .sfs extension files, have all been burnt to CD/DVD).')
$(eval_gettext 'But, they are still installed in the current ${DISTRO_FILE_PREFIX}save.2fs, the Puppy persistent storage file (where all your stuff gets saved, so it is there next time Puppy is booted).')
$(gettext "All those files are duplicated, but will that do any harm if you boot the new CD? ...should be okay, but I recommend that you uninstall as much as possible right now, before booting the new CD. Note, do not uninstall them after booting the new CD as that will also 'delete' the packages burnt onto the CD.")

$(eval_gettext "Or, easiest and cleanest option, boot Puppy with 'pfix=ram' boot parameter then at shutdown create a brand new '${DISTRO_FILE_PREFIX}save' file.")
$(gettext "In fact, this is the wisest thing to do regardless, as you don't even know if the custom CD is going to work properly -- so it is best to bootup in ram only to find out!")" #'geany
Xdialog --wrap --left --title "$m_01" --msgbox "$m_33" 0 80


####THE END######
