#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 18.3 $ generated on Fri Jul 20 16:06:41 EDT 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=PHNE_24413

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

# SDautoinsert -- AUTO GENERATED CODE -- DO NOT EDIT HERE
# This, through to the __EOF__SDbuilder line, is generated
# code for patching product SD scripts.  Do not edit this
# code here, because any edits will be lost the next time
# the generation is run.  Any edits need to be made to the
# original components from which this is assembled.
# Generated at hpisrph:/u/laverne/Work/SDstuff/third_version
#################################################################
# This code would be inserted if the include file had been
# defined and installed in its place.  Since it isn't, the
# file itself is here.
#	if [ ! -f $UTILS.SDfix ]; then
#	    echo "ERROR:   Cannot find $UTILS.SDfix"
#	    exit 1
#	fi
#	
#	. $UTILS.SDfix
#################################################################
#########################################################################
#									#
#	These are functions to patch Software Distribution Files	#
#									#
#	The patch mechanism does not support operations on these	#
#	files, so this has to work around the normal SD operations.	#
#									#
#	This version DOES NOT support ADD or DELETE of files,		#
#				only changes				#
#									#
#########################################################################
#
# Must be defined before these functions are invoked:
#	_PATCHID	the patch number of this patch
#
# Defined constants/literals:
swprod_dir=/var/adm/sw/products
swsave_dir=/var/adm/sw/save
swinfo=INFO
swproduct=""
swfileset=""
swcontrol=""
swleave=2
#
# Define patch related information, determine patch INFO mtime
swourself=$swprod_dir/$_PATCHID/pfiles/$swinfo
swoursave=$swsave_dir/$_PATCHID
if ${swpatch_there:-true}
then
	swmtime=`awk -v INFO=$swinfo '
		/^control_file/	{cf = 1; info = 0}
		/^file/		{cf = 0; info = 0}
		/^path/		{
			if( (cf == 1) && ($2 == INFO) )
				{cf = 0; info = 1}
			else
				{cf = 0; info = 0}
		}
		/^mtime/	{
			if( info == 1 )
				{print $2; exit}
		}
		/^tag/		{cf = 0; info = 0}
		' $swourself`
	if [[ -z "$swmtime" ]]
	then
		echo "ERROR:   Cannot find necessary data for SD work for patch"
		echo "         $_PATCHID.  No mtime of patch $swinfo file found."
		exit 1
	fi
fi
#

# Internal function -- extract directory for Product, given Filset and File
# globals in:	swproduct
#	swextract Fileset_Name Control_File_Name
function swextract {
	swlist -v $swproduct.$1 | awk -v swc="$2" '
		/^control_file/	{
			nf = split($2, f, "/")
			if( f[nf] == swc )
				{print $2; exit}
		}
		'
}

# Internal function -- locate files for Product, given Filset and File
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
# globals out:	swdirectory swgood swfileset swcontrol
#		thisfile thisINFO savethis saveINFO tempINFO
#	swlocate Fileset_Name Control_File_Name
function swlocate {
	if [[ -z "$swfileset" ]]
	then
		# Locate our Product directory for changes
		swd=`swextract $1 $2`
		if [[ -z "$swd" ]]
		then
			for swp in `ls -d $swprod_dir/$swproduct*/$1`
			do
				mv $swp/$swinfo $swp/$swinfo.$$

				cat <<- "__EOF__" > $swp/$swinfo
				control_file
				path dummy
				tag dummy
				__EOF__

				cat $swp/$swinfo.$$ >> $swp/$swinfo

				swd=`swextract $1 dummy`
				mv $swp/$swinfo.$$ $swp/$swinfo
				if [[ ! -z "$swd" ]]
				then
					break
				fi
			done
		fi
		swdirectory=`echo $swd | awk -v swl="$swleave" '
			{
				for( x = 1; x <= NF; x++ )
				{
					nf = split($x, f, "/")
					fs = ""
					# f[1] null, must be absolute filename
					for( i = 2; i <= nf - swl; i++ )
					{
						if( f[i] != "" )
						{
							fs = fs "/" f[i]
						}
					}
					if( fs > swd )
					{
						swd = fs
					}
				}
				print swd
			}
			'`
		#
		# Was everything found okay?
		if [[ -z "$swdirectory" || ! -d $swdirectory ]]
		then
			echo "WARNING:  Cannot find necessary directory for SD work to"
			echo "          Product \"$swproduct\": \"$swdirectory\"."
			if [[ $exitval -ne $FAILURE ]]
			then
				exitval=$WARNING
			fi
			swgood=false
		else
			swgood=true
		fi
	fi
	swfileset=$1
	swcontrol=$2
	thisfile=$swdirectory/$swfileset/$swcontrol
	thisINFO=$swdirectory/$swfileset/$swinfo
	savethis=$swoursave$thisfile
	saveINFO=$swoursave$thisINFO
	tempINFO=/var/tmp/$swinfo.$$
}
function swlocate_bad {
	# The key operation in swlocate is the swlist -v in swextract.
	# The verbose option does not work when run from a postinstall
	# script.  Therefore, the SDcheck_file operation uses this.
	# Until this is corrected (DTS # unknown...), we cannot do the
	# SDcheck_file operation, and must depend on checking the log
	# file from the subsequent reboot (/etc/rc.log), so put a note
	# in the swagent.log, and do other stuff here.
	if [[ -z "$swfileset" ]]
	then
		echo "NOTE:    Check /etc/rc.log after the system is rebooted to confirm"
		echo "         successful operation of $_PATCHID actions to product"
		echo "         \"$swproduct\" SD scripts ($swfilecount scripts)."
	fi
	swgood=false
	swfileset=$1
	swcontrol=$2
}

