:
#	@(#) ctconfig 25.2 92/09/17 
#
#	Copyright (C) 1988-1992 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	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.
#
#
#	CTCONFIG - SCO UNIX System V/386 QIC-02 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 "\n$* (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
}

# deduce architecture from whether /dev/mcapos exists and can be read
Getarch() {
	arch=AT
	maxtype=9
	if cat /dev/mcapos 1>/dev/null 2>/dev/null
	then	arch=MC
		maxtype=4
	fi
}
Getdfl() {
	read prm1 prm2 prm3 prm4 < $TMPFILE
	modified=
	case "$prm3" in
	2|2[4-9]|3[0-1])
		[ $prm3 -eq 2 ] && prm3=9 || prm3=`expr $prm3 - 16`
		modified=1
		;;
	esac
}
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() {
    if [ $arch = MC ]
    then
	echo "
	Tape Parameters		Values	Comments
	---------------         ------	--------
	1. Controller Type	$prm1	1 = mountain   3 = everex/archive/
							   tandberg
                                	2 = ibm6157    4 = tecmar/wangtek
	2. DMA Channel		$prm2	0 = Auto, else 1 or 3 (2 for type 2)
	3. Interrupt Vector	$prm3	0 = Auto, else 3 or 5 (6 for type 2)
	4. Base Address		$prm4	I/O base address: 0 = Auto, else e.g.
					0x200 if 1, 0x3120 if 2, 0x300 if 3/4

	Zero Values imply Auto-Configuration"
    else
	echo "
	Tape Parameters		Values	Comments
	---------------         ------	--------
	1. Controller Type	$prm1	1 = archive   3 = wangtek  
					4 = emerald   5 = mountain  
					6 = tecmar    7 = everex/tandberg
	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"
    fi
}
TestIt() {
	error=
	case "$prm1" in
	[0-9])	type=$prm1	;;
	*)	type=100	;;
	esac
	[ $type -gt $maxtype ] && \
		error="\nController Type must be a decimal integer, 0 to $maxtype."
	case "$prm2" in
	[0-7])	;;
	*)	error="$error\nDMA Channel must be a decimal integer, 0 to 7."
		;;
	esac
	case "$prm3" in
	2|2[4-9]|3[0-1])
		[ $prm3 -eq 2 ] && UNIXprm3=9 || UNIXprm3=`expr $prm3 - 16`
		echo "Using the UNIX equivalent $UNIXprm3 instead of $prm3."
		prm3=$UNIXprm3
		;;
	[0-9]|1[0-5])
		;;
	*)	error="$error\nInterrupt Vector must be a decimal integer, 0 to 15."
		;;
	esac
	case "$prm4" in
	0|0x0|0x00|0x000|0x0000)
		;;
	0x[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]|\
	0x[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])
		[ "$prm1" -eq 0 ] && \
			error="$error\nIf Controller Type is zero, then Base Address must be 0."
		;;
	*)	error="$error\nBase Address must be a hexadecimal value e.g. 0x1234."
		;;
	esac
	[ "$error" ] && {
		echo "$error"
		return 1
	}
	return 0
}

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

Getarch
Getcur
Getdfl

while echo "\nQIC Cartridge Tape Configuration

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

Enter your choice or q to quit: \c"
do
	read ans
	case "$ans" in
	1)	Showcur
		;;
	2)
		Showcur
		while echo "\nEnter a parameter to modify or q to return to the main menu: \c"
		do
			read y
[ "$y" = "4" ] && {
echo "\nHexadecimal values must be entered with prefix 0x.
For example, hexadecimal 300 should be entered as 0x300.\c"
}
			case "$y" in
			[1-4])	while :
echo "\nEnter 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 || {
							Showcur
							continue 2
						}
						break
						;;
					"")	break
						;;
					*)	echo "Please enter a numeric value."
						;;
					esac
				done
				;;
			[qQ])	TestIt || {
					Showcur
					continue
				}
				break
				;;
			*)	echo "Please enter a choice, 1 2 3 4 or q."
				;;
			esac
		done
		;;
	3)	[ $modified ] && {
			Getdfl
			Showcur
		}
		;;
	4)	Reset
		Showcur
		;;
	[qQ])	[ $modified ] && Setcur 
		break
		;;
	*)	echo "Please enter a choice, 1 2 3 4 or q."
		;;
	esac
done
eval $cleanup
