#!/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
#180601 users may forget to put a trailing slash. a leading slash is not allowed.
#190223 remove gettext.
#190227 e2fsck is now in easy.sfs mounted on tnew. no longer support MAXSIZE.
#190314 BOOT_SPECS no longer has *_DISKID, *_FS, *_PARTNUM, and has *_UUID
#190314 fatal and non-fatal error exits.
#190828 check for bad blocks. no, don't think any point doing this on modern drives.
#200302 support discard for SSDs.
#200303 display current date.
#200307 fsck -p may fail, then use -y
#200611 remove "-E discard", perhaps not desirable for nvme.
#200726 temporary easy.sfs unmounted before call this script, using static e2fsck.

WKG_DRV=$1 #ex: sda
WKG_DEV=$2 #ex: sda2
. /BOOT_SPECS #has BOOT_UUID BOOT_DIR, WKG_UUID, WKG_DIR
#180601 users may forget to put a trailing slash... a leading slash is not allowed...
[ "$BOOT_DIR" ] && [ "${BOOT_DIR##*/}" ] && BOOT_DIR="${BOOT_DIR}/"
[ "$WKG_DIR" ] && [ "${WKG_DIR##*/}" ] && WKG_DIR="${WKG_DIR}/"
[ "$BOOT_DIR" ] && [ "${BOOT_DIR:0:1}" == "/" ] && BOOT_DIR="${BOOT_DIR:1:99}"
[ "$WKG_DIR" ] && [ "${WKG_DIR:0:1}" == "/" ] && WKG_DIR="${WKG_DIR:1:99}"

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

CHECK="`cat /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg | cut -f 3 -d ','`" #"MAXIMAL", see rc.shutdown, quicksetup.

#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 "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 "Filesystem operations are to be performed upon the working partition."
echo "Working partition: ${WKG_DEV}"
echo -en "\\033[0;39m"

case "$CHECK" in
 MAXIMAL) echo "  Performing filesystem check, maximal mount count..." ;;
 REQUEST) echo "  Performing filesystem check, on request..." ;;
 *) echo "  Performing filesystem check, after improper shutdown..." ;;
esac

#200303
echo -n "  Current date: "
echo "$DATE"

#200726 removed chroot into tnew. (ref: prev script archived at input552/fscheck)
e2fsck -C 0 -p -f /dev/${WKG_DEV} 2>/fsck-err.log #200726
busybox sync
#200307 fsck may fail...
if [ -f /fsck-err.log ];then
 if grep -q 'UNEXPECTED INCONSISTENCY' /fsck-err.log;then
  #can force it like this...
  echo -e "\\033[1;31m  Unexpected inconsistency fixing filesystem. Trying again...\\033[0;39m" #red
  e2fsck -y -f /dev/${WKG_DEV} >/dev/null 2>&1
  busybox sync
 fi
 rm -f /fsck-err.log
fi
echo "  Filesystem check completed!"

sync
#190314 WKG_FS is exported from init, return error-code...
mount -t ${WKG_FS} /dev/${WKG_DEV} /mnt/${WKG_DEV}
[ $? -ne 0 ] && exit 2
rm -f /mnt/${WKG_DEV}/${WKG_DIR}.session/.fsckme.flg
exit 0
###end###
