#!/bin/sh
#
# Copyright 1996-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#pragma ident	"@(#)i.drivers	1.17	03/05/02 SMI"

#  This script can be either the 'i.drivers' class action script for
#  the LOM root package (SUNWlomr), or the 'postpatch' script for the
#  corresponding patch.

exec 1>&2						# all output to stderr
SCRIPT=`basename $0`
case "$SCRIPT"
in
*patch)	ACTION=1								;;
i.*)	ACTION=2; PARAM=$1; ROOTDIR=${PKG_INSTALL_ROOT:-$BASEDIR}		;;
*)	echo "$SCRIPT (FATAL): Don't know what I'm supposed to be!"; exit 1	;;
esac
echo "$SCRIPT (INFO): Starting"

if [ $ACTION = 1 ]
then
	if [ "`pkginfo -R $ROOTDIR SUNWlomr 2>/dev/null`" = "" ]
	then
		echo "$SCRIPT (SUCCESS): Root package not installed, nothing to do"
		exit 0
	fi
fi

# Add variables to define the driver specific processing configuration
DRVLIST="lomp lomv:lomv:SUNW,lomv:SUNW,lomv+lomv:\M0 lom:lom:SUNW,lomh+SUNW,lom:SUNW,lomh+SUNW,lom"
DRVSOLD="lom lomp lomv lomh"
DRVPERM='* 0644 root sys'

DLTAB="$ROOTDIR/etc/devlink.tab"
DLTMP=/tmp/devlink.$$

PATH=$PATH:/usr/bin:/a/usr/bin


DRVS=							# list of drivers to install
TMPDRV="/tmp/$SCRIPT.$$"
if [ $ACTION = 2 ]					# class action script...
then
	while read SRC DST				# read and copy files
	do
		test -f $DST && rm -f $DST
		echo "$SCRIPT (INFO): Installing $SRC"
		cp -p $SRC $DST
		echo "`basename $DST`" >>$TMPDRV	# save driver name for later
	# Add the driver name into the driver name list for later
	done
else							# post patch script...
	for D in $DRVLIST				# use list of drivers
	do
		echo "$D" | sed -e 's/:.*$//' >>$TMPDRV	# save driver name
	done
fi
echo

set -- `sort -u $TMPDRV`; DRVS=$*; rm -f $TMPDRV	# sort/unique driver list
if [ "$DRVS" = "" ]					# see if any drivers found
then
	echo "$SCRIPT (WARNING): No new drivers identified"
	exit 0
fi
echo "$SCRIPT (INFO): Identified drivers '$DRVS'"


if [ \( $ACTION = 2 \) -a \( "$PARAM" != "ENDOFCLASS" \) ]
then							# see if need to do anything
	echo "$SCRIPT (SUCCESS): Finished"
	exit 0
fi


if [ ! -f $DLTAB ]					# check 'devlink.tab' access
then
	echo "$SCRIPT (ERROR): file '$DLTAB' does not exist"
	exit 1
else
	if [ ! -w $DLTAB ]
	then
		echo "$SCRIPT (ERROR): file '$DLTAB' is not writeable"
		exit 1
	fi
fi


if [ "$ROOTDIR" = "/" ]					# select add_drv(1M) options
then
	DRVOPT=""					# no options
else
	DRVOPT="-b $ROOTDIR"				# with base directory
fi


T=`echo "\t\c"`
STATUS=0
for D in ${DRVSOLD}
do
	DRV=`echo "$D" | sed -e 's/=.*$//'`
	ALIAS=`echo "$D" | sed -e 's/^.*=//'`
	test "$DRV" = "$ALIAS" && ALIAS=

	echo "$SCRIPT (INFO): Cleaning up old driver '$DRV'..."

	if grep "^${DRV} " $ROOTDIR/etc/name_to_major >/dev/null 2>&1
	then
		echo "driver is present, removing it"
		rem_drv ${DRVOPT} ${DRV}
	fi

	for D in ${DRV} ${ALIAS}
	do
		R=`egrep "name=(SUNW,)?${D}[ 	]" $DLTAB`
		if [ "$R" ]
		then
			echo "Cleaning up old devlink entry  '$R'"
			egrep -v "name=(SUNW,)?${D}[ 	]" $DLTAB >$DLTMP
			cat $DLTMP >$DLTAB
			rm -f $DLTMP
		fi
	done
done


DRVS1=`echo ",$DRVS," | sed -e 's/ /,/g'`		# for member test later
for D in ${DRVLIST}
do
	set -- `echo "$D" | sed -e 's/:/ /g'`		# decode driver spec
	NAME=$1
	DRV=${2:-$NAME}					# add_drv name
	ALIASES=`echo "$3" | sed -e 's/+/ /g'`		# add_drv aliases
	LINK=`echo "$4" | sed -e 's/+/ /g'`		# devlink.tab names
	SPEC="$5"					# devlink.tab spec
	: ${LINK:=$DRV} ${SPEC:=$DRV}

	echo
	if expr "$DRVS1" : ".*,$DRV," >/dev/null
	then
		echo "$SCRIPT (INFO): Adding driver '$NAME'..."
		echo "  driver  = '$DRV'"
		echo "  aliases = '$ALIASES'"
		echo "  link    = '$LINK'"
		echo "  spec    = '$SPEC'"
		echo

		for D in ${LINK}
		do
			if expr "${D}" : '.*bus$' >/dev/null
			then
				echo "Not adding bus driver devlink entry"
				continue
			fi

			R="type=ddi_pseudo;name=${D}$T$SPEC"
			echo "Adding devlink entry '$R'"
			echo "$R" >>$DLTAB
		done

		echo "adding driver with aliases '$ALIASES' perm '$DRVPERM'"
		add_drv ${DRVOPT} -m "${DRVPERM}" ${ALIASES:+"-i $ALIASES"} ${DRV}; S=$?
		if [ $S != 0 ]
		then
			echo "$SCRIPT (ERROR): Failed to add driver, status $S"
			STATUS=$S
		fi
	else
		echo "$SCRIPT (NOTICE): Not selected to install driver '$DRV'"
	fi
done


if [ $STATUS = 0 ]
then
	echo "$SCRIPT (SUCCESS): Finished"
else
	echo "$SCRIPT (ERROR): Failed with status $STATUS"
fi
echo
exit $STATUS
