#!/bin/sh
#
#ident	"@(#)postpatch	1.8	05/11/09 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH="/usr/bin:/usr/sbin:${PATH}"
ARCH=i386
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
}

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

	DRVR_NAME=qlc
	DRVR_PERM="-m '* 0600 root sys'"
	DRVR_CLASS="-c fibre-channel"
	DRVR_ALIASES_sparc="-i '\"pci1077,2200\" \"pci1077,2300\" \"pci1077,2312\" \"pci1077,2422\" \"pciex1077,2432\" \"pci1077,2432\"'"
	DRVR_ALIASES_i386="-i '\"pci1077,2200\" \"pci1077,2300\" \"pci1077,2312\" \"pci1077,132\" \"pci1077,2422\" \"pciex1077,2432\" \"pci1077,2432\"'"

	HARDWARE_STRING_sparc="SUNW,qlc|pci1077,2422|pciex1077,2432|pci1077,2432"
	HARDWARE_STRING_i386="SUNW,qlc|pci1077,132|pci1077,2422|pciex1077,2432|pci1077,2432"

	if [ ${ARCH} = "sparc" ]; then
		DRVR_ALIASES=$DRVR_ALIASES_sparc
		HARDWARE_STRING=$HARDWARE_STRING_sparc
	elif [ ${ARCH} = "i386" ]; then
		DRVR_ALIASES=$DRVR_ALIASES_i386
		HARDWARE_STRING=$HARDWARE_STRING_i386
	else
		echo "\n$0 Failed: Hardware is not supported.\n" >&2
	fi

	# 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
