:
#
# @(#) mgxconfig.sh 1.6 93/02/26 
#
# Copyright (C) 1992 The Santa Cruz Operation, Inc.
#
# The information in this file is provided for the exclusive use of the
# licensees of The Santa Cruz Operation, Inc.  Such users have the right 
# to use, modify, and incorporate this code into other products for 
# purposes authorized by the license agreement provided they include
# this notice and the associated copyright notice with any such 
# product.  The information in this file is provided "AS IS" without 
# warranty.
#
#
#	Driver configuration script to be run after installing the
#	Microfield Graphics Server Supplement for ODT
#
#	Copyright (c) 1992  Microfield Graphics, Inc.
#	Copyright (c) 1992  Santa Cruz Operation
#	All Rights Reserved
#
# 	Derived from /usr/lib/custom/tcprt.rmv
#


##### ENVIRONMENT DEFINITIONS #####

PATH=/bin:/usr/bin:/etc
LANG=english_us.ascii	# Used when scripts are internationalized.
export PATH LANG

ERRFILE=/tmp/mgx.err
DRIVER=$1
INSTSTAT=0		# flag - driver is installed or not


##### RETURN VALUES #####

: ${OK=0} ${FAIL=1} ${DUP=2} ${EXIT=3} ${OKDEL=4} ${STOP=10} ${HALT=11} 


##### PATHS #####

CONFDIR=/etc/conf
CONFBIN=${CONFDIR}/bin
TEMPLATE=/usr/lib/vidconf/ihv/microfield/kernelconf


##### MESSAGES #####

DNAME="Microfield Graphics X8 Driver"
ERRORLOG="\nErrors have been written to the file $ERRFILE."
ABORTING="Aborting the configuration.\n\tThe driver has not been modified.\n"
NOTOOLS="The Installable Driver utilities are not installed.\n\tThe driver cannot be installed without them.\n"
NODRIVER="The /dev/${DRIVER} installation files are not in \n\t${TEMPLATE}.\n\tThe driver cannot be installed without them.\n"
DELDRIVER="The ${DNAME} /dev/${DRIVER} has been removed from kernel configuration files.  You must relink the kernel for this change to take affect.\n"


##### FUNCTION DEFINITIONS #####

#**** Traps for critical and non critical code. ****

set_trap()  {	
	trap 'echo "\nInterrupted! Exiting ..."; exit ${FAIL}' 1 2 3 15
}

unset_trap()  {
	trap '' 1 2 3 15
}

clearscr() {
	[ "$TERM" ] && [ -x /bin/clear ] && clear 2> /dev/null
}

 
#**** Prompt for continuation. ****
pause() {
	while	echo "\nHit [Enter] to continue\c">&2
	do	read yn rest
		case $yn in
		*)	return $OK			;;
		esac
	done
}

#**** Print an error message. ****

error() {
	echo "\nError:\t$*" >&2
}


#**** Prompt for yes or no answer - returns non-zero for no. ****

getyn() {
	while	echo "\n$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		[yY])	return ${OK} 			;;
		[nN])	return ${FAIL}			;;
		*)	echo "Please answer y or n" >&2	;;
		esac
	done
}

#**** Delete the driver (if it is installed) ****

mgxdel () 
{
	if [ "$INSTSTAT" != 0 ]
	then
     	    /etc/conf/bin/idinstall -d ${DRIVER}
	    # idinstall -d doesn't pull mgx from sdevice
	    sed -e "/mgx/d" < $CONFDIR/cf.d/sdevice > $CONFDIR/cf.d/sdevice.new
	    mv $CONFDIR/cf.d/sdevice.new $CONFDIR/cf.d/sdevice
	    rm -f /dev/mgx 	# delete the old device node if left around

   	fi
    	return ${OK}
}


#************************* PRIMARY FUNCTION ******************************

