#!/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

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" 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 "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"

##peform f.s. check and optional resize...
#if [ "${CHECK}" == "MAXSIZE" ];then #NOTE: probably don't need this anymore with Easy Linux.
# echo "  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 "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 "  Performing filesystem check, maximal mount count..." ;;
# MAXSIZE) echo "  Performing filesystem check, prior to growing it..." ;;
 REQUEST) echo "  Performing filesystem check, on request..." ;;
 *) echo "  Performing filesystem check, after improper shutdown..." ;;
esac

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

#190828 filesystem check script...
BLOCKSIZE="$(stat -f -c %S /dev/${WKG_DEV})"
echo "#!/bin/ash
dumpe2fs -b /dev/${WKG_DEV} 2>/dev/null >/knownbadblocks.log
# -s prints progress ...
badblocks -b ${BLOCKSIZE} -o /newbadblocks.log -i /knownbadblocks.log -s /dev/${WKG_DEV}
if [ -s /newbadblocks.log ];then
 echo -e \"\\\\033[1;31mWARNING: There are new bad blocks. The drive may be failing.\\\\033[0;39m\" #red
fi
# -C 0 prints progress to console, -E discard discards freed inodes (good for flash)...
" > tnew/fscheck.sh
if [ "$TRIMflg" ];then #200302 exported from init.
 echo "e2fsck -C 0 -l /newbadblocks.log -E discard -p -f /dev/${WKG_DEV}" >> tnew/fscheck.sh
else
 echo "e2fsck -C 0 -l /newbadblocks.log -p -f /dev/${WKG_DEV}" >> tnew/fscheck.sh
fi
chmod 755 tnew/fscheck.sh

#e2fsck -p -f /dev/${WKG_DEV}
#chroot tnew /bin/busybox ash -c "/sbin/e2fsck -p -f /dev/${WKG_DEV}"
#chroot tnew /bin/busybox ash -c "/fscheck.sh" #190828
if [ "$TRIMflg" ];then #200302 exported from init. 200307 log error...
 chroot tnew /bin/busybox ash -c "/sbin/e2fsck -C 0 -E discard -p -f /dev/${WKG_DEV} 2>/fsck-err.log" #190828 keep the prev method.
else
 chroot tnew /bin/busybox ash -c "/sbin/e2fsck -C 0 -p -f /dev/${WKG_DEV} 2>/fsck-err.log"
fi
busybox sync
#200307 fsck may fail...
if [ -f tnew/fsck-err.log ];then
 if grep -q 'UNEXPECTED INCONSISTENCY' tnew/fsck-err.log;then
  #can force it like this...
  echo -e "\\033[1;31m  Unexpected inconsistency fixing filesystem. Trying again...\\033[0;39m" #red
  chroot tnew /bin/busybox ash -c "/sbin/e2fsck -y -f /dev/${WKG_DEV} >/dev/null 2>&1"
  busybox sync
 fi
 rm -f tnew/fsck-err.log
fi
echo "  Filesystem check completed!"

#if [ "${CHECK}" == "MAXSIZE" ];then
# echo "  Resizing filesystem to fill partition..."
# resize2fs /dev/${WKG_DEV}
# busybox sync
# echo "  ...done"
#fi

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###