# Internal function -- update INFO file "thisINFO"
# globals in:	thisINFO tempINFO saveINFO swcontrol swinfo
#	swupinfo mode cksum_output mtime INFOmtime
function swupinfo {
	awk -v THIS="$swcontrol" -v SAVE="$saveINFO" -v INFO="$swinfo" \
		-v MODE="$1" -v CKSUM="$2" -v MTIME="$3" -v ITIME="$4" '
		BEGIN {
			split(CKSUM, pieces)
			SUM = pieces[1]
			SIZE = pieces[2]
			s_size = "d"
			s_sum = "d"
			s_mode = "d"
			s_mtime = "d"
		}
		function new(value) {
			if( (value == "d") || (value == "") )
			{
				$0 = ""
			}
			else if( value != "x" )
			{
				$2 = value
			}
		}
		/^control_file/	{cf = 1; info = 0; this = 0}
		/^file/		{cf = 0; info = 0; this = 0}
		/^path/		{
			if( (cf == 1) && ($2 == INFO) )
				{cf = 0; info = 1; this = 0}
			else if( (cf == 1) && ($2 == THIS) )
				{cf = 0; info = 0; this = 1}
			else
				{cf = 0; info = 0; this = 0}
		}
		/^tag/		{cf = 0; info = 0; this = 0}
		/^size/		{
			if( this == 1 )
			{
				s_size = $2
				new(SIZE)
			}
		}
		/^cksum/	{
			if( this == 1 )
			{
				s_sum = $2
				new(SUM)
			}
		}
		/^mode/		{
			if( this == 1 )
			{
				s_mode = $2
				new(MODE)
			}
		}
		/^mtime/	{
			if( this == 1 )
			{
				s_mtime = $2
				new(MTIME)
			}
			if( info == 1 )
			{
				new(ITIME)
			}
		}
		{
			if( $0 != "" )
			{
				print $0
			}
		}
		END {
			print THIS" "s_size" "s_sum" "s_mode" "s_mtime >> SAVE
		}
		' $thisINFO >| $tempINFO
	cp $tempINFO $thisINFO
	rm $tempINFO
}
#

# Declare product to be checked/fixed/unfixed
# globals out:	swproduct swfileset swcontrol swfilecount
#	SDproduct Product_Name File_Count
function SDproduct {
	swproduct="$1"
	swfileset=""
	swcontrol=""
	swfilecount="$2"
}

# Check file conditions for changing
# Print any errors/warnings from setup into the sw log.  We only produce
# warnings, so we want to proceed.  Can check the exitval if we care.
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
#	SDcheck_file Fileset_Name Control_File_Name Mode_Value < contents
function SDcheck_file {
	# The key operation in swlocate is the swlist -v in swextract.
	# The verbose option does not work when run from a postinstall
	# script.  Therefore, use this until it gets fixed:
	swlocate_bad $1 $2
}

# Write changed file (save old if not saved),
# and update INFO file for it (save original values if file saved)
# Standard Input is contents of file, normally a "here-document"
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
#	SDfix_file Fileset_Name Control_File_Name Mode_Value < contents
function SDfix_file {
	swlocate $1 $2
	swmode=$3
	if $swgood
	then
		# Save file if necessary
		if [[ ! -f $savethis ]]
		then
			mkdir -p `dirname $savethis`
			mv -f $thisfile $savethis
		else
			# No info to save
			saveINFO="/dev/null"
		fi

		cat >| $thisfile

		# Set mode, and mtime to patch's
		chmod $swmode $thisfile
		touch -m -r $swourself $thisfile
		# Update info
		swupinfo "$swmode" "`cksum $thisfile`" "$swmtime" "$swmtime"
		touch -m -r $swourself $thisINFO
		echo "         $swproduct.$swfileset.$swcontrol updated."
	fi
}

# Restore original file from saved
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
#	SDunfix_file Fileset_Name Control_File_Name
function SDunfix_file {
	swlocate $1 $2
	if $swgood
	then
		if [[ -f $savethis ]]
		then
			# Restore file (and remove saved)
			mv -f $savethis $thisfile
			# Get & remove info line;  remove info file if empty
			grep -s  "^$swcontrol " $saveINFO | read s_this s_size s_sum s_mode s_mtime
			grep -sv "^$swcontrol " $saveINFO > $tempINFO
			cp $tempINFO $saveINFO
			if [[ ! -s $saveINFO ]]
			then
				rm $saveINFO
			fi
			rm $tempINFO
			# Restore info, without changing its mtime
			saveINFO="/dev/null"
			touch -m -r $thisINFO /tmp/junk.$$
			swupinfo "$s_mode" "$s_sum $s_size" "$s_mtime" "x"
			touch -m -r /tmp/junk.$$ $thisINFO
			rm -f /tmp/junk.$$
			# Remove directory if empty
			rmdir -p `dirname $savethis` 2> /dev/null
			echo "         $swproduct.$swfileset.$swcontrol restored."
		fi
	fi
}

