:
#	@(#) vpixld.sh 1.11 89/08/28 
#
#	Copyright (C) The Santa Cruz Operation, 1988, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#
# vpixld - UNIX 3.2 VP/ix line discipline initialization
#
PATH=/etc:/bin:/usr/bin
LANG=english_us.ascii
export PATH LANG

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

tmp="/tmp/vp$$"
prefix="vpix"
funcs="vpopen vpclose ttread ttwrite vpioctl vpin ttout nulldev"
name=vpld

# 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 "\nInterrupted! 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 "$* (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
}

# 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 "\nDo 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
}
 
# ------- VP/ix CONFIGURE ROUTINES ---- These routines extract information 
#					from and modify kernel configuration
#					files and parameters.

# This routine checks to see if the -Y option in the configure field 
# of the sdevice file is set. Since this is a line discipline it has no
# major number.
# Where variables are set:
# name: script start	
vpchk () {
	cd /etc/conf/sdevice.d
	if [ -f $name ] 
	then
		fieldval=`awk '{print $2}' $name` || {
			error "awk failed to read $name sdevice file."
			cleanup $FAIL
		}
		[ "$fieldval" = "Y" ] && return $OK
		return $FAIL
	else
		error "
This device has no $name sdevice entry. This file must exist in order to 
add vpixld." 
		cleanup $FAIL
	fi
}

# Change sdevice file to effect addition of driver.
# Where variables are set:
# name: script start	
vp_add() {
	cd /etc/conf/sdevice.d
	echo "\nUpdating driver configuration file ...\c" >&2
	sed "s/N/Y/" $name > $tmp.sed || { 
		error "\nsed unable to edit /etc/conf/sdevice.d/$name."
		return $FAIL
	}
	unset_trap
	mv $tmp.sed $name
	set_trap
	change=YES
	rm -f conflog
	echo 
	return $OK
}

# Configure driver out of system configuration files by unsetting -Y option.
# Where variables are set:
# name: script start	
vp_rm() {
	cd /etc/conf/sdevice.d
	echo "
Updating driver configuration file to effect removal of driver ...\c" >&2
	sed "s/Y/N/" $name > $tmp.sed || { 
		error "\nsed unable to edit /etc/conf/sdevice.d/$name."
		return $FAIL
	}
	unset_trap
	mv $tmp.sed $name
	set_trap
	change=YES
	rm -f conflog
	echo 
	return $OK
}

# ---------- MAIN ROUTINES ---------- 	These routines execute the flow of 
#					events to complete the task 
#					corresponding to the main menu.

vpix_add()  {
	vpchk || {
		cd /etc/conf/cf.d
		vp_add || return $FAIL
	}
	# Check for entries in line discipline switch table.
	# Add if they are not present.
	/etc/conf/bin/idaddld -c $prefix || {
		echo "
Adding line discipline declaration to kernel configuration files ...\c"
		/etc/conf/bin/idaddld -a $prefix $funcs || return $FAIL
		echo
		change=YES
	}
	return $OK
}

vpix_rm()  {
	vpchk && {
		cd /etc/conf/cf.d
		vp_rm || return $FAIL
	}

	# Check for entries in line discipline switch table.
	# Remove if they are present.
	/etc/conf/bin/idaddld -c $prefix && {
		echo "
Removing line discipline declaration from kernel configuration files ...\c"
		/etc/conf/bin/idaddld -d $prefix || return $FAIL
		echo
		change=YES
	}
	return $OK
}

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

cd /
set_trap
permschk
linkchk

clearscr
mesg="VP/ix Line Discipline Initialization Program \n
	1. Add line discipline to the system
	2. Remove line discipline from the system
	
Select an option "

prompt || cleanup $OK
	case $cmd in
      		1) vpix_add || cleanup $FAIL	;;
		2) vpix_rm  || cleanup $FAIL  ;;
	esac
if [ "$change" = "YES" ]
then

	if [ "$_RELINK" -o "$_NOPROMPT" ]
	then
		klink
	else
		asklink && klink  
	fi
else
	echo "\nConfiguration files already reflect desired changes."
fi
       		  	   
cleanup $OK
