#ident	"@(#)kit:em935/pkg/postinstall	1.00"
#ident  "$Header: $"
#
#       Cogent eMASTER+ EISA XL Unix Streams Driver Install Script
#               for Univel UnixWare System V release 4.2 
#
#       ver. 1.00
#
ERR=/tmp/em935.err
TMPDIR=/tmp/em935
ERROR1="Errors have written to the file $ERR."
ERROR2="The eMASTER+ EISA XL stream driver was not installed due to an error."
ROOT=
ETC=${ROOT}/etc
NETINFO=/usr/sbin/netinfo
CONFDIR=/etc/conf
CONFBIN=${CONFDIR}/bin

# Define return values
: ${OK=0} ${FAIL=1} ${STOP=10} ${HALT=11}
set -h

param_err () {
	menu_colors warn
	menu -r -f ${MENUDIR}/menu.err -o /tmp/$$.parmerr </dev/tty >/dev/tty 2>>$ERR
	. /tmp/$$.parmerr
	rm -f /tmp/$$.parmerr
	[ "$REPLY" = "Y" -o "$REPLY" = "" ] && {
		menu -c >/dev/tty
		exit 1
	}
}


set_boardinfo () {
unset NO_IRQ NO_SIOA
unset AVAIL_IRQ IRQ_AVAIL AVAIL_SIOA SIOA_AVAIL

# get unused interrupt vectors
NO_IRQ=0
AVAIL_IRQ="//"
for j in 10 11 12 5
do
		${CONFBIN}/idcheck -v $j 
		rc=$?
		# $rc > 0 and < 100 means this intv is in use
		if [ $rc -le 0 -o $rc -ge 100  ]
		then
		NO_IRQ=`expr $NO_IRQ + 1`
		AVAIL_IRQ="${AVAIL_IRQ}${j}::${j}//"
		IRQ_AVAIL="${IRQ_AVAIL} ${j}"
		fi	
done
[ ${NO_IRQ} -eq 0 ] && {
NO_IRQ=1
AVAIL_IRQ="//*::*//"
}

# get unused I/O base addresses
NO_SIOA=0
AVAIL_SIOA="//"
for j in 0 1000 2000 3000 4000 5000 6000 7000 8000
do
                case "$j" in
		      0) Eioa=0;;
		   1000) Eioa=1fff;;
		   2000) Eioa=2fff;;
		   3000) Eioa=3fff;;
		   4000) Eioa=4fff;;
		   5000) Eioa=5fff;;
		   6000) Eioa=6fff;;
		   7000) Eioa=7fff;;
		   8000) Eioa=8fff;;
                esac
		rc=0
		[ ${j} != 0 ] && {
		${CONFBIN}/idcheck -a -l$j -u$Eioa 
		rc=$?
		}
		# $rc > 0 and < 100 means this Sioa - Eioa is in use
		if [ $rc -le 0 -o $rc -ge 100  ]
		then
		NO_SIOA=`expr $NO_SIOA + 1`
		AVAIL_SIOA="${AVAIL_SIOA}${j}::${j}//"
		SIOA_AVAIL="${SIOA_AVAIL} ${j}"
		fi	
done
[ ${NO_SIOA} -eq 0 ] && {
NO_SIOA=1
AVAIL_SIOA="//*::*//"
}

export NO_IRQ NO_SIOA
export AVAIL_IRQ IRQ_AVAIL AVAIL_SIOA SIOA_AVAIL
cnt=0
export cnt board_count
while [ $cnt -lt $board_count ]
do
	unset IRQ Sioa 
	menu_colors regular
	menu -r -f ${MENUDIR}/menu.param -o /tmp/$$.param </dev/tty >/dev/tty 2>>$ERR
	. /tmp/$$.param
	rm -f /tmp/$$.param

	case "$Sioa" in
		   0) Eioa=0;;
		1000) Eioa=1fff;;
		2000) Eioa=2fff;;
		3000) Eioa=3fff;;
		4000) Eioa=4fff;;
		5000) Eioa=5fff;;
		6000) Eioa=6fff;;
		7000) Eioa=7fff;;
		8000) Eioa=8fff;;
	esac

	if [ "$IRQ" = "*" -o "$Sioa" = "*" ]
	then
		UsedIRQ=`cat ${TMPDIR}/irqused.$$ 2> /dev/null`
		UsedSIOA=`cat ${TMPDIR}/sioaused.$$ 2> /dev/null`
		export UsedIRQ UsedSIOA
		param_err
	else