mgxcfg () 
{
RETVCHK=0


REQINTV=10
REQLADD=10D
REQHADD=10F
REQLMEM=B0000
REQHMEM=B1FFF
ADDRLIST="A0000 B0000 E0000 D00000 E00000 F00000"

INT=${REQINTV}
LADD=${REQLADD}
HADD=${REQHADD}
LMEM=${REQLMEM}
HMEM=${REQHMEM}


if [ ! -d ${TEMPLATE}/${DRIVER} ]
then
	error ${NODRIVER}
	return ${FAIL}
fi

cd ${TEMPLATE}/${DRIVER}

#**** Get Driver Configuration ****

/etc/conf/bin/idcheck -p ${DRIVER} 2> /dev/null
INSTSTAT=$?
if [ "$INSTSTAT" != 0 ]
then
	PROMPT="Strike the ENTER key to use the defaults,\nto change the defaults type C and strike ENTER,\nto delete the driver and exit, type D and strike ENTER,\nto cancel the installation type Q and strike ENTER."
else
	PROMPT="Strike the ENTER key to use the defaults,\nto change the defaults type C and strike ENTER,\nto cancel the installation type Q and strike ENTER."
fi

echo "\nThis script will configure /dev/${DRIVER} using the following defaults:"
echo "\n\tInterrupt Vector = ${INT}, I/O Port = 0x${LADD}, Memory Address = 0x${LMEM}"

DANSWER=""
message -d $PROMPT
read DANSWER

if [ "x$DANSWER" = xQ -o "x$DANSWER" = xq ]
then
	error ${ABORTING}
	return $FAIL
fi

# D | d option only provided if driver already installed
if [ "$INSTSTAT" != 0 ]
then 
  if [ "x$DANSWER" = xD -o "x$DANSWER" = xd ]
  then
	mgxdel
	message -d ${DELDRIVER}
	exit $OKDEL
  fi
fi 

if [ "x$DANSWER" = xC -o "x$DANSWER" = xc ]
then
	# delete the old driver first otherwise the previously used
	# intr, I/O port and memory selections are not available.
	[ "$INSTSTAT" -ne 0 ] && mgxdel

	# find out what interrupts are available and then
	# prompt the user to select one

	VECTMSG=""
	# 11 is a valid vector but it gets used by SCSI but not reported.
	for VECT in 3 5 7 9 10 12 15
	do
		${CONFBIN}/idcheck -v $VECT
		RETVCHK=$?
		if [ "$RETVCHK" = 0 ]
		then
			VECTMSG="${VECTMSG}\n\t${VECT}"
			VECTLIST="${VECTLIST} ${VECT}"
		fi
	done
	
	if [ "$VECTLIST" = "" ]
	then
	    echo "\nThe ${DNAME} cannot be installed\nbecause all interrupts \
are in use!\n"
	    return $FAIL
	else
		while true
		do
			VANSWER=""
			message -d "Available interrupt(s) for the ${DNAME}: \
${VECTMSG}\n\nPlease type one of the above and strike the ENTER key or\n\
type Q to cancel installation [default = ${REQINTV}]."
			read VANSWER
			if [ "x$VANSWER" = x ]
			then
				VANSWER=${REQINTV}
			fi
			if [ "x$VANSWER" != xQ -a "x$VANSWER" != xq ]
			then
				for i in $VECTLIST
				do
					if [ "x$VANSWER" = "x$i" ]
					then
						INT="$VANSWER"
						break 2
					fi
				done
			else
				return $FAIL
			fi
			message -d "${VANSWER} is an invalid answer"
		done
	fi

	# find out if selected I/O addresses are available and then
	# prompt the user to select one

	IOMSG=""
	for LADR in 10D 1DD 30D 3DD
	do
		ADDCHK=`grep "$LADR" ioaddress`
		set `echo $ADDCHK`
		LADR=$1
		HADR=$2
		${CONFBIN}/idcheck -a -l $LADR -u $HADR > /dev/null 2>&1
		if [ "$?" = 0 ]
		then
			IOMSG="${IOMSG}\n\t${LADR}"
			IOLIST="${IOLIST} ${LADR}"
		fi
	done
	if [ "$IOLIST" = "" ]
	then
		echo "\nThe ${DNAME} cannot be installed\nbecause all optional \
I/O addresses are in use!\n"
		return $FAIL
	else
		while true
		do
			RANS=""
			message -d "Available address(es) for the ${DNAME}: \
${IOMSG}\n\nPlease type the address and strike the ENTER key or\n\
type Q to cancel installation [default = ${REQLADD}]."
			read RANS
			if [ "x$RANS" = x ]
			then
				RANS=${REQLADD}
			fi
			if [ "x$RANS" != xQ -a "x$RANS" != xq ]
			then
				for i in $IOLIST
				do
				    VANSWER=`echo "$RANS"|tr '[a-f]' '[A-F]'`
				    if [ "$VANSWER" = "$i" ]
				    then
					ADDCHK=`grep "$VANSWER" ioaddress`
					set `echo $ADDCHK`
					LADD=$1
					HADD=$2
					break 2
				    fi
				done
			else
				return $FAIL
			fi
			message -d "${VANSWER} is an invalid answer"
		done
	fi

	# find out if selected memory addresses are available and then
	# prompt the user to select one

	MEMMSG=""
#	for LMEM in A0000 B0000 E0000 D00000 E00000 F00000
	for LMEM in ${ADDRLIST}
	do
		ADDCHK=`grep "$LMEM" memaddress`
		set `echo $ADDCHK`
		LMEM=$1
		HMEM=$2
		${CONFBIN}/idcheck -c -l $LMEM -u $HMEM > /dev/null 2>&1
		if [ "$?" = 0 ]
		then
			MEMMSG="${MEMMSG}\n\t${LMEM}"
			MEMLIST="${MEMLIST} ${LMEM}"
		fi
	done
	if [ "$MEMLIST" = "" ]
	then
		echo "\nThe ${DNAME} cannot be installed\nbecause all optional \
memory addresses are in use!\n"
		return $FAIL
	else
		while true
		do
			RANS=""
			message -d "Available base address for the ${DNAME}: \
${MEMMSG}\n\nPlease type the address and strike the ENTER key or\n\
type Q to cancel installation [default = ${REQLMEM}]."
			read RANS
			if [ "x$RANS" = x ]
			then
				RANS=${REQLMEM}
			fi
			if [ "x$RANS" != xQ -a "x$RANS" != xq ]
			then
				for i in $MEMLIST
				do
				    VANSWER=`echo "$RANS"|tr '[a-f]' '[A-F]'`
					if [ "$VANSWER" = "$i" ]
					then
					    ADDCHK=`grep "$VANSWER" memaddress`
					    set `echo $ADDCHK`
					    LMEM=$1
					    HMEM=$2
					    break 2
					fi
				done
			else
				return $FAIL
			fi
			message -d "${VANSWER} is an invalid answer"
		done
	fi
fi

cd ${TEMPLATE}/${DRIVER}

ENABLE=Y

sed -e "s/N/${ENABLE}/" -e "s/LMEM/${LMEM}/" -e "s/HMEM/${HMEM}/" -e "s/LADD/${LADD}/" -e "s/HADD/${HADD}/" -e "s/INT/${INT}/" < System.tmpl > System

echo "\nThe new configuration is:\n"
cat System

return $OK

}


#########################
# main()

set_trap

rm -f $ERRFILE > /dev/null 2>&1

mgxcfg || {
	cd /tmp
	unset_trap
	exit $FAIL
}

cd /tmp
rm -f $ERRFILE > /dev/null 2>&1

unset_trap

exit $OK

