#!/sbin/sh
# @(#) $Revision: 78.1 $
#
##################################################################
#
# Name :
#	bcheckrc - /sbin/bcheckrc
#
# Description
# 	This file contains scripts/commands necessary to -
#		1)  activates LVM (if appplicable).
#		2)  runs eisa_config in automatic mode
#		    (if applicable).
#		3)  checks the file systems before mounting.
#		    (the scripts required for file system specific
#		     checking reside in /sbin/fs/<fstype>/bcheckrc)
#		4)  and anything else that should be done before
#		    mounting any file systems.
#
# 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) file system specific bcheckrc scripts located in
#	    the file system specific directory (/sbin/fs/<fstype>/)
#	    are also used by 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
#
##################################################################
#


#
# Set trap for an interrupt signal
#

trap "" 2


#
# Assign and Display script's name
#

CMDNAME="bcheckrc"		# used in the script, later on.

echo "/sbin/bcheckrc:"


#*********************************************************************
# Activate LVM volume groups.
# Each logical volume will be activated according to its mirror
# consistency recovery policy.
#*********************************************************************

if [ -x /sbin/lvmrc ]
then
	echo "Checking for LVM volume groups and Activating (if any exist)"
	/sbin/lvmrc
fi


#*********************************************************************
# Run eisa_config in automatic mode (if appropriate).
# First figure out if we should run eisa_config. The possible cases are: 
#   o diskless client: root file system will be clean, run eisa_config
#   o local root disk, root file system clean: run eisa_config
#   o local root disk, root file system inconsistent: 
#       don't run eisa_config - config files may be corupt/absent 
#       and running eisa_config could cause them to be changed.
#       Instead we will rely on the fsck of the root disk later in this 
#       script to force a reboot, after which eisa_config will be run 
#*********************************************************************

if is_local_root          
then						# root is a local mount
	case `/sbin/fstyp /dev/root` in
		"hfs")	/sbin/fsclean /dev/rroot
			status=$?
			;;
		"vxfs")	/sbin/fsck -F vxfs -m /dev/rroot
			status=$?
			;;
		*)	status=0
			;;
	esac
else						# root is a diskless client
	status=0
fi

if  [ $status = 0 ]
then

	# Run the eisa_config program if it is present
	if [ -x /sbin/eisa_config ]
	then
	    /sbin/eisa_config -a
	    # Now handle the return code from eisa_config. Most codes mean
	    # go on with the boot. A few of the codes, however, mean reboot
	    # the system or halt the system. All error messages are
	    # displayed by the eisa_config program.
	    case $? in
		1 | 10)		/sbin/reboot ;;
		11 | 12)	/sbin/reboot -h ;;
	    esac
	else
	    echo "eisa_config is not present -- cannot check eisa configuration"
	fi

fi


#*********************************************************************
# Load the appropriate keymap.  If the interface_type is HIL, query the 
# keyboard for its language.  If the interface type is PS2, install 
# the mapping specified by "-l <map_name>".
#*********************************************************************

itemap_option=""
if [ -f /etc/kbdlang ]
then
	read MAP_NAME filler < /etc/kbdlang
	if [ $MAP_NAME ]
	then
		itemap_option="-l $MAP_NAME"
	fi
fi

if [ -x /sbin/itemap ] && [ -f /etc/X11/XHPKeymaps ]
then
	/sbin/itemap -i -L $itemap_option -w /etc/kbdlang
fi


#*********************************************************************
# Check and clean file systems
#*********************************************************************

#
# Check and clean file systems, if necessary.  Only clean file systems
# can be mounted.  
#

#
# Check for clean file systems - STEP #1 - determine the different types
# of file systems supported.
#

#
#	DEFAULTLIST - a last resort list of file system types. If
#		      everything fails this list will be used. This
#		      list contains "hfs", plus any other file system
#		      available as default.  With Release 10.Davis
#		      vxfs is also supported as a root filesystem.
#

DEFAULTLIST="hfs vxfs"

#
#	IGNORELIST  - a list of values in the fstab's file system type
#		      field that need be ignored.  Example - swap.
#

IGNORELIST="swap swapfs ignore"

#
#	FSTAB	    - the static file system table name, /etc/fstab.
#

FSTAB="/etc/fstab"

#
#	FSLOCATION  - is "/sbin/fs".
#	FSCK	    - is "fsck"
#		      The variables FSLOCATION and FSCK combined
#		      together, with the file system type, will 
#		      yield the location of the file system
#		      specific fsck(1M) command.
#
#		      It is also used to determine file system
#		      specific bcheckrc's location, later.
#

FSLOCATION="/sbin/fs"
FSCK="fsck"

#
#	FSTYP	    - a command that determine, the kernel configured,
#		      file system types.
#

FSTYP="/sbin/fstyp -l"

#
#	CONFIGLIST  - a list of configured file system types. 
#		      Determined from FSTYP's exceution.  The 
#		      default is the DEFAULTLIST.
#

CONFIGLIST=`$FSTYP 2>/dev/null`
if [ $? -ne 0 ]
then
        CONFIGLIST=${CONFIGLIST:-$DEFAULTLIST}
fi

#
#	FSTABLIST   - a list of all the different file system types
#		      present in the static file system table, FSTAB.
#		      The list is determined by the an awk of the 
#		      static file system table, FSTAB.  The default
#		      is the CONFIGLIST.  FSTABLIST does not contain
#		      the tokens present in the IGNORELIST.
#

FSTABLIST=`cat $FSTAB | awk '{ if (($0 != "" ) && ($1 != "#"))
                        TMPLIST[$3]=$3;
		} END {
                        for (i in TMPLIST)
                                printf("%s ",i);
                        printf("\n");
                }'`

for i in $FSTABLIST
do
        IGNOREFLAG=""

        for j in $IGNORELIST
        do
                if [ "$i" = "$j" ]
                then
                        IGNOREFLAG="TRUE"
                fi
        done

        if [ "$IGNOREFLAG" != "TRUE" ]
        then
                TMPFSTABLIST="$TMPFSTABLIST $i"
        fi
done

FSTABLIST="$TMPFSTABLIST"

FSTABLIST=${FSTABLIST:-$CONFIGLIST}


#
#	 FSTYPELIST - a list of all possible file systems types that
#		      have a file system specific fsck(1M) available.
#		      The list is generated from the FSTABLIST and
#		      a stat on $FSLOCATION/"TYPE"/$FSCK.
#

for i in $FSTABLIST
do
        if [ -f $FSLOCATION/$i/$FSCK ]
        then
                FSTYPLIST="$FSTYPLIST $i"
        fi
done


#
# Check for clean file systems - STEP #2 -  Run fsck(1M) on each file
# system type.
#

#
#	trap Interrupt
#

trap "echo Interrupt" 2


#
#	initialize status 
#
#	Note - do not initialize again in file system specific
#	bcheckrc
#

status=0

for fstype in $FSTYPLIST
do
	if [ -f $FSLOCATION/$fstype/$CMDNAME ]
	then
		echo "Checking $fstype file systems"
		. $FSLOCATION/$fstype/$CMDNAME
	else
		echo "cannot find " $FSLOCATION/$fstype/$CMDNAME
		status=1
	fi
done

echo 			# console readability during bootup

exit $status


#*********************************************************************
# End of bcheckrc
#*********************************************************************