# Build the boot-up rc script to do all the work outside of SD
# Standard Input is contents of file, normally a "here-document"
# globals in:	_PATCHID UTILS
#	SDbuild postinstall/preremove < contents
function SDbuild {
	S900="/sbin/rc2.d/S900SD.$_PATCHID"
	for x in 1
	do

		cat <<- "__EOF__S900__1"
		#!/sbin/sh
		# This is a generated boot rc script to perform the
		# SD control script changes for a patch.
		# When it runs successfully, it removes itself.
		__EOF__S900__1

		echo "_PATCHID=$_PATCHID"
		echo "S900=$S900"
		# Define name, don't execute UTILS because redefines PATH
		echo "UTILS=$UTILS"
		echo "exitval=0"
		echo "PATH=/sbin:/usr/sbin:/usr/bin"
		if [[ "$1" = "preremove" ]]
		then
			echo "swpatch_there=false"
			echo "swchange=restoration"
		else
			echo "swchange=changes"
		fi

		cat <<- "__EOF__S900__2"
		case $1 in
			start_msg)
				echo "Execute SD script $swchange from $_PATCHID"
				exit 0
				;;
			stop_msg)
				exit 0
				;;
			stop)
				exit 0
				;;
			start)
				# fall thru case to start below
				;;
			*)
				echo "USAGE: $0 {start_msg | stop_msg | start | stop}" >&2
				exit 1
				;;
		esac

		# Start
		__EOF__S900__2

		cat
		cat <<- "__EOF__S900__3"
		if [[ $exitval -ne 0 ]]
		then
			exitval=1
		fi
		rm -f $S900; exit $exitval
		__EOF__S900__3

	done >| $S900
	chmod 544 $S900
}

#########################################################################
#									#
#	FILE NAMES and FILE CONTENTS -- Control File data to change	#
#									#
#	Each call to SDproduct defines the Product to be corrected	#
#	Each call to SDfix_file changes a control file and its INFO	#
#									#
#########################################################################
#
#	SDproduct Product_Name File_Count
#	SDfix_file Fileset_Name Control_File_Name Mode_Value << __EOF__
#	SDfix_file ...
#	SDproduct Product_Name File_Count
#	SDfix_file ...
#

#########################################################################
#									#
#	FILE NAMES without file contents -- Control Files to restore	#
#									#
#	Each call to SDproduct defines the Product to be restored	#
#	Each call to SDunfix_file restores a control file and its INFO	#
#									#
#########################################################################
#
#	SDproduct Product_Name File_Count
#	SDunfix_file Fileset_Name Control_File_Name
#	SDunfix_file ...
#	SDproduct Product_Name File_Count
#	SDunfix_file ...
#

#########################################################################
	SDproduct Networking 5
	SDcheck_file LAN-KRN configure
	SDcheck_file LAN-KRN postinstall
	SDcheck_file LAN-KRN verify
	SDcheck_file LAN-RUN unconfigure
	SDcheck_file LAN-RUN verify
	SDbuild postinstall << "__EOF__SDbuilder"
#################################################################
# This code would be inserted if the include file had been
# defined and installed in its place.  Since it isn't, the
# file itself is here.
#	if [ ! -f $UTILS.SDfix ]; then
#	    echo "ERROR:   Cannot find $UTILS.SDfix"
#	    exit 1
#	fi
#	
#	. $UTILS.SDfix
#################################################################
#########################################################################
#									#
#	These are functions to patch Software Distribution Files	#
#									#
#	The patch mechanism does not support operations on these	#
#	files, so this has to work around the normal SD operations.	#
#									#
#	This version DOES NOT support ADD or DELETE of files,		#
#				only changes				#
#									#
#########################################################################
#
# Must be defined before these functions are invoked:
#	_PATCHID	the patch number of this patch
#
# Defined constants/literals:
swprod_dir=/var/adm/sw/products
swsave_dir=/var/adm/sw/save
swinfo=INFO
swproduct=""
swfileset=""
swcontrol=""
swleave=2
#
# Define patch related information, determine patch INFO mtime
swourself=$swprod_dir/$_PATCHID/pfiles/$swinfo
swoursave=$swsave_dir/$_PATCHID
if ${swpatch_there:-true}
then
	swmtime=`awk -v INFO=$swinfo '
		/^control_file/	{cf = 1; info = 0}
		/^file/		{cf = 0; info = 0}
		/^path/		{
			if( (cf == 1) && ($2 == INFO) )
				{cf = 0; info = 1}
			else
				{cf = 0; info = 0}
		}
		/^mtime/	{
			if( info == 1 )
				{print $2; exit}
		}
		/^tag/		{cf = 0; info = 0}
		' $swourself`
	if [[ -z "$swmtime" ]]
	then
		echo "ERROR:   Cannot find necessary data for SD work for patch"
		echo "         $_PATCHID.  No mtime of patch $swinfo file found."
		exit 1
	fi
fi
#

