#!/bin/sh
# @(#)preremove	1.1 00/06/13 Sun Microsystems
# SUNWi2cr.u preremove script
# Copyright (c) 1997-2000 by Sun Microsystems, Inc.
# All rights reserved.
#

#
#	Defining convenient variables for later
#
REM_DRV=rem_drv
DRV1=i2c
DRV2=i2cadc
DRV3=i2cgpio

#
# Select the correct rem_drv options to execute.
# Only attempt to unload the driver on a running system
#
if [ "${BASEDIR}" != "/" ]; then
	#
	# On a client,
	# modify the system files and touch /reconfigure ??
	# for reconfigure reboot ??
	#
	REM_DRV="${REM_DRV} -b ${BASEDIR}"
fi


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

#if [ `egrep -c "^(${DRV1}|${DRV2}) " ${BASEDIR}/etc/name_to_major` -eq 2 ]; then

#
#	Must remove child driver(s) before removing the nexus driver
#

#	Remove the I2CADC driver
if [ `egrep -c "^(${DRV2}) " ${BASEDIR}/etc/name_to_major` -eq 1 ]; then

#echo "${REM_DRV} ${DRV2}"
	${REM_DRV} ${DRV2} || {
		echo "\n${PKGINST}: Failed ${REM_DRV} ${DRV2}\n" >&2
		exit 1
	}

fi
#	Remove the I2CGPIO driver
if [ `egrep -c "^(${DRV3}) " ${BASEDIR}/etc/name_to_major` -eq 1 ]; then

#echo "${REM_DRV} ${DRV3}"
	${REM_DRV} ${DRV3} || {
		echo "\n${PKGINST}: Failed ${REM_DRV} ${DRV3}\n" >&2
		exit 1
	}
fi

#	Remove the I2C nexus driver
if [ `egrep -c "^(${DRV1}) " ${BASEDIR}/etc/name_to_major` -eq 1 ]; then
#echo "${REM_DRV} ${DRV1}"
	${REM_DRV} ${DRV1} || {
		echo "\n${PKGINST}: Failed ${REM_DRV} ${DRV1}\n" >&2
		exit 1
	}

fi

exit 0
