#!/sbin/sh
###############
# Product: PHKL_18543 (big bubble)
# Fileset: PHKL_18543
# postinstall
# @(#) postinstall $Date: 1999/06/04 11:33:13 $Revision: r11ros/cup_ros_ep3_pb/5 PATCH_11.00 (PHKL_18543)
################
#
# (c) Copyright Hewlett-Packard Company, 1998,1999
#############################################################
# The purpose of this 
# script is to prepare for a kernel build when required by the     
# install conditions.  The script can also drive events that must 
# occur before a system reboot occurs.                           
#------------------------------------------------------------------

_PATCHID=PHKL_18543

UTILS="/usr/lbin/sw/control_utils"

if [ ! -f $UTILS ]; then
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi

. $UTILS
exitval=$SUCCESS

#####
##### Insert any code you may require at this point in the script.
##### Pay attention to the exitval variable and set appropriately
##### in your code.  If you are unsure as to what types of operations
##### are legal in this file and what types aren't, consult the 
##### "Guidelines for SD Control Scripts" document.
#####
##### START_CUSTOMIZATION_HERE

SED=sed
MV="mv -f"
RM="rm -f"
GREP=grep

########
# FUNCTIONS
########
# AddDriverEntry
#
#   Purpose:  To add from the system file a driver that this fileset has 
#	delivered Set the $exitval value based on the return value from 
#	mod_systemfile().  If multiple additions, ensure that a FAILURE 
#	value of $exitval is not reduced by a later SUCCESS return.
#
AddDriverEntry()
{
     mod_systemfile $SW_SYSTEM_FILE_PATH -a $1
     if [[ $? -ne 0 ]]
       then
         print  "ERROR:   Cannot update required $1 functionality ($FILESET)."
         exitval=$FAILURE
     fi
} #END AddDriverEntry

########
# disable_it  type
#
# Modify master file line which pertains to AdvJournalFS ( vxadv) to be only
#  a comment.  Then print mesg to customer of this fact.  Finally, set return
#  for script to WARNING while preserving any worse return states.
#
#  type- this is WARNING: or NOTE:
#
#  Uses $master to find the file.  Assumes it exists.
#
#
disable_it ()
{
    if [ "$1" = WARNING: ]
        then
        word=After
    else
        word=If
    fi # [ "$1" = WARNING: ]
    $MV $master /var/tmp/master$PPID
    $SED  's/^vxadv/* Disabling AdvJournalFS: vxadv/' /var/tmp/master$PPID > $master
    print "$1 \"AdvJournalFS\" product is now disabled.  Any vxadv driver in the"
    print "         system file will be ignored.  $word you install the 11.00 version"
    print "         of \"AdvJournalFS\", you must again install this patch, $_PATCHID."
    print "         (The reinstall* options must be set to false - the default.)"
    if [ $exitval != $FAILURE -a "$1" = WARNING: ]
    then
        exitval=$WARNING
    fi
    chmog 444 bin bin $master
    # the IPD is fixed up in the configure script, since then all commands
    # are available
    $RM /var/tmp/master$PPID
} # end disable_it

########
# enable_it
#
# Modify master file line which pertains to AdvJournalFS ( vxadv) to no longer
#  be a comment.  Then print mesg to customer of this fact. 
#
#  Uses $master to find the file.  Assumes it exists.
#
#
enable_it ()
{
    $MV $master /var/tmp/master$PPID
    
    $SED  's/^* Disabling AdvJournalFS: vxadv/vxadv/' /var/tmp/master$PPID > $master
    print "NOTE:    \"AdvJournalFS\" product is now enabled.  Any vxadv driver in the"
    print "         system file will be loaded."
    chmog 444 bin bin $master
    # the IPD is fixed up in the configure script, since then all commands
    # are available
    $RM /var/tmp/master$PPID
} # end enable_it

