:

#           (C) Copyright Altos Computer Systems, 1991.
#                      All rights reserved.
#
# This program provides a menu interface to the strmcfg utility and 
# a way to add and remove STREAMS configurations to the kernel. 
#
trap "" 0 2

USER=`id | awk '{ print $1 }' | awk '{ FS = "(" ; print $2 }' | awk '{ FS = ")" ; print $1 }'`
bold=`tput smso`
nobold=`tput rmso`
ln=`tput lines`
newscr=`tput cup 3 0; tput ed`
opt1="1.  Run ${bold}strmcfg${nobold} with default options" 
opt2="2.  Tune kernel with new parameters"
opt3="3.  Remove added parameters from kernel" 
opt4="4.  Display Streams Parameters"
OPT1="${bold}1.  Run strmcfg with default options${nobold}" 
OPT2="${bold}${opt2}${nobold}"
OPT3="${bold}${opt3}${nobold}"
OPT4="${bold}${opt4}${nobold}"
PATH=/bin:/usr/bin:/etc:/etc/conf/bin
tunedir=/usr/altos/tuning/streams
tmp=/tmp/str
_RELINK=				# gets set to true by SAMI if nec.
sederror=
conf_string=""

: ${OK=0} ${FAIL=1}

# Function Definitions

# usage: safemv file1 file2
# move file1 to file2 without chance of interruption
safemv() {
	trap "" 1 2 3 15
	mv -f $1 $2  2> /dev/null
	if [ $? -eq 2 ] ; then
		echo $1 " move failed"
		lf -l $1
	fi
	trap 1 2 3 15
}

# Prompt for yes or no answer with message passed as argument - 
# returns non-zero for no
getyn() {
	while	echo "\n$* (y/n/q) \c">&2
	do	read yn rest
		case $yn in
			[yY]) return 0				;;
			[nN]) return 1			;;
			[qQ]) cleanup 0			;;
		*)	echo "Please answer y,n or q" >&2	;;
		esac
	done
}

# Display "wait" message
go_main() {	
		echo ""
		echo -n "               Hit Return to continue"
		read dummy
}

# Build configuration string
conf_value () {
	if [ "$2" = "parameter" ]
	then
		echo "The STREAMS parameter $1 does not exist"
	else
		echo "Configuring $1 to $3 (was $2). "
		conf_string="$conf_string $1=$3"
	fi
}

# Remove temp files and exit with the status passed as argument
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

# Test to see if link kit is installed
chklinkkit() {
    until	fixperm -i -d LINK $LKPERM
    do	case $? in
	4)	echo "The Link Kit is not installed." >&2		;;
	5)	echo "The Link Kit is only partially installed." >&2	;;
	*)	echo "Error testing for Link Kit. Exiting."; cleanup 1 ;;
	esac
	# Not fully installed. Do so here
	while	echo "Do you wish to install it now? (y/n) \c"
	do	read ANSWER
		case $ANSWER in
		Y|y)	custom -o -i LINK
			break
			;;
		N|n)	echo "Drivers cannot be installed without the Link Kit."
			cleanup 1
			;;
		*)	echo "Please answer 'y' or 'n'. \c"
			;;
		esac
	done
    done

	# check for streams in kernel
	for name in "str" "clone" "log" "ptm"  \
			"pts" "ldterm" "ptem" "timod" \
		    	"tirdwr" "sp" ; do
    		idcheck -p $name 
    		if [ $? -ne 31 ] ; then
			echo "NFS cannot be installed without $name." >&2
			echo "Use mkdev streams to add streams drivers." >&2
			cleanup 1;
    		fi
	done
}

# Re-make new kernel
relink() {
    cd /etc/conf/cf.d
    if [ -z "$_NOPROMPT" ] ; then
	echo "\nRe-linking the kernel ... \c" >&2
    fi
    ./link_unix 2>&1 | tee -a kmakelog || {
	    echo "\nError:  Kernel link failed.
Check /etc/conf/cf.d/kmakelog for details." >&2
	    cleanup 1
    }
    rm -f kmakelog
    return 0
}
	
