:
#		(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
#  11 - Sep - 95   Add support to incremental interface in "netconfig"
#                  This code is based on Avrahamf's code.
#                                                      Rivki Vizen.
#
#
#
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;
#
# Set Maximize number of boards
#
MAX_BD=3
#
# 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
}

#  This function creates string that contains the number between $1 and $2
# and echo it to the standart output.
get_range() {
i=$1
max_range=$2
range=""
while [ $i -le $max_range ]
do
   range="$range $i"
   i=`expr $i + 1`
done
echo $range
}


#  This function checks if board $1 is allready inatalled.
ins_bd() {
if /bin/grep ${drv}$1 $chains > /dev/null 2>&1
then return $OK
else return $FAIL
fi
}

#  This function removes the files (if they exsist) of board $1 in the
# netconfig directories,if they exist.

rmv_bd_files () {
  [ -f $netconfigdir/info/${drv}$1 ] && {
    rm -f $netconfigdir/info/${drv}$1
    rm -f $netconfigdir/init/${drv}$1
    rm -f $netconfigdir/remove/${drv}$1
    rm -f $netconfigdir/reconf/${drv}$1
  }
}

#  This function adds board $1's files (if they don't exsist) , to
# the netconfig dircetories
add_bd_files () {

   newboard=$1
   currdir1=`pwd`
   [ -f $netconfigdir/info/${drv}$newboard ] && return

   cd $netconfigdir/info
   newfile=${drv}$newboard
   cp ${drv}0 $newfile
   sed -e '/^NAME=.*'"[^0]"'/s/0\"/'$newboard'\"/p' $newfile > /tmp/bog$$
   sed -e '/^DESCRIPTION=.*'"[^0]"'/s/0\"/'$newboard'\"/p' /tmp/bog$$ > $newfile
   rm -r /tmp/bog$$
   chown bin $newfile
   chgrp bin $newfile
   chmod 750 $newfile

   cd $netconfigdir/init
   ln ${drv}0 ${drv}$newboard > /dev/null 2>&1

   cd $netconfigdir/remove
   ln ${drv}0 ${drv}$newboard > /dev/null 2>&1

   cd $netconfigdir/reconf
   ln ${drv}0 ${drv}$newboard > /dev/null 2>&1

   cd $currdir1
}

#  This function checks the consistantion of the netconfig directories,
# compareable to the chains file in lli directory. The function checks that
# the upper installed board nunber and all the lower boards have there files,
# and if all the lower boards are installed, its add new board files for the
# custemer use.

check_bds() {

bds=`get_range 0 $MAX_BD`
not_ins_bds=""                  #contains a list of not installed boards.
max_ins_bd=""                   #contains the number of the biggest installed board.


for i in $bds
do
  if ins_bd $i
  then
    max_ins_bd=$i
  else
    not_ins_bds="$not_ins_bds $i"
  fi
done

if [ -z "$max_ins_bd" ]
then
  # If none of the boards are installed we remove all the
  #  upper boards from netconfig directories.
  upper_bds=`get_range 1 $MAX_BD `
  for j in $upper_bds
  do
    rmv_bd_files $j
  done
  return
else
 #adds the biggest installed board to netconfig directories.
 add_bd_files $max_ins_bd
fi


if [ $max_ins_bd -eq $MAX_BD ]
then
   return
else
   lower_bd=`expr $max_ins_bd - 1`
   upper_bd=`expr $max_ins_bd + 1`
   lower_bds=`get_range 0 $lower_bd`
   upper_bds=`get_range $upper_bd $MAX_BD `
   for i in $lower_bds
   do
     if echo $not_ins_bds | grep $i >/dev/null 2>&1
     then
     #  If there is a lower board lower then the max board installed that is not
     # installed yet, then we shouldn't add upper board to the netconfig dirs.
       for j in $upper_bds
       do
         rmv_bd_files $j
       done
       return
     fi
   done

   #  If all the boards until 'max board installed' are installed, we shoul add
   # the next board files to netconfig dir and delete the upper.
   add_bd_files $upper_bd
   upper_bd=`expr $upper_bd + 1`
   upper_bds=`get_range $upper_bd $MAX_BD`
   for j in $upper_bds
   do
      rmv_bd_files $j
   done
fi
}

cleanup() {

	exit_stat=$1
	
	cd /
	rm -fr /tmp/dev$$
	rm -fr /tmp/$base

	if [ "$exit_stat" -eq "$RELINK" ]
	then
	   check_bds
        fi

        exit $exit_stat
}

system_actn() {

	# 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 : '.*\(.\)'`
netconfigdir=/usr/lib/netconfig

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
	actn)	system_actn $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
