#!/sbin/sh
# @(#) $Revision: 78.1 $
#
##################################################################
#
# Name :
#	 bcheckrc (hfs) - /sbin/fs/hfs/bcheckrc
#
# Description
# 	This file contains scripts/commands necessary to checks
#	the hfs file systems before mounting.
#	
#	It should be invoked by the generic bcheckrc script only.
#
# Input Parameters:
#	None
#
# Expected results:
#	see description.
#
# Side effects of this test:
#  	none
#
# Supporting files and Relationship:
#        i) various "/sbin" commands are used by this script.
#       ii) generic bcheckrc scrtipt located in the /sbin
#           directory invokes this script.
#
# Important Note :
#	This script is not configurable !  Any changes made to this
#       scipt will be overwritten when you upgrade to the next
#	release of HP-UX.
#
##################################################################
#


#
# Description - This function will check (fsck) all the hfs file systems
# in the static file system table, /etc/fstab.
#
# Result values - status = 0 for success
#                 status = non-0 for failure
#
# Used (exclusively) by - /sbin/bcheckrc's fsck_hfs portion
#


#
#	ROOTSHELL   - is the command that gives the user a shell in which
#		      to run fsck interactively.  Someday fsck -y should
#		      be fixed to give the right answers to the questions,
#		      rather than always "yes", so this error-prone
#		      interactive fixing business can go away entirely.
#

	ROOTSHELL='/sbin/sh'

#	set stty (for manual mode)

	/sbin/stty clocal icanon echo opost onlcr ixon icrnl ignpar erase "^h"

#	
#	In the remaining part of the function is devoted to the actual
#	cleaning of the hfs file systems.
#
#		1) run fsclean -v and determine if any hfs file systems
#		   need cleaning.
#
#		2) check exit status of fsclean -
#
#			0 - indicates (and lists) that all the hfs file
#			    systems were verified clean.  Continues to
#			    boot.
#
#		        1 - indicates and lists the file systems that 
#			    were not properly shutdown.  File systems
#			    that are corrupt should be fixed before
#			    continuing with the boot.
#
#			3 - indicates a corrupt static file system
#			    table and/or corrupt file systems.  File
#			    systems that are corrupt should be fixed
#			    before continuing with the boot.
#
#			* - other un-expected errors.  Warn and continue
#			    boot'ing.
#
#		3) exit the function with the appropriate return status.
#
#
#		When the file systems are corrupt the following steps are
#		taken ...
#		
#			1) fsck_hfs(1M) command is invoked using the
#			following command line - "/sbin/fsck -F hfs -P -f"
#			(refer to the fsck_hfs(1M) man page)
#			
#			2) The following actions are taken according to
#			the appropriate return values ...
#				 0 - the corrupt file system was fixed.
#				     Continue with boot'ing.
#				 4 - the hfs root file system was fixed.
#				     The system will be rebooted.
#				 8 - could not automatically fix the
#				     corrupt file system, going into 
#				     manual mode (shell).
#				12 - fsck was interrupted, going into 
#				     manual mode (shell).
#				 * - other un-expected errors, going into
#				     manual mode (shell).
#


	/sbin/fsclean -v 
	clean_status=$?
	case $clean_status in
	0)
    		echo "HFS file systems are OK, not running fsck"
    		;;

	1|3)
		status=1
    		if [ $clean_status = 3 ]
    		then
			echo "\007fsclean RETURNED AN ERROR, CHECK /etc/fstab FILE"
			status=3
    		fi

    		echo "\007\007FILE SYSTEM(S) NOT PROPERLY SHUTDOWN, BEGINNING FILE SYSTEM REPAIR"

    		/sbin/fsck -F hfs -P -f	# check HFS filesystems only
    		case $? in
    		0)
			echo "FILE SYSTEM IS NOW FIXED"
			;;

    		4)
			echo "\007\n\n"
			echo "ROOT FILE SYSTEM MODIFIED"
			echo "\007\007REBOOTING SYSTEM TO UPDATE KERNEL DATA STRUCTURES"
			exec /sbin/reboot -n -q
			;;

    		8)
			echo "\007\n\n"
			echo "COULD NOT FIX FILE SYSTEM WITH fsck -P, RUN fsck INTERACTIVELY!"
			echo "LOGGING IN AS root FOR MANUAL fsck, ENTER ^D WHEN FILE SYSTEM FIXED"
			if [ $clean_status = 3 ]
			then
	    			echo "THIS COULD BE DUE TO A BAD /etc/fstab FILE"
	    			echo "IF SO, FIX /etc/fstab AND ENTER ^D WHEN FINISHED"
			fi

			PS1="(in bcheckrc)# "
			export PS1
			$ROOTSHELL
			echo "CONTINUING bcheckrc"
			;;

    		12)
			echo "\007\n\n"
			echo "FSCK INTERRUPTED"
			echo "LOGGING IN AS root FOR MANUAL fsck, ENTER ^D WHEN FILE SYSTEM FIXED"
			PS1="(in bcheckrc)# "
			export PS1
			$ROOTSHELL
			echo "CONTINUING bcheckrc"
			;;

    		*)
			echo "\007\n\n"
			echo "UNEXPECTED ERROR DURING fsck -P, RUN fsck INTERACTIVELY!"
			echo "LOGGING IN AS root FOR MANUAL fsck, ENTER ^D WHEN FILE SYSTEM FIXED"
			PS1="(in bcheckrc)# "
			export PS1
			$ROOTSHELL
			echo "CONTINUING bcheckrc"
			;;
		
    		esac
    		;;

	*)
    		echo "\007fsclean RETURNED AN ERROR, CONTINUING, BUT CHECK /etc/fstab FILE"
		status=2
	;;
	esac



#*********************************************************************
# End of bcheckrc (hfs)
#*********************************************************************
