:
#	@(#) adapt_config 23.3 91/08/08 
#
#	Copyright (C) 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.
#

#
#	Compaq SCSI Tape Adapter Configuration
#

TMP_FILE=/tmp/adconfig$$
PATH=/etc:/bin:/usr/bin:/etc/conf/bin
param_index=
mod_failed=1
nomod=1

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

# Define traps for critical and non critical code.
set_trap()  {	
	trap 'echo "Interrupted! Exiting ..."; cleanup 1' 1 2 3 15
}

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 -f $TMP_FILE
	exit $1
}

reset_param() {
	addr=130
	upper=133
	dma=7
	intr=5
	mod_failed=0
	nomod=0
}

get_param() {
	# Extract interrupt and base address from sdevice file.
	sdevice=`cat /etc/conf/sdevice.d/cpqs 2> /dev/null` || {
	addr=
	intr=
	return $FAIL
	}
	set $sdevice
	addr=$7
	intr=$6

	# Extract dma channel from mdevice file.
	mdevice=`grep cpqs /etc/conf/cf.d/mdevice` || {
	dma=
	return $FAIL
	}
	set $mdevice
	dma=$9
}

set_param() {
# Update sdevice entries for interrupt vector and address.
	[ -d /etc/conf/pack.d/cpqs ] || {
		echo "\nError: /etc/conf/pack.d/cpqs does not exist."
		return $FAIL
	}
	cd /etc/conf/pack.d/cpqs

	# Edit System file "cpqs".
	echo "cpqs	Y	8	4	1	$intr	$addr	$upper	0	0" >System 2>&1 || {
		echo "\nError: Cannot update cpqs sdevice file."
		return $FAIL
	}

	# Update mdevice entry for dma.

	# Edit "Master" file.
	echo "cpqs	Iocrwis	ioDHc	cpqs	0	0	1	8	$dma" >Master 2>&1 || {
		echo "\nError: Cannot update mdevice file."
		return $FAIL
	}

	idinstall -u -m -s cpqs || {
		echo "\nError: Cannot update sdevice and mdevice files."
		return $FAIL
	}
	return $OK
}

show_param() {
	if [ "$addr" = "" -o "$dma" = "" -o "$intr" = "" ]
	then
	tmpaddr=$addr
	tmpdma=$dma
	tmpintr=$intr
	[ "$addr" = "" ] && tmpaddr="none"
	[ "$dma" = "" ] && tmpdma="none"
	[ "$intr" = "" ] && tmpintr="none"
	echo ""
	echo "\tNo Compaq SCSI Tape Adapters are currently configured in the system"
	echo ""
	echo "
	Index  Parameters	 Current Value	Vaild Values
	-----  ------------      -------------	------------------------
	  1    Base Address	     $tmpaddr	130, 330 or *120 (Hex)
	  2    DMA Channel	     $tmpdma		7 or 5
	  3    Interrupt Vector	     $tmpintr		5, 3, *10, or *11

				*Not supported SCSI Tape Adapter (001379)"
	else
	echo "
	Index  Parameters	 Current Value	Vaild Values
	-----  ------------      -------------	------------------------
	  1    Base Address	     $addr	130, 330 or *120 (Hex)
	  2    DMA Channel	     $dma		7 or 5
	  3    Interrupt Vector	     $intr		5, 3, *10, or *11

				*Not supported SCSI Tape Adapter (001379)"
	fi
}

show_eisa() {
	if [ "$addr" = "" ]
	then
	echo ""
	echo "\tNo Compaq SCSI Tape Adapters are currently configured into EISA NVM"
	echo "\tInstall the Tape Adapter into the system with the Compaq EISA"
	echo "\tConfiguration Program."
	echo ""
	else
	echo "
	The following Parameters have been set from values stored in EISA
	Non-Volatile Memory.

		Parameters	 Current Value	
	       ------------      -------------
	       EISA Slot Number      $slot
	       Base Address	     $addr
	       DMA Channel	     $dma
	       Interrupt Vector	     $intr"
	sleep 3
	fi
}

check_param() {
	case $param_index in
	1) 	[ "$param_value" != "130" -a "$param_value" != "330" -a "$param_value" != "120" ] && {
			echo "\nInvalid base address: Must enter 130 or 330"
			return $FAIL
		}
		addr=$param_value
		upper=`expr $param_value + 3`
		mod_failed=0
		nomod=0
		;;
	2)	[ "$param_value" != "7" -a "$param_value" != "5" ] && {
			echo "\nInvalid dma channel : Must enter 7 or 5"
			return $FAIL
		}
		dma=$param_value
		mod_failed=0
		nomod=0
		;;
	3)	[ "$param_value" != "5" -a "$param_value" != "3"  -a "$param_value" != "10" -a "$param_value" != "11" ] && {
			echo "\nInvalid interrupt vector: Must enter 5 or 3"
			return $FAIL
		}
		intr=$param_value
		mod_failed=0
		nomod=0
		;;
	esac

	return $OK
}


