:
#
# setthresh (Online Diagnostics)
#

# shell functions below - start main shell script later #

# define a function to read input and screen for improper values
# arguments:
#	$1 = descriptive prompt string
#	$2 = lower limit for acceptable range
#	$3 = upper limit for acceptable range
getval() \
{ \
     GETVAL=0
     while [ $GETVAL -lt $2 -o $GETVAL -gt $3 ]
     do
	echo; \
	shstr=`echo $1`; \
	echo "Enter new VALUE for $shstr\nor <enter> to leave field unchanged: \c"; \
	read GETVAL ; \
	if [ ! "$GETVAL" ]
	then
		GETVAL=-1; \
		break;
	elif [ $GETVAL -lt $2 -o $GETVAL -gt $3 ]
	then
		# invalid entry; notify user, pause, & discard
		echo "Invalid value entered..." ; \
		echo "Hit <Enter> to continue \c" ; \
		read GETVAL ; \
		GETVAL=-1 ; \
	fi \
     done
}

#
# function to read for new action to be taken upon threshold crossing
# arguments:
#	$1 = descriptive prompt string
getact() \
{ \
    while [ "$GETACT" != "c" -o "$GETACT" != "C" -o "$GETACT" != "n" -o "$GETACT" != "N" ]
    do
	shstr=`echo $1`; \
	echo "Enter new ACTION for $shstr\nor <enter> to leave field unchanged: \c"; \
	read GETACT ; \
	if [ ! "$GETACT" ]
	then
		GETACT=-1; \
		break
	else
		case $GETACT in
			"") break	
				;;
			c|C) GETACT="C"; \
		             break	
				;;
			n|N) GETACT="N"; \
		             break	
				;;
			*) # invalid entry; notify user, pause, & discard
				echo "Invalid value entered..." ; \
				echo "Hit <Enter> to continue \c" ; \
				read GETACT ; \
				GETACT=-1 ; \
				;;
		esac; \
	fi; \
    done
}

#
# function to allow user selection of device thresholds
#
mod_dev() \
{ \
	while true
	do
		clear; 
		case $1 in
			"hd") devstr="Hard Drive" ;;
			"fd") devstr="Floppy Drive" ;;
			"tp") devstr="Tape Drive" ;;
			"mdc") devstr="Multidrop Board" ;;
			"sio") devstr="SIO Board" ;;
			"krn") devstr="Kernel" ;;
			"mis") devstr="Miscellaneous" ;;
			*) continue ;;
		esac;
		echo; 
		echo "		Current $devstr Error Threshold Settings"; 
		echo; 
		echo "	Item #	Error Type			  Current Value		Action*"
		# use `eval' command to generate string identifying the fields
		# for this device for which error messages are cross-referenced
		eval devfields=$"$1"fields
		# set initial field count to 0
		numf=0
		for i in $devfields
		do
			eval ttval=$"$1""val$i"
			eval tval="$1$ttval"
			eval ttstr=$1"str$i"
			eval tstr="$"$ttstr""
			eval echo "'	   '$i\)'	'\"$tstr\"''$"$tval"'		    '$""$tval"ACT""
			# count number of fields for this device
			numf=`expr $numf + 1`
		done; 
		echo; 
		echo "* N = Mail notice to root only  C = Notify root & Call remote Diagnostic Center"; 
		echo; 
		echo "Select field number to modify or enter Q to quit \c"; \
		read SELECT; \
		case $SELECT in
			q|Q) break ;;
			"") continue ;;
			*) if [ $SELECT -ge 1 -a $SELECT -le $numf ]
				then
					eval ttval=$1"val$SELECT"
					eval tval=$"$ttval"
					eval ttstr=$1"str$SELECT"
					eval tstr="$"$ttstr""
					getval "$devstr $tstr" 1 20000
					if [ $GETVAL -ne -1 ]
					then
						eval $1$tval=$GETVAL;
					fi;
					getact "$devstr $tstr"
					if [ $GETACT != "-1" ]
					then
						eval $1"$tval"ACT=$GETACT;
					fi;
				fi
				;;
		esac; \
	done; \
}

# end of script functions - start of shell script 

#define file equates to shorten shell script lines
xfile=/usr/lib/err.xref
tfile=/usr/lib/err.thresh

# check for superuser id

if [ `logname` != "root" ]
then
	echo "Must be super-user to execute setthresh routine"
	exit 1
fi

#
# we need to get device/error definitions and current thresholds settings from 
# err.xref (error-desc cross-reference) and err.thresh (settings) files.
#

# check first if cross-ref files is present

if [ ! -f $xfile ]
then
	# cross-reference file doesn't exist; make default one
	/usr/lib/aodmkxref -h
	RETVAL=$?
	if [ $RETVAL -ne 0 ]
	then
		echo "Unable to create cross-reference deafult file; aborting"
		exit `expr $RETVAL + 8`
	fi
fi

# turn on flag for progress messages
HEADINGS="on"

# read cross-reference data
.   /usr/lib/aodrdset

# check if thresholds settings file exists
if [ ! -f $tfile ]
then
	# thresholds settings file does not exist; create default
	# via script file executed within this shell process
	.   /usr/lib/aodsetdef
else
	# threshold settings file DOES exist, so read the settings from it
	echo "Reading current settings, one moment please...\c"; 
	eval `awk '{printf("%s=%s\n%sACT=%s\n",$1,$2,$1,$3);}' $tfile`
	echo "done"
fi

#
# loop through editor function until finished
#

finished=0
while true
do
	clear
	echo
	echo "	Select a Device for which to Modify the Threshold Settings"
	echo
	echo "			1) Hard Disk Drive"
	echo "			2) Floppy Disk Drive"
	echo "			3) Tape Drive"
	echo "			4) Multidrop Board"
	echo "			5) SIO Board"
	echo "			6) Kernel Settings"
	echo "			7) Miscellaneous"
	echo
	echo "	Or enter 'q' to quit"
	echo
	echo "		Your Selection? \c"
	read SELECTION
	case $SELECTION in
		1) mod_dev "hd" ;;
		2) mod_dev "fd" ;;
		3) mod_dev "tp" ;;
		4) mod_dev "mdc" ;;
		5) mod_dev "sio" ;;
		6) mod_dev "krn" ;;
		7) mod_dev "mis" ;;
		q|Q) echo;break ;;
	esac
done


# now write these defaults to the threshold file
# if threshold file exists, save it first & allow for restoration on error

if [ -f /usr/lib/err.thresh ]
then
	cp /usr/lib/err.thresh /usr/lib/aod.thresh
	trap "mv /usr/lib/aod.thresh /usr/lib/err.thresh" 0 2 3 15
	rm /usr/lib/err.thresh
fi
cp /dev/null /usr/lib/err.thresh

echo
echo "Saving error detection thresholds file...\c"

for device in hd fd tp mdc sio krn mis
do
	eval fieldlist=$"$device""fields"
	for fieldtype in $fieldlist
	do
		eval fieldname=$""$device"val"$fieldtype""
		eval echo "$device""$fieldname" $""$device""$fieldname"" $""$device""$fieldname"ACT" >> /usr/lib/err.thresh
	done
done
echo "done"
echo

trap 0 2 3 15
# done, so get out of here
exit 0