# Internal function -- extract directory for Product, given Filset and File
# globals in:	swproduct
#	swextract Fileset_Name Control_File_Name
function swextract {
	swlist -v $swproduct.$1 | awk -v swc="$2" '
		/^control_file/	{
			nf = split($2, f, "/")
			if( f[nf] == swc )
				{print $2; exit}
		}
		'
}

# Internal function -- locate files for Product, given Filset and File
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
# globals out:	swdirectory swgood swfileset swcontrol
#		thisfile thisINFO savethis saveINFO tempINFO
#	swlocate Fileset_Name Control_File_Name
function swlocate {
	if [[ -z "$swfileset" ]]
	then
		# Locate our Product directory for changes
		swd=`swextract $1 $2`
		if [[ -z "$swd" ]]
		then
			for swp in `ls -d $swprod_dir/$swproduct*/$1`
			do
				mv $swp/$swinfo $swp/$swinfo.$$

				cat <<- "__EOF__" > $swp/$swinfo
				control_file
				path dummy
				tag dummy
				__EOF__

				cat $swp/$swinfo.$$ >> $swp/$swinfo

				swd=`swextract $1 dummy`
				mv $swp/$swinfo.$$ $swp/$swinfo
				if [[ ! -z "$swd" ]]
				then
					break
				fi
			done
		fi
		swdirectory=`echo $swd | awk -v swl="$swleave" '
			{
				for( x = 1; x <= NF; x++ )
				{
					nf = split($x, f, "/")
					fs = ""
					# f[1] null, must be absolute filename
					for( i = 2; i <= nf - swl; i++ )
					{
						if( f[i] != "" )
						{
							fs = fs "/" f[i]
						}
					}
					if( fs > swd )
					{
						swd = fs
					}
				}
				print swd
			}
			'`
		#
		# Was everything found okay?
		if [[ -z "$swdirectory" || ! -d $swdirectory ]]
		then
			echo "WARNING:  Cannot find necessary directory for SD work to"
			echo "          Product \"$swproduct\": \"$swdirectory\"."
			if [[ $exitval -ne $FAILURE ]]
			then
				exitval=$WARNING
			fi
			swgood=false
		else
			swgood=true
		fi
	fi
	swfileset=$1
	swcontrol=$2
	thisfile=$swdirectory/$swfileset/$swcontrol
	thisINFO=$swdirectory/$swfileset/$swinfo
	savethis=$swoursave$thisfile
	saveINFO=$swoursave$thisINFO
	tempINFO=/var/tmp/$swinfo.$$
}
function swlocate_bad {
	# The key operation in swlocate is the swlist -v in swextract.
	# The verbose option does not work when run from a postinstall
	# script.  Therefore, the SDcheck_file operation uses this.
	# Until this is corrected (DTS # unknown...), we cannot do the
	# SDcheck_file operation, and must depend on checking the log
	# file from the subsequent reboot (/etc/rc.log), so put a note
	# in the swagent.log, and do other stuff here.
	if [[ -z "$swfileset" ]]
	then
		echo "NOTE:    Check /etc/rc.log after the system is rebooted to confirm"
		echo "         successful operation of $_PATCHID actions to product"
		echo "         \"$swproduct\" SD scripts ($swfilecount scripts)."
	fi
	swgood=false
	swfileset=$1
	swcontrol=$2
}

# Internal function -- update INFO file "thisINFO"
# globals in:	thisINFO tempINFO saveINFO swcontrol swinfo
#	swupinfo mode cksum_output mtime INFOmtime
function swupinfo {
	awk -v THIS="$swcontrol" -v SAVE="$saveINFO" -v INFO="$swinfo" \
		-v MODE="$1" -v CKSUM="$2" -v MTIME="$3" -v ITIME="$4" '
		BEGIN {
			split(CKSUM, pieces)
			SUM = pieces[1]
			SIZE = pieces[2]
			s_size = "d"
			s_sum = "d"
			s_mode = "d"
			s_mtime = "d"
		}
		function new(value) {
			if( (value == "d") || (value == "") )
			{
				$0 = ""
			}
			else if( value != "x" )
			{
				$2 = value
			}
		}
		/^control_file/	{cf = 1; info = 0; this = 0}
		/^file/		{cf = 0; info = 0; this = 0}
		/^path/		{
			if( (cf == 1) && ($2 == INFO) )
				{cf = 0; info = 1; this = 0}
			else if( (cf == 1) && ($2 == THIS) )
				{cf = 0; info = 0; this = 1}
			else
				{cf = 0; info = 0; this = 0}
		}
		/^tag/		{cf = 0; info = 0; this = 0}
		/^size/		{
			if( this == 1 )
			{
				s_size = $2
				new(SIZE)
			}
		}
		/^cksum/	{
			if( this == 1 )
			{
				s_sum = $2
				new(SUM)
			}
		}
		/^mode/		{
			if( this == 1 )
			{
				s_mode = $2
				new(MODE)
			}
		}
		/^mtime/	{
			if( this == 1 )
			{
				s_mtime = $2
				new(MTIME)
			}
			if( info == 1 )
			{
				new(ITIME)
			}
		}
		{
			if( $0 != "" )
			{
				print $0
			}
		}
		END {
			print THIS" "s_size" "s_sum" "s_mode" "s_mtime >> SAVE
		}
		' $thisINFO >| $tempINFO
	cp $tempINFO $thisINFO
	rm $tempINFO
}
#

