#! /usr/bin/sh
#
# ident "@(#)postpatch 1.4     02/10/10 SMI"
#
# Copyright 2002 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
DRV1=uflash
DRV1_ALIAS="userflash"
OS_64BIT="sparcv9"
DRVPERM='* 0666 root sys'
#
#	Check to see if the hardware is there or not.
#	Consider it exists only if *exactly* the 3 nodes are found
#
nodes_found=`prtconf -p | egrep -c "\'${DRV1_ALIAS}\'"`


#
# 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

# Only attempt to attach the driver on a system running 64_bit OS or
# running 32-bit with 32-bit user flash PROM pkg already installed.
# Otherwise
# modify the system files only; i.e. don't try to "attach"

         isalist | egrep  "${OS_64BIT}" > /dev/null 2>&1
         running_64B=$?

# return code   0 = 64-bit OS is running on the host.
# return code   1 = 32-bit OS is running on the host.

         if [ "$running_64B" = "1" ]; then

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

             egrep -s "^(${DRV1}) " ${PKG_INSTALL_ROOT}/etc/name_to_major
             uflash_instld=$?
             if [ "$uflash_instld" = "1" ]; then
                echo "\nNOTE: System is running 32-bit OS, 64-bit userflash"
                echo "timer pkg, SUNWufrx.u is installed, however 64-bit driver"
                echo "will note be attached. To attach boot -rv in 64-bit"
                echo "mode.\n"

                # modify the system files only; i.e. don't try to "attach"
                #
                 ADD_DRV="${ADD_DRV} -n -b ${PKG_INSTALL_ROOT}"
                 ${ADD_DRV} -m "${DRVPERM}" -i '"'"${DRV1_ALIAS}"'"' ${DRV1}
  
                 exit 0
             fi

         fi

	case ${nodes_found} in
		#
		# On a running system with hardware,
		# modify the system files and attach the driver
		#
		1 )
			ADD_DRV="${ADD_DRV} -b ${PKG_INSTALL_ROOT}"
			;;
		#
		# On a running system of the relevant arch with *no* hardware,
		# modify the system files only; i.e. don't try to "attach"
		#
		* )
			ADD_DRV="${ADD_DRV} -n -b ${PKG_INSTALL_ROOT}"
			;;
	esac
else
	#
	# On a client,
	# modify the system files and touch /reconfigure ??
	# for reconfigure reboot ??
	#
	ADD_DRV="${ADD_DRV} -n -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.
#

#
# The problem here is if we're installing on the service,
# /export/Solaris_8, there is neither an etc nor a dev directory.
# So only do the add_drv if we're not on /export/Solaris_8.
#

if [ -f ${PKG_INSTALL_ROOT}/etc/name_to_major ] ; then
    egrep -s "^(${DRV1}) " ${PKG_INSTALL_ROOT}/etc/name_to_major 2> /dev/null || {


#	Add user flash PROM  driver
	rm -f ${PKG_INSTALL_ROOT}/dev/uflash 
	${ADD_DRV} -m "${DRVPERM}" -i '"'"${DRV1_ALIAS}"'"' ${DRV1} || {
		echo "\n${PKGINST}: Failed ${ADD_DRV} -m "'"'"${DRVPERM}"'" -i "'"${DRV1_ALIAS}"'"'" ${DRV1}\n" >&2
		exit 1
	}
    }

    # Add the user flash PROM driver node in devlink.tab file.
    egrep 'flashprom' ${PKG_INSTALL_ROOT}/etc/devlink.tab 2> /dev/null || {
	echo 'type=ddi_pseudo;name=flashprom	uflash\N0' >> ${PKG_INSTALL_ROOT}/etc/devlink.tab
}

fi

exit 0