########
# get_revision_state
#
# Print a word that corresponds to a given fileset's install state.
#       $1      :  Software to test:  PRODUCT.FILESET
#       revision:  OS release (i.e. B.10.20, B.11.00)
#       state   :  installed or configured
#
# This function is essentially the get_install_state function from
# control_utils.  However, that function does not get revision data.
#
get_revision_state ()
{
    typeset prod_fs=${1%%,*}
    typeset revision=""
    typeset state=""


    # SD's debugging output messes up parsing, so unset it for this function.
    if [[ -n $SDU_DEBUG ]]
    then
        typeset SDU_DEBUG_SAVED=$SDU_DEBUG
        unset SDU_DEBUG
    fi

    set -- $(/usr/sbin/swlist -l fileset -a state -a revision $prod_fs 2>/dev/null | $SED '/^#/d')
    while [[ $1 != $prod_fs && $# -gt 1 ]]
    do
        shift
    done

    if [[ $1 = $prod_fs ]]
    then
        revision=$2
        state=$3
    fi

    echo "$revision $state"

    if [[ -n $SDU_DEBUG_SAVED ]]
    then
        SDU_DEBUG=$SDU_DEBUG_SAVED
    fi
} # end get_revision_state

#########################################################################
# This script part is to replace the whole custom part of the script with
# another patch if needed.  At initial patch release time there is no 
# basepatch_utils file
#

BASEPATCH_UTILS="$SW_ROOT_DIRECTORY/usr/lbin/patch/basepatch_utils"

if [ -f $BASEPATCH_UTILS ] 
then
    # replace with a fixed version if needed
    print -u2 "NOTE:    $BASEPATCH_UTILS installed."
    print -u2 "         Using control script functions from this file."
    . $BASEPATCH_UTILS
    postinstall_cntl || exitval=$? # retain a non-zero return value
    exit $exitval
fi # [ -f $BASEPATCH_UTILS ]

#########################################################################
# This script part is intended to insert post 11.00 drivers that are needed
# by the kernel when new driver patches are installed and certain hardware 
# is present.
#
# This addition must be done at the patch level since it is fixing a 
# problem caused by the early truncate_sys call from the control scripts
# in the CORE-KRN fileset, but depends on matching the new drivers defined 
# in master.d/core-hpux which is delivered in KERN2-RUN.
#########################################################################

if [[ -x /sbin/ioscan ]]
  # determine hardware present on the machine
  then
    scanout=$(/sbin/ioscan -f)

    for word in $scanout; do
        case $word in
        PCI)    if [[ -z $diag1done  ]]; then
                # ###### PCI diag1 driver new for all machines ##### 
                AddDriverEntry diag1
                diag1done="yes"
            fi
            ;;
        saga)   if [[ -z $sagadone  ]]; then
                # ###### saga driver new for Vclass ##### 
                AddDriverEntry saga
                sagadone="yes"
            fi
            ;;
        @((ike)|(sba)))    if [[ -z $sbadone  ]]; then
                # ###### sba driver new for Prelude ##### 
                AddDriverEntry sba
                sbadone="yes"
            fi
            ;;
        @((ROPEStoPCI)|(lba)))    if [[ -z $lbadone  ]]; then
                # ###### lba and sapic driver new for Prelude ##### 
                AddDriverEntry lba
                AddDriverEntry sapic
                lbadone="yes"
            fi
            ;;
    
        esac
    done

else
   print  "ERROR:   Cannot determine if new drivers are needed due to hardware present."
   exitval=$FAILURE
fi # if ioscan

#########################################################################
# This part of the script is intended to disable the building of the Adv 
# JFS .o's into the kernel if mixed patched/unpatched .o's are there.
# This is done by modified the BASE JFS master file (master.d/vxfs) for 
# included the ADV JFS (xvfs) entry appropriately.
#
#########################################################################

master=$SW_ROOT_DIRECTORY/usr/conf/master.d/vxfs
if [ -f $master ]
then
  
    disabled=`$GREP '* Disabling AdvJournalFS: ' $master`
    Fileset="AdvJournalFS.VXFS-ADV-KRN"

    is_software_selected $Fileset; fselected=$?
    if [[ $fselected -eq 0 ]]
    then
        # if AdvJournalFS is to be installed (eg selected), remove any comment
        # in the master file which disables the definition of the driver(vxadv)
        if [[ -n  $disabled ]]
        then
            enable_it
        fi 
    else
        # AdvJournalFS is not be installed 
        get_revision_state $Fileset | read frevision fstate
        if [ "$fstate" = configured -o "$fstate" = installed ]
        then
            # At this point, we believe the AdvJournalFS is installed.
            if [[ $frevision = "B.11.00" ]]
            then
                # A matching BASE and AdvJournalFS is installed on the system
                # modify the master file back to define the AdvJournalFS 
                # driver (vxadv) to allow a kernel built with vxadv
                if [[ -n  $disabled ]]
                then
                    enable_it
                fi 
            else
                # The JFS BASE (11.patched) and AdvJournalFS do not match.
                # revise the master file to disallow a kernel built with vxadv
                if [[ -z $disabled ]]
                then
                    disable_it "WARNING:"
                fi
            fi # [[ $frevision = "B.11.00" ]]
        else
            # no ADV JFS fileset is around
            # revise  the master file to disallow a kernel built with vxadv
            # this is in case the customer later adds AdvJournalFS, since it
            # does not check if the BASE JFS is a match
            if [[ -z $disabled ]]
            then
                disable_it "NOTE:   "
            fi
        fi #[ "$fstate" = configured -o "$fstate" = ...

    fi # $fselected -eq 0
fi # [ -f $master ]

exit $exitval
