#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 19.1 $ generated on Mon Jul 23 13:20:52 PDT 2001
#
# (c)Copyright 1983-2001 Hewlett-Packard Co.,  All Rights Reserved.
#
#------------------------------------------------------------------#
# SD postinstall script template for 11.X patches.                 #
#------------------------------------------------------------------#
# This file is optional for all 11.X patches.  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_24729

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

_BASE_PATCHID=PHKL_18543

# _OVERLAP_FILES contains all files that are in both the incremental
# patch and the base patch, with the exception of the object files, as
# these are not tracked in the IPD. 
#
# Each line contains a fileset name followed by overlap files in this
# fileset. There may be more than one line per fileset. Each line must
# be shorter (in characters) than the OS MAX_LINE limit.
#
# Each line looks like:
#   Fileset file1 file2 file3...
#
# When we install/remove an incremental patch, the IPD for the
# overlapping files in the basepatch is incorrect. This can cause
# swverify errors.  To avoid the unpleasant swverify errors, we fix
# the IPD for the overlapped files.
#
_OVERLAP_FILES=""


echo "$_OVERLAP_FILES" | 
 while read Fset FilesInFset ; do
  # Fset is the fileset for this line of $_OVERLAP_FILES  

  # FilesInFset is a space delimited list of overlapped files in
  # the fileset we are processing.

  PFset=$_BASE_PATCHID.$Fset

  # First we check if this fileset is currently installed in the
  # system. If it is not, we don't have to do anything for any of it's
  # files.
  pstate=$(get_install_state $PFset)
  if [ "$pstate" = configured -o "$pstate" = installed ]
  then
    swmodify -x files="$FilesInFset" $PFset
    if [ $? -ne 0 ] ; then
	# swmodify has failed. Let's try the list of files one by one
	# to isolate the failure (slow, but at least we might reduce
	# the problems for the users.)
	FailedFiles=""
	for _File in $FilesInFset ; do	
	    swmodify -x files="${_File}" $PFset
	    if [ $? -ne 0 ] ; then
		FailedFiles="${_File} ${FailedFiles}"
	    fi
	done
	if [ -n "$FailedFiles" ] ; then
	    print -u2 "WARNING: Failed to update IPD for the files:"
	    print -u2 "         ${FailedFiles}"
	    print -u2 "         This will cause swverify errors on patch fileset:"
	    print -u2 "         $PFset"
	    print -u2 "         To fix these errors, please run the following command:"
	    print -u2 "         swmodify -x files=\"${FailedFiles}\" $PFset"
	    FailedFiles="";
            exitval=2
	fi  
    fi
  fi

done


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

master_1020="/usr/conf/master.d/ccio2"
driver_sba="sba"
driver_ccio="ccio2"
device="ccio2"

########
# FUNCTIONS
########
# AddDriverEntry
#
# input: $1 - (string) the driver
#
# Purpose:  To add to 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

########
# DeleteDriverEntry
#
# input: $1 - (string) the driver
#
# Purpose: To delete from the driver delivered by this patch from the 
#          system file.  It is OK if the driver does not get removed,
#          but it is cleaner if it is.
#
DeleteDriverEntry()
{
   mod_systemfile ${SW_SYSTEM_FILE_PATH} -d $1
   if [[ $? -ne $SUCCESS ]]
   then
       print "WARNING: The attempt to remove driver \"$1\" from the system" 
       print "         file has failed for the above reason.  This will cause a"
       print "         WARNING during kernel build."
       exitval=$WARNING
   fi
} #DeleteDriverEntry()


#########################################################################
#########################################################################
# 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 
# difficulty 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 ]] && ( /sbin/ioscan -f | grep -q $device )
then
    AddDriverEntry $driver_sba
    DeleteDriverEntry $driver_ccio
    [[ -e $master_1020 ]] && ( $RM $master_1020 )
fi   #if ioscan

exit $exitval
