:
#
# @(#) mfgconfig.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/mfg.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 DMA 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) ****

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


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

mfgcfg () 
{
RETVCHK=0

REQINTV=10
REQLADD=3DD
REQHADD=3DE
REQDMAV=7

INT=${REQINTV}
LADD=${REQLADD}
HADD=${REQHADD}
DMA=${REQDMAV}
ENABLE=N

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}, DMA Channel = ${DMA}, I/O Port = 0x${LADD}"

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
	mfgdel
	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 ] && mfgdel

	# 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 9 10 12
	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 3DD 30D 1DD 10D
	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 DMA channels are available and then
	# prompt the user to select one

	DMAMSG=""
	for CHAN in 5 6 7
	do
		${CONFBIN}/idcheck -d $CHAN
		if [ "$?" = 0 ]
		then
			DMAMSG="${DMAMSG}\n\t${CHAN}"
			CHANLIST="${CHANLIST} ${CHAN}"
		fi
	done
	if [ "$CHANLIST" = "" ]
	then
		echo "\nThe ${DNAME} cannot be installed\nbecause DMA channels \
7, 6 and 5 are in use!\n"
		return $FAIL
	else
		while true
		do
			VANSWER=""
			message -d "Available channel(s) for the ${DNAME}: \
${DMAMSG}\n\nPlease type one of the above and strike the ENTER key or\n\
type Q to cancel installation [default = ${REQDMAV}]."
			read VANSWER
			if [ "x$VANSWER" = x ]
			then
				VANSWER=${REQDMAV}
			fi
			if [ "x$VANSWER" != xQ -a "x$VANSWER" != xq ]
			then
				for i in $CHANLIST
				do
					if [ "x$VANSWER" = "x$i" ]
					then
						DMA="$VANSWER"
						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/LADD/${LADD}/" -e "s/HADD/${HADD}/" -e "s/INT/${INT}/" < System.tmpl > System

sed -e "s/DMA/${DMA}/" < Master.tmpl > Master

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

return $OK

}


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

set_trap

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

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

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

unset_trap

exit $OK

