:
#ident @(#)aen0.sh	1.1 91/02/14
#
# Altos ACPA/AT driver setup script
#
#set -x
# Globals
#
LKPERM=/etc/perms/inst
PATH=/bin:/usr/bin:/etc:/etc/conf/bin
tmp=/tmp/aen0$$
ROOT=
CONF=${ROOT}/usr/sys/conf
DEVDIR=${ROOT}/dev
ETC=${ROOT}/etc

# function definitions
#
# usage: safemv file1 file2
# move file1 to file2 without chance of interruption
safemv() {
	trap "" 1 2 3 15
	mv $1 $2
	trap 1 2 3 15
}

# usage: safecopy file1 file2
# copies file1 to file2 without chance of interruption
safecopy() {
	trap "" 1 2 3 15
	cp $1 $2
	trap 1 2 3 15
}

# Prompt for yes or no answer with message passed as argument - 
# returns non-zero for no
getyn() {
	while	echo "\n$* (y/n/q) \c">&2
	do	read yn rest
		case $yn in
			[yY]) return 0				;;
			[nN]|[qQ]) return 1			;;
		*)	echo "Please answer y,n or q" >&2	;;
		esac
	done
}

# print a prompt
#
p() {
	set -f
	/bin/echo $*
	set +f
	return $?
}

# Remove temp files and exit with the status passed as argument
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

# Test to see if link kit is installed
chklinkkit() {
    until	fixperm -i -d LINK $LKPERM
    do	case $? in
	4)	echo "The Link Kit is not installed." >&2		;;
	5)	echo "The Link Kit is only partially installed." >&2
		echo "Attempting to install/deinstall the driver anyway." >&2
		return 0
		;;
	*)	echo "Error testing for Link Kit. Exiting."; cleanup 1 ;;
	esac
	# Not fully installed. Do so here
	while	echo "Do you wish to install it now? (y/n) \c"
	do	read ANSWER
		case $ANSWER in
		Y|y)	custom -o -i LINK
			break
			;;
		N|n)	echo "Drivers cannot be installed without the Link Kit."
			cleanup 1
			;;
		*)	echo "Please answer 'y' or 'n'. \c"
			;;
		esac
	done
    done
}

# Re-link new kernel
relink() {
    cd /etc/conf/cf.d
    if [ -z "$_NOPROMPT" ] ; then
	echo "\nRe-linking the kernel ... \c" >&2
    fi
    ./link_unix 2>&1 | tee -a kmakelog || {
	    echo "\nError:  Kernel link failed.
Check /etc/conf/cf.d/kmakelog for details." >&2
	    cleanup 1
    }
    rm -f kmakelog
    return 0
}

# aen0 driver banner message
aen0prompt() {
/bin/cat - <<	EOF

				ACPA AT SETUP

For the ACPA AT card there are three parameters which you will need to
supply, the Interrupt Vector Number, the IO Base Address, and the window
address (memory).  The suggested settings are 5 for the Interrupt Vector
Number, 310 for the IO Base Address, and the Window address range suggested
is d0000 - dffff (64K).

Before you continue you should make sure that your board is correctly
configured for the settings you are choosing. If you wish to go back and
reconfigure your board or read the ACPA AT documentation answer "no" to 
the next question.

EOF
}

window() {
/bin/cat - <<	EOF

			WINDOW SETUP:	

	A.		b0000 - bffff	(64K)

	B.		c0000 - cffff	(64K)
	C.		c4000 - cffff	(48K)
	D.		c8000 - cffff	(32K)

	E.		d0000 - dffff	(64K)
	F.		d4000 - dffff	(48K)

	G.		e0000 - effff	(64K)

EOF
}

# main()

cd /

# check to see if the drivers can be installed
#

chklinkkit

while	echo "Do you wish to install or delete the aen0 driver (i/d/q) \c"
do	read ANSWER
	case $ANSWER in
	I|i)	
		break
		;;
	D|d)	
		if getyn "\nDo you  want to continue ?"
		then
			echo "Removing aen0 driver..."
			cd /etc/conf/sdevice.d
			echo "aen0	N	1	5	1	5	310	31f	d0000	dffff" > ./aen0
			cd /
			# Relink the kernel with the new driver information 
			if getyn "\nDo you wish to relink the kernel now?"
			then
		    	eval $_RELINK relink
			else
		    	echo "\nChanges will not take effect until the kernel is relinked." >&2
		    	cleanup 0
			fi
			cleanup 0
		else
			echo "\nAborting remove."
			cleanup 1
		fi
		;;
	Q|q)
		cleanup 0
		;;
	*)	echo "Please answer 'i', 'd' or 'q'."
		;;
	esac
done
#
# Deal with aen0
#
aen0prompt
getyn "Do you want to continue?"
if [ $? = "1" ]
then
	p "You can rerun mkdev at a later time"
	exit 0
fi
echo ""
echo "Installing aen0 driver..."


# Get the interrupt vector, IO address, and mem address

DEFVEC=5
DEFIO="310"
DEFWIN="d0000 - dffff"

while	p "Interrupt Vector (2, 3, 5, 10, 11, or 15) [${DEFVEC}]: \c"
do	read vector
	case "$vector" in
	"")
		vector=$DEFVEC
		break;
		;;
	[235])
		break; 
		;;
	[1][015])
		break
		;;
	*) p "Vector number must be 2, 3, 5, 10, 11, or 15"
		;;
	esac
done

while	p "IO base address (in hexadecimal 310 or 330) [${DEFIO}]: \c"
do	read ioaddr
	case "$ioaddr" in
	"")
		ioaddr=$DEFIO
		break;
		;;
	[3][13][0])	
		break; 
		;;
	*) p "Address must be 310 or 330"
		;;
	esac
done

#  get window/memory address
window
while p "Window addresses [${DEFWIN}]:  \c"
do
read wind
case $wind in 
A|a) 
	waddr=b0000
	wendaddr=bffff
	break 
	;;
B|b) 
	waddr=c0000
	wendaddr=cffff
	break 
	;;
C|c) 
	waddr=c4000
	wendaddr=cffff
	break 
	;;
D|d) 
	waddr=c8000
	wendaddr=cffff
	break 
	;;
E|e) 
	waddr=d0000
	wendaddr=dffff
	break 
	;;
F|f) 
	waddr=d4000
	wendaddr=dffff
	break 
	;;
G|g) 
	waddr=e0000
	wendaddr=effff
	break 
	;;
"") 
	waddr=d0000
	wendaddr=dffff
	break 
	;;
*)	p "Only the window addresses displayed above are valid"
	continue
esac
done

if [ $ioaddr = 310 ] 
then
	endioaddr=31f
else
	endioaddr=33f
fi

cd /etc/conf/sdevice.d
echo "aen0	Y	1	5	1	$vector	$ioaddr	$endioaddr	$waddr	$wendaddr" > ./aen0

# Relink the kernel with the new driver information 
if getyn "\nDo you wish to relink the kernel now?"
then
	eval $_RELINK relink
else
	echo "\nThe aen0 driver will not work until the kernel is relinked." >&2
	echo ""
	cleanup 0
fi

# install if requested
#
echo ""
echo "To complete installation the system must be rebooted via init 6\n" >&2
