#!/bin/sh
#
#ident	"@(#)postpatch	1.5	05/12/12 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
}

CheckAlias()
{
	DRIVER=$1
	PCI_STRING=$2
	
	grep "^${DRIVER}[ 	]\"*${PCI_STRING}\"*" ${ROOTDIR}${ALIAS_FILE} > /dev/null 2>&1
	STATUS=$?
	return $STATUS
}

GenerateSupportList()
{
	LPFC_DRVR=lpfc
	GEN_LIST=""
	
	for HBA in ${SUPPORT_LIST}
	do
		CheckAlias ${LPFC_DRVR} ${HBA}
		if [ $STATUS -ne 0 ]; then
			if [ -z "${GEN_LIST}" ]; then
				GEN_LIST="\"$HBA\""
			else
				GEN_LIST="${GEN_LIST} \"$HBA\""
			fi
			HARDWARE_STRING="${HARDWARE_STRING}|${HBA}"
		fi
	done
}

LocalZones()
{
	return 0
}

ExecuteDefaultCmds()
{
	if [ -z "${ROOTDIR}" ]; then
		echo "\n$0 Failed: ROOTDIR is not set.\n" >&2
		exit 1
	fi

	# Driver definitions
	DRVR_NAME=emlxs
	DRVR_PERM="-m '* 0600 root sys'"
	DRVR_CLASS="-c fibre-channel"
	ALIAS_FILE=/etc/driver_aliases
	HARDWARE_STRING="emlxs"

	SUPPORT_LIST="\
		pci10df,f0a5 \
		pci10df,f800 \
		pci10df,f900 \
		pci10df,f980 \
		pci10df,fa00 \
		pci10df,fc00 \
		pci10df,fc10 \
		pci10df,fc20 \
		pci10df,fd00 \
		pci10df,fe00 \
		pciex10df,fc20 \
		pciex10df,fe00 \
		lpfs \
		"

	GenerateSupportList
	
	DRVR_ALIASES="-i '${GEN_LIST}'"

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

	if [ "$ROOTDIR" = "/" ] ; then
		# Check for hardware
		prtconf -pv | egrep "${HARDWARE_STRING}" > /dev/null 2>&1
		hardware_result=$?
	else
		hardware_result=1
	fi

	if [ $hardware_result -eq 0 ]; then
		# Hardware is present, attach the drivers
		ADD_DRV="add_drv -b ${ROOTDIR}"
	else
		# No hardware found on the system
		# or alternate root install
		ADD_DRV="add_drv -n -b ${ROOTDIR}"
	fi

	eval ${ADD_DRV} "${DRVR_PERM}" ${DRVR_CLASS} "${DRVR_ALIASES}" ${DRVR_NAME}
	if [ $? -ne 0 ]; then
		echo "\nCommand Failed:" >&2
		echo "${ADD_DRV} "${DRVR_PERM}" ${DRVR_CLASS} "${DRVR_ALIASES}" ${DRVR_NAME}\n" >&2
		exit 1
	fi
}


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

exit 0