# Declare product to be checked/fixed/unfixed
# globals out:	swproduct swfileset swcontrol swfilecount
#	SDproduct Product_Name File_Count
function SDproduct {
	swproduct="$1"
	swfileset=""
	swcontrol=""
	swfilecount="$2"
}

# Check file conditions for changing
# Print any errors/warnings from setup into the sw log.  We only produce
# warnings, so we want to proceed.  Can check the exitval if we care.
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
#	SDcheck_file Fileset_Name Control_File_Name Mode_Value < contents
function SDcheck_file {
	# The key operation in swlocate is the swlist -v in swextract.
	# The verbose option does not work when run from a postinstall
	# script.  Therefore, use this until it gets fixed:
	swlocate_bad $1 $2
}

# Write changed file (save old if not saved),
# and update INFO file for it (save original values if file saved)
# Standard Input is contents of file, normally a "here-document"
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
#	SDfix_file Fileset_Name Control_File_Name Mode_Value < contents
function SDfix_file {
	swlocate $1 $2
	swmode=$3
	if $swgood
	then
		# Save file if necessary
		if [[ ! -f $savethis ]]
		then
			mkdir -p `dirname $savethis`
			mv -f $thisfile $savethis
		else
			# No info to save
			saveINFO="/dev/null"
		fi

		cat >| $thisfile

		# Set mode, and mtime to patch's
		chmod $swmode $thisfile
		touch -m -r $swourself $thisfile
		# Update info
		swupinfo "$swmode" "`cksum $thisfile`" "$swmtime" "$swmtime"
		touch -m -r $swourself $thisINFO
		echo "         $swproduct.$swfileset.$swcontrol updated."
	fi
}

# Restore original file from saved
# globals in:	swprod_dir swproduct swfileset swinfo swleave swoursave
#	SDunfix_file Fileset_Name Control_File_Name
function SDunfix_file {
	swlocate $1 $2
	if $swgood
	then
		if [[ -f $savethis ]]
		then
			# Restore file (and remove saved)
			mv -f $savethis $thisfile
			# Get & remove info line;  remove info file if empty
			grep -s  "^$swcontrol " $saveINFO | read s_this s_size s_sum s_mode s_mtime
			grep -sv "^$swcontrol " $saveINFO > $tempINFO
			cp $tempINFO $saveINFO
			if [[ ! -s $saveINFO ]]
			then
				rm $saveINFO
			fi
			rm $tempINFO
			# Restore info, without changing its mtime
			saveINFO="/dev/null"
			touch -m -r $thisINFO /tmp/junk.$$
			swupinfo "$s_mode" "$s_sum $s_size" "$s_mtime" "x"
			touch -m -r /tmp/junk.$$ $thisINFO
			rm -f /tmp/junk.$$
			# Remove directory if empty
			rmdir -p `dirname $savethis` 2> /dev/null
			echo "         $swproduct.$swfileset.$swcontrol restored."
		fi
	fi
}

# Build the boot-up rc script to do all the work outside of SD
# Standard Input is contents of file, normally a "here-document"
# globals in:	_PATCHID UTILS
#	SDbuild postinstall/preremove < contents
function SDbuild {
	S900="/sbin/rc2.d/S900SD.$_PATCHID"
	for x in 1
	do

		cat <<- "__EOF__S900__1"
		#!/sbin/sh
		# This is a generated boot rc script to perform the
		# SD control script changes for a patch.
		# When it runs successfully, it removes itself.
		__EOF__S900__1

		echo "_PATCHID=$_PATCHID"
		echo "S900=$S900"
		# Define name, don't execute UTILS because redefines PATH
		echo "UTILS=$UTILS"
		echo "exitval=0"
		echo "PATH=/sbin:/usr/sbin:/usr/bin"
		if [[ "$1" = "preremove" ]]
		then
			echo "swpatch_there=false"
			echo "swchange=restoration"
		else
			echo "swchange=changes"
		fi

		cat <<- "__EOF__S900__2"
		case $1 in
			start_msg)
				echo "Execute SD script $swchange from $_PATCHID"
				exit 0
				;;
			stop_msg)
				exit 0
				;;
			stop)
				exit 0
				;;
			start)
				# fall thru case to start below
				;;
			*)
				echo "USAGE: $0 {start_msg | stop_msg | start | stop}" >&2
				exit 1
				;;
		esac

		# Start
		__EOF__S900__2

		cat
		cat <<- "__EOF__S900__3"
		if [[ $exitval -ne 0 ]]
		then
			exitval=1
		fi
		rm -f $S900; exit $exitval
		__EOF__S900__3

	done >| $S900
	chmod 544 $S900
}

#########################################################################
#									#
#	FILE NAMES and FILE CONTENTS -- Control File data to change	#
#									#
#	Each call to SDproduct defines the Product to be corrected	#
#	Each call to SDfix_file changes a control file and its INFO	#
#									#
#########################################################################
#
#	SDproduct Product_Name File_Count
#	SDfix_file Fileset_Name Control_File_Name Mode_Value << __EOF__
#	SDfix_file ...
#	SDproduct Product_Name File_Count
#	SDfix_file ...
#

