:
#	@(#) streams.sh 1.11 89/08/04 
#
#	Copyright (C) The Santa Cruz Operation, 1988, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated as Confidential.
#

#	UNIX 3.2 -- Streams initialization script
#

PATH=/etc:/bin:/usr/bin
LANG=english_us.ascii
export PATH LANG

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

names="str clone log ptm pts ldterm ptem timod tirdwr sp" 
tmp="/tmp/str$$"

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

# This routine checks to see if streams are currently configured in system
# files. It returns OK if driver is installed.
strchk () {
	cd /etc/conf/sdevice.d
	# For each driver in the list of names check to see if it is 
	# presently configured.
	for name in $names
	do
		if [ -f $name ] 
		then
			fieldval=`awk '{print $2}' $name` || {
				error "awk failed to read sdevice file."
				cleanup $FAIL
			}
			[ "$fieldval" = "N" ] && return $FAIL
		else
			error "
The $name driver has no entry in the /etc/conf/sdevice.d directory. 
Cannot add streams without this file."
			cleanup $FAIL
		fi
	done
	return $OK
}

# Update configuration files to effect addition of driver in next kernel build.
stradd() {
	cd /etc/conf/sdevice.d
	sed "s/N/Y/" $name > $tmp || { 
		error "\nsed unable to edit /etc/conf/sdevice.d/$name."
		return $FAIL
	}
	unset trap; mv $tmp $name
	set_trap
	change=YES
	rm -f conflog
	return $OK
}

# Configure driver out of system configuration files by unsetting -Y option.
strrm() {
	cd /etc/conf/sdevice.d
	sed "s/Y/N/" $name > $tmp || { 
		error "\nsed unable to edit /etc/conf/sdevice.d/$name."
		return $FAIL
	}
	unset trap; mv $tmp $name
	set_trap
	change=YES
	rm -f conflog
	return $OK
}

# tune kernel parameters to optimize performance with streams
# ktune () {
#	echo "\nModifying system configuration parameters ...\c"
#	for i in "NQUEUE=256"    "NSTREAM=32"   "NMUXLINK=64"  "NSTREVENT=64" \
#		 "MAXSEPGCNT=1"  "NBLK8192=0"   "NBLK4096=0"   "NBLK2048=20" \
#		 "NBLK1024=12"   "NBLK512=8"    "NBLK256=16"   "NBLK128=64" \
#                 "NBLK64=256"    "NBLK16=128"   "NBLK4=128"    "NSTRPUSH=9" \
#		 "STRMSGSZ=4096" "STRCTLSZ=512" "STRLOFRAC=80" "STRMEDFRAC=90"
#	do
#		param=`expr "$i" : "\(.*\)=.*"`
#		old_val=`./configure -y $param`	
#		new_val=`expr "$i" : ".*=\(.*\)"`
#		[ $new_val -gt $old_val ] && ./configure $i 
#	done
#	echo "\n\nCertain kernel parameters have been modified to optimize \n\
#system performance with streams." 
#return $OK
#}

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

addstreams () {
	strchk && {
		if [ "$_NOPROMPT" ]
		then
			:
		else
			getyn "
Streams are currently configured in system files. 
Do you wish to continue?" || return $FAIL

		fi
	}
	echo "\nAdding streams to system configuration files ...\c" >&2
	# add streams 
	for name in $names
	do
		stradd || cleanup $FAIL
	done
	echo "\n\nSystem configuration files have been successfully modified."
	# set streams kernel parameters using configure
	# *** Paramters are not modified at this point
	# ktune  
}

# list of functions called for removing streams
rmstreams () {
	# remove streams 
	echo "\nUpdating system files to effect removal of streams ...\c"

	for name in $names
	do
		strrm || cleanup $FAIL
	done
	echo "\n\nSystem configuration files have been successfully modified."
}

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

set_trap
cd /

option=$1

# make sure perms list is there.
permschk
# make sure link kit is installed
linkchk
# Go to configuration directory
cd /etc/conf/cf.d

case $option in
1|2)
	case $option in
	1) addstreams
		;;
	2) rmstreams
		;;
	esac
	;;
*)

clearscr
while
    mesg="\n\n\nStreams Initialization Program \n
	1. Add streams modules to the kernel
	2. Remove streams modules from the kernel
	
Select an option "
do
	prompt || break
  	case $cmd in
		1) addstreams && break	;;

		2) rmstreams && break	;;

	esac
done
	
	;;
esac

if [ "$change" = "YES" ] 
then
	if [ "$_RELINK" -o "$_NOPROMPT" ]
	then
		klink
	else
		asklink && klink  
	fi
fi
cleanup $OK
