:
#	@(#) cdrom.sh 22.1 90/03/26 
#
#	Copyright (C) The Santa Cruz Operation, 1986-1990.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation and is Confidential.
#
# 	UNIX 3.2 CD-ROM and WORM Initialization
#
PATH=/etc:/bin:/usr/bin
LANG=english_us.ascii
export PATH LANG

# Set change to NO
change=NO
tmp=/tmp/cd.$$

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

# Function definitions
##########################

# ---------- STANDARD ROUTINES -------- These routines are commen to scripts
#					requiring kernel relinking.

# 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
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

# clear the screen if it is supported
clearscr() {
	# check if the terminal environment is set up
	[ "$TERM" ] && clear 2> /dev/null
}

# 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 $OK 			;;
		[nN])	return $FAIL			;;
		*)	echo "Please answer y or n" >&2	;;
		esac
	done
}

# Prompt with mesg, return non-zero on q
prompt() {
	while	echo "\n${mesg}or enter q to ${quit}: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return $FAIL					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an argument use it as the default
			# else loop until 'cmd' is set
			[ "$1" ] && { 
				cmd=$1
				return $OK
			}
			: continue
			;;
		*)	return $OK					;;
		esac
	done
}

# Print an error message
error() {
	echo "\nError: $*" >&2
	return $FAIL
}

# Configure error message
conferr()  {
	error "\nConfigure failed to update system configuration.
Check /etc/conf/cf.d/conflog for details."
}

# perms list needed if link kit must be installed
permschk () {
	if [ -f /etc/perms/extmd ]; then
		PERM=/etc/perms/extmd
	else
		error "Cannot locate /etc/perms/extmd. Needed to verify
 linkkit installation."
		cleanup $FAIL
	fi
	return $OK
}

# test to see if link kit is installed
linkchk()  {
	cd /
	until	fixperm -i -d LINK $PERM
	do	case $? in
		4)  echo "\nThe Link Kit is not installed." >&2	;;
		5)  echo "\nThe Link Kit is only partially installed." >&2  ;;
		*)  echo "\nError testing for Link Kit.  Exiting." >&2 
			cleanup $FAIL  ;;
		esac

		# Not fully installed. Do so here
		getyn "Do you wish to install it now?" || {
			# answered no
			echo "
The link kit must be installed to run this program.  Exiting ..."
			cleanup $OK
		}

		# answered yes, so install link kit
		echo "\nInvoking /etc/custom\n"
		/etc/custom -o -i LINK || {
			# custom exited unsuccessfully
			error "custom failed to install Link Kit successfully.  Please try again."
			cleanup $FAIL
		}
	done
}

asklink()  {
	getyn "
You must create a new kernel to effect the driver change you specified.
Do you wish to create a new kernel now?"  ||  {
		echo "
To create a new kernel execute /etc/conf/cf.d/link_unix. Then you
must reboot your system by executing  /etc/shutdown -i0  before the 
changes you have specified will be implemented.\n"
			return $FAIL 
		}
	return $OK
}

# re-link new kernel
klink() {
	cd /etc/conf/cf.d
	./link_unix || { 
		echo "\nError: Kernel link failed."
		cleanup $FAIL
	}
	return $OK
}
 
# ---------- CONFIGURE ROUTINES ------- These routines extract information 
#					from and modify kernel configuration
#					files and parameters.

# This routine sets cf_state and the device major number if applicable. 
# Possible values for cf_state are as follows. 
# 	NO: Device not present in mdevice file and therefore has
#	      no major number.
# 	PART: Device present in mdevice file but the -Y option in the configure
#      	      field of the sdevice file is not set. This causes the driver to 
#	      be excluded from the next reconfiguration.
# 	All:  Device has a major number and the -Y option is set in  
#	      the sdevice file.
confchk () {
	name=$1
	cf_state= 	major=
	cd /etc/conf/cf.d
	major=`./configure -j $name` || {
		cf_state=NO
		return $FAIL
	}
	cf_state=PART
	if [ -f ../sdevice.d/$name ] 
	then
		fieldval=`awk '{print $2}' ../sdevice.d/$name` || {
			error "awk failed to read sdevice file."
			cleanup $FAIL
		}
		[ "$fieldval" = "Y" ] && {
			cf_state=YES
			return $OK
		}
	else
		getyn "
This device has an entry in the master device file but no corresponding
system device entry. Do you wish to continue and have a default 
entry created?" || cleanup $FAIL
	fi
	return $FAIL
}

