#ident "@(#)funcs	1.3-92/07/08" 
# Functions that are used to configure the network drivers
PATH=${PATH}:/usr/lbin; export PATH
TEMPF=/tmp/$DNAME.$$; export TEMPF
# Set up the traps and get the driver information
set_env()
{
	trap "rm -f /tmp/*.$$; exit 3" 2 3 15
	flags="-qq -k$$"

	cp ${SDEVICEDIR}/$DNAME ${TEMPSDEV}
	[ -n "${MDEVICE}" -a -n "${TEMPMDEV}" ] && cp ${MDEVICE} ${TEMPMDEV}
	eval `awk '/'$DNAME'/ { 
		printf("irqvec=%s stioa=%s endioa=%s stmema=%s endmema=%s\n", \
			$6, $7, $8, $9, $10) 
		}' ${TEMPSDEV}`
	if [ -f /${TEMPMDEV} ]
	then
		eval dmachan=`grep '^[^#]' ${TEMPMDEV} | grep ${DNAME} | cut -f10`
	fi
}

# check for the existence of the sdevice file and abort if gone
bad_sdevdir()
{
	if [ ! -f ${SDEVICEDIR}/$DNAME ]
	then
		echo "Cannot find configuration file ${SDEVICEDIR}/$DNAME, aborting."
		return 0
	fi
	return 1
}

# Print out the header for the config screen
do_hdr()
{
cat <<EOF

                            INTERACTIVE
                 Network Driver Software Installation 

                   $NAME Hardware Configuration

The $DNAME driver is currently configured with the following $NAME hardware
settings:

EOF
}

# change the IRQ for the device
do_changeirq()
{
	range="$1"
	oirq=$irqvec
	changed=yes
	while true
	do 
		echo "Please enter the IRQ value for the board [ $oirq ]: \c"
		read irqvecx
		irqvec=${irqvecx:-$oirq}
		for ir in ${range}
		do
			[ "${irqvec}" = "${ir}" ] && break 2
		done
		frange="`echo ${range} | sed 's/ /, /g'`"
		echo "Only ${frange} are valid"
		echo "interrupt levels for the $NAME board." 
	done
awk '/'$DNAME'/ { OFS="\t"; $6="'$irqvec'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	irqlev=$irqvec 
	mv ${TEMPF} ${TEMPSDEV}
	echo " "
}


# Display the IRQ conflict message
irqconflict()
{
	MATCHDEV=$1
	IRQVEC=$2
		cat <<EOF

There is a conflict with the device "${matchdev}" on interrupt level
${irqvec}.  Please resolve this conflict by changing the Interrupt
Request line of either the conflicting device or the $NAME board.

Change the IRQ value of "$DNAME" in the ${SDEVICEDIR}/$DNAME file or make
modifications to the configuration file of each conflicting device
resident in the ${SDEVICEDIR} directory named after the corresponding
device.  If you are changing the interrupt vector and request
line of the $NAME board then you will need to do the following:

     1    Abort this installation.
     2    Change the IRQ jumper on the $NAME board to the desired IRQ
	  level to reflect the desired IRQ level.
     3    Restart this installation script by executing sysadm, giving
	  the correct value for the interrupt level when asked by the
	  script.

This conflict must be resolved before the new kernel can be linked.
Please refer to the INTERACTIVE Network Driver software and $NAME 
hardware installation manual for more detailed instructions on changing
the interrupt vector, I/O address etc..

Now interrupt this script (press: CTRL \) to abort the
installation...
EOF
}

# get the io address for eisa boxes,
# They are different enough to get one of their own routines
get_iostart_eisa()
{
	while true
	do
		slot=`echo $stioa | sed 's/000//'`
		echo "EISA I/O Port addresses are based on EISA slot number."
		echo "Please enter the EISA slot number for the board [ $slot ]: \c"
		read newslot
		newslot=${newslot:-slot}
		case "$newslot" in
			[1-9]|1[0-5])
			stioa=${stioa:-$slot}000
			break ;;
		*) 
			echo "The legal EISA slot numbers are 1 - 15."
			echo "The current value is $slot."
			;;
		esac
	done
}

# get the end io address for eisa boxes
get_ioend_eisa()
{
	oendioa=$endioa
	changed=yes
	endioa=${stioax:-$slot}FFF
awk '/'$DNAME'/ { OFS="\t"; $8="'$endioa'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	mv ${TEMPF} ${TEMPSDEV}
}

