#ident	"@(#)postinstall	1.13	99/03/02 SMI"
#
# Copyright (c) 1999, by Sun Microsystems, Inc.
# All Rights Reserved.
#

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH
EXIT=0

ARCH="`uname -p`"

#
# Routine to remove current entries for a device in /devices,
# /dev, and /etc/path_to_inst.
#
RmDevice()
{
	rm -f $1

	linkslist=`find dev -type l -print`
	for l in $linkslist
	do
		dorm=`ls -l $l | grep $1`
		if [ "$dorm"X != "X" ]
		then
			rm -f $l
		fi
	done

	instname=`expr $1 : devices'\(.*'$m'.*\):.*`
	cp $BASEDIR/etc/path_to_inst /tmp/p2i.$$
	cat /tmp/p2i.$$ | 
	    sed '\?'$instname'?d' > /tmp/np2i.$$
	if [ -s /tmp/np2i.$$ ]
	then
		mv /tmp/np2i.$$ $BASEDIR/etc/path_to_inst
		chmod 444 $BASEDIR/etc/path_to_inst
		chown root $BASEDIR/etc/path_to_inst
		chgrp sys $BASEDIR/etc/path_to_inst
	fi
	rm /tmp/p2i.$$
}
# This function will check if the module has an entry in etc/name_to_major
# If not simply calls add_drv with the arguments given. If there is
# such an entry in name_to_major file, it adds entries in driver_aliases
# driver_classes and minor_perm if necessary.
# The syntax of this function is the same as add_drv.

check_add_drv()
{
	basedir=/
	alias=""
	class=""
	ADD_ALIAS=0
	ADD_CLASS=0
	ADD_MINOR=0
	OPTIND=1
	IS_NET_DRIVER=0
	
	cmd="add_drv"
	
	while getopts i:b:m:c:n  opt
	do
		case $opt in
			i )     ADD_ALIAS=1
				alias=$OPTARG
				cmd=$cmd" -i '$alias'"
				;;
			m )     ADD_MINOR=1
				minor=$OPTARG
				cmd=$cmd" -m '$minor'"
				;;
			c)      ADD_CLASS=1
				class=$OPTARG
				cmd=$cmd" -c $class"
				;;
			b)      basedir=$OPTARG
				cmd=$cmd" -b $basedir"
				;;
			n)      IS_NET_DRIVER=1
				;;
			\?)     echo "check_add_drv can not handle this option"
				return
				;;
			esac
	done
	shift `/usr/bin/expr $OPTIND - 1`

	drvname=$1
	
	cmd=$cmd" "$drvname

	drvname=`echo $drvname | /usr/bin/sed 's;.*/;;g'`

	/usr/bin/grep "^$drvname[       ]" $basedir/etc/name_to_major > /dev/null 2>&1

	if [ $? -ne 0 ]
	then
		eval $cmd
	else
		# entry already in name_to_major, add alias, class, minorperm
		# if necessary
		if [ $ADD_ALIAS = 1 ]
		then
			for i in $alias
			do
				/usr/bin/egrep "^$drvname[      ]+$i" $basedir/etc/driver_aliases>/dev/null 2>&1
				if [ $? -ne 0 ]
				then
					echo "$drvname $i" >> $basedir/etc/driver_aliases
				fi
			done
		fi
		if [ $ADD_CLASS = 1 ]
		then
			/usr/bin/egrep "^$drvname[ 	]+$class( |	|$)" $basedir/etc/driver_classes > /dev/null 2>&1
			if [ $? -ne 0 ]
			then
				echo "$drvname\t$class" >> $basedir/etc/driver_classes
			fi
		fi
	
		if [ $ADD_MINOR = 1 ]
		then
			/usr/bin/grep "^$drvname:" $basedir/etc/minor_perm > /dev/null 2>&1
			if [ $? -ne 0 ]
			then
				minorentry="$drvname:$minor"
				echo $minorentry >> $basedir/etc/minor_perm
			fi
		fi
	fi

		# The following clone device/dev is needed for Custom Jumpstart

		if [ $IS_NET_DRIVER -eq 1 ]
		then
			CLONE_DEVICE=devices/pseudo/clone@0:$drvname
			set `/usr/bin/grep "^clone[     ]" $basedir/etc/name_to_major`
			CLONE_MAJ=$2
			set `/usr/bin/grep "^$drvname[  ]" $basedir/etc/name_to_major`
			DRIVER_MAJ=$2
			mknod $basedir/$CLONE_DEVICE c $CLONE_MAJ $DRIVER_MAJ
			chmod 600 $basedir/$CLONE_DEVICE
			chgrp sys $basedir/$CLONE_DEVICE
			chown root $basedir/$CLONE_DEVICE
			ln -s ../$CLONE_DEVICE $basedir/dev/$drvname
		fi

}

#
# Several mouse drivers on the Intel platform will have different
# device names with SunOS 5.6 than with previous releases.  We want to
# remove the old names at this time so that the configuration reboot
# will cleanly create the new device names.
#
if [ "${ARCH}" = "i386" ]
then

	for m in logi msm kdmouse asy
	do
		cd ${BASEDIR}
		devlist=`find devices -name $m"*" -print`

		for d in $devlist
		do
			prefix=`expr $d : '\(.*'$m'\).*'`
			fulldev=`basename $d`
			ioaddr=`expr $fulldev : $m'@\(.*\),.*'`

			#
			# Chance to bail now if we don't appear to need
			# to do the conversion.
			#
			if [ "$ioaddr" = "1" ]
			then
				continue
			else
				RmDevice $d
			fi		
		done
	done
	
	touch ${BASEDIR}/reconfigure

	DELCONF1="platform/i86pc/kernel/drv/logi.conf"
	DELCONF2="platform/i86pc/kernel/drv/msm.conf"

	if [ -f $BASEDIR/$DELCONF1 ] ; then
		rm $BASEDIR/$DELCONF1
	fi

	if [ -f $BASEDIR/$DELCONF2 ] ; then
		rm $BASEDIR/$DELCONF2
	fi
fi

exit 0
