#! /sbin/sh
###############
# Product: OS-Core
# Fileset: KERN-RUN
# postinstall
# @(#) $Revision: 1.4.98.1 $
################
#
# (c) Copyright Hewlett-Packard Company, 1993
#
########################################################################
#                                                                      #
# General requirements:                                                #
#                                                                      #
########################################################################

# The global variables SUCCESS, FAILURE, WARNING, EXCLUDE, PATH, ROOT,
# SW_CTL_SCRIPT_NAME, _pf, PRODUCT, and FILESET are all set by control_utils.

# Variable declarations.

UTILS=/usr/lbin/sw/control_utils
if [[ ! -f $UTILS ]]
then
    print "ERROR:   Cannot find the sh functions library $UTILS"
    exit 1
fi
. $UTILS

exitval=$SUCCESS                # Anticipate success


if [[ -n "$KERN_DEBUG" ]]; then	# Set up debugging.
    PS4='    + ${0##*/}:$LINENO -> '
    set -x
    BOOTFUNC=/CTL_TEST/KERN-RUN/bootlif_funcs
else
    BOOTFUNC=${SW_CONTROL_DIRECTORY}/bootlif_funcs
fi

if [[ ! -f $BOOTFUNC ]]; then
    print "ERROR: Cannot find the sh functions library $BOOTFUNC."
    exit $FAILURE
fi

. $BOOTFUNC		# sets BOOTCONF & the ReadBoot exitvalues as side effect

#########################
#			#
# Function Declarations #
#			#
#########################

########
# sd_msg - stolen tt_tools function used to allow the shell library
#          bootlif_funcs to use the standard approved tt_tools output
#          format.
#
# Output a message in a format compatible with the SD guidelines.
# The output is written to standard out.
#
# Usage: sd_msg [-e|-p|-w|-b] word [...]
#        text | sd_msg [-e|-p|-w|-b]
#   
#   -e  prefix the message with "ERROR:  "
#   -p  prefix the message with "ERROR:  " (hard coded to SD compatibility.)
#   -w	prefix the message with "WARNING:" (hard coded to SD compatibility.)
#   -b  prefix the message with "        " (blanks)
#
#   otherwise the message is prefixed with "NOTE:" and is line wrapped if
#   it extends beyond column 72. Remember to escape special shell characters
#   in "word".
#
#   sd_msg will accept message text which is piped in also.
#
sd_msg ()
{
    typeset -L8 MSGTYPE="NOTE:"
    typeset -L8 BLANKS=""
    typeset MSG
    typeset word
    typeset len
    typeset LINE
    typeset MSG_TYPE

    case $1 in
	-w) MSGTYPE="WARNING:"
            shift
	    ;;
        -e) MSGTYPE="ERROR:"
            shift
	    ;;
        -p) MSGTYPE="ERROR:"
            shift
	    ;;
        -b) MSGTYPE=""
            shift
	    ;;
    esac
    
    MSG_TEXT=""
    MSG="${MSGTYPE}"

		#
		# The message text is on the command line.
		#
    if [[ $# > 0 ]]
    then
        MSG_TEXT=$@
		#
		# The message text is being piped in.
		#
    else
        while read -r LINE
        do
                MSG_TEXT="${MSG_TEXT} ${LINE}"
        done
    fi
    for word in $MSG_TEXT
    do
        (( len = ${#MSG} + ${#word} ))
        if (( len > 72 ))
        then
                print - "${MSG}"
                MSG="${BLANKS}"
        fi
        MSG="${MSG} ${word}"
    done

    print - "${MSG}"
}

################
#              #
# Main Program #
#              #
################


########################
#                      #
# Update the Boot Area #
#                      #
########################

#
# Find the boot area
#

BootAreas=`FindBoot $BOOTCONF $SW_ROOT_DIRECTORY`	# Function handles error messages
Result=$?

case $Result in 
    $FAILURE)		exit $FAILURE
			;;
    $ALT_ROOT_FAILURE)
			print "NOTE:    Installing to an alternate root that isn't a mount point"
			print "         ($SW_ROOT_DIRECTORY). The boot area(s) will not be updated."
			exit $SUCCESS
			;;
    $LVM_SUCCESS)	Flags="-l"		# "Flags" is passed to mkboot, "SaveFlags" to putconfig.
			SaveFlags="l"		# This is slightly different from the checkinstall script.
			exitval=$SUCCESS
			;;
    $WHOLE_SUCCESS)	Flags="-W -u"
			SaveFlags="w"
			exitval=$SUCCESS
			;;
    $PARTITION_SUCCESS) Flags="-H"
			SaveFlags="p"
			exitval=$SUCCESS
			;;
    $LVM_WARNING)	Flags="-l"
			SaveFlags="l"
			exitval=$WARNING
			;;
    $WHOLE_WARNING)	Flags="-W -u"
			SaveFlags="w"
			exitval=$WARNING
			;;
    $PARTITION_WARNING)	Flags="-H"
			SaveFlags="p"
			exitval=$WARNING
			;;