# Get the io start address
get_iostart()
{
	while true
	do
		echo "Please enter the Start I/O address \c"
		echo "for the board in hex [ $ostioa ]: \c"
		read newstioa
		stioa=${newstioa:-$ostioa}
		case "$stioa" in
			${1})
				break 
				;;
			*) 
				echo The I/O Base address must be specified in hexadecimal 
				;;
		esac
	done
	awk '/'$DNAME'/ { OFS="\t"; $7="'$stioa'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	mv ${TEMPF} ${TEMPSDEV}
}

# Get the io end address
get_ioend()
{
	while true
	do 
		echo "Please enter the End I/O address \c"
		echo "for the board in hex [ $oendioa ]: \c"
		read newendioa
		endioa=${newendioa:-$oendioa}
		case "$endioa" in
			[23][0-9a-fA-F][0-9a-fA-F]) break ;;
			*) echo The End I/O address must be specified in hexadecimal ;;
		esac
	done
awk '/'$DNAME'/ { OFS="\t"; $8="'$endioa'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	mv ${TEMPF} ${TEMPSDEV}
	echo " "
}

# change the io addresses
do_changeio()
{
	startval="${1}"
	endval="${2}"
	archtype=${3}
	while true
	do 
		ostioa=$stioa
		changed=yes
		if [ "${archtype}" = "EISA" ]
		then
			get_iostart_eisa ${startval}
		else
			get_iostart ${startval}
		fi
		[ $? -eq 0 ] && break
	done
	while true
	do 
		oendioa=$endioa
		changed=yes;
		if [ "${archtype}" = "EISA" ]
		then
			get_ioend_eisa ${endval}
		else
			get_ioend ${endval}
		fi
		[ $? -eq 0 ] && break
	done
}

# Check for interrupt vector conflicts
chk_intvec()
{
echo "Checking for interrupt vector conflicts - Please wait..."
match=No 
eval `cat ${SDEVICEDIR}/* | 
	awk '{  if ( $1 != "'$DNAME'" && $2 != "N" && $6 == "'$irqvec'" ) {
		printf ("matchdev=%s match=y\n",$1);
		}
	} '`
    case "${match:-No}" in
	[yY]*)
		irqconflict
		read junk ;;
    esac
}

# save the sdevice file
saveit()
{
	string="${1}"
	driver_on
	[ -n "${MDEVICE}" -a -n "${TEMPMDEV}" ] && cp ${TEMPMDEV} ${MDEVICE}
	mv ${TEMPSDEV} ${SDEVICEDIR}/$DNAME 
	echo "\nNew ${SDEVICEDIR}/$DNAME file is:"
	cat ${SDEVICEDIR}/$DNAME
	echo ""
if grep "^$DNAME" /etc/cnfgdrv > /dev/null 2>&1
then
	:
else
	cat >> /etc/cnfgdrv <<!
$DNAME	-	-	-	-	-	-	${string}
!
fi
}

# Build the kernel?
askbuild()
{
	if checkyn ${flags} -f "Do you wish to build a new kernel at this time?"
	then
		sh runlevel
		/etc/kconfig  -r /
	fi
}

# Change the controller memory address
do_changemem()
{
	startrange=$1
	endrange=$2
	archtype=$3
	while true
	do 
		ostmema=$stmema
		changed=yes
		if [ "${archtype}" = "EISA" ]
		then
			get_memstart_eisa
			break
		else
			get_memstart ${startrange}
			break
		fi
	done
	while true
	do 
		oendmema=$endmema
		changed=yes;
		if [ "${archtype}" = "EISA" ]
		then
			get_memend_eisa
			break
		else
			get_memend ${endrange}
			break
		fi
	done
}

get_memstart_eisa()
{
	omem=${stmema}
	changed=yes;
	while true
	do 
		echo "Please enter the Start \c"
		echo "Memory address for the board [ ${omem} ]: \c"
		read stmemx
		stmema=${stmemx:-$omem}
		case "$stmema" in
			*[0-9A-Fa-f][048Cc]000)
				case "$stmema" in
					[C-Fc-f]????)                     break ;;
					[1-9a-fA-F]?????)                 break ;;
					[1-9a-fA-F][0-9a-fA-F]?????)      break ;;
					[1-3][0-9a-fA-F][0-9a-fA-F]?????) break ;;
					*) 	;;
				esac
				;;
			*) ;;
		esac
		echo "Invalid address"
		echo "Valid addresses are hex C0000 through \c"
		echo "3FFFC000 in 4000 hex increments."
		echo "The current address is $omem"
	done
	awk '/'$DNAME'/ { OFS="\t"; $9="'$stmema'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	mv ${TEMPF} ${TEMPSDEV}
}

