#!/bin/bash
#
#	Script adapted from "AutoFsck V3.2 by Jonathan Musther"
#	by Jonathan Musther, for Peter  -  1st May 2008
#
#	This script should be called when the machine is to be shutdown, it
#	will test to see if fsck is due, if it is, it will be run and the
#	machine will then be halted.  This script should be called thus:
#
#	/usr/bin/autofsck
#
#	where /usr/bin/autofsck is the path to this script.
#
#	Contact: jon@slymail.com
#
#------------------SCRIPT STARTS--------------------
#
#
#========================== Check on halt ==========================
function setupfsck {
	#Force checks.
	touch /forcefsck
	#Now create the script which will cause halt after the check:
	touch /tmp/postfsckhalt.sh
		#Actually create the script
		echo \#! /bin/sh >> /tmp/postfsckhalt.sh
		echo \# >> /tmp/postfsckhalt.sh
		echo \# This is a temporary script, it should not be >> /tmp/postfsckhalt.sh
		echo \# here while the system is in multi-user mode. >> /tmp/postfsckhalt.sh
		echo \# >> /tmp/postfsckhalt.sh
		echo rm /etc/init.d/postfsckhalt.sh >> /tmp/postfsckhalt.sh
		echo rm /etc/rc2.d/S*postfsckhalt.sh >> /tmp/postfsckhalt.sh
		echo rm /etc/rc3.d/S*postfsckhalt.sh >> /tmp/postfsckhalt.sh
		echo rm /etc/rc4.d/S*postfsckhalt.sh >> /tmp/postfsckhalt.sh
		echo rm /etc/rc5.d/S*postfsckhalt.sh >> /tmp/postfsckhalt.sh
		echo init 0 >> /tmp/postfsckhalt.sh
	#Put the script in init.d and update-rc
	mv /tmp/postfsckhalt.sh /etc/init.d/postfsckhalt.sh
	chmod a+x /etc/init.d/postfsckhalt.sh
	update-rc.d postfsckhalt.sh defaults
	#Tidy up so that the script isn't run during shutdown
	#but only during the subsequent boot.
	rm /etc/rc0.d/K*postfsckhalt.sh
	rm /etc/rc1.d/K*postfsckhalt.sh
	rm /etc/rc2.d/K*postfsckhalt.sh
	rm /etc/rc3.d/K*postfsckhalt.sh
	rm /etc/rc4.d/K*postfsckhalt.sh
	rm /etc/rc5.d/K*postfsckhalt.sh
	rm /etc/rc6.d/K*postfsckhalt.sh
}

#Consolidate info about the filesystems, given by mount.
function mountread {
	for i in $( mount | grep --regexp="/dev/hd" --regexp="/dev/sd" ); do
		echo $i
	done
}

function checkcount {
#Basically this section checks the mount command for mounted 
#partitions, /dev/sdX and /dev/hdX.  Then it checks the mount count 
#and maximum mount count of each one, any which is nearly ready for 
#checking causes $CHECK to be flagged to "1" and therefore starts 
#the main user interactive bit of the script.
CHECK="0"
#Loop to determine filesystems that need checking.
#MC = Mount count - MMC = Max Mount Count.
for i in $( mountread | grep --regexp="/dev/hd" --regexp="/dev/sd" ); do
	MC=$(tune2fs -l $i | grep --regexp="Mount count:" | tr -d "Mount count:")
	MMC=$(tune2fs -l $i | grep --regexp="Maximum mount count:" | tr -d "Maximum mount count:")
	MMC=$(echo $(($MMC-1)))
if [ "$MC" -ge "$MMC" ]; then
	CHECK="1"
fi
done
#Find out if check required.
if [ "$CHECK" = "0" ]; then #If no check required, halt.
	init 0
fi				#If a check is required, set up checks - using function setupfsck:
setupfsck
}

##################### FUNCTION CALLS #####################

checkcount
init 6
exit