#########################################################################
#									#
#	FILE NAMES without file contents -- Control Files to restore	#
#									#
#	Each call to SDproduct defines the Product to be restored	#
#	Each call to SDunfix_file restores a control file and its INFO	#
#									#
#########################################################################
#
#	SDproduct Product_Name File_Count
#	SDunfix_file Fileset_Name Control_File_Name
#	SDunfix_file ...
#	SDproduct Product_Name File_Count
#	SDunfix_file ...
#

#########################################################################
	SDproduct Networking 5
	SDfix_file LAN-KRN configure 0544 << "__EOF__SDfixer"
#!/sbin/sh
#############################################################################
#  Product: Networking
#  Fileset: LAN-KRN
#  configure
#  @(#) $Revision: 1.4.118.2 $
#############################################################################
# (c) Copyright Hewlett-Packard Company, 1994
#############################################################################

####### add our entry to the kernel config file only if on a client system
if [ -z "$SW_DEFERRED_KERNBLD" ] ; then
    # we are on a standalone or server; the postinstall script has already
    # updated the kernel config file
    exit 0
fi

####### setup environment 
UTILS="/usr/lbin/sw/control_utils"
if [ ! -f $UTILS ] ; then
    echo "ERROR:   Cannot find \"$UTILS\"."
    exit 1
fi
. $UTILS


####### remove old, on-longer-valid config file entries
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan01
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan0
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan1


####### add our entry to the kernel config file
LAN0=0
LAN1=0
LAN2=0
LAN3=0
for LINE in `ioscan -F | sed 's/ /~/g'` ; do
    SVERS_MODEL=`echo "$LINE" | awk -F':' '{ print $12 }' | sed 's/~/ /g' | \
	    awk '{printf("%02x%02x%02x\n",$5,$6,$7)}' | \
            awk '{ $1=substr($1,2,5); print $1 }'`
    case $SVERS_MODEL in
        00050)
            # 0x50 ---> LANbrusca old NIO LAN card
	    LAN1=1
            ;;
        00072 | 0008a)
            # 0x72 ---> s700 built-in LAN
            # 0x8a ---> LASI LAN
	    LAN2=1
            ;;
        00052 | 00060)
            # 0x52 ---> Miura NIO LAN card
            # 0x60 ---> Diablo or Countache NIO LAN/Console card
	    LAN3=1
            ;;
        *)
            if [ -n "`echo $LINE | grep eisa`" ] ; then
                EISA_ID=`echo $LINE | awk -F':' '{ print $12 }' | \
			sed 's/~/ /g' | \
                        awk '{printf("%02x%02x%02x%02x\n",$1,$2,$3,$4)}'`
		if [ "$EISA_ID" = "22f01850" ] ; then
		    # 0x22f01850==HWP1850 ---> Tornado EISA LAN card
		    LAN2=1
		fi
            elif [ -n "`echo $LINE | grep cio_ca0`" ] ; then
                CIO_ID=`echo $LINE | awk -F':' '{ print $12 }' | \
			sed 's/~/ /g' | \
                        awk '{printf("%02x%02x%02x%02x\n",$1,$2,$3,$4)}'`
		if [ "$CIO_ID" = "00000006" ] ; then
		    # 0x00000006 ---> CIO LAN card
		    LAN0=1
		fi
	    fi
            ;;
    esac
done

EXITVAL=0

####### add the drivers to the kernel
RES0=0
RES1=0
RES2=0
RES3=0
RESD=0
if [ $LAN0 -eq 1 ] ; then
    echo "WARNING: This version of the kernel does not support the   "
    echo "         the LAN hardware installed in your system.        "
    RES0=1
fi
if [ $LAN1 -eq 1 ] ; then
    echo "WARNING: This version of the kernel does not support the   "
    echo "         the LAN hardware installed in your system.        "
    RES1=1
fi
if [ $LAN2 -eq 1 ] ; then
    mod_systemfile $SW_SYSTEM_FILE_PATH -a lan2
    RES2=$?
fi
if [ $LAN3 -eq 1 ] ; then
    mod_systemfile $SW_SYSTEM_FILE_PATH -a lan3
    RES3=$?
fi
mod_systemfile $SW_SYSTEM_FILE_PATH -a dlpi
RESD=$?

ln -f -s "${SW_ROOT_DIRECTORY}"dev/dlpi "${SW_ROOT_DIRECTORY}"dev/lan
ln -f -s "${SW_ROOT_DIRECTORY}"dev/dlpi "${SW_ROOT_DIRECTORY}"dev/snap

####### see if the mod_systemfiles worked
if [ $RES0 -ne 0 -o $RES1 -ne 0 -o $RES2 -ne 0 -o $RES3 -ne 0 -o $RESD -ne 0 ] ; then
    echo "ERROR:   Cannot modify the kernel configuration file.  The"
    echo "         appropriate LAN driver must be manually configured"
    echo "         into the kernel."
    EXITVAL=1
