# !/sbin/sh
#
# @(#) if_truncate $Revision: 1.2.98.2 $
#
# Returns 0 if the incoming kernel core (libhp-ux.a) is sufficiently different
#           from the last one installed to warrant forcing an update of all of
#           the optional kernel drivers by truncating the system file
#
# Returns 1 if the changes in the kernel are sufficiently small that old optional
#           drivers are still supported.
#
# Returns 2 if called incorrectly.
#
# Messages are printed to stderr in the event of a bad invocation or corrupted
# kernel release file.
#
#############
#
# Copyright 1994 by Hewlett Packard
#
##############

Usage="ERROR:   Usage: ${0##*/} [-r CurrentRelease] [-p pseudoroot] [ -d]
Where:  \"CurrentRelease\" is the release number in DD.dd format
	\"pseudoroot\" is the alternate root to search for /stand/kernrel in
        -d means \"Debug Mode\"."

#######################
#
# Function Definitions
#
#######################

# CheckKernRel()
#
# Echoes current release number to stdout and exits 0 on success.
# Assumes absence of file means 9.X. (HINT: only call when not doing
# a fresh install of the pseudo-root.)
#
CheckKernRel()
{
    [[ -n "$DEBUG" ]] && set -x

    if [[ ! -f ${RELFILE} ]]; then
	print "9.X"
    elif [[ -r ${RELFILE} ]]; then
	typeset -i length=$(grep -v '^#' ${RELFILE} | wc -l)
	if [[ $length -ne 1 ]]; then
	    print -u2 "WARNING: ${RELFILE} is corrupt. Unable to determine the"
	    print -u2 "         current release of the kernel software. Assuming 9.X"
	    print "9.X"
	    return 2
	fi
	grep -v '^#' ${RELFILE}
    else
	print -u2 "WARNING: ${RELFILE} is not readable. Unable to determine the"
	print -u2 "         current release of the kernel software. Assuming 9.x"
	print "9.X"
	return 2
    fi
    return 0
} # End of CheckKernRel

# Initialize variables.

PSEUDOROOT="/"		# Default value, adjustable via command line.
CurrentRelease=""	# Defaults to `uname -r`

typeset -i exit_status=0

# Process command line.

while getopts ":r:dp:" opt; do
    case $opt in
	r  ) CurrentRelease="$OPTARG" 
		;;
	d  ) DEBUG="y" 
		;;
	p  ) PSEUDOROOT="$OPTARG"
	     if [[ ! -d "$PSEUDOROOT" ]]; then
		print -u2 "ERROR:   ${PSEUDOROOT} is not a directory."
		print -u2 "$Usage"
		exit 2
	     fi
		;;
	\? ) print -u2 "$Usage"
	     exit 2
		;;
    esac
done

[[ -n "$DEBUG" ]] && set -x
shift $(( $OPTIND - 1 ))


RELFILE=${PSEUDOROOT%/}/stand/kernrel	# File containing the last installed release of the kernel

# Get Current Release if needed.

if [[ -z "$CurrentRelease" ]]; then
    CurrentRelease=$( uname -r )
    CurrentRelease=${CurrentRelease#*.}	# Strip off leading [A-Z].
else					# Verify format
    if [[ "$CurrentRelease" != @(+([0-9]).[0-9X][0-9a-zA-Z]) ]]; then
	print "ERROR:   \"$CurrentRelease\" is not a properly formatted release number."
	print "$Usage"
	exit 2
    fi
fi

# Get last release

OldRelease=$( CheckKernRel )
exit_status=$?		# Will either be 0 or 2

# Compare

if [[ ( ${CurrentRelease%.*} -gt ${OldRelease%.*} ) || ${OldRelease#*.} != +([0-9]) || \
      (( ${CurrentRelease%.*} -eq "10" && ${CurrentRelease#*.} -ge 10 ) && 
       ( ${CurrentRelease#*.} != ${OldRelease#*.} )) ]]
		# Change in major num, or alphabetic character in minor num (pre-release)
then 		# OR current release >= 10.10 and change in minor num
    exit 0		# Truncate
else
    if [[ $exit_status -eq 0 ]]; then
	exit 1	# Don't bother
    else
	exit $exit_status # Something seriously wrong here, let caller figure it out
    fi
fi

exit 2		# If you get here, something is very wrong.