# Add driver according to cf_state. 
# Global variables used and where set:
# 	$fns: add routines; $cf_state: confchk(); 
#	$major: confchk() or add routine
confadd() {
	
	cd /etc/conf/cf.d
	case $cf_state in

	NO) 	echo "\nAdding device to system configuration files ...\c" >&2
		major=`./configure -j NEXTMAJOR`
		[ "$ipl" ] && IPL="-l $ipl"
		[ "$iv" ] && IV="-v $iv"
		./configure -a $fns -b -c -m $major $IPL $IV > conflog 2>&1 || {
			conferr
			return $FAIL
		}
		./configure -d -b -c -m $major -R > conflog 2>&1 || {
			conferr
			return $FAIL
		} ;;
	PART)   echo "\nUpdating system configuration ...\c" >&2
	        ./configure -a -b -c -m $major -Y > conflog ||  {
			conferr
			return $FAIL
		} ;;
	esac
	echo "\n\nSystem files have been successfully updated."
	change=YES
	rm -f conflog
	return $OK
}

# Configure driver out of system configuration files by unsetting -Y option.
# Global variables used and where set:
#	$major: confchk() 
confrm() {
	cd /etc/conf/cf.d
	echo "\nUpdating system files to effect removal of driver ...\c"
	./configure -b -c -d -m $major -Y > conflog 2>&1 || {
		conferr
		return $FAIL
	}
	echo "\n\nSystem files have been updated."
	rm -f conflog
	change=YES
	return $OK
}

# -------TAPE SPECIFIC ROUTINES ------- These routines are specific to 
#					mkdev tape. 
#					

# ------- Add Drivers -------- 		These routines add tape drivers to 
#					configuration files and create 
#					necassary devices

# This program examines the mscsi file and returns the number of
# SCSI tape devices configured. SCSIpos is used to calculate minor
# device numbers for SCSI tapes.
# Global variables set: 
#	SCSIpos
find_SCSIpos() {
	[ -r /etc/conf/cf.d/mscsi ] || {
		error "\nCannot read /etc/conf/cf.d/mscsi."
		cleanup $FAIL
	}
	num=`sed '/^*/d' /etc/conf/cf.d/mscsi | grep -c "$DRIVER"` 
	[ -n "$num" ] || {
		error "\nGrep failed to read /etc/conf/cf.d/mscsi."
		cleanup $FAIL
	}
	# SCSI numbering starts at 0.
	SCSIpos=`expr $num - 1`
	return $OK
}

# Create all non-default devices. 
# Global variables used and where set:
# 	$SCSIpos: find_SCSIpos(); $MINIpos: find_MINIpos()
mk_nodes() {
	minor=`expr 0 + $SCSIpos` 
	echo "$DRIVER	$DEVNAME$minor	b	$minor"	>> /etc/conf/node.d/$DRIVER
	echo "$DRIVER	r$DEVNAME$minor	c	$minor"	>> /etc/conf/node.d/$DRIVER
}

rm_nodes() {
	minor=`expr 0 + $SCSIpos` 
	cp /etc/conf/node.d/$DRIVER /tmp/$DRIVER.$$
	grep -v $DEVNAME$minor /tmp/$DRIVER.$$ > /etc/conf/node.d/$DRIVER
	rm /tmp/$DRIVER.$$
}

# Global variables used and where set:
# 	$DRIVER, $DESCR: menu routines; $major: confchk() or confadd() 
# 	$SCSIpos: find_SCSIpos()
# Global variables set:
# 	dev_major, change
install () {
	# Check for the presence of the link kit. Install if it not present.
	permschk && linkchk

	# Configure the driver if necassary.
	confchk "$DRIVER"  || {
		# Add scsi driver to config files.
		confadd || cleanup $FAIL
	}
	find_SCSIpos ; oSCSIpos=$SCSIpos
	[ $oSCSIpos -ge 3 ] && {
		echo "
Sorry, the maximum of four $NAME Drives has been reached. Unable to
configure another one."
		return $FAIL
	}
	getyn "The $NAME Driver must be configured before use. 
Do you wish to configure the $NAME Driver now?" || {
		echo "
The SCSI $NAME Driver has been added to system configuration files.
However, /etc/conf/cf.d/mscsi must also be updated before using
a SCSI $NAME Drive. It is advisable to reinvoke mkdev $MYNAME to 
accomplish this."
		return $FAIL
}
	# Edit /etc/conf/cf.d/mscsi
	/bin/sh /usr/lib/mkdev/.scsi -a $DEVTYPE -n || return $FAIL

	while :
	do 	find_SCSIpos ; nSCSIpos=$SCSIpos
	 	differ=`expr $nSCSIpos - $oSCSIpos`
	 	case $differ in
	 	0) 	
			getyn "
