#!/bin/sh
#nicOS-SFS-Merger.  An application to merge/combine loaded sfs files. (c) Copyright 2022, amethyst.

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

. /etc/rc.d/PUPSTATE

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

ANSWER=$(Xdialog --stdout --left --wrap --icon /usr/local/lib/X11/pixmaps/nicOS.png --no-cancel --title "SFS-Merger" --help "This application merges/combines loaded sfs files.

Select the sfs files you want to merge into one new sfs from
items 2 to 12.
Note that you may get a warning message when selecting item 2
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.

NB:  DO NOT DESELECT ITEMS 1 AND 13.

The operation will take time, please be patient...." --checklist "Click the 'Help' button for instructions.
 
Click 'OK' after making your selections." 0 0 14 "1" "Choose Work Area" "on" "2" "Choose loaded extra sfs files to include" "off" "3" "fdrv" "off" "4" "zdrv" "off" "5" "base sfs" "off" "6" "gdrv" "off" "7" "edrv" "off" "8" "ddrv" "off" "9" "cdrv" "off" "10" "bdrv" "off" "11" "ydrv" "off" "12" "adrv" "off" "13" "Building the new sfs" "on") 
 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="/"

rm -f $WKGMNTPT/Merge
sleep 0.1
mkdir -p $WKGMNTPT/Merge ;;&
 */2/*) sfs_load
 cp -a /initrd/pup_ro20/* /initrd/pup_ro19/* /initrd/pup_ro18/* /initrd/pup_ro17/* /initrd/pup_ro16/* /initrd/pup_ro15/* /initrd/pup_ro14/* /initrd/pup_ro13/* /initrd/pup_ro12/* /initrd/pup_ro11/* /initrd/pup_ro10/* /initrd/pup_ro9/* /initrd/pup_ro8/* /initrd/pup_ro7/* /initrd/pup_ro6/* /initrd/pup_ro5/* /initrd/pup_ro4/* /initrd/pup_ro3/*  $WKGMNTPT/Merge ;;&
 */3/*) cp -a /initrd/pup_f/* $WKGMNTPT/Merge ;;&  
 */4/*) cp -a /initrd/pup_z/* $WKGMNTPT/Merge ;;&
 */5/*) cp -a /initrd/pup_ro2/* $WKGMNTPT/Merge ;;&  
 */6/*) cp -a /initrd/pup_g/* $WKGMNTPT/Merge ;;&
 */7/*) cp -a /initrd/pup_e/* $WKGMNTPT/Merge ;;&  
 */8/*) cp -a /initrd/pup_d/* $WKGMNTPT/Merge ;;&
 */9/*) cp -a /initrd/pup_c/* $WKGMNTPT/Merge ;;&  
 */10/*) cp -a /initrd/pup_b/* $WKGMNTPT/Merge ;;&
 */11/*) cp -a /initrd/pup_y/* $WKGMNTPT/Merge ;;&  
 */12/*) cp -a /initrd/pup_a/* $WKGMNTPT/Merge ;;&
 */13/*) 
squash() {
	echo $0 $@
	rxvt -bg lightblue -fg black -title "$m_01" -geometry 80x6+360+360 -e mksquashfs $@ 2> /dev/null
}

COMP="-comp gzip -b 16384"

squash $WKGMNTPT/Merge $WKGMNTPT/New.sfs $COMP
rm -rf $WKGMNTPT/Merge
rox -s $WKGMNTPT/New.sfs 
;;&
 
esac

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


