#!/bin/ksh
# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms. 
#
# ident	"@(#)postpatch	1.2	10/03/05 SMI"
#

# Definitions for override safety mechanism
PATCH_OVERRIDE_LIB="${ROOTDIR}/usr/lib/patch/patch_override_lib"

# This function prints out content of the patch messaging file to stdout
#
# Usage:
#
# print_patch_script_messaging
#
print_patch_script_messaging()
{
	TMP_DIRNAME="/var/run"
	MSG_FILENAME="${TMP_DIRNAME}/patchadd_msg_file_${PatchNum}"

	if [ -r "${MSG_FILENAME}" -a -s "${MSG_FILENAME}" ]; then
		/usr/bin/cat "${MSG_FILENAME}"
	fi

	/usr/bin/rm -f "${MSG_FILENAME}"

	return 0
}

# Check if we messaging is ready. Returns 0 on success, 1 on failure
# On successful run it leaves MSG_FILENAME variable defined
# 
# Usage:
#
# check_patch_script_messaging
#
check_patch_script_messaging()
{
	TMP_DIRNAME="/var/run"
	MSG_FILENAME="${TMP_DIRNAME}/patchadd_msg_file_${PatchNum}"

	if [ -w "${MSG_FILENAME}" ] ; then
		unset TMP_DIRNAME
		return 0
	else
		unset TMP_DIRNAME
		unset MSG_FILENAME
		return 1
	fi
}

# This function prepares a message to be printed to stdout towards the end
# of postpatch execution. If the message file is not ready, it at least tries
# immediate output to stdout which is currently know to work only in patch
# level scripts.
#
# Usage:
#
# print_patch_message "text_of_message_to_be_printed_out"
#
print_patch_message()
{
	if check_patch_script_messaging; then
		echo "$*" >> "${MSG_FILENAME}"
	else
		echo "$*"
	fi

	return 0
}

# script in a function so that it can be overridden if needed.
postpatch()
{
#!/bin/sh
#
#ident	"@(#)postpatch	1.14	08/05/21 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH="/usr/bin:/usr/sbin:${PATH}"
PKGCOND=/usr/bin/pkgcond

CheckZones()
{
	if [ "$ROOTDIR" = "/" -a -x /usr/bin/zonename ]; then
		ZONENAME=`/usr/bin/zonename`
		if [ ${ZONENAME} = "global" ]; then
			GLOBAL_ZONE=true
		else
			GLOBAL_ZONE=false
		fi
	else
		# Unable to determine zone
		GLOBAL_ZONE=true
	fi
}

LocalZones()
{
	return 0
}

UpdateIMA()
{
	if [ -z "${ROOTDIR}" ]; then
		echo "\n$0 Failed: ROOTDIR is not set.\n" >&2
		return 1
	fi
	
	VSL_32_NAME=com.sun.ima
	VSL_64_NAME=com.sun.ima64
	LIB_32_PATH=/usr/lib/libsun_ima.so.1
	LIB_64_PATH=/usr/lib/64/libsun_ima.so.1

	[ "${ROOTDIR}" = "/" ] && ROOTDIR=""

	CONF_FILE=${ROOTDIR}/etc/ima.conf


	if [ ! -f ${CONF_FILE} ] ; then

		cat <<EOF > ${CONF_FILE}
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)postpatch	1.13	06/02/24 SMI"
#
#
# This file contains names and references to HBA libraries
#
# Format:
#
# <library name>  <library pathname>
#
# The library name should be prepended with the domain of
# the manufacturer or driver author.
EOF

	fi


	# Append entries if missing
	if   grep "${LIB_32_PATH}" ${CONF_FILE} > /dev/null ; then :
	else
		echo "${VSL_32_NAME}		${LIB_32_PATH}"  >> ${CONF_FILE}
	fi

	# Append entries if missing
	if   grep "${LIB_64_PATH}" ${CONF_FILE} > /dev/null ; then :
	else
		echo "${VSL_64_NAME}		${LIB_64_PATH}"  >> ${CONF_FILE}
	fi

}

ExecuteDefaultCmds()
{

	if [ -z "${ROOTDIR}" ]; then
		echo "\n$0 Failed: ROOTDIR is not set.\n" >&2
		return 1
	fi
	
	DRVR_NAME=iscsi
	DRVR_PERM="-m '* 0600 root sys'"
	DRVR_NAME_UPDATE=sd
	DRVR_ALIAS_UPDATE="-i '\"scsiclass,00\"'"
	CHK_DRVR_ALIAS_UPDATE="scsiclass,00"
	DRIVER_ALIASES_FILE=${ROOTDIR}/etc/driver_aliases

	# Remove existing definition, if it exists. 
	rem_drv -b "${ROOTDIR}" ${DRVR_NAME} > /dev/null 2>&1

	if [ "$ROOTDIR" = "/" ] ; then
	  ADD_DRV="add_drv -n"
	  UPDATE_DRV="update_drv -a"
	else
	  ADD_DRV="add_drv -b ${ROOTDIR}"
	  UPDATE_DRV="update_drv -b ${ROOTDIR} -a"
	fi

	eval ${ADD_DRV} "${DRVR_PERM}" ${DRVR_NAME}
	if [ $? -ne 0 ]; then
		echo "\nCommand Failed:\n\t${ADD_DRV} ${DRVR_PERM} ${DRVR_NAME}\n" >&2
		return 1
	fi
	
	grep "^${DRVR_NAME_UPDATE}[ 	]"\"*${CHK_DRVR_ALIAS_UPDATE}\"* $DRIVER_ALIASES_FILE  > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		eval ${UPDATE_DRV} "${DRVR_ALIAS_UPDATE}" ${DRVR_NAME_UPDATE}
		if [ $? -ne 0 ]; then
			echo "\nCommand Failed:" >&2
			echo "${UPDATE_DRV} "${DRVR_ALIAS_UPDATE}" ${DRVR_NAME_UPDATE}\n" >&2
			return 1
		fi
	fi

	UpdateIMA
}

ExecuteInProperEnvironment()
{

	if $PKGCOND is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
		# Execute non-global whole root zone commands.
		# Should be same action as the default action.
		return 0
	fi

	if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
		# Execute non-global zone commands. Should be no action here
		return 0
	fi

	if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; then
		# Execute commands applicable to patching the mini-root.
		# There are usually no actions to take here since your patching
		# the mini-root on an install server.
		return 0
	fi

	if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
		# Execute commands specific to the mini-root
		return 0
	fi

	if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
		# Execute commands specific to diskless client
		return 0
	fi

	if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
		# Execute commands specific to an alternate root
		ExecuteDefaultCmds
		return 0
	fi

	if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
		# In a global zone and system is mounted on /.
		# Execute all commands.
		ExecuteDefaultCmds
		return 0
	fi

	return 1
}

if [ -x $PKGCOND ] ; then
	ExecuteInProperEnvironment
else
	CheckZones

	if [ "${GLOBAL_ZONE}" = "true" ]; then
		ExecuteDefaultCmds
	else
		LocalZones
	fi
fi

return 0
}

# load the override lib if it exists
if [ -f "${PATCH_OVERRIDE_LIB}" -a -r "${PATCH_OVERRIDE_LIB}" ] ; then
	. "${PATCH_OVERRIDE_LIB}"
fi

# execute the script function
postpatch "$@"
print_patch_script_messaging

exit 0