The number of SCSI $NAME Drives has not been changed.
Do you wish to try again?" || return $FAIL
			;;
		[1-4]) 	break
			;;
		[5-9]) 	echo "
A maximum of four SCSI $NAME Drives are allowed. Please reconfigure."
			;;
		*) 	error "$differ not valid."
			return $FAIL
			;;
		esac
		echo "
Invoking the interface to the master SCSI device file."
		/bin/sh /usr/lib/mkdev/.scsi || return $FAIL
	done
	change=YES

	# Set dev_major for use in mkdevice(). 
	dev_major=$major

	SCSIpos=$oSCSIpos
	while [ $SCSIpos -lt $nSCSIpos ]
	do
		SCSIpos=`expr $SCSIpos + 1`
		# Create pure scsi devices.
		mk_nodes "$DRIVER"
	done
	return $OK
}


# 	$DRIVER, $DESCR: menu routines; 
# 	$SCSIpos: find_SCSIpos() 
remove() {
	confchk "$DRIVER" || {
		echo "
The $NAME Driver is not currently configured.\n"
		return $FAIL
	}
	find_SCSIpos ; oSCSIpos=$SCSIpos
	if [ $oSCSIpos -lt 0 ] 
	then
		getyn "
There are currently no SCSI $NAME Drives configured in the /etc/conf/cf.d/mscsi
file. Do you wish to continue."	|| return $FAIL
		# Pretend we have 4 so we can remove any nodes created and
		# remove scsi driver from kernel.
		oSCSIpos=3
	else
		# Edit /etc/conf/cf.d/mscsi
		/bin/sh /usr/lib/mkdev/.scsi -r $DEVTYPE -n || return $FAIL
	fi
	find_SCSIpos ; nSCSIpos=$SCSIpos
	differ=`expr $oSCSIpos - $nSCSIpos`
	case $differ in
 	0) 	
		echo "
The number of SCSI $NAME Drives has not been changed."
		return $FAIL
		;;
	[1-4]) 	:	# OK
		;;
	*) 	error "$differ not valid."
			return $FAIL
		;;
	esac
	change=YES

	SCSIpos=$oSCSIpos
	while [ $SCSIpos -gt $nSCSIpos ]
	do
		SCSIpos=`expr $SCSIpos - 1`
	done

	[ $SCSIpos -eq -1 ] && { 
		permschk && linkchk
		confrm || cleanup $FAIL
	echo "
The SCSI $NAME Driver and associated devices have been removed."
	}
	return $OK
}

# main()
#########################

set_trap
cd /
clearscr
MYNAME=`basename $0`
case $MYNAME in
	cdrom)
		DRIVER=Srom
		NAME=CD-ROM
		DEVNAME=cd
		DEVTYPE=-c
		fns="Srominit Sromopen Sromclose Sromread Sromstrategy Sromioctl"
		break
		;;
	worm)
		DRIVER=Swrm
		NAME=WORM
		DEVNAME=worm
		DEVTYPE=-w
		fns="Swrminit Swrmopen Swrmclose Swrmread Swrmwrite Swrmstrategy Swrmioctl"
		break
		;;
	*)
		echo "configuration of `basename $0` not supported"
		exit $FAIL
esac

while mesg="\n\n$NAME Configuration Program \n
	1. Install a $NAME Drive
	2. Remove a $NAME Drive

	Select an option "
	quit="quit"
do prompt || break
# reset these flags prior to each pass through menu
major=
IPL=
IV=
select=$cmd
case $select in
	1) install		;;
	2) remove		;;
	*) echo "\n	Please select either 1 or 2"
	   continue		;;
esac
done

if [ "$change" = "YES" -a ! "$_RELINK" ] 
then
	asklink && klink  
fi

cleanup $OK
