:
#ident	"@(#)ctconfig	1.3	90/09/27"
#	@(#) ctconfig 1.6 89/01/19 
#
#	Copyright (C) The Santa Cruz Operation, 1988.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, Microsoft Corporation
#	and AT&T, and should be treated as Confidential.
#
# CTCONFIG	- XENIX System V (QIC) cartridge tape configuration.
#

PATH=/etc:/bin:/usr/bin
CTFILE=space.c
TMPFILE=/tmp/ctconfig$$

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

cleanup="rm -rf $TMPFILE"

trap 'eval $cleanup; exit $FAIL' 1 2 3 15

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

Getdfl() {
	read prm1 prm2 prm3 prm4 < $TMPFILE
	modified=
}
Reset() {
	prm1=0 prm2=0 prm3=0 prm4=0x0
	modified=1
}
Getcur() {
	echo `sed -n -e '
		/@PARM/	{
			s?[^0-9]*??
			s?/.*??
			p
			}
	' $CTFILE` > $TMPFILE
}
Setcur() {
ed - $CTFILE <<-EOF
	/@PARM1/s?L[^/]*?LR 	$prm1	?
	/@PARM2/s?H[^/]*?H 	$prm2	?
	/@PARM3/s?N[^/]*?NT 		$prm3	?
	/@PARM4/s?E[^/]*?E 		$prm4	?
	w
	q
	EOF

}
Showcur() {
	echo "`clear`

	Tape Parameters		Values	Comments
	---------------		------	--------
	1.  Controller Type	$prm1	1 = type A, 3 = type W, 
					4 = type E, 5 = type M, 
					6 = type T, 7 = type X
	2.  DMA Channel		$prm2	1 or 3
	3.  Interrupt Vector	$prm3	Logical vector number
	4.  Base Address	$prm4	I/O addresses start here

	Zero Values Imply Auto-Configuration
"
	if [ "${display_only}" = "1" ]
	then
		echo "
	Press return to continue:  \c"
		read a
	fi
}
TestIt() {
	[ "$prm4" = "0x0000" -o "$prm4" = "0x0" ] || [ $prm1 -ne 0 ] && return 0
	clear
	echo "

	Base Address is non-zero, therefore Controller 
	Type must be non-zero.

	Press return to continue:  \c"
	read a
	Showcur
	return 1
}

# beginning
case $# in
0)
	;;
*)
	echo "USAGE: ctconfig"
	eval $cleanup
	exit $FAIL
	;;
esac

status=0
Getcur
Getdfl

while echo "`clear`

	QIC Cartridge Tape Configuration


	1. Display current tape parameters
	2. Modify current tape parameters
	3. Revert to previous tape parameters
	4. Select default tape parameters

	Enter your selection or type 'q' to quit:  \c"
do
	read ans
	case "$ans" in
	1)	display_only=1
		Showcur
		display_only=0
		;;
	2)
		while 
			Showcur
			echo "
	Enter a parameter to modify or 'q' to 
	return to the main menu:  \c"
		do
			read y
			case "$y" in
			[1-4])	while echo "
	Hexadecimal values must be entered with 
	prefix 0x.  For example, Hexadecimal 300 
	would be entered as 0x300.

	Enter the new value or <RETURN> to use the 
	existing value:  \c"
				do
					read z
					case "$z" in
					[0-9]*)
						eval prm$y=$z
						modified=1
						TestIt || continue 2
						break
						;;
					"")	break
						;;
					*)	echo "
	Please enter a numeric value."
						sleep 2
						;;
					esac
				done
				;;
			[qQ])	TestIt || continue
				break
				;;
			*)	echo "
	Please enter a choice, 1-4 or 'q'."
				sleep 2
				;;
			esac
		done
		;;
	3)	[ $modified ] && {
			Getdfl
			display_only=1
			Showcur
			display_only=0
			}
		;;
	4)	Reset
		display_only=1
		Showcur
		display_only=0
		;;
	[qQ])	[ $modified ] && Setcur 
		break
		;;
	*)	echo "
	Please enter 1, 2, 3, 4 or 'q'."
		sleep 2
		;;
	esac
done
#eval $cleanup