get_memend_eisa()
{
     smadre=`expr $stmema : '.*\(....\)'`
     t=`expr $smadre : '\(.\)'`
     case $t in
     	[04])
			t=`expr $t '+' 3` ;;
     	8)
			t="b" ;;
     	[Cc])
			t="F" ;;
     	esac
     t=$t"FFF"
     endmema=`echo $stmema | sed s/$smadre/$t/`
awk '/'$DNAME'/ { OFS="\t"; $10="'$endmema'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
      mv ${TEMPF} ${TEMPSDEV}
}

# Change DMA channel
do_dma()
{
	TEMPFF=/tmp/nlmdev.$$
	RANGE="${1}"
	odma=$dmachan; 
	changed=yes
	while true
	do 
		echo "Please enter the DMA value for the board [ $odma ]: \c"
		read dmachanx
		dmachan=${dmachanx:-$odma}
		for dm in ${RANGE}
		do
			[ "${dmachan}" = "${dm}" ] && break 2
		done
		echo "Only ${RANGE} are valid"
		echo "DMA channels for the $NAME board." 
	done
	grep "${DNAME}" ${TEMPMDEV} | sed -e 's/'${odma}$/${dmachan}'/'  > ${TEMPFF}
	sed "/^${DNAME}/d" ${TEMPMDEV} >> ${TEMPFF}
	mv ${TEMPFF} ${TEMPMDEV}
}

# Change the controller memory address
get_memstart()
{
	omem=${stmema}
	changed=yes;
	while true
	do 
		echo "Please enter the Start \c"
		echo "Memory address for the board [ ${omem} ]: \c"
		read stmemx
		stmema=${stmemx:-$omem}
		case "$stmema" in
			${1})
				break
				;;
			*) 	
				echo "Invalid address"
				echo "Valid addresses are hex C0000 through \c"
				echo "3FFFC000 in 4000 hex increments."
				echo "The current address is $omem"
				;;
			esac
	done
	awk '/'$DNAME'/ { OFS="\t"; $9="'$stmema'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	mv ${TEMPF} ${TEMPSDEV}
}

get_memend()
{
	oendmema=${endmema}
	while true
	do
		echo "Enter the end Controller Memory Address [ ${endmema} ]: \c"
		read endmemx
		endmema=${endmemx:-$endmema}
		case "${endmema}" in
			[0-9a-fA-F]*)
				break
				;;
		esac
	done
	awk '/'$DNAME'/ { OFS="\t"; $10="'$endmema'" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	mv ${TEMPF} ${TEMPSDEV}
}

# Turn on the driver
driver_on()
{
	awk '/'$DNAME'/ { OFS="\t"; $2="Y" ; print $0 }' ${TEMPSDEV} > ${TEMPF}
	mv ${TEMPF} ${TEMPSDEV}
}

# multiple board handling
do_mult()
{
	DEFAULT=${1}
	descstring="${2}"
	avail="${3}"
	valid="${4}"
	q="Do you want to install a ${descstring}" 
	h="If you want to configure a Western Digital Card type Yes"
	(
	oktochange Yes "${q}" "${h}"
	)
	if [ $? -eq 1 ]
	then
		while true
		do 
			echo "Enter a ${descstring} (${avail}) [${DEFAULT}] \c"
			read bdnum
			bdnum=${bdnum:-$DEFAULT}
			for brds in ${valid}
			do
				[ "${bdnum}" = "${brds}" ] && DNAME=${bdnum} && break 2
			done
			echo "\nOnly ${valid} are valid selections"
		done
	else
		exit
	fi
}

oktochange()
{
	DEFAULT=${1}
	QUESTION="${2}"
	HELP="${3}"
	while :
	do
		echo "${QUESTION} [${DEFAULT}]: \c"
		read RESPONSE
		case "${RESPONSE:-${DEFAULT}}" in 
			[Yy]*)
				ret=1
				break
				;;
			[nN]*)
				ret=0
				break
				;;
			\?)	
				echo "${HELP}"
				;;
			*)
				echo "Answer Yes or No, default is ${DEFAULT}"
				;;
		esac
	done
	return ${ret}
}

