:
#	@(#) tune 23.1 91/10/03 
#
#	Copyright (C) 1990-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.
#
#	This is a sh script which is called by adjusttunable() defined
#	in add.c. This is a script to perform the tunable parameters 
#	adjustment for netconfig. 
#
#	It looks at the cf.d/mtune file and checks if the adjusted value 
#	is valid. It then uses /etc/conf/bin/idtune to configure the 
#	tunable parameter into mtune/stune file.
#
#	$0 = identity of the product
#	$2 = tunable parameter value to be changed
#	$3 = minimun value
#	$4 = maximum value
#	$5 = state which indicate to add the changed value if $5=1 or
#	     subtract from the existed mtune value if $5=0
#

OK=0	FAIL=1	RELINK=2

INCREMENT=1
DECREMENT=0

# is ident defined in mtune?

checkmtune()
{
	grep $ident mtune > /dev/null 2>&1
	result=$?
	if [ "$result" = "$FAIL" ]
		then echo "The tunable parameter $ident does not exist in the mtune file"
	fi
}

# get the parameters of ident from the mtune file

getpara()
{
	cur_dir=`pwd`
	cd /etc/conf/cf.d
	default=`./configure -y $ident`
	mtunemin=`grep "^$ident[ 	]" mtune | awk '{ print $3 }'`
	mtunemax=`grep "^$ident[ 	]" mtune | awk '{ print $4 }'`
	cd $cur_dir
}

#
# check the range, validate and coerce ranges, and coerce the new
# value to be within those ranges.
#

checkrange()
{
	# first validate ranges
	[ "$min" -lt "$mtunemin" ] && min=$mtunemin

	if [ "$state" -eq "$INCREMENT" ] 
	then
		newval=`expr $default + $change`
	else
		newval=`expr $default - $change`
	fi

	[ "$newval" -lt "$min" ] && newval=$min
	[ "$newval" -gt "$max" ] && newval=$max
}

##########
#
# changeparm ()
#
# Calls: configure expr
#
# Purpose: Change the value of kernel parameters
#
changeparm () {
	# may need to update the mtune maximum
	[ $newval -gt $mtunemax ] && {
		sed -e "
			/NAME/s/NAME/NREGION/
			/VALUE/s/VALUE/600/
		" << done > /tmp/bmx$$
		awk '/^NAME/ {
			NEW=\$4
			if (NEW < (\$2+VALUE))
				NEW=\$2+VALUE
			printf "%s\t%s\t%s\t%s\n",\$1,\$2,\$3,NEW
			next
		}
		{ print } ' < /etc/conf/cf.d/mtune > /tmp/bm\$\$
		mv /tmp/bm\$\$ /etc/conf/cf.d/mtune
done
		chmod 775 /tmp/bmx$$
		/tmp/bmx$$
		rm /tmp/bmx$$
	}
	echo Changing $ident from $default to $newval
	/etc/conf/bin/idtune -f $ident $newval
}

# main

currdir=`pwd`
cd /etc/conf/cf.d
ident=$1 change=$2 min=$3 max=$4 state=$5

checkmtune
getpara
checkrange
changeparm

cd $currdir
exit $OK
