#!/bin/sh
#(c) Copyright Barry Kauler, 31 January 2017. Licence: GPL v3 (/usr/share/doc/legal).
#called from /init, passed params are $WKG_DRV, $WKG_DEV

WKG_DRV=$1 #ex: sda
WKG_DEV=$2 #ex: sda2
. /BOOT_SPECS #has BOOT_DISKID, BOOT_PARTNUM, BOOT_FS, BOOT_DIR, WKG_DISKID, WKG_PARTNUM, WKG_FS, WKG_DIR

err_exit() {
 echo -e "\\033[1;31mERROR: ${1}\\033[0;39m" #red
 rm -f /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg
 exit 1
}

CHECK="`cat /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg | cut -f 3 -d ','`" #"MAXIMAL" or "MAXSIZE", see rc.shutdown, quicksetup.
if [ "$CHECK" == "MAXSIZE" ];then
 START_WKG_DEV="`cat /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg | cut -f 4 -d ','`" #start of partition, in sectors.
 END_DRV="`cat /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg | cut -f 5 -d ','`" #end of drive, in sectors.
 NEW_END_PART="`busybox expr $END_DRV - 1024`" #leave 512K unallocated. 161010 cannot find expr
fi
#note, .fsckme.flg also has WKG_DEV and WKG_FS, but don't believe it.

#the f.s. is mounted. could do a f.s. check, but prefer to unmount it...
sync
umount /dev/$WKG_DEV
[ $? -ne 0 ] && err_exit "$(LANG=${wkgLANG} gettext 'Could not check the filesystem of working partition, unable to unmount')"

echo -e "\\033[1;35m" #purple. 34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black.
echo "$(LANG=${wkgLANG} gettext 'Filesystem operations are to be performed upon the working partition.')"
echo "$(LANG=${wkgLANG} gettext 'Working partition:') ${WKG_DEV}"
echo -en "\\033[0;39m"

#peform f.s. check and optional resize...
if [ "${CHECK}" == "MAXSIZE" ];then #NOTE: probably don't need this anymore with Easy Linux.
 echo "  $(LANG=${wkgLANG} gettext 'Growing partition to fill drive...')"
 #sanity check...
 if [ -h /bin/fdisk ];then #170110
  REAL_START="$(fdisk -u -l /dev/${WKG_DRV} | busybox tr -s ' ' | grep "^ ${WKG_PARTNUM} " | cut -f 3 -d ' ')"
 else
  REAL_START="$(fdisk -u -l /dev/${WKG_DRV} | busybox tr -s ' ' | grep "^/dev/${WKG_DEV} " | cut -f 2 -d ' ')" #full fdisk.
 fi
 if [ "$REAL_START" != "${START_WKG_DEV}" ];then
  echo
  echo "  ERROR: ${WKG_DEV} seems to be the wrong partition."
  echo "  Start of ${WKG_DEV}: ${REAL_START} sectors. Expected start: ${START_WKG_DEV} sectors."
  echo '  Have you changed the removable drives since the last bootup?'
  echo '  ABORTING resizing of partition to fill drive.'
  mount -t ${WKG_FS} /dev/${WKG_DEV} /mnt/${WKG_DEV}
  err_exit "$(LANG=${wkgLANG} gettext 'Unable to grow working partition, not found')"
 fi
 #weird, but have to delete partition then create a new one...
 echo "d
${WKG_PARTNUM}
n
${WKG_PARTNUM}
${START_WKG_DEV}
${NEW_END_PART}
w" | fdisk -u /dev/${WKG_DRV} #> /dev/null
 sync
fi

case "$CHECK" in
 MAXIMAL) echo "  $(LANG=${wkgLANG} gettext 'Performing filesystem check, maximal mount count...')" ;;
 MAXSIZE) echo "  $(LANG=${wkgLANG} gettext 'Performing filesystem check, prior to growing it...')" ;;
 REQUEST) echo "  $(LANG=${wkgLANG} gettext 'Performing filesystem check, on request...')" ;;
 *) echo "  $(LANG=${wkgLANG} gettext 'Performing filesystem check, after improper shutdown...')" ;;
esac

e2fsck -p -f /dev/${WKG_DEV}
busybox sync
echo "  $(LANG=${wkgLANG} gettext 'Filesystem check completed!')"

if [ "${CHECK}" == "MAXSIZE" ];then
 echo "  $(LANG=${wkgLANG} gettext 'Resizing filesystem to fill partition...')"
 resize2fs /dev/${WKG_DEV}
 busybox sync
 echo "  $(LANG=${wkgLANG} gettext '...done')"
fi

sync
mount -t ${WKG_FS} /dev/${WKG_DEV} /mnt/${WKG_DEV}
rm -f /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg
exit 0
###end###
