#!/bin/sh
#
#    Copyright (c) 2003-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Set (and reset) env for faster boot
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/fabos/sbin:/fabos/bin

# swbd
#
# Retrieve the current system platform name, of the form "SWBDn", where n
# is cardinal number, assuming a sin/hinv input stream.
#
swbd() {
    /bin/sed -n -e 's/^.*\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp'
}

# Determine the system platform identifier.
SWBD=$(sin | swbd 2> /dev/null)

#
# main
#
case "$1" in

'fast')

	cur=`bootenv BootTest 2>/dev/null`
	if [ ! -z "${cur}" ] ; then
		# Save previous setting so we can restore later.
		/sbin/bootenv FastBootTest "${cur}" > /dev/null 2>&1
	        # Then delete.
		/sbin/bootenv -u BootTest > /dev/null 2>&1
	fi
	cur=`bootenv InitTest 2>/dev/null`
	if [ ! -z "${cur}" ] ; then
		# Save previous setting so we can restore later.
		/sbin/bootenv FastInitTest "${cur}" > /dev/null 2>&1
		/sbin/bootenv -u InitTest > /dev/null 2>&1
	fi
	al=`bootenv AutoLoadTimeout 2>/dev/null`
	if [ $? != 0 -o X${al} != X0 ]; then
		/sbin/bootenv AutoLoadTimeout 0
	fi

	# To reduce hareboot time, set bootdelay to 2. As of now do it for A-Wing.
	if [ "$SWBD" = "SWBD178" ]; then
		delay=`bootenv bootdelay 2>/dev/null`
		if [ $? != 0 -o X${delay} != X2 ]; then
			/sbin/bootenv bootdelay 2
		fi
	fi

	# quiet added to OSLoadOptions in kernel.spec.in. and here too
	OPT="$(echo $(cat /boot/extra_boot_opts 2>/dev/null) quiet)"
	bootenv OSLoadOptions "$OPT;$OPT"

	;;

'regular')

	# restore saved InitTest setting.
	cur=`bootenv FastInitTest 2>/dev/null`
	if [ -z "${cur}" ] ; then
		nv="MEM()"
	else
		nv=${cur}
	fi
	it=`bootenv InitTest 2>/dev/null`
	if { [ $? != 0 ]  || [ -z "${it}" ] ; }
	then
		/sbin/bootenv InitTest "${nv}"
	fi

	# Wipe setting after InitTest is restored.
	if [ ! -z "${cur}" ] ; then
		/sbin/bootenv -u FastInitTest > /dev/null 2>&1
	fi

	# restore saved BootTest setting.
	cur=`bootenv FastBootTest 2>/dev/null`
	# No default
	if [ ! -z "${cur}" ] ; then
		it=`bootenv BootTest 2>/dev/null`
		if { [ $? != 0 ]  || [ -z "${it}" ] ; }
		then
		    /sbin/bootenv BootTest "${cur}"
		fi

		# Wipe setting after InitTest is restored.
		/sbin/bootenv -u FastBootTest > /dev/null 2>&1
	fi

	# Do not enable PROM if FIPS requires it
	fips=$(cat /etc/fabos/pwron_fips_status 2>/dev/null)
	if [ -z "$fips" -o "$fips" = "0" ] ; then
		if bootenv AutoLoadTimeout >/dev/null 2>&1; then
			/sbin/bootenv -u AutoLoadTimeout > /dev/null 2>&1
		fi
	fi

	if [ ! -e /etc/fabos/lo/persistent ]
	then
		rm -f /var/log/fsslo.data
	fi

	;;

*)
	echo "Usage: $0 {fast | regular}"

	;;

esac