fi
#
# Export the master file to the LAN-RUN fileset.
#
file="${SW_LOCATION}usr/conf/master.d/lan"
if swlist -l file $PRODUCT.$FILESET 2>/dev/null | grep -q $file
then        # master file not yet exported
  export_master $file
  if [[ $? -ne 0 ]]
    then
      echo "WARNING: Cannot give the master file(s) \"$file\""
      echo "         to ${FILESET%%-KRN}-RUN.  If you delete this"
      echo "         fileset, SAM will not be able to handle your"
      echo "         device files"
      if [[ $EXITVAL -ne $FAILURE ]]
        then
          EXITVAL=$WARNING
      fi # [[ $EXITVAL -ne $FAILURE ]]
    fi # [[ $? -ne 0 ]]
fi # swlist -l file $PRODUCT.$FILESET

exit $EXITVAL
__EOF__SDfixer
	SDfix_file LAN-KRN postinstall 0544 << "__EOF__SDfixer"
#!/sbin/sh
#############################################################################
#  Product: Networking
#  Fileset: LAN-KRN
#  postinstall
#  @(#) $Revision: 1.3.118.5 $
#############################################################################
# (c) Copyright Hewlett-Packard Company, 1994
#############################################################################

####### add our entry to the kernel config file only if server or standalone
if [ -n "$SW_DEFERRED_KERNBLD" ] ; then
	# we are on a client; the configure script will update kernel config
	exit 0
fi

####### setup environment 
PATH=${SW_PATH}:${SW_ROOT_DIRECTORY}usr/lbin/sw
UTILS="/usr/lbin/sw/control_utils"
if [ ! -f $UTILS ] ; then
        echo "ERROR:   Cannot find \"$UTILS\"."
        exit 1
fi
. $UTILS


####### remove old, on-longer-valid config file entries
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan01
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan0
mod_systemfile $SW_SYSTEM_FILE_PATH -d lan1


####### add our entry to the kernel config file
LAN0=0
LAN1=0
LAN2=0
LAN3=0
for LINE in `ioscan -F | sed 's/ /~/g'` ; do
    SVERS_MODEL=`echo "$LINE" | awk -F':' '{ print $12 }' | sed 's/~/ /g' | \
            awk '{printf("%02x%02x%02x\n",$5,$6,$7)}' | \
	    awk '{ $1=substr($1,2,5); print $1 }'`
    case $SVERS_MODEL in
        00050)
            # 0x50 ---> LANbrusca old NIO LAN card
	    LAN1=1
            ;;
        00072 | 0008a)
            # 0x72 ---> s700 built-in LAN
            # 0x8a ---> LASI LAN
	    LAN2=1
            ;;
        00052 | 00060)
            # 0x52 ---> Miura NIO LAN card
            # 0x60 ---> Diablo or Countache NIO LAN/Console card
	    LAN3=1
            ;;
        *)
            if [ -n "`echo $LINE | grep eisa`" ] ; then
                EISA_ID=`echo $LINE | awk -F':' '{ print $12 }' | \
		sed 's/~/ /g' | \
                        awk '{printf("%02x%02x%02x%02x\n",$1,$2,$3,$4)}'`
		if [ "$EISA_ID" = "22f01850" ] ; then
                    # 0x22f01850==HWP1850 ---> Tornado EISA LAN card
		    LAN2=1
		fi
            elif [ -n "`echo $LINE | grep cio_ca0`" ] ; then
                CIO_ID=`echo $LINE | awk -F':' '{ print $12 }' | \
		sed 's/~/ /g' | \
                        awk '{printf("%02x%02x%02x%02x\n",$1,$2,$3,$4)}'`
		if [ "$CIO_ID" = "00000006" ] ; then
		    # 0x00000006 ---> CIO LAN card
		    LAN0=1
		fi
	    fi
            ;;
    esac
done

EXITVAL=0

####### add the drivers to the kernel
RES0=0
RES1=0
RES2=0
RES3=0
RESD=0
if [ $LAN0 -eq 1 ] ; then
        echo "WARNING: This version of the kernel does not support the   "
        echo "         the LAN hardware installed in your system.        "
	RES0=1
fi
if [ $LAN1 -eq 1 ] ; then
        echo "WARNING: This version of the kernel does not support the   "
        echo "         the LAN hardware installed in your system.        "
	RES1=1
fi
if [ $LAN2 -eq 1 ] ; then
	mod_systemfile $SW_SYSTEM_FILE_PATH -a lan2
	RES2=$?
fi
if [ $LAN3 -eq 1 ] ; then
	mod_systemfile $SW_SYSTEM_FILE_PATH -a lan3
	RES3=$?
fi
mod_systemfile $SW_SYSTEM_FILE_PATH -a dlpi
RESD=$?

ln -f -s "${SW_ROOT_DIRECTORY}"dev/dlpi "${SW_ROOT_DIRECTORY}"dev/lan
ln -f -s "${SW_ROOT_DIRECTORY}"dev/dlpi "${SW_ROOT_DIRECTORY}"dev/snap

####### see if the mod_systemfiles worked
if [ $RES0 -ne 0 -o $RES1 -ne 0 -o $RES2 -ne 0 -o $RES3 -ne 0 -o $RESD -ne 0 ] ; then
	echo "ERROR:   Cannot modify the kernel configuration file.  The"
	echo "         appropriate LAN driver must be manually configured"
	echo "         into the kernel."
	EXITVAL=1
fi

exit $EXITVAL
__EOF__SDfixer
	SDfix_file LAN-KRN verify 0544 << "__EOF__SDfixer"
