#!/bin/sh
#nicOS-Remaster-Classic  An application to remaster Puppy Linux with user input options (c) Copyright 2022, nic007.

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

KERNELVER="`uname -r`"

. /etc/rc.d/PUPSTATE
[ "$PUP_LAYER" = "" ] && PUP_LAYER="/pup_ro2"

. /etc/DISTRO_SPECS
PUPPYSFS="$DISTRO_PUPPYSFS" 
ZDRVSFS="$DISTRO_ZDRVSFS"   
SFSBASE="`basename $PUPPYSFS .sfs`"

m_10="$(gettext 'Size')"
m_11="$(gettext 'Free')" 

squash() {
	echo $0 $@
	rxvt -bg beige -fg black -title "Compressing" -geometry 80x6+360+360 -e mksquashfs $@ 2> /dev/null
}

ANSWER=$(Xdialog --stdout --left --wrap --icon /usr/local/lib/X11/pixmaps/nicOS.png --no-cancel --title "Remaster-Classic" --help "This remaster script is broadly based on the traditional remaster script that comes with
Puppy but with enhanced features and hopefully more user friendly.  Files are copied from
/ which makes it ideal to deal correctly with white-out files.  White-out files are files that
are 'deleted' from a read-only SFS-file like the base sfs.  White-out files will be excluded
from the new base sfs with this remaster process.

The contents of all mounted additional drives (except the fdrv and zdrv) and all loaded
extra sfs files will be included in the new base sfs by default.  You should boot without
the additional drives (other than the fdrv and zdrv) that you want to exclude.  

Select item 1 to choose which loaded extra sfs files to include/exclude.  Note that you
may get a warning message if you are not running a savefile/folder.  Ignore this and
click 'OK' until you have made your choices.  Only exit the sfs_load choices box after
you have made your selections.  All loaded extra sfs files will be included by default
if item 1 is not selected.

Select item 3 if you do not want to save your current hardware settings.  You should
choose this option if you are going to use your remastered Puppy on another/different
machine.

NB: DO NOT DESELECT ITEMS 2 AND 4.

Select item 5 to also make an ISO, otherwise only a new base sfs will be created.

The operation will take time, please be patient...." --checklist "Click the 'Help' button for instructions. 
 
Click 'OK' after making your selections." 0 0 6 "1" "Choose extra sfs files to include" "off" "2" "Recording changes to system" "on" "3" "Discard current hardware settings" "off" "4" "Building new base sfs" "on" "5" "Select to also make an ISO" "off") 
 case /$ANSWER/ in 
 */1/*) sfs_load ;;&
 */2/*) SAVEPART="$PDEV1" 
CDR="/dev/$SAVEPART"

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 
  MNTSTATUS="($m_07)" 
  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 " 
  AFREE="`df -m | grep "$AFPATTERN" | tr -s " " | cut -f 4 -d " "`"
  FLAG_RO='no' 
  if [ ! "$AFREE" ];then
   MNTSTATUS="($m_08)" 
   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' 
    umount /dev/$ATAG
   else
    continue
   fi
  else
   [ "`mount | grep "$AFPATTERN" | grep ' (rw'`" = "" ] && FLAG_RO='yes' 
  fi
  [ "$FLAG_RO" = "yes" ] && continue   
  [ "$AFS" ] && echo "${ATAG} \"${m_09}: $AFS  ${m_10}: ${ASIZE}M  ${m_11}: ${AFREE}M ${MNTSTATUS}\" \\" >> /tmp/schoices.txt 
 done
 SCHOICES="`cat /tmp/schoices.txt`"

SIZETMPFSM=`df -m | grep '^tmpfs' | grep -E "/initrd/pup_rw|/initrd/mnt/tmpfs" | tr -s " " | cut -f 4 -d " "`
TMPFSMSG=''

if [ "$SIZETMPFSM" != "" ];then
 TOTALTMPFSM=`df -m | grep '^tmpfs' | grep -E '/initrd/pup_rw|/initrd/mnt/tmpfs' | tr -s " " | cut -f 2 -d " "`
 if [ "$SCHOICES" = "" ];then 
  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 
m_13="$(gettext 'A working area is required to create the new sfs file.')

$(gettext 'Select choice from the list then click OK button.')"
 echo '#!/bin/sh' > /tmp/savedlg
 echo -n "Xdialog --wrap --left --stdout --title \"Choose working area\" --menubox \"$m_13\" 0 80 10 " >> /tmp/savedlg
 echo "$SCHOICES"  >> /tmp/savedlg
 echo ' > /tmp/tag.txt' >> /tmp/savedlg
 chmod 755 /tmp/savedlg

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

[ "$WKGPART" = "ramdisk" ] && WKGMNTPT="/"
WPATTERN="/dev/$WKGPART "
[ "$WKGMNTPT" = "" ] && WKGMNTPT="`mount | grep "$WPATTERN" | 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 
 WKGMNTPT="/mnt/$WKGPART"
fi
[ "$WKGMNTPT" = "/" ] && WKGMNTPT="/"
 
 DIRHOME=""
[ -d /sys ] && DIRSYS="/sys"
[ -d /lost+found ] && DIRLOST="/lost+found"
rm -f $WKGMNTPT/PuppyRemaster/$PUPPYSFS 2> /dev/null
sync

[ -d /dev/snd ] && [ "`ls /dev/snd`" != "" ] && DIRDEVSNDFILES="/dev/snd/*" 
[ -d /dev/.udev ] && DIRDEVUDEV="/dev/.udev"

[ -f /usr/share/icons/hicolor/icon-theme.cache ] && ICONCACHE=/usr/share/icons/hicolor/icon-theme.cache 
TOPPLCDB=''
[ -e /PuppyRemaster ] && TOPPLCDB='/PuppyRemaster'
TOPPUPSFS=''
[ -e /${PUPPYSFS} ] && TOPPUPSFS="/${PUPPYSFS}"

rm -r /lib/firmware
rm -r /lib/modules
sleep 1
cp -a /initrd${PUP_LAYER}/lib/firmware /initrd${PUP_LAYER}/lib/modules  /lib

rm -f $WKGMNTPT/PuppyRemaster
sleep 0.1
mkdir -p $WKGMNTPT/PuppyRemaster

COMP="-comp gzip -b 16384"

squash / $WKGMNTPT/PuppyRemaster/$PUPPYSFS $COMP -e /home /media /etc /proc /initrd /var /tmp /archive /mnt /root $TOPPLCDB $ANOTHER_REMOVE $DIRHOME $DIRSYS $DIRLOST $TOPPUPSFS $DIRDEVSNDFILES $DIRDEVUDEV $ICONCACHE  
sync

mksquashfs /initrd${PUP_LAYER}/proc $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync
mksquashfs /initrd${PUP_LAYER}/tmp $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync
mksquashfs /initrd${PUP_LAYER}/mnt $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync

kill $XPID

rm -rf $WKGMNTPT/tmp 2> /dev/null
sleep 1
mkdir $WKGMNTPT/tmp 
cp -af /root $WKGMNTPT/tmp/root
if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/globicons ];then
 mkdir -p $WKGMNTPT/tmp/root/.config/rox.sourceforge.net/ROX-Filer/
 cp -af /root/.config/rox.sourceforge.net/ROX-Filer/globicons $WKGMNTPT/tmp/root/.config/rox.sourceforge.net/ROX-Filer/
fi
if [ -f /root/.config/rox.sourceforge.net/ROX-Filer/Options ];then
 mkdir -p $WKGMNTPT/tmp/root/.config/rox.sourceforge.net/ROX-Filer/
 cp -af /root/.config/rox.sourceforge.net/ROX-Filer/Options $WKGMNTPT/tmp/root/.config/rox.sourceforge.net/ROX-Filer/
fi
grep -v '/root/.pup_event/drive_' $WKGMNTPT/tmp/root/Choices/ROX-Filer/PuppyPin > $WKGMNTPT/tmp/remaster-PuppyPin
mv -f $WKGMNTPT/tmp/remaster-PuppyPin $WKGMNTPT/tmp/root/Choices/ROX-Filer/PuppyPin
cp -af /root/.xinitrc $WKGMNTPT/tmp/root/
[ -f /root/.xset.sh ] && cp -af /root/.xset.sh $WKGMNTPT/tmp/root/

echo -n "" > $WKGMNTPT/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 
   mkdir -p $WKGMNTPT/tmp$ONEFILE
  fi
  if [ -f $ONEFILE ];then 
   DIRNAME="`dirname $ONEFILE`"
   mkdir -p $WKGMNTPT/tmp$DIRNAME
   cp -a -f $ONEFILE $WKGMNTPT/tmp$DIRNAME/
  fi
 done
done

cp -a -f /root/.packages $WKGMNTPT/tmp/root/
touch /root/.packages/user-installed-packages
cat /root/.packages/user-installed-packages >> $WKGMNTPT/tmp/root/.packages/woof-installed-packages
sort -u --key=1 --field-separator="|" $WKGMNTPT/tmp/root/.packages/woof-installed-packages > $WKGMNTPT/tmp/woof-installed-packages-tmp 
mv -f $WKGMNTPT/tmp/woof-installed-packages-tmp $WKGMNTPT/tmp/root/.packages/woof-installed-packages
echo -n "" > $WKGMNTPT/tmp/root/.packages/user-installed-packages 
sync 
rm -f $WKGMNTPT/tmp/root/.packages/*.files 
rm -f $WKGMNTPT/tmp/root/.packages/*.remove 

[ -f $WKGMNTPT/tmp/root/.XLOADED ] && rm -f $WKGMNTPT/tmp/root/.XLOADED 

sync
[ "`ls $WKGMNTPT/tmp/root/.packages/*.files`" = "" ] && CLEARPKGLOG="yes" || CLEARPKGLOG="no" 

rm -rf $WKGMNTPT/tmp/etc 2> /dev/null
cp -af /etc $WKGMNTPT/tmp/etc

echo -n "" > $WKGMNTPT/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 
   mkdir -p $WKGMNTPT/tmp$ONEFILE
  fi
  if [ -f $ONEFILE ];then 
   DIRNAME="`dirname $ONEFILE`"
   mkdir -p $WKGMNTPT/tmp$DIRNAME
   cp -a -f $ONEFILE $WKGMNTPT/tmp$DIRNAME/
  fi
 done
done
sync
cp -af /etc/Puppybackgroundpicture $WKGMNTPT/tmp/etc/

idPATTERN="s%^DISTRO_IDSTRING.*%DISTRO_IDSTRING='${IDSTRING}'%"
sed -i -e "$idPATTERN" $WKGMNTPT/tmp/etc/DISTRO_SPECS
cp -f $WKGMNTPT/tmp/etc/DISTRO_SPECS $WKGMNTPT/tmp/DISTRO_SPECSupdated 

sync
MODIFETC="$(find $WKGMNTPT/tmp/etc/modules -mindepth 1 -maxdepth 1 -name 'firmware.dep.inst.*')"
if [ "$MODIFETC" != "" ];then
 rm -f $WKGMNTPT/tmp/etc/modules/firmware.dep.inst.*
 touch $WKGMNTPT/tmp/etc/personal_settings_popup_disabled
 touch $WKGMNTPT/tmp/etc/personal_data_save_disabled
fi
[ -f $WKGMNTPT/tmp/etc/.XLOADED ] && rm -f $WKGMNTPT/tmp/etc/.XLOADED

rm -rf $WKGMNTPT/tmp/home 2> /dev/null
cp -af /initrd${PUP_LAYER}/home $WKGMNTPT/tmp/home
rm -rf $WKGMNTPT/tmp/media 2> /dev/null
cp -af /initrd${PUP_LAYER}/media $WKGMNTPT/tmp/media  

sync ;;&
 */3/*) 
 rm -r $WKGMNTPT/tmp/etc/codepage 
 rm -r $WKGMNTPT/tmp/etc/fontmap 
 rm -r $WKGMNTPT/tmp/etc/keyboardtype 
 rm -r $WKGMNTPT/tmp/etc/keymap 
 rm -r $WKGMNTPT/tmp/etc/mousebuttons 
 rm -r $WKGMNTPT/tmp/etc/mousedevice 
 rm -r $WKGMNTPT/tmp/etc/videomode 
 rm -r $WKGMNTPT/tmp/etc/eth0mode 
 rm -r $WKGMNTPT/tmp/etc/eth1mode 
 rm -r $WKGMNTPT/tmp/etc/resolv.conf
 cp -a /initrd/pup_ro2/etc/resolv.conf $WKGMNTPT/tmp/etc/  
 rm -f $WKGMNTPT/tmp/etc/localtime 
 cp -a /initrd/pup_ro2/etc/localtime $WKGMNTPT/tmp/etc/ 
 rm -r $WKGMNTPT/tmp/etc/TZ 
 rm -r $WKGMNTPT/tmp/etc/cdburnerdevice 
 rm -r $WKGMNTPT/tmp/etc/dvddevice 
 rm -r $WKGMNTPT/tmp/etc/modemdevice 
 rm -r $WKGMNTPT/tmp/etc/securetelnetrc 
 rm -r $WKGMNTPT/tmp/etc/modules.conf
 cp -a /initrd/pup_ro2/etc/modules.conf $WKGMNTPT/tmp/etc/ 
 rm -r $WKGMNTPT/tmp/etc/modprobe.d
 cp -a /initrd/pup_ro2/etc/modprobe.d $WKGMNTPT/tmp/etc/
 rm -r $WKGMNTPT/tmp/etc/rdesktoprc 
 rm -r $WKGMNTPT/tmp/etc/windowmanager 
 rm -r $WKGMNTPT/tmp/etc/xextraoptions 
 rm -r $WKGMNTPT/tmp/etc/X11/xorg.conf 
 rm -r $WKGMNTPT/tmp/etc/wvdial.conf 
 rm -r $WKGMNTPT/tmp/etc/network-wizard
 cp -a /initrd/pup_ro2/etc/network-wizard $WKGMNTPT/tmp/etc/ 
 rm -r $WKGMNTPT/tmp/etc/simple_network_setup/*
 cp -a /initrd/pup_ro2/etc/simple_network_setup/* $WKGMNTPT/tmp/etc/ ;;&
 */4/*) Xdialog -left -wrap -title "Final Check" -msgbox "Check and edit/change the contents of $WKGMNTPT/tmp/root, $WKGMNTPT/tmp/etc, $WKGMNTPT/tmp/home and $WKGMNTPT/tmp/media (if applicable) and if necessary.  BEST TO LEAVE THINGS AS IS if you are not sure what you are doing.  Click 'OK' when ready to proceed." 0 50
mksquashfs $WKGMNTPT/tmp/home $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync
rm -rf $WKGMNTPT/tmp/home
mksquashfs $WKGMNTPT/tmp/media $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync
rm -rf $WKGMNTPT/tmp/media
mksquashfs $WKGMNTPT/tmp/etc $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync
rm -rf $WKGMNTPT/tmp/etc
mksquashfs $WKGMNTPT/tmp/root $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync
rm -rf $WKGMNTPT/tmp/root
rm -rf $WKGMNTPT/tmp/var 2> /dev/null
cp -a /initrd${PUP_LAYER}/var $WKGMNTPT/tmp/var 

echo -n "" > $WKGMNTPT/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 
   mkdir -p $WKGMNTPT/tmp$ONEFILE
  fi
  if [ -f $ONEFILE ];then 
   DIRNAME="`dirname $ONEFILE`"
   mkdir -p $WKGMNTPT/tmp$DIRNAME
   cp -a -f $ONEFILE $WKGMNTPT/tmp$DIRNAME/
  fi
 done
done
sync
[ "$CLEARPKGLOG" = "yes" ] && rm -f $WKGMNTPT/tmp/var/log/packages/* 

sync
mksquashfs $WKGMNTPT/tmp/var $WKGMNTPT/PuppyRemaster/$PUPPYSFS -keep-as-directory
sync
rm -rf $WKGMNTPT/tmp/var
chmod a+r $WKGMNTPT/PuppyRemaster/* &>/dev/null
chmod a-x $WKGMNTPT/PuppyRemaster/*.sfs &>/dev/null
rox -d $WKGMNTPT/PuppyRemaster ;;& 
 */5/*) m_21="$(eval_gettext 'Creating an ISO')
$(gettext "To make an ISO: add initrd.gz, vmlinuz, the zdrv and any fdrv (unless you selected to include them in the base sfs) from your current running system to $WKGMNTPT/PuppyRemaster .  Also add the other files on the original CD/ISO (except for boot.catalog and those already copied from the running system).  Make sure that a bootloader, ie. isolinux.bin and/or grldr is included as well as the uefi image file (efi.img) if you want to make a bootable UEFI ISO.  Lastly, you can add any other files you want to include in the ISO, to $WKGMNTPT/PuppyRemaster now before continuing.")
$(gettext "Click Yes to make the ISO, No to abort.")" 
Xdialog --wrap --left --title "Build a bootable ISO" --default-no --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$m_21" 0 80 
if [ $? -eq 0 ];then
		BOOTLOADER=""
		[ -f $WKGMNTPT/PuppyRemaster/grldr ] && BOOTLOADER="grldr"
		[ -f $WKGMNTPT/PuppyRemaster/isolinux.bin ] && BOOTLOADER="isolinux.bin"
		if [ -f $WKGMNTPT/PuppyRemaster/efi.img ] ; then
			EFI_IMG='efi.img'
		fi
else
		Xdialog --title "Alert" --msgbox "Remaster aborted by user !" 0 0 && exit 1
fi

if [ "$EFI_IMG" ];then
		UEFI='-uefi'
		UEFI_OPT='-u'
		# make uefi iso
		rxvt -bg orange -fg black -title "Making UEFI ISO" -e mkisofs -iso-level 4 -D -R $VOLI -o $WKGMNTPT/PuppyRemaster/custom-puppy${UEFI}.iso -b ${BOOTLOADER} -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -eltorito-platform efi -b "$EFI_IMG" -no-emul-boot $WKGMNTPT/PuppyRemaster/
else
		rxvt -bg green -fg black -title "Making ISO" -e mkisofs -iso-level 4 -D -R $VOLI -o $WKGMNTPT/PuppyRemaster/custom-puppy.iso -b ${BOOTLOADER} -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table $WKGMNTPT/PuppyRemaster/	
fi

case $BOOTLOADER in *"isolinux"*)
		if which isohybrid ; then
			echo "isohybrid ${UEFI_OPT} $WKGMNTPT/PuppyRemaster/custom-puppy${UEFI}.iso"
			isohybrid ${UEFI_OPT} "$WKGMNTPT/PuppyRemaster/custom-puppy${UEFI}.iso"
		fi 
esac

;;&



 esac
 

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


