# Perform file system check
if mount | grep /home/acradyne/g4dev > /dev/null; then
	echo "Rear SD already mounted, exiting"
	exit 1    
fi

# Perform file system checks
fsck.ext4 -y /dev/mmcblk1p1
echo 'Gen4 File fsck exit code: ' $? > /dev/kmsg

# Retries for mounting rear SD
echo 'Mounting Gen4 Rear SD' $(date) > /dev/kmsg
mntCmd="mount -t ext4 -o rw,suid,dev,exec,noauto,sync,nouser,noatime /dev/mmcblk1p1 /home/acradyne/g4dev"
$mntCmd
mntOutput=$?
timeout=$(($(date +%s) + 10))
until [ $mntOutput -eq 0 ] || [ $(date +%s) -gt $timeout ]; do
	echo "SD Mount Failure: ${mntOutput}, trying again" > /dev/kmsg
	sleep 1
	$mntCmd
	mntOutput=$?
done

# Check final values
if [ $mntOutput -eq 0 ]; then
	echo 'Gen4 Rear SD Mounted' > /dev/kmsg
else
	echo 'Gen4 Rear SD Not Mounted' > /dev/kmsg
fi
exit 0
