#! /usr/bin/sh
#
# ident "@(#)postpatch 1.13     03/01/31 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
#	Defining convenient variables for later
#
PKG_INSTALL_ROOT=$ROOTDIR

ADD_DRV=add_drv
REM_DRV=rem_drv
DRV1=i2c
DRV2=i2cadc
DRV3=i2cgpio
DRV1_NODENAME=i2c
DRV2_NODENAME=adc
DRV3_NODENAME=gpio
DRV1_PATTERN=8584
DRV2_PATTERN=8591
DRV3_PATTERN=8574
DRVPERM="* 0644 root sys"
DRV2_PERM="40* 0644 root sys,* 0444 root sys"
DRV3_PERM="* 0644 root sys"

#
# Select the correct add_drv options to execute.
# Only attempt to attach the driver
# on a running system with the hardware present.
#
if [ "${PKG_INSTALL_ROOT}" = "/" ]; then
	#
	# No need to add_drv if the running system is of a different arch
	# than the package
	#
	karch=`uname -p`.`uname -m`
	if [ "${karch}" != "${ARCH}" ]; then
		exit 0
	fi
#
#	Check to see if the hardware is there or not.
#	Consider it exists only if at least 1 nodes is found
#
	nodes_found=`prtconf -p | egrep -c "\'${DRV1_NODENAME}\'|\'${DRV2_NODENAME} | \'${DRV3_NODENAME}\'\'"` 

	case ${nodes_found} in
		#
		# On a running system of the relevant arch with *no* hardware,
		# modify the system files only; i.e. don't try to "attach"
		#
		0 )
			ADD_DRV="${ADD_DRV} -n"
			;;
		#
		# On a running system with hardware,
		# modify the system files and attach the driver
		#
		* )
#			ADD_DRV="add_drv"
			;;
	esac
else
	#
	# On a client,
	# modify the system files and touch /reconfigure ??
	# for reconfigure reboot ??, do *not* attach
	#
	ADD_DRV="${ADD_DRV} -n -b ${PKG_INSTALL_ROOT}"
	REM_DRV="${REM_DRV} -b ${PKG_INSTALL_ROOT}"
fi


#
#	(Rudimentary) Test to see if add_drv has already been run before:
#   o	If found no aliases, add_drv hasn't been run before, so run it.
#   o	Else, add_drv has run once already, so don't run it again.
#

#
#	To extract all aliases of a node, the complicated-looking command does:
#   o	Execute prtconf -vp
#   o	Retain only line matching pattern.
#   o	Retain only the 1st line that matches.
#   o	Translate ' to " (i.e. single-quote to double-quote).
#   o	Remove all:
#		" +" pattern
#		Everything before the colon.
#		Trailing "*generic"
#   o	The end-result is a list of aliases: "alias1" "alias2" ... "aliasn"
#

egrep -s "^(${DRV1}) " ${PKG_INSTALL_ROOT}/etc/name_to_major 2> /dev/null || {
	 DRV1_ALIAS='"i2cpcf,8584"'
#	Nexus driver shouldn't have an entry in minor_perm so don't use -m flag.
#echo "${ADD_DRV} -i '${DRV1_ALIAS}' ${DRV1}"
	${ADD_DRV} -i "${DRV1_ALIAS}" ${DRV1} || {
		echo "\n${PKGINST}: Failed ${ADD_DRV} -i '${DRV1_ALIAS}' ${DRV1}\n" >&2
		exit 1
	}
}

egrep -s "^(${DRV2}) " ${PKG_INSTALL_ROOT}/etc/name_to_major 2> /dev/null || {
	DRV2_ALIAS='"i2cpcf,8591" "i2ctype,9"'
#echo "${ADD_DRV} -i '${DRV2_ALIAS}' -m '${DRV2_PERM}' ${DRV2}"
	${ADD_DRV} -i "${DRV2_ALIAS}" -m "${DRV2_PERM}" ${DRV2} || {
		echo "\n${PKGINST}: Failed ${ADD_DRV} -i '${DRV2_ALIAS}' -m '${DRV2_PERM}' ${DRV2}\n" >&2
		exit 1
	}
}

egrep -s "^(${DRV3}) " ${PKG_INSTALL_ROOT}/etc/name_to_major 2> /dev/null || {
	DRV3_ALIAS='"i2cpcf,8574a" "i2ctype,7" "i2cpcf,8574" "i2ctype,4"'
#echo "${ADD_DRV} -i '${DRV3_ALIAS}' -m '${DRV3_PERM}' ${DRV3}"
	${ADD_DRV} -i "${DRV3_ALIAS}" -m "${DRV3_PERM}" ${DRV3} || {
		echo "\n${PKGINST}: Failed ${ADD_DRV} -i '${DRV3_ALIAS}' -m '${DRV2_PERM}' ${DRV3}\n" >&2
		exit 1
	}
}

exit 0