# Add or increase STREAMS value, and configure the kernel
adding () {
	
	# Read in values as they are set know:

	if [ ! -d $tunedir/config ]
	then
		mkdir -p $tunedir/config 2> /dev/null
	fi
	cd $tunedir/config
	packages=`ls | grep -v ".cfg" 2>/dev/null`
	if [ -n "$packages" ]
	then
		echo "\nAdjusting STREAMS parameters ..."
		for i in $packages
		do
			safemv $i $i.cfg
		done
	else
		echo "\n\tNo Configuration files exists, return to main menu"
		echo "\tand run ${bold}strmcfg${nobold}, Option 1"
		return
	fi
	set `cat $tunedir/results`
	cd /etc/conf/cf.d
	echo $newscr
	conf_string=""
	while [ $# -gt 0 ]
	do
		OLD_BLKS=`./configure -y "$1" | awk '{print $1}'`
		if [ $OLD_BLKS -eq $2 ] 
		then
			:
		else
			conf_value $1 $OLD_BLKS $2
		fi
		shift; shift; shift
	done
	./configure $conf_string
	if getyn "\nDo you wish to relink the kernel now?"
	then
		relink
	else
		echo "\nYour changes will not take effect until the kernel is relinked." >&2
	fi
}

# Remove or decrease STREAMS value, and configure the kernel
removing () {
	
	# Read in values as they are set know:
	if [ ! -d $tunedir/config ]
	then
		mkdir -p $tunedir/config 2> /dev/null
	fi
	cd $tunedir/config
	d_list=""
	oldconfig=`ls *.cfg 2>/dev/null`
	if [ -n "$oldconfig" ]
	then
		echo "\nAdjusting STREAMS parameters ..."
		echo "\nYour kernel is configured for the following products:" 
		for i in $oldconfig
		do
			prod=`grep "^#" $i|head -1`
			prod=`expr "$prod" : '.\(.*\)'`
			prod=`echo $prod | awk '{ FS="("; print $1 }'`
			if getyn "\t$prod: Delete parameters ? "
			then
				d_list="$d_list $i"
			fi
		done
	else
		echo "\nYour STREAMS parameters is not configured for any Communication packages"
		return
	fi
	if [ -n $d_list ]
	then
		set `cat $tunedir/results`
		cd /etc/conf/cf.d
		conf_string=""
		echo $newscr
		while [ $# -gt 0 ]
		do
			val=0
			for i in $d_list
			do
				nextval=`grep "^$1	" ${tunedir}/config/$i | awk '{print $2}'`
				if [ -n "$nextval" ]
				then
					val=`expr $val + $nextval`
				fi
			done
			OLD_VAL=`./configure -y "$1" | awk '{print $1}'`
			if [ $OLD_VAL -le $val ]
			then
				val=0
			fi
			val=`expr $OLD_VAL - $val`
			if [ $OLD_VAL != $val ]
			then
				conf_value $1 $OLD_VAL $val
			fi
			shift; shift; shift
		done
		./configure $conf_string
		if getyn "\nDo you wish to relink the kernel now?"
		then
			relink
		else
			echo "\nYour changes will not take effect until the kernel is relinked." >&2
		fi
		cd ${tunedir}/config
		for i in $d_list
		do
			rm $i
		done
		for i in `ls | grep -v ".cfg" 2>/dev/null`
		do
			rm $i
		done
	else
		echo "No products selected"
	fi		
}

#main menu
while true
do

clear
echo ""
echo ""
echo "                   ${bold}STREAM CONFIGURATION Utility:${nobold}"
echo ""
echo "               $opt1"
echo "" 
echo "               $opt2"
echo "               $opt3"
echo ""
echo "               $opt4"
echo ""
echo "               q.  Quit"
echo ""
echo ""
echo -n "               Enter choice>> "

read CHOICE

case "$CHOICE" in
	"1")	clear
		echo ""
		echo $OPT1
		/usr/altos/bin/strmcfg
		go_main
		;;
	"2")	clear
		echo ""
		echo $OPT2
		adding
		go_main
		;;
	"3")	clear
		echo ""
		echo $OPT3
		removing
		go_main
		;;
	"4") clear
		echo ""
		echo $OPT4
		echo ""
		/usr/altos/bin/strmcfg -p | more
		go_main
		;;
	"q")  exit
		;;
	*)	continue
		;;
esac
 
done