esac

if [[ -z "$BootAreas" ]]; then
    print "ERROR:   No updatable boot areas found on the the root disk."
    exit $FAILURE
fi

#
# Process each LIF in turn.
#

for item in $BootAreas; do

    #
    # Edit the AUTO file
    #

    AUTO=$(lifcp ${item%% }:AUTO -)
    Kernel=$(echo $AUTO | sed -e's;);) ;' | awk '{ if ( $NF ~ /^\// ) print $NF}')
    if [[ -n "$Kernel" ]]; then		# if the kernel path is explicitly set,
					#  modify it, otherwise ignore
	if [[ "$Kernel" = "/hp-ux" ]]; then
	    partition=$(echo $AUTO | sed -e's/.*;[ ]*//' | sed -e's/[ ]*).*//')

	    # for 9.x -> 10.x upgrade, 
	    # disk partitions 0 and 2 are swapped on 800's
	    if [[ `get_arch` = 800 ]]; then
		if [[ "$partition" = "0" ]]; then   
		    AUTO=$(echo $AUTO | sed -e's/;[ ]*0[ ]*)/;2)/')
			# above does not handle ;0,n) format, 
			# but this is not valid for disks
		fi
	    fi

	    AUTO=${AUTO%/hp-ux}/stand/vmunix
	elif [[ "$Kernel" != "/stand/vmunix" ]]; then
	    print "WARNING: The kernel in file $Kernel which used to be referenced"
	    print "         in the AUTO file of the boot area is no longer the default"
	    print "         kernel. The AUTO file now points to /stand/vmunix which is"
            print "         being built as a by-product of the kernel update."
	    AUTO=${AUTO%$Kernel}/stand/vmunix
	    if [[ $exitval -ne $FAILURE ]]; then
		exitval=$WARNING
	    fi
	fi
    fi

    #
    # If updating a 9.x 700 whole disk format, make sure that there is room.
    #

    if [[ "$SaveFlags" = "w" ]]; then
	mkboot -c -W -u $item >/dev/null 2>&1 
	if [[ $? -ne 0 ]]; then
	    print "ERROR:   There is not enough room to update the boot LIF on device"
	    print "         $item. Diagnostics from the mkboot command follows:"
	    mkboot -v -c -W -u $item 2>&1 | sed -e 's/^/         /'
	    exit $FAILURE
	fi
    fi

    #
    # Update the boot area.
    #

    mkboot $Flags -p LABEL $item 2>&1 > /tmp/krun.$$
    if [[ $? -ne 0 ]]; then
	print "ERROR:   Failed to update the boot area on device ${item}."
	print "         Diagnostics from mkboot follow:"
	sed -e 's/^/         /' /tmp/krun.$$
	rm -f /tmp/krun.$$
	exit $FAILURE
    fi

    # 
    # Update the AUTO file.
    #

    mkboot -a "$AUTO" $item 2>&1 > /tmp/krun.$$		# Might as well overwrite the same temp file.
    if [[ $? -ne 0 ]]; then
	TestAuto=$(lifcp ${item%% }:AUTO -)
	if [[ "${AUTO%% }" != "${TestAuto%% }" ]]; then
	    print "ERROR:   Failed to update the AUTO file in the boot area on"
	    print "         device ${item}. Diagnostics from mkboot follow:"
	    sed -e 's/^/         /' /tmp/krun.$$
	    rm -f /tmp/krun.$$
	    exit $FAILURE
	fi
    fi

    rm -f /tmp/krun.$$ 		# Temp file no longer needed: clean up.

    #
    # Verify (as far as practicable), the LIF area.
    #

    TestAuto=$(lifcp ${item%% }:AUTO -)
    if [[ "${AUTO%% }" != "${TestAuto%% }" ]]; then
        print "ERROR:   Failed to update the boot area on device ${item}."
        exit $FAILURE
    fi

done

#
# If all went well and there isn't a bootconf file, save the data.
#

if [[ $exitval -eq $SUCCESS && ! -f $BOOTCONF ]]; then
    PutConfig $BOOTCONF $SaveFlags $BootAreas
    if [[ $? -ne 0 ]]; then
	print "WARNING: Couldn't save boot device information to ${BOOTCONF}."
	exitval=$WARNING
    fi
fi

############
# Finished #
############

exit $exitval