#		echo "$IRQ " >> ${TMPDIR}/irqused.$$
#		NO_IRQ=0
#		AVAIL_IRQ="//"
#		I=`echo $IRQ_AVAIL`
#		IRQ_AVAIL=""
#		for j in `echo $I`
#		do
#			grep $j ${TMPDIR}/irqused.$$ >/dev/null 2>&1
#			[ $? -ne 0 ] && {
#			NO_IRQ=`expr $NO_IRQ + 1`
#			AVAIL_IRQ="${AVAIL_IRQ}${j}::${j}//"
#			IRQ_AVAIL="${IRQ_AVAIL} ${j}"
#			}
#		done
#		[ ${NO_IRQ} -eq 0 ] && {
#		NO_IRQ=1
#		AVAIL_IRQ="//*::*//"
#		}

		[ ${Sioa} != 0 ] && {
		echo "$Sioa " >> ${TMPDIR}/sioaused.$$
		}
		NO_SIOA=0
		AVAIL_SIOA="//"
		I=`echo $SIOA_AVAIL`
		SIOA_AVAIL=""
		for j in `echo $I`
		do
			grep $j ${TMPDIR}/sioaused.$$ >/dev/null 2>&1
			[ $? -ne 0 ] && {
			NO_SIOA=`expr $NO_SIOA + 1`
			AVAIL_SIOA="${AVAIL_SIOA}${j}::${j}//"
			SIOA_AVAIL="${SIOA_AVAIL} ${j}"
			}
		done
		[ ${NO_SIOA} -eq 0 ] && {
		NO_SIOA=1
		AVAIL_SIOA="//*::*//"
		}

echo "emC\tY\t8\t6\t2\t$IRQ\t$Sioa\t$Eioa\t0\t0\t-1">>${CONFDIR}/sdevice.d/emC
echo "clone\temC_$cnt\tc:$cnt\temC" >>${CONFDIR}/node.d/emC
$NETINFO -a -d emC_$cnt
	fi

cnt=`expr $cnt + 1`
done

# clean up temp files
rm -f ${TMPDIR}/irqused.$$ ${TMPDIR}/sioaused.$$ 2>/dev/null
menu -c >/dev/tty
return 0
}

# main routine 
#
#
#  Make sure LANG environment variable is set.  If it's not set
#  coming in to this request script, then default to the C-locale.
#
[ ${LANG} ] || LANG="C"
export LANG

#
#  Set up to use menu_colors; default to C-locale if ${LANG}'s dir has
#  no menu_colors.sh
#
if [ -f /etc/inst/locale/${LANG}/menus/menu_colors.sh ]
then
        . /etc/inst/locale/${LANG}/menus/menu_colors.sh
else
        . /etc/inst/locale/C/menus/menu_colors.sh
fi

MENUDIR=${TMPDIR}/menu
DIRSAV=`pwd`

cd $TMPDIR
${CONFBIN}/idcheck -p emC 2>$ERR
if [ $? -ne 0 ]
then
	${CONFBIN}/idinstall -d emC > ${ERR} 2>&1
fi

${CONFBIN}/idinstall -a emC > $ERR 2>&1
rc=$?
if [ ${rc} -ne 0 ]
then
	${CONFBIN}/idinstall -u emC > ${ERR} 2>&1
	rc=$?
fi

cd ${DIRSAV}
[ $? -ne 0 ] && {
        echo "There was an error during package installation.
	$ERROR1 $ERROR2" > /dev/tty
        exit 1
}

# configure driver
trap 'rm -f ${TMPDIR}/irqused.$$ ${TMPDIR}/sioaused.$$ $ERR 2> /dev/null; menu -c >/dev/tty; exit 3' 1 2 3 15
menu_colors regular
menu -r -f ${MENUDIR}/menu.eth -o ${TMPDIR}/boards 2>${ERR} </dev/tty >/dev/tty 2>>$ERR
. ${TMPDIR}/boards
	rm -f ${TMPDIR}/boards
	set_boardinfo
	rc=$?
	rm -rf ${TMPDIR}/menu
	if [ ${rc} -ne 0 ]
	then
		exit $rc
	fi

#configure tcp/ip
#cnt=0
#while [ $cnt -lt $board_count ]
#do
#	echo "\nConfigure TCP/IP ... emC_${cnt}\n"
#	/etc/confnet.d/configure -i -p inet -d emC_${cnt}
#cnt=`expr $cnt + 1`
#done
# only configure first board on tcp/ip. For additional board, user has to
# run /etc/confnet.d/configure -i manually.
/etc/confnet.d/configure > /dev/null

# rebuild the kernel.
echo "\nBuilding a new kernel now ..." > /dev/tty
${CONFBIN}/idbuild </dev/tty 2>>$ERR
[ $? -ne 0 ] && {
	echo "The installation cannot be completed due to an error" > /dev/tty
	echo "in the kernel reconfiguration" > /dev/tty
	echo "$ERROR1 $ERROR2" > /dev/tty
	exit 1
}

echo "Installation of <em935> was successful." > /dev/tty
exit 10

