#!/bin/sh
#nicOS-Save2SFS.  An application to save the contents of your savefile/folder to a ydrv or adrv, (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
ADRVSFS="$DISTRO_ADRVSFS" 
YDRVSFS="$DISTRO_YDRVSFS"

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

ANSWER=$(Xdialog --stdout --left --wrap --icon /usr/local/lib/X11/pixmaps/nicOS.png --no-cancel --title "Save2SFS" --help "This application saves the contents of your savefile/folder to a ydrv or adrv.

Select one of items 2, 3 or 4.

Choose the ydrv option  to save all changes to your system which will include
the contents of your existing savefile/folder, changes to the system during this
session (which has not been saved to savefile/folder yet) as well as the contents
of any existing ydrv and adrv,  to a new ydrv.  This new ydrv will replace any
existing savefile/folder as well as any existing adrv and ydrv.  This is a good
option to choose if you have a big savefile/folder with lots of installed stuff
and want to save all this to a ydrv, replacing the savefile/older.

Choose the adrv option to save the contents of your savefile/folder, changes to
the system during this session (which has not been saved to savefile/folder yet)
and any existing adrv to a new adrv.  This new adrv will replace any existing
savefile/folder and the previous adrv (if you had one).  You will typically
choose this option if you already have previously saved the bulk of your changes
to a ydrv and therefor should be much faster.

Choose the 'Exclude existing adrv and ydrv' option to save the contents of your
savefile/folder (thus replacing it) and changes to the system during this session
(which has not been saved to savefile/folder yet) to a new adrv.  If you already
have an adrv, you can try to rename that to a ydrv (if you do not have a ydrv) or
use your old adrv as an extra sfs file but be careful.  If your old adrv contains
some system configurations you should rather choose item 2 or 3.

NB:  DO NOT DESELECT ITEM 1.

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" "Choose Work Area" "on" "2" "Save to ydrv" "off" "3" "Save to adrv" "off" "4" "Exclude existing adrv and ydrv" "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="/" ;;&
 */2/*) rm -f $WKGMNTPT/tmp/Builder
sleep 0.1
mkdir -p $WKGMNTPT/tmp/Builder
cp -a /initrd/pup_y/* /initrd/pup_a/* /initrd/pup_ro1/* /initrd/pup_rw/*   $WKGMNTPT/tmp/Builder
alsactl store
cp -a /var/lib/alsa/asound.state  $WKGMNTPT/tmp/Builder/etc
cd $WKGMNTPT/tmp/Builder
rm -rf ./tmp ./dev ./mnt ./initrd ./sys ./root/.XLOADED ./etc/.XLOADED ./archive ./proc

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

COMP="-comp gzip -b 16384"

squash $WKGMNTPT/tmp/Builder $WKGMNTPT/$YDRVSFS $COMP

rm -r $WKGMNTPT/tmp/Builder

rox -d $WKGMNTPT ;;
 */3/*) rm -f $WKGMNTPT/tmp/Builder
sleep 0.1
mkdir -p $WKGMNTPT/tmp/Builder
cp -a /initrd/pup_a/* /initrd/pup_ro1/* /initrd/pup_rw/*   $WKGMNTPT/tmp/Builder 
alsactl store
cp -a /var/lib/alsa/asound.state  $WKGMNTPT/tmp/Builder/etc
cd $WKGMNTPT/tmp/Builder
rm -rf ./tmp ./dev ./mnt ./initrd ./sys ./root/.XLOADED ./etc/.XLOADED ./archive ./proc

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

COMP="-comp gzip -b 16384"

squash $WKGMNTPT/tmp/Builder $WKGMNTPT/$ADRVSFS $COMP

rm -r $WKGMNTPT/tmp/Builder

rox -d $WKGMNTPT ;;

*/4/*) rm -f $WKGMNTPT/tmp/Builder
sleep 0.1
mkdir -p $WKGMNTPT/tmp/Builder
cp -a /initrd/pup_ro1/* /initrd/pup_rw/*   $WKGMNTPT/tmp/Builder 
alsactl store
cp -a /var/lib/alsa/asound.state  $WKGMNTPT/tmp/Builder/etc
cd $WKGMNTPT/tmp/Builder
rm -rf ./tmp ./dev ./mnt ./initrd ./sys ./root/.XLOADED ./etc/.XLOADED ./archive ./proc

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

COMP="-comp gzip -b 16384"

squash $WKGMNTPT/tmp/Builder $WKGMNTPT/$ADRVSFS $COMP

rm -r $WKGMNTPT/tmp/Builder

rox -d $WKGMNTPT ;;
        
esac

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



