#! /bin/sh
### BEGIN INIT INFO
# Provides:          checkfs
# Required-Start:    checkroot
# Required-Stop:
# Should-Start:
# Default-Start:     S
# Default-Stop:
# X-Interactive:     true
# Short-Description: Check all filesystems.
### END INIT INFO

# Include /usr/bin in path to find on_ac_power if /usr/ is on the root
# partition.
setvar PATH = "/sbin:/bin:/usr/bin"
setvar FSCK_LOGFILE = "/var/log/fsck/checkfs"
test $FSCKFIX || setvar FSCKFIX = 'no'
source /lib/init/vars.sh

source /lib/lsb/init-functions
source /lib/init/mount-functions.sh
source /lib/init/swap-functions.sh

proc do_start {
	# Trap SIGINT so that we can handle user interupt of fsck.
	trap "" INT

	# See if we're on AC Power.  If not, we're not gonna run our
	# check.  If on_ac_power (in /usr/) is unavailable, behave as
	# before and check all file systems needing it.

# Disabled AC power check until fsck can be told to only check the
# file system if it is corrupt when running on battery. (bug #526398)
#	if which on_ac_power >/dev/null 2>&1
#	then
#		on_ac_power >/dev/null 2>&1
#		if [ $? -eq 1 ]
#		then
#			[ "$VERBOSE" = no ] || log_success_msg "Running on battery power, so skipping file system check."
#			BAT=yes
#		fi
#	fi
	setvar BAT = """"
	setvar fscheck = ""yes""

	if is_fastboot_active
	{
		test $fscheck = yes && log_warning_msg "Fast boot enabled, so skipping file system check."
		setvar fscheck = 'no'
	}

	#
	# Check the rest of the file systems.
	#
	if test $fscheck = yes && test ! $BAT && test $FSCKTYPES != "none"
	{

		# Execute swapon command again, in case there are lvm
		# or md swap partitions.  fsck can suck RAM.
		swaponagain 'lvm and md'

		if test -f /forcefsck || grep -q -s -w -i "forcefsck" /proc/cmdline
		{
			setvar force = ""-f""
		} else {
			setvar force = """"
		}
		if test $FSCKFIX = yes
		{
			setvar fix = ""-y""
		} else {
			setvar fix = ""-a""
		}
		setvar spinner = ""-C""
		case (TERM) {
		  dumb|network|unknown|"" {
			setvar spinner = """"
			}
		}
		test $(uname -m) = s390x && setvar spinner = """"  # This should go away
		setvar FSCKTYPES_OPT = """"
		test $FSCKTYPES && setvar FSCKTYPES_OPT = ""-t $FSCKTYPES""
		proc handle_failed_fsck {
			log_failure_msg "File system check failed. 
A log is being saved in ${FSCK_LOGFILE} if that location is writable. 
Please repair the file system manually."
			log_warning_msg "A maintenance shell will now be started. 
CONTROL-D will terminate this shell and resume system boot."
			# Start a single user shell on the console
			if ! sulogin $CONSOLE
			{
				log_failure_msg "Attempt to start maintenance shell failed. 
Continuing with system boot in 5 seconds."
				sleep 5
			}
		}
		if test $VERBOSE = no
		{
			log_action_begin_msg "Checking file systems"
			logsave -s $FSCK_LOGFILE fsck $spinner -M -A $fix $force $FSCKTYPES_OPT
			setvar FSCKCODE = ""$?

			if test $FSCKCODE -eq 32
			{
				log_action_end_msg 1 "code $FSCKCODE"
				log_warning_msg "File system check was interrupted by user"
			} elif test $FSCKCODE -gt 1
			{
				log_action_end_msg 1 "code $FSCKCODE"
				handle_failed_fsck
			} else {
				log_action_end_msg 0
			}
		} else {
			if test $FSCKTYPES
			{
				log_action_msg "Will now check all file systems of types $FSCKTYPES"
			} else {
				log_action_msg "Will now check all file systems"
			}
			logsave -s $FSCK_LOGFILE fsck $spinner -V -M -A $fix $force $FSCKTYPES_OPT
			setvar FSCKCODE = ""$?
			if test $FSCKCODE -eq 32
			{
				log_warning_msg "File system check was interrupted by user"
			} elif test $FSCKCODE -gt 1
			{
				handle_failed_fsck
			} else {
				log_success_msg "Done checking file systems. 
A log is being saved in ${FSCK_LOGFILE} if that location is writable."
			}
		}
	}
	rm -f /fastboot /forcefsck 2>/dev/null
}

case (1) {
  start|"" {
	do_start
	}
  restart|reload|force-reload {
	echo "Error: argument '$1' not supported" >&2
	exit 3
	}
  stop|status {
	# No-op
	}
  * {
	echo "Usage: checkfs.sh [start|stop]" >&2
	exit 3
	}
}

: