#! /sbin/sh
###############
# Product: OS-Core
# Fileset: CORE_KRN
# configure
# @(#) $Revision: 1.3.98.2 $
################
#
# (c) Copyright Hewlett-Packard Company, 1994
#
########################################################################
#                                                                      #
# General requirements:                                                #
#                                                                      #
# 1) When configuring kernels, call if_truncate to determine whether   #
#    the system file should be cleared and call truncate_sys as needed.#
#                                                                      #
# 2) Create /dev/config for autoconfig, if needed.         	       #
#                                                                      #
# 3) Adjust the tunable parameter DEFAULT_DISK_IR if on a 700 and      #
#    configuring kernels                                               #
#								       #
# 4) Update $RELFILE (/stand/kernrel) to have the current release num. #
#								       #
########################################################################

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

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

#
# Set global variables.
#

exitval=$SUCCESS                		# Anticipate success
VERBOSE=""					# Debugging flag

RELFILE=${SW_ROOT_DIRECTORY}stand/kernrel	# Location of the kernel release data
KERNEL_RELEASE="10.20"				# Current release to be 
						# configured

CONFIG=${SW_ROOT_DIRECTORY}dev/config	# /dev/config - used by S800 autoconfigurator

CORE_MASTER_FILE=${SW_ROOT_DIRECTORY}usr/conf/master.d/core-hpux
NEED_TRUNCATE=${SW_ROOT_DIRECTORY}usr/lbin/sw/if_truncate
TRUNCATE=${SW_ROOT_DIRECTORY}usr/lbin/sysadm/truncate_sys

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

# UpdateKernRel()
#
# Puts the current uname string in RELFILE.
#
UpdateKernRel()
{
    if [[ $# -lt 1 ]]; then
	RelBase=$(uname -r)
	RelBase=${RelBase#*.}
    else
	RelBase=$1
    fi
    rm -f ${RELFILE}
    typeset -i result=$?
    print "# File reserved for kernel configuration scripts." 2>/dev/null \
	> ${RELFILE}
    (( result = $result + $? ))
    print "# Do not alter or delete." 2>/dev/null >> ${RELFILE}
    (( result = $result + $? ))
    print $RelBase 2>/dev/null >> ${RELFILE}
    (( result = $result + $? ))
    if [[ $result -gt 0 ]]; then
	return 1
    else
	chmod -w ${RELFILE}
	return 0
    fi
} # end UpdateKernRel

######################################################
# Only modify the system file if this is a diskless  #
# client installation, otherwise postinstall already #
# handled it.					     #
######################################################

if [[ -n "${SW_DEFERRED_KERNBLD}" ]]
then
    
    
    ######################################################
    #							 #
    # 1) Call truncate_sys to preprocess the system file #
    #							 #
    ######################################################

    ${NEED_TRUNCATE} -r ${KERNEL_RELEASE} -p ${SW_ROOT_DIRECTORY} # exits 0 for truncate, 1 for don't truncate, 2 for error
    result=$?
    if [[ $result -eq 0 ]]; then
	${TRUNCATE} -m ${CORE_MASTER_FILE} -s ${SW_SYSTEM_FILE_PATH}	# truncate_sys handles messages.
	[[ $? -ne 0 ]] && exitval=$FAILURE
    elif [[ $result -eq 2 ]]; then
	exitval=$FAILURE	# if_truncate handles messages.
    fi

    #########################################
    #                                       #
    # 2) Create /dev/config for autoconfig. #
    #                                       #
    #########################################

    if [[ ! -c ${CONFIG} ]]   # Does config file already exist ?
    then
	if [[ -n "${VERBOSE}" ]]
	then
	    print "NOTE:    Creating ${CONFIG}."
	fi

	rm -f $CONFIG # Just in case something other than a character device is here.
	mknod ${CONFIG} c 69 0
	chown root ${CONFIG}
	chgrp sys  ${CONFIG}
	chmod 644  ${CONFIG}    # rw-r--r--
    fi # [[ ! -c ${CONFIG} ]]

    ##########################################
    #					     #
    # 3) Set DEFAULT_DISK_IR to ON for S700. #
    #					     #
    ##########################################

    if [[ $( get_arch ) = "700" ]]; then
	value="$( query_systemfile ${SW_SYSTEM_FILE_PATH} "default_disk_ir" )"
	if [[ $? -ne 0 ]]; then
	    [[ $exitval -ne $FAILURE ]] && exitval=$WARNING	# Only fails if sys file unreadable - function handles messages.
	else
	    mod_systemfile ${SW_SYSTEM_FILE_PATH} -d DEFAULT_DISK_IR
	    if [[ -z "$value" ]]; then					# Don't reset if there currently is a value.
		mod_systemfile ${SW_SYSTEM_FILE_PATH} -t "default_disk_ir" 1
		if [[ $? -ne 0 ]]; then
		    print "WARNING: The tunable parameter \"default_disk_ir\" has been left OFF. This may"
		    print "         result in lower performance for some SCSI devices."
		    [[ $exitval -ne $FAILURE ]] && exitval=$WARNING
		fi # [[ $? -ne 0 ]]
	    fi # [[ -z "$value" ]]
	fi # [[ $? -ne 0 ]]
    fi # [[ "$(get_arch)" = "700" ]]

fi	# end of "if [[ -z ${SW_DEFERRED_KERNBLD}" ]]

#############################################
#					    #
# 4) Update $RELFILE to the current release #
#					    #
#############################################

UpdateKernRel $KERNEL_RELEASE
if [[ $? -ne 0 ]]; then
    print "ERROR:   Unable to update the file \"${RELFILE}\" with the current release"
    print "         number (${KERNEL_RELEASE}). This would result in errors when you upgrade"
    print "         your system to the next major release. Please verify that the file"
    print "         \"${RELFILE}\" is writable and/or createable and run the command"
    print "         \"swconfig -v OS-Core.CORE-KRN\" to finish your installation."
    print "         (It should be unnecessary to reinstall this fileset.)"
    exitval=$EXCLUDE
fi

#############
# Finished. #
#############

exit $exitval
