#!/sbin/sh
#
#  Copyright (c) 1999 by Sun Microsystems, Inc.
#  All rights reserved.
#
#pragma ident	"@(#)postinstall.sh	1.3	99/01/01 SMI"

check_add_drv()
{
	if [ "$BASEDIR" = "" ]; then
		BASEDIR=/  
	fi
	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
	
}
check_add_drv -i '"pcie11,4070" "pcie11,4082" "pcie11,4083" "pcie11,409a" "pcie11,409b" "pcie11,409c" "pcie11,409d" "pcie11,4091" "pcie11,4080" "pci103c,3225" "pci103c,3211" "pci103c,3212" "pci103c,3234" "pci103c,3235" "pci103c,3223" "pci103c,3237"' -b "$BASEDIR" -c scsi cpqary3
exit 0
