#!/bin/sh
#
# |-----------------------------------------------------------|
# | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
# | All Rights Reserved                                       |
# |-----------------------------------------------------------|
# |          Restricted Rights Legend                         |
# | Use, duplication, or disclosure by the Government is      |
# | subject to restrictions as set forth in                   |
# | subparagraph (c)(1)(ii) of the Rights in Technical        |
# | Data and Computer Software Clause of DFARS 252.227-7013.  |
# |         MIPS Computer Systems, Inc.                       |
# |         950 DeGuigne Avenue                               |
# |         Sunnyvale, California 94088-3650, USA             |
# |-----------------------------------------------------------|
#
# $Header: idtune,v 1.3.2.1 92/06/24 13:34:46 marker Exp $
# v4.51 RISC/OS version of the AT&T idtune utility.
#
# The syntax has been modified as follows:
#
# idtune  [ -f | -m ] [ -s suffix ] name  value
# Where <suffix> is the system name suffix to be used.
#
# Attempt to set the value of tuneable parameter 'name' to 'value'
# If a value is already given to this parameter (via any of the master files 
# or kernel.rxxxx_suffix file ), the user will be asked to confirm the change 
# unless the -f option is used. If the -m option is used, we silently 
# accept existing values greater than the new value.
#
#	MODIFICATION HISTORY
#	12/21/90	TDhar	Initial Coding
#	03/25/91	TDhar   Call idmakesys unconditionally. 
#	04/010/91	TDhar	Default suffix changed to local/ijc_local

root=/
masterd=/usr/src/uts/mips/master.d
boot=/usr/src/uts/mips/bootarea_i
mips=/usr/src/uts/mips

USAGE="USAGE: idtune [ -m | -f ] [ -s suffix ] name value"



force=0
min=0
#suffix=sys
model=`uname -t`
case $model in
	m2000-6|m2000-8)
		suffix=ijc_local
		;;
	*)
		suffix=local
		;;
esac

case "$1" in
	-f)	force=y;  shift;;
	-m)	min=y;  shift;;
esac

if [ $# != 2 -a $# != 4 ]
then
	echo ${USAGE}
	exit 99
fi

case "$#" in
	4)	if [ $1 = '-s' ]
		then
			suffix=$2
		else
			echo $USAGE
			exit 99
		fi
		name=$3
		new_value=$4;;
	2)	name=$1
		new_value=$2;;
esac

expr ${new_value} \* 2 > /dev/null 2>&1
if [ $? != 0 ]
then
	echo $USAGE
	exit 99
fi

expr ${name} \* 2 > /dev/null 2>&1
if [ $? = 0 ]
then
	echo $USAGE
	exit 99
fi

expr ${suffix} \* 2 > /dev/null 2>&1
if [ $? = 0 ]
then
	echo $USAGE
	exit 99
fi

# Build the corresponding kernel and sysgen filenames
stdunix=`/etc/conf/bin/idfindsys -u`
newunix=`echo $stdunix | sed "s/_.*\$/_$suffix/"`
newkernel=`echo $newunix | sed 's/unix/kernel/'`
newsysgen=`echo $newunix | sed 's/unix/sysgen/'`

STUNE=${masterd}/${newkernel}

/etc/conf/bin/idmakesys -s ${suffix}
if [ $? != 0 ]
then
	echo "
${STUNE} does not exist also can not be created"
	exit 99
fi

trap '' 1 2 3 9 15

lne="#define $name $new_value"

#first check all Master files
echo "Checking all master files first, please wait"
vi ${masterd}/Makefile << ! > /dev/null 2>&1
/MASTERS
ma
/include
mb
:'a,'bw!/tmp/trp
:q!
!
list=`cat /tmp/trp`
rm /tmp/trp
found=0
for i in ${list}
do
	if [ -f ${masterd}/$i ]
	then
		line=`grep "^\#define[ 	]$name[ 	]" ${masterd}/$i` 
		if [ $? = 0 ]
		then
			set $line
			old_value=0
			old_value=$3
			if [ $old_value = $new_value ]
			then
				exit 0
			fi
			if [ $min != 0 ]
			then
				if [ $old_value -ge $new_value ]
				then
					exit 0
				fi
			fi
			if [ $force = 0 ]
			then
				echo "\nTuneable Parameter \"$name\" is currently set to ${old_value}."
				echo "Is it OK to change it to $new_value? (y/n) \c"
				read ans
				case $ans in
					y*|Y*)	;;
					*)	echo "\n\"$name\" left at ${old_value}.\n"
						exit 0;;
				esac
			fi
			cp ${masterd}/$i ${masterd}/${i}.bu
			ed ${masterd}/$i <<! > /dev/null 2>&1
				/^\#define[ 	]$name[ 	]$old_value/d
				i
$lne
.
				w
				q
!
			ecode=$?
			if [ $ecode != 0 ]
			then
				echo "$name defined in ${masterd}/$i, But \"idtune\" can not edit ${masterd}/$i"
				exit ecode
			fi
			found=1
		fi 				# $? = 0 
	fi 					#  -f ${masterd}/$i 
done
if [ $found = 1 ]
then
	exit 0
fi

xx=`grep "^\#define[ 	]$name[ 	]" $masterd/kernel_common` 
if [ $? != 0 ]
then
	echo "$0 Error: Illegal name $name"
	exit 1
fi
line=`grep "^\#define[ 	]$name[ 	]" $STUNE` 
if [ $? = 0 ]
then
	set $line
	old_value=0
	old_value=$3
	if [ $old_value = $new_value ]
	then
		exit 0
	fi
	if [ $min != 0 ]
	then
		if [ $old_value -ge $new_value ]
		then
			exit 0
		fi
	fi
	if [ $force = 0 ]
	then
		echo "\nTuneable Parameter \"$name\" is currently set to ${old_value}."
		echo "Is it OK to change it to $new_value? (y/n) \c"
		read ans
		case $ans in
			y*|Y*)	;;
			*)	echo "\n\"$name\" left at ${old_value}.\n"
				exit 0;;
		esac
	fi
ed $STUNE <<! > /dev/null 2>&1
/^\#define[ 	]$name[ 	]
d
i
$lne
.
w
q
!
	errcode=$?
else
	ed $STUNE <<! > /dev/null 2>&1
		/^\#include[ 	]\"kernel_common\"
		i
$lne

.
		w
		q
!
	errcode=$?
fi
exit $errcode
