#!/sbin/sh
#------------------------------------------------------------------#
#
# (c)Copyright 1983-1999 Hewlett-Packard Co.,  All Rights Reserved.
#
#------------------------------------------------------------------#

PATH="${SW_PATH%:}:/usr/sbin:/usr/lbin/sw"

set -a		# Export all vars
exitval=0	# Anticipate success :  1 = failure  2 = warning

FAILURE=1

if [ -z "${SW_ROOT_DIRECTORY}" ]; then
	SW_ROOT_DIRECTORY=/
fi

#
# Make sure files delivered by the patch are in place
#

if [[ ! -f ${SW_ROOT_DIRECTORY}usr/conf/reflash ]]; then
	echo "ERROR:   Cannot find the utility: /usr/conf/reflash"
	exit $FAILURE
fi

if [[ ! -f ${SW_ROOT_DIRECTORY}usr/conf/pdc.wrap ]]; then
	echo "ERROR:   Cannot find the utility: /usr/conf/pdc.wrap"
	exit $FAILURE
fi

if [[ ! -f ${SW_ROOT_DIRECTORY}usr/conf/modelstr ]]; then
	echo "ERROR:   Cannot find the utility: /usr/conf/modelstr"
	exit $FAILURE
fi

#
# Use the delivered "modelstr" command to find the system model number
#

MODEL=`${SW_ROOT_DIRECTORY}usr/conf/modelstr`

#
# Only setup firmware reflash for supported models
#

case "$MODEL" in
9000/785/B1000|\
9000/785/B2000|\
9000/785/C3000|\
9000/785/C3600|\
9000/785/J5000|\
9000/785/J5600|\
9000/785/J6000|\
9000/785/J7000)

	#
	# Make sure we have the utilties we need.
	#

	LIFCP=`whence lifcp`
	if [[ $? -ne 0 ]]; then
		echo "ERROR:   Cannot find the utility: lifcp"
		exit $FAILURE
	fi

	MKBOOT=`whence mkboot`
	if [[ $? -ne 0 ]]; then
		echo "ERROR:   Cannot find the utility: mkboot"
		exit $FAILURE
	fi

	#
	# Copy the reflash utility and firmware image to /stand
	#

	REFLASH=${SW_ROOT_DIRECTORY}usr/conf/reflash
	ROMIMAGE=${SW_ROOT_DIRECTORY}usr/conf/pdc.wrap

	cp ${REFLASH} ${SW_ROOT_DIRECTORY}stand/reflash
	if [[ $? -ne 0 ]]; then
		echo "ERROR:   Cannot copy reflash to /stand"
		exit $FAILURE
	fi

	cp ${ROMIMAGE} ${SW_ROOT_DIRECTORY}stand/pdc.wrap
	if [[ $? -ne 0 ]]; then
		echo "ERROR:   Cannot copy pdc.wrap to /stand"
		exit $FAILURE
	fi

	#
	# Search the bootconf file for boot disk entries.
	# The entries should be specified by a l, p or w and be
	# followed by a space and the device file path.
	# e.g.
	# l /dev/dsk/c0t6d0
	# p /dev/dsk/c0t6d0
	# w /dev/dsk/c0t6d0
	#
	# All other entries are ignored.

	BOOTCONF=${SW_ROOT_DIRECTORY}stand/bootconf

	if [ ! -f $BOOTCONF ]; then
		echo "ERROR:   Cannot find the file $BOOTCONF."
		exit $FAILURE
	fi

	exec 9<${BOOTCONF}

	BOOTDEV=""

	while ( true ); do
		read -u9 OPERATION BOOTDEV JUNK
		READ_STATUS=$?

		if [[ ${READ_STATUS} != 0 ]]; then
			break
		fi

		case ${OPERATION} in
			l|p|w)
				break
				;;
			*)
				;;
		esac
	done

	if [[ -z "$BOOTDEV" ]]; then
		echo "ERROR:   Cannot find the boot disk."
		exit $FAILURE
	fi

	#
	# Modify the AUTO file in the LIF image of the boot disk
	# to execute "reflash" on the next boot
	#

	TMP_FILE1=/tmp/autoA$$

	rm -f ${TMP_FILE1}

	${LIFCP} -r ${BOOTDEV}:AUTO ${TMP_FILE1}
	if [[ $? -ne 0 ]]; then
		echo "ERROR:   copy of old ${BOOTDEV}:AUTO failed"
		rm -f ${TMP_FILE1}
		exit $FAILURE
	fi

	${MKBOOT} -a "`(echo hpux reflash; sed -n 'p' ${TMP_FILE1})`" ${BOOTDEV}
	if [[ $? -ne 0 ]]; then
		echo "ERROR:   creation of new ${BOOTDEV}:AUTO failed"
		rm -f ${TMP_FILE1}
		exit $FAILURE
	fi

	rm -f ${TMP_FILE1}

	#
	# Now create the rc script to remove "reflash" and "pdc.wrap"
	# from /stand
	#

	rm -f /sbin/init.d/reflash /sbin/rc1.d/S000reflash

	REFLASH="/sbin/init.d/reflash"

	echo '#!/sbin/sh' > $REFLASH
	echo '' >> $REFLASH
	echo 'PATH=/usr/lbin/sw/bin:/sbin:/usr/bin:/usr/sbin' >> $REFLASH
	echo 'export PATH' >> $REFLASH
	echo '' >> $REFLASH
	echo 'case $1 in' >> $REFLASH
	echo 'start_msg)' >> $REFLASH
	echo '	echo "Remove reflash utilities"' >> $REFLASH
	echo '	;;' >> $REFLASH
	echo '' >> $REFLASH
	echo '"start")' >> $REFLASH
	echo '	rm -f /stand/reflash /stand/pdc.wrap' >> $REFLASH
	echo '	rm -f /sbin/init.d/reflash /sbin/rc1.d/S000reflash' >> $REFLASH
	echo '	;;' >> $REFLASH
	echo 'esac' >> $REFLASH
	echo '' >> $REFLASH
	echo 'exit 0' >> $REFLASH

	chmod 555 /sbin/init.d/reflash

	ln -s /sbin/init.d/reflash /sbin/rc1.d/S000reflash
	;;

*)
	;;
esac

exit $exitval