# select_param()
# This routine prompts the user to select a parameter to modify.
# If they do so, it sets the global param to the index of the
# variable and returns successfully.  If not, it returns with failure.
select_param() {
	while :
	do
		echo "
Enter index of the parameter to modify or 'q' to return to the main menu: \c"
		read param_index
		case "$param_index" in
		[1-3])	return $OK
			;;
		[qQ])	return $FAIL
			;;
		*)	echo "Please enter a choice, 1-3 or 'q' to quit.\n"
			;;
		esac
	done
}


# enter_param()
# Prompt user for parameter specified by global param_index.
enter_param() {
	while :
	do
		echo "
Enter the new value or <RETURN> to use the existing value: \c"
		read param_value
		case "$param_value" in
		[0-9]*) check_param || continue
			return
			;;
		"")	return
			;;
		*)	echo "Please enter a numeric value. \n"
			;;
		esac
	done
}

check_eisa() {
# This will tell you if you are on an EISA based computer.
dd if=/dev/mem bs=1048537 skip=1 count=1 | dd bs=4 count=1 2> /dev/null | hd | grep EISA > /dev/null
iseisa=$?
}

eisa_running() {
# This will tell you if the EISA NVM Driver is running.
	[ -c /dev/eisa0 ] && {
		</dev/eisa0 2> /dev/null 1> /dev/null
		return $?
	}
	return 1
}

conflicts() {
# Use idcheck to verify that there are no conflicts
ioerr=`idcheck -a -l $addr -u $upper -r`
[ "$ioerr" -a "$ioerr" != "cpqs" ] && {
echo "There is an io conflict with $ioerr."
fatal=1
}
dmaerr=`idcheck -d $dma -r`
[ "$dmaerr" -a "$dmaerr" != "cpqs" ] && {
echo "There is a dma conflict with $dmaerr."
fatal=1
}
interr=`idcheck -v $intr -r`
[ "$interr" -a "$interr" != "cpqs" ] && {
echo "There is a irq conflict with $interr."
fatal=1
}
if [ "$fatal" -ne 0 ]
then
echo "Installation cannot continue."
return $FAIL
fi

}

set_eisa() {
# Set the IRQ, DMA, and I/O addrs to values set in EISA NVM
fatal=0
eisa_cfg=`/etc/eisa_nvm board_id cpqfd17 0xffffffff`
if [ "$eisa_cfg" = "" ]
then
  echo "EISA NVM Indicates that no SCSI Tape Adapters have been installed."
  echo "Please update EISA NVM with the Compaq EISA Config utility and run"
  echo "mkdev tape again."
  return $FAIL
fi
set $eisa_cfg
slot=$1
addr=$5
upper=$6
dma=$3
intr=$4
# Check for conflicts in IRQ, DMA, and I/O addrs
conflicts
if [ "$?" -eq "$OK" ] 
then
  	mod_failed=0
	nomod=0
	return $OK
else
	addr=
	dma=
	intr=
	return $FAIL
fi
}

# main()

set_trap

check_eisa
get_param

if [ "$iseisa" -eq 0 ]
then
	eisa_running || { 
	  echo "The EISA NVM Driver must be running to install this tape."
	  idcheck -p eisarom && {
	    echo "Please reboot and run mkdev tape."
	    } || {
	    echo "Cannot locate eisarom driver"
	    }
	  exit $FAIL
	}

	# Just display the parameters
	set_eisa || cleanup $FAIL
	show_eisa
	if [ "$mod_failed" -eq 0 ] 
	then
		set_param || cleanup $FAIL
	fi
else
	while echo "\nCompaq SCSI Host Adapter Configuration

	1. Display current parameters
	2. Modify current parameters
	3. Select default parameters

Enter an option or 'q' to quit: \c"
do
	read ans
	case "$ans" in
	1)	show_param
		;;
	2)	show_param
		while select_param
		do
			enter_param
		done
		;;
	3)	reset_param
		show_param
		;;
	[qQ])	if [ "$mod_failed" -eq 0 ]
		then
			if [ "$addr" = "" -o "$dma" = "" -o "$intr" = "" ]
			then
			   echo "\n\tYou must set addr, dma, and intr values.\n"
				mod_failed=1
				continue
			fi
			set_param || cleanup $FAIL
		fi
		break
		;;
	*)	echo "\nPlease enter '1', '2', '3' or 'q'.\n"
		;;
	esac
done
fi

if [ "$nomod" -eq 1 -o "$mod_failed" -eq 0 ]
then
	cleanup $OK
else
	cleanup $FAIL
fi
