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

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

ANSWER=$(Xdialog --stdout --left --wrap --icon /usr/local/lib/X11/pixmaps/nicOS.png --no-cancel --title "Remaster-Alternative" --help "This is an alternative remaster script.  It differs from the classic remaster
script in that files are copied directly from their mounting points in /initrd.
One benefit using this method is that the contents of all loaded sfs files
(except the base sfs) are excluded by default so you don't have to exclude
any sfs's before the bootup process.

NB:  Items 1 and 3 are essential.  Do not deselect them.

Select item 2 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. 

Tick item 4 if you want to make a bootable 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 5 "1" "Recording changes to system" "on" "2" "Discard current hardware settings" "off" "3" "Building the new base sfs" "on" "4" "Select to also make an ISO" "off") 
 case /$ANSWER/ in
 */1/*) 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 /home ] && DIRHOME="/home"
[ -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 -f $WKGMNTPT/PuppyRemaster
sleep 0.1
mkdir -p $WKGMNTPT/PuppyRemaster

rsync -a /initrd/pup_ro2/* $WKGMNTPT/PuppyRemaster "$@"
 
rsync -a /initrd/pup_ro1/* $WKGMNTPT/PuppyRemaster "$@"
 
rsync -a /initrd/pup_rw/* $WKGMNTPT/PuppyRemaster "$@"
        
rm -r $WKGMNTPT/PuppyRemaster/root
rm -r $WKGMNTPT/PuppyRemaster/etc
rm -r $WKGMNTPT/PuppyRemaster/var

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

alsactl store
cp -af /var/lib/alsa/asound.state  $WKGMNTPT/tmp/etc/
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 

sync

cp -a $WKGMNTPT/tmp/etc $WKGMNTPT/PuppyRemaster
sync
rm -rf $WKGMNTPT/tmp/etc
cp -a $WKGMNTPT/tmp/root $WKGMNTPT/PuppyRemaster
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/*
cp -a $WKGMNTPT/tmp/var $WKGMNTPT/PuppyRemaster 
sync
rm -rf $WKGMNTPT/tmp/var

;;&

 */2/*)
 rm -r $WKGMNTPT/PuppyRemaster/etc/codepage 
 rm -r $WKGMNTPT/PuppyRemaster/etc/fontmap 
 rm -r $WKGMNTPT/PuppyRemaster/etc/keyboardtype 
 rm -r $WKGMNTPT/PuppyRemaster/etc/keymap 
 rm -r $WKGMNTPT/PuppyRemaster/etc/mousebuttons 
 rm -r $WKGMNTPT/PuppyRemaster/etc/mousedevice 
 rm -r $WKGMNTPT/PuppyRemaster/etc/videomode 
 rm -r $WKGMNTPT/PuppyRemaster/etc/eth0mode 
 rm -r $WKGMNTPT/PuppyRemaster/etc/eth1mode 
 rm -r $WKGMNTPT/PuppyRemaster/etc/resolv.conf
 cp -a /initrd/pup_ro2/etc/resolv.conf $WKGMNTPT/PuppyRemaster/etc/  
 rm -f $WKGMNTPT/PuppyRemaster/etc/localtime 
 cp -a /initrd/pup_ro2/etc/localtime $WKGMNTPT/PuppyRemaster/etc/ 
 rm -r $WKGMNTPT/PuppyRemaster/etc/TZ 
 rm -r $WKGMNTPT/PuppyRemaster/etc/cdburnerdevice 
 rm -r $WKGMNTPT/PuppyRemaster/etc/dvddevice 
 rm -r $WKGMNTPT/PuppyRemaster/etc/modemdevice 
 rm -r $WKGMNTPT/PuppyRemaster/etc/securetelnetrc 
 rm -r $WKGMNTPT/PuppyRemaster/etc/modules.conf
 cp -a /initrd/pup_ro2/etc/modules.conf $WKGMNTPT/PuppyRemaster/etc/ 
 rm -r $WKGMNTPT/PuppyRemaster/etc/modprobe.d
 cp -a /initrd/pup_ro2/etc/modprobe.d $WKGMNTPT/PuppyRemaster/etc/
 rm -r $WKGMNTPT/PuppyRemaster/etc/rdesktoprc 
 rm -r $WKGMNTPT/PuppyRemaster/etc/windowmanager 
 rm -r $WKGMNTPT/PuppyRemaster/etc/xextraoptions 
 rm -r $WKGMNTPT/PuppyRemaster/etc/X11/xorg.conf 
 rm -r $WKGMNTPT/PuppyRemaster/etc/wvdial.conf 
 rm -r $WKGMNTPT/PuppyRemaster/etc/network-wizard
 cp -a /initrd/pup_ro2/etc/network-wizard $WKGMNTPT/PuppyRemaster/etc/ 
 rm -r $WKGMNTPT/PuppyRemaster/etc/simple_network_setup/*
 cp -a /initrd/pup_ro2/etc/simple_network_setup/* $WKGMNTPT/PuppyRemaster/etc/ 
 ;;&
 
 */3/*) COMP="-comp gzip -b 16384" 

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

rm -r $WKGMNTPT/NewPuppy
sleep 1
mkdir -p $WKGMNTPT/NewPuppy
rm -r $WKGMNTPT/PuppyRemaster/initrd
rm -r $WKGMNTPT/PuppyRemaster/sys
sleep 1
YDRIVE=/initrd/pup_y
ADRIVE=/initrd/pup_a
SAVE1=/initrd/pup_ro1
SAVE2=/initrd/pup_rw
find $YDRIVE -name ".wh.*" 2>/dev/null | while IFS= read -r NAME; do actual_file=`echo "$NAME" | sed -e "s#$YDRIVE##g" -e 's/.wh.//g'`; rm -fr "$WKGMNTPT/PuppyRemaster/$actual_file"; done
find $ADRIVE -name ".wh.*" 2>/dev/null | while IFS= read -r NAME; do actual_file=`echo "$NAME" | sed -e "s#$ADRIVE##g" -e 's/.wh.//g'`; rm -fr "$WKGMNTPT/PuppyRemaster/$actual_file"; done
find $SAVE1 -name ".wh.*" 2>/dev/null | while IFS= read -r NAME; do actual_file=`echo "$NAME" | sed -e "s#$SAVE1##g" -e 's/.wh.//g'`; rm -fr "$WKGMNTPT/PuppyRemaster/$actual_file"; done
find $SAVE2 -name ".wh.*" 2>/dev/null | while IFS= read -r NAME; do actual_file=`echo "$NAME" | sed -e "s#$SAVE2##g" -e 's/.wh.//g'`; rm -fr "$WKGMNTPT/PuppyRemaster/$actual_file"; done
sleep 1
cd $WKGMNTPT/PuppyRemaster
find . -name "*.wh." -type f -delete
sleep 1
Xdialog -left -wrap -title "Final Check" -msgbox "All the files for the new base sfs are located at $WKGMNTPT/PuppyRemaster.  You can check and /edit/change anything now if you wish and know what you are doing (BEST TO LEAVE EVERYTHING AS IS).  Especially note the contents of $WKGMNTPT/PuppyRemaster/root, $WKGMNTPT/PuppyRemaster/etc as well as $WKGMNTPT/PuppyRemaster/home and $WKGMNTPT/PuppyRemaster/media (if applicable).  Click 'OK' when ready to proceed." 0 80 
squash $WKGMNTPT/PuppyRemaster $WKGMNTPT/NewPuppy/$PUPPYSFS $COMP
rm -rf $WKGMNTPT/PuppyRemaster 
chmod a+r $WKGMNTPT/PuppyRemaster/* &>/dev/null
chmod a-x $WKGMNTPT/PuppyRemaster/*.sfs &>/dev/null
rox -d $WKGMNTPT/NewPuppy ;;&
 */4/*) m_21="$(eval_gettext 'Creating an ISO')
$(gettext "To make an ISO: add initrd.gz, vmlinuz, the zdrv and any fdrv from your current running system to $WKGMNTPT/NewPuppy .  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 UEFI ISO.  Lastly, you can add any other files you want to include in the ISO, to $WKGMNTPT/NewPuppy 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/NewPuppy/grldr ] && BOOTLOADER="grldr"
		[ -f $WKGMNTPT/NewPuppy/isolinux.bin ] && BOOTLOADER="isolinux.bin"
		if [ -f $WKGMNTPT/NewPuppy/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/NewPuppy/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/NewPuppy/
else
		rxvt -bg green -fg black -title "Making ISO" -e mkisofs -iso-level 4 -D -R $VOLI -o $WKGMNTPT/NewPuppy/custom-puppy.iso -b ${BOOTLOADER} -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table $WKGMNTPT/NewPuppy/	
fi

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

 esac 


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


