:
#	@(#) mousconfig 23.4 91/11/18 
#
#	Copyright (C) 1988-1991 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.
#
#
#	MOUSCONFIG - SCO UNIX System V/386 Busmouse configuration.
#

PATH=/etc:/bin:/usr/bin

MOUSFILE=space.c

TMPFILE=/tmp/mc$$

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

# Define return codes for functions called from menu
M_READY=0;	M_STAY=1
M_UPONE=2;	M_CONT1=3
M_CONT2=4;	M_UPTWO=5 
M_UPTHREE=6;	M_TOP=7

# Define traps for critical and non critical code.
set_trap()  {	
	if [ $OASH ]; then
		trap 'error "Interrupted! Exiting ..."; cleanup $FAIL' 1 2 3 15
	else
		trap 'echo "Interrupted! Exiting ..."; cleanup $FAIL' 1 2 3 15
	fi
}

unset_trap()  {
	trap '' 1 2 3 15
}
 
# Remove temp files and exit with the status passed as argument
cleanup() {
	trap '' 1 2 3 15
	rm -rf $TMPFILE
	[ $OASH ] && oaend
	exit $1
}

# 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
}

Getdfl() {
	read prm1 prm2 < $TMPFILE
	modified=
}
Reset() {
	prm1=5 prm2=0x00
	modified=1
}
Getcur() {
	echo `sed -n -e '
		/@PARM/	{
			s?.*[ET]??
			s?/.*??
			p
			}
	' $MOUSFILE` > $TMPFILE
}
Setcur() {
	ed - $MOUSFILE <<-EOF
		/@PARM1/s?T[^/]*?T	 	$prm1	?
		/@PARM2/s?E[^/]*?E 	$prm2	?
	w
	q
	EOF
}
Showcur() {
	echo "
	Busmouse Parameters	Values	Comments
	-------------------     ------	--------
	1. Interrupt Vector	$prm1	logical vector number
	2. Base Address		$prm2	i/o addresses start here
	
	Zero Values Imply Auto-Configuration"
}

fnV () {
	if [ $OASH ]; then
		oawin_open $Large
		oawin_title ${title:-"Current Busmouse Parameters"}
		Showcur >/tmp/mscan.$$
		scan -e"Press <Esc> to continue" /tmp/mscan.$$
		rm /tmp/mscan.$$
		oawin_close $Large
	else
		Showcur 
	fi
	return $M_STAY
}

fnM () {
	if [ $OASH ]; then
		while :
		do
			mform=/tmp/mconf.$$
			cat >$mform <<EOF
((modify
A='TEXT'
B='TEXT'
*
''
'        Interrupt Vector:  [   ]'
''
'        Hex Base Address:  [      ]'
''
'    Zero values imply auto-configuration.'
'    Hexidecimal values must be preceeded by "0x".'
''
*
A, '$prm1'
B, '$prm2'
))
EOF
			oawin_open $Medium
			oawin_title "Modify Busmouse Parameters"
			form $mform "((modify"
			rm -f $mform
			hd="[0-9a-fA-F]"
			dd="[0-9]"
			case "$FIELD0" in
			0x$hd|$dd|1[0-5])
				;;
			*)	error "Error: Bad interrupt vector value"
				oawin_close $Medium
				continue
				;;
			esac
			case "$FIELD1" in
			0x$hd|0x$hd$hd|0x$hd$hd$hd|0x$hd$hd$hd$hd)
				break
				;;
			$dd|$dd$dd|$dd$dd$dd|$dd$dd$dd$dd|$dd$dd$dd$dd$dd)
				if [ "$FIELD1" -gt 65535 ]; then
					error "Error: Bad base address"
					oawin_close $Medium
					continue
				fi
				break
				;;
			*)	error "Error: Bad base address"
				oawin_close $Medium
				continue
				;;
			esac
		done
		oawin_close $Medium
		if [ "$prm1" != "$FIELD0" -o "$prm2" != "$FIELD1" ]; then
			prm1="$FIELD0"
			prm2="$FIELD1"
			modified=1
		fi
	else
		Showcur
		while echo "\nEnter a parameter to modify or q to return to the main menu: \c"
		do
			read y
			case "$y" in
			[1-2])	while echo "\nHexadecimal values must be entered with prefix '0x'.
For example Hexadecimal 300 should 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
						break
						;;
					"")	break
						;;
					*)	echo "Please enter a numeric value. \n"
						;;
					esac
				done
				;;
			[qQ])	break
				;;
			*)	echo "Please enter a choice, 1 2 or q.\n"
				;;
			esac
		done
	fi
	return $M_STAY
}

fnP () {
	title="Returned to Previous Busmouse Configuration"
	if [ $modified ]; then
		Getdfl
		fnV
	else
		[ $OASH ] && fnV
	fi
	unset title
	return $M_STAY
}

fnD () {
	title="Reset to Default Busmouse Configuration"
	Reset
	fnV
	return $M_STAY
}

fnL () {
	return $M_READY
}
	
if eval oainit 2>/dev/null
then
	# If oainit suceeded we are running in oash...
	OASH=${OASH:-/bin/oash}
	export OASH
	. $OAFUNCS
	oabegin || cleanup $FAIL
else
	# Oainit failed.  If OASH is correctly set, run
	# this script through it, otherwise unset OASH
	if [ "$OASH" -a -x "$OASH" ]
	then
		exec $OASH $0 "$@"
	fi
	unset OASH
fi

set_trap

# beginning
case $# in
0)
	;;
*)
	[ $OASH ] && error "Usage: mousconfig" || echo "USAGE: mousconfig"
	cleanup $FAIL
	;;
esac

status=0
Getcur
Getdfl

[ $OASH ] && {
	dateline "mousconfig"
	menu $OAMENUS/mousconf "Top"
} || {
	while echo "\nBusmouse Configuration

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

Enter your choice or q to quit: \c"
	do
		read ans
		case "$ans" in
		1)	fnV	;;
		2)	fnM	;;
		3)	fnP	;;
		4)	fnD	;;
		[qQ])	break	;;
		*)	echo "\nPlease enter  a choice, 1 2 3 4 or q.\n"
				;;
		esac
	done
}
[ $modified ] && Setcur 
cleanup $OK
