:
#		(C) 1989-1990 The Santa Cruz Operation, Inc.  All Rights
#		Reserved.  The user has unlimited right to use, modify
#		and incorporate this code into other products provided
#		it is used with SCO products and the user includes
#		this notice and the associated copyright notices with
#		any such application.
# This is a script for removing an LLI driver using netconfig
# 
#
# ISV (DC21x4) Modification HIstory
# ====================================
#
#  30 - Jan - 94   General cleanup and porting   
#                                                      Ilan Hindy
#
#
LIB=/usr/lib/lli
CONF=/etc/conf
PATH=/bin:/usr/bin:/etc/:$CONF/bin:$LIB
#
# Set possible return codes for this script
#
OK=0;	FAIL=1;	RELINK=2;

#
# prompt the user to answer a yes no question or 'q' to quit
# Usage:
#	prompt_yn "Message" default
prompt_yn() {
	mesg=$1
	default=$2

	while :
	do
		echo "${mesg} (y/n) [${default}] : \c"
		read result

		case $result in
		y|Y) result="Y"; return $OK;;
		n|N) result="N"; return $OK;;
		"") result=`echo $default | tr "yn" "YN"`; return $OK;;
		esac

		echo "Illegal value, please type 'y' 'n' or 'q'"
	done
}

#
# Fake up an mdevice and an sdevice for idcheck
#
makedevs() {
	rm -fr /tmp/dev$$
	mkdir /tmp/dev$$
	cd /etc/conf/cf.d
	cp mdevice /tmp/dev$$
	cd ../sdevice.d
	cat * > /tmp/dev$$/sdevice
}

cleanup() {
	cd /
	rm -fr /tmp/dev$$
	rm -fr /tmp/$base
	exit $1
}

system_dce() {

	# decrement the streams buffers we added

	currdir=`pwd`

	cd /etc/conf/cf.d
	
	echo "Modifying Streams Data Configuration Parameters"

	sparam=0
	sparam=`./configure -y NBLK2048`
        echo -n Value of NBLK2048 Changed from $sparam t
	sparam=`expr $sparam - 10`
        echo o $sparam
	idtune -f NBLK2048 $sparam
	
	sparam=0
	sparam=`./configure -y NBLK64`
        echo -n Value of NBLK64 Changed from $sparam t
	sparam=`expr $sparam - 10`
        echo o $sparam
	idtune -f NBLK64 $sparam

        cd $currdir
 
}

#
# if tcp is installed we remove the board from /etc/tcp and /etc/strcf
#
cleantcp() {
	driver=$1

	[ -f /etc/tcp ] && {
		sed "/$driver/d" /etc/tcp > /tmp/bog$$
		cp /tmp/bog$$ /etc/tcp
	}
	[ -f /etc/strcf ] && {
		sed "/$driver/d" /etc/strcf > /tmp/bog$$
		cp /tmp/bog$$ /etc/strcf
	}
	rm -f /tmp/bog$$
}

# restorevector check for sio (vecs 3-4) and pa drivers (vec 7)
# if these are being released by lli then let us restore them to sio or pa
restorevector() {
	currdir=`pwd`
	cd /etc/conf/cf.d

	intvector=$1
	test="0"
	[ "$intvector" = "3" ] && test="1"
	[ "$intvector" = "4" ] && test="1"
	if [ "$test" = "1" ]
	then
		prompt_yn "Restore vector $intvector to sio driver" y
		[ "$result" = "Y" ] && {
			echo "Restoring vector $intvector to sio driver"
			siomajor=`./configure -j sio`
			./configure -m $siomajor -c -v $intvector -a -Y
			sed "s/	iHct	sio	/	iHctr	sio	/" mdevice > /tmp/mdevice
			mv /tmp/mdevice mdevice
			return $OK
		}
	fi
	test="0"
	[ "$intvector" = "7" ] && test="1"
	if [ "$test" = "1" ]
	then
		prompt_yn "Restore vector $intvector to pa driver" y
		[ "$result" = "Y" ] && {
			echo "Restoring vector $intvector to pa driver"
			pamajor=`./configure -j pa`
			./configure -m $pamajor -c -v $intvector -a -Y
			sed "s/	ictoH	pa	/	ictoHr	pa	/" mdevice > /tmp/mdevice
			mv /tmp/mdevice mdevice
			return $OK
		}
	fi
	return $OK
}

# main()
#

#
# get the name of the init script being run, since one script
# is used for multiple drivers; get the number at the end of the
# script's name
#
if [ $# -gt 1 ]
then
	name_below=$1; if_below=$2
	name_above=$3; if_above=$4
	configure=$5
fi

base=`basename $0`

drv=`echo $base | sed -e 's/[0-9]*$//`
bd=`expr $base : '.*\(.\)'`

echo "NODE=/etc/conf/node.d/$base" >/tmp/$base.src
chmod 777 /tmp/$base.src

if [ "$configure" = "N" -o "$configure" = "n" ]
then
	exit $OK
fi

#
# check to see if the driver is already in the kernel link-kit so we can
# either add it or update it later on
#
makedevs
idcheck -p $base
if [ $? -gt 16 ]
then
	installed="TRUE"
else
	installed="FALSE"
fi

echo "Removing $base..."

if [ "$installed" = "FALSE" ]
then
	cleanup $OK
fi

#
# Check and Manage our internal chains file.
#
# if our board (base) is not in the chain then we always remove the board
# if our board is in the chain then we check for the chain and remove it.
# if after our chain is removed from the chains file the board is still there
#    then we do not remove it.
#
chains=/usr/lib/lli/chains
chain=$base:$name_above
grep $base: $chains > /dev/null 2>& 1 && {
	grep $chain $chains > /dev/null 2>& 1 || {
		cleanup $OK
	}
	# remove our chain
	sed -e "/$chain/d" < $chains > /tmp/bog$$
	cp /tmp/bog$$ $chains
	rm -f /tmp/bog$$
	# Check if it is there in another chain
	grep $base: $chains > /dev/null 2>& 1 && {
		cleanup $OK
	}
}

# Check if we are board zero that no other boards of this type are configured
[ "$bd" -eq "0" ] && {
	grep ${drv}[1-3] $chains > /dev/null 2>& 1 && {
		echo "Warning, You are removing board 0 before other boards"
		echo "You must remove the other $drv boards next"
		echo "or your link-kit will be left in an invalid state"
	}
}


#
# Do board dependent processing
#
case $drv in
	dce)	system_dce $bd;;
esac

# if [ $bd -gt 0 ]
# then
#	idinstall -d -e $base
#	cleanup $RELINK
# fi

# get the interrupt vector for the board that we are removing
cd /etc/conf/sdevice.d
ivec=`awk '{ print $6 }' < $base`

cd /tmp; rm -rf $base

mkdir $base; cd $base

echo "${base}\tN\t1\t5\t1\t9\t0\t0\t0\t0" >System
idinstall -u -e -s $base

cleantcp $base
restorevector $ivec
cleanup $RELINK