#!/sbin/sh
#############################################################################
#  Product: Networking
#  Fileset: LAN-KRN
#  verify
#  @(#) $Revision: 1.2.118.3 $
#############################################################################
# (c) Copyright Hewlett-Packard Company, 1994
#############################################################################

####### setup environment 
PATH=${SW_PATH}:${SW_ROOT_DIRECTORY}usr/lbin/sw
UTILS="/usr/lbin/sw/control_utils"
if [ ! -f $UTILS ] ; then
        echo "ERROR:   Cannot find \"$UTILS\"."
        exit 1

fi
. $UTILS

EXITVAL=0


####### make sure files were installed correctly
FILES="\
	usr/conf/lib/liblan.a \
"
for FILE in $FILES ; do
	FNAME=${SW_ROOT_DIRECTORY}${FILE}
	if [ ! -f "$FNAME" ] ; then
		echo "ERROR:   The kernel library \"$FNAME\""
		echo "         is not installed correctly."
		EXITVAL=1
	fi
done

exit $EXITVAL
__EOF__SDfixer
	SDfix_file LAN-RUN unconfigure 0544 << "__EOF__SDfixer"
#!/sbin/sh
#############################################################################
#  Product: Networking
#  Fileset: LAN-RUN
#  unconfigure
#  @(#) $Revision: 1.3.119.2 $
#############################################################################
# (c) Copyright Hewlett-Packard Company, 1994
#############################################################################


####### delete DLPI device files
rm -f /dev/dlpi[0-9]*

####### delete LLA device files
rm -f /dev/lan[0-9]*
rm -f /dev/ether*
rm -f /dev/ieee*
rm -f /dev/diag/lan*

exit 0
__EOF__SDfixer
	SDfix_file LAN-RUN verify 0544 << "__EOF__SDfixer"
#!/sbin/sh
#############################################################################
#  Product: Networking
#  Fileset: LAN-RUN
#  verify
#  @(#) $Revision: 1.4.119.2 $
#############################################################################
# (c) Copyright Hewlett-Packard Company, 1994
#############################################################################

####### setup environment 
PATH=${SW_PATH}:${SW_ROOT_DIRECTORY}usr/lbin/sw
UTILS="/usr/lbin/sw/control_utils"
if [ ! -f $UTILS ] ; then
        echo "ERROR:   Cannot find \"$UTILS\"."
        exit 1

fi
. $UTILS

####### hope for success
EXITVAL=0


####### make sure files were installed correctly
FILES=" \
	usr/sbin/arp \
	usr/lib/nls/C/arp.cat \
	usr/sbin/lanadmin \
	usr/sbin/landiag \
	usr/lib/lanadmin/libdllan3.sl \
	usr/lib/lanadmin/libdslan2.sl \
	usr/lib/lanadmin/libdslan3.sl \
	usr/lib/nls/C/lanadmin.cat \
	usr/lib/nls/C/dllan3.cat \
	usr/lib/nls/C/dslan2.cat \
	usr/lib/nls/C/dslan3.cat \
	usr/sbin/lanscan \
	usr/lib/lanscan/libpelan2.sl \
	usr/lib/lanscan/libpelan3.sl \
	usr/lib/nls/C/lanscan.cat \
	usr/lib/nls/C/pelan2.cat \
	usr/lib/nls/C/pelan3.cat \
	usr/sbin/linkloop \
	usr/lib/nls/C/linkloop.cat \
"
for FILE in $FILES ; do
	FNAME=${SW_ROOT_DIRECTORY}${FILE}
	if [ ! -f "$FNAME" ] ; then
		echo "ERROR:   The file \"$FNAME\""
		echo "         is not installed correctly."
		EXITVAL=1
	fi
done


####### make sure DLPI device files were installed correctly
DLPI_DEV_FILES="
	dev/dlpi \
	dev/dlpi0 \
	dev/dlpi1 \
	dev/dlpi2 \
	dev/dlpi3 \
	dev/dlpi4 \
	dev/lan \
	dev/snap \
"
for FILE in $DLPI_DEV_FILES ; do
	FNAME=${SW_ROOT_DIRECTORY}${FILE}
	if [ ! -c "$FNAME" ] ; then
		echo "ERROR:   The DLPI device file \"$FNAME\""
		echo "         is not installed correctly."
		EXITVAL=1
	fi
done


exit $EXITVAL
__EOF__SDfixer
__EOF__SDbuilder

cp ${SW_ROOT_DIRECTORY}stand/system  $swoursave/.system
mod_systemfile ${SW_ROOT_DIRECTORY}stand/system -a token_arp
rm -f ${SW_ROOT_DIRECTORY}usr/share/man/cat1m.Z/lanadmin.1m
rm -f ${SW_ROOT_DIRECTORY}usr/share/man/cat1m.Z/lanscan.1m
#
# Fix for product:  These two lines do what should have been done in
# Networking/LAN-KRN/postinstall and Networking/LAN-KRN/configure
ln -f -s "${SW_ROOT_DIRECTORY}"dev/dlpi "${SW_ROOT_DIRECTORY}"dev/lan
ln -f -s "${SW_ROOT_DIRECTORY}"dev/dlpi "${SW_ROOT_DIRECTORY}"dev/snap

exit $exitval
