#!/bin/sh
# $Id: rlock_tune.sh,v 1.3 2003/10/10 18:31:01 rwb Exp $ 
# SCCSID(@(#)rlock_tune.sh	1.5) Modified 13:10:15 7/10/01
# Adjust the file server the file server shared data structure sizes
# from the setting in /etc/default/merge
# NOTE: Only run this when there are NO Windows or DOS sessions running!

Auto_Adjust()
{
	CHANGE=nochange
	if [ "_$MERGE_CDRIVE_G_LOCKS" = "_off" ]
	then
		MERGE_RLOCK_AUTO_SCALE=not_used
	fi
	if [ "_$MERGE_RLOCK_AUTO_SCALE" != "_on" \
	   -a  $MERGE_RLOCK_FILETABLE_SIZE -ne 0 ]
	then
		return
	fi
	SESSIONS=`"$STATICMERGE"/bin/max_sessions`
	Log SESSIONS=$SESSIONS
	if [ "x$SESSIONS" = "x" ]
	then
		Log "max_sessions failed.  Here is the output:"
		"$STATICMERGE"/bin/max_sessions >> "$LOGFILE" 2>&1
		VAL=$?
		Log "exit code $VAL"
		SESSIONS=1
	fi

	# The multi-session settings are somewhat guesses based
	# on what some alpha and beta testers reported.
	# The actual settings depend a lot on what the actual
	# Windows applications that users are using at the time.
	# So this auto-adjust will only make adjustments upwards.
	# Note: to disable this automatic adjustment, set
	# MERGE_RLOCK_AUTO_SCALE to "off" in /etc/default/merge
	
	if [ "_$MERGE_CDRIVE_G_LOCKS" = "_off" ]
	then
		# Only need the rlock tables for files on shared drives.
		# So don't need large tables for most situations.
		FILETABLE=512
		OPENTABLE=768
		LOCKTABLE=1024
		HASHTABLE=100
	elif [ $SESSIONS -le 2 ]
	then
		# Make sure that the settings are at least as large as the
		# values needed to run Winstone96 and Winstone97 in a single
		# session.  Some testing has also shown that for two
		# sessions these numbers work while using MS-Office apps.
		FILETABLE=512
		OPENTABLE=768
		LOCKTABLE=1024
		HASHTABLE=512

	elif [ $SESSIONS -le 5 ]
	then
		# For 5 users (450Mhz 512MbRAM)
		FILETABLE=1536
		OPENTABLE=2304
		LOCKTABLE=3072
		HASHTABLE=1024
	
	elif [ $SESSIONS -le 10 ]
	then
		# For 10-12 users (833Mhz 512Mb)
		FILETABLE=7168
		OPENTABLE=9216
		LOCKTABLE=8192
		HASHTABLE=2048

	elif [ $SESSIONS -le 25 ]
	then
		# For 25 users (2x933Mhz 1Gb)
		FILETABLE=8192
		OPENTABLE=8192
		LOCKTABLE=8192
		HASHTABLE=8192

	else 
		# Big numbers!
		FILETABLE=10240
		OPENTABLE=10240
		LOCKTABLE=10240
		HASHTABLE=10240
	fi

	# Other tweaks the admin might have to make:
        # Up the linux file-max parameter (/proc/sys/fs/file-max)
        # Up the linux inode limit (2.2 Kernels only) (/proc/sys/fs/inode-max)

	if [ "$MERGE_RLOCK_FILETABLE_SIZE" -lt $FILETABLE ]
	then
		MERGE_RLOCK_FILETABLE_SIZE=$FILETABLE
		CHANGE=changed
		Log MERGE_RLOCK_FILETABLE_SIZE changed to $FILETABLE
	fi
	if [ "$MERGE_RLOCK_OPENTABLE_SIZE" -lt $OPENTABLE ]
	then
		MERGE_RLOCK_OPENTABLE_SIZE=$OPENTABLE
		CHANGE=changed
		Log MERGE_RLOCK_OPENTABLE_SIZE changed to $OPENTABLE
	fi
	if [ "$MERGE_RLOCK_LOCKTABLE_SIZE" -lt $LOCKTABLE ]
	then
		MERGE_RLOCK_LOCKTABLE_SIZE=$LOCKTABLE
		CHANGE=changed
		Log MERGE_RLOCK_LOCKTABLE_SIZE changed to $LOCKTABLE
	fi
	if [ "$MERGE_RLOCK_HASHTABLE_SIZE" -lt $HASHTABLE ]
	then
		MERGE_RLOCK_HASHTABLE_SIZE=$HASHTABLE
		CHANGE=changed
		Log MERGE_RLOCK_HASHTABLE_SIZE changed to $HASHTABLE
	fi
}
# + + + + Text handling functions + + + + + + + + + + + + + + + + + + + + + +

# This function prints to stdout, and properly deals with escaped chars.
# On some system, plain "echo" does this, but others need a special option.
Text()
{
	echo $EPT "$*"
}

# Print out to the log file when ok to log, and to the user when trace is on.
Log()
{
	[ "$OK_TO_LOG" = "ok_to_log" ] && echo $EPT "$*" >> "$LOGFILE"
	[ "x$TRACE" = "xtrace" ]       && echo $EPT "$*"
}

# Print out to the user, and to the log file when ok to log.
Echo()
{
	[ "$OK_TO_LOG" = "ok_to_log" ] && echo $EPT "$*" >> "$LOGFILE"
	[ "$DO_SILENT" = "notsilent" ] && echo $EPT "$*"
}

# + + + Configuration functions + + + + + + + + + + + + + + + + + + + + + +
Get_System_Variables()
{
	# Note, none of the errors detected in mrgssv.sh should
	# ever happen, so there is no need to get tricky and to
	# suppress the error message output if the "silent" option
	# might be used.
	
	# Get the STATICMERGE, VARMERGE, DEFAULTMERGE, SYSTEMTYPE, HOME,
	# etc settings by dotting in mrgssv.sh.
	. /etc/mrgssv.sh

	LOGFILE="$VARMERGE/log/rlock_tune.log"
	touch "$LOGFILE"
	if [ -f $LOGFILE -a -w $LOGFILE -a ! -L $LOGFILE ]
	then
		date > "$LOGFILE"
		OK_TO_LOG="ok_to_log"
	else
		LOGFILE=/dev/null
		OK_TO_LOG="no_log"
	fi
	if [ "$SYSTEMTYPE" = "linux" ]
	then
		EPT="-e"
	fi
}


# + + + MAIN --- start here --- + + + + + + + + + + + + + + + + + + + + + +  +
# (Everything above here must only be functions)

NULL=/dev/null
DO_SILENT=notsilent
TRACE=notrace
TMPDIR="$VARMERGE"/tmp
TMPF1="$TMPDIR"/rlk1$$.tmp
TMPF2="$TMPDIR"/rlk2$$.tmp

Get_System_Variables

if [ -f "$VARMERGE/MERGE_CHROOT" ]
then
	echo "rlock_tune found MERGE_CHROOT, doing nothing"
	exit
fi

MERGE_RLOCK_AUTO_SCALE=on
MERGE_RLOCK_FILETABLE_SIZE=0
MERGE_RLOCK_OPENTABLE_SIZE=0
MERGE_RLOCK_LOCKTABLE_SIZE=0
MERGE_RLOCK_HASHTABLE_SIZE=0

EDM=/etc/default/merge

grep '^MERGE_CDRIVE_G_LOCKS=' "$EDM" > "$TMPF1"
grep '^MERGE_RLOCK_' "$EDM" >> "$TMPF1"
if [ ! -f "$TMPF1" ]
then
	# Error, either cannot write to the temp file
	# or /etc/default/merge does not exist.
	# So bail out.
	exit 1
fi
# Set the current values from /etc/default/merge
. "$TMPF1"
rm -f "$TMPF1"

CHANGE=nochange
Auto_Adjust

if [ "$CHANGE" = "changed" ]
then

	# Clear out the old settings
	sed \
		-e '/^MERGE_RLOCK_AUTO_SCALE/d' \
		-e '/^MERGE_RLOCK_FILETABLE_SIZE/d' \
		-e '/^MERGE_RLOCK_OPENTABLE_SIZE/d' \
		-e '/^MERGE_RLOCK_LOCKTABLE_SIZE/d' \
		-e '/^MERGE_RLOCK_HASHTABLE_SIZE/d' \
		"$EDM" > "$TMPF2"

	cp "$TMPF2" "$EDM"
	rm -f "$TMPF2"

	# Put in the new settings

	if [ "_$MERGE_RLOCK_AUTO_SCALE" = "_on" \
	  -o "_$MERGE_RLOCK_AUTO_SCALE" = "_off" ]
	then
		echo "MERGE_RLOCK_AUTO_SCALE=$MERGE_RLOCK_AUTO_SCALE" \
			>> "$EDM"
	fi
	echo "MERGE_RLOCK_FILETABLE_SIZE=$MERGE_RLOCK_FILETABLE_SIZE" \
		>> "$EDM"
	echo "MERGE_RLOCK_OPENTABLE_SIZE=$MERGE_RLOCK_OPENTABLE_SIZE" \
		>> "$EDM"
	echo "MERGE_RLOCK_LOCKTABLE_SIZE=$MERGE_RLOCK_LOCKTABLE_SIZE" \
		>> "$EDM"
	echo "MERGE_RLOCK_HASHTABLE_SIZE=$MERGE_RLOCK_HASHTABLE_SIZE" \
		>> "$EDM"
fi

SET_RLOCK_TABLES()
{
	# Check to see if we need to set any rlock table sizes

	PARAM=""

	grep "^MERGE_RLOCK_FILETABLE_SIZE" "$EDM" >"$TMPDIR"/rlock$$.tmp
	if [ $? -eq 0 ]
	then
		. "$TMPDIR"/rlock$$.tmp
		PARAM="filetable=$MERGE_RLOCK_FILETABLE_SIZE"
	fi

	grep "^MERGE_RLOCK_OPENTABLE_SIZE" "$EDM" >"$TMPDIR"/rlock$$.tmp
	if [ $? -eq 0 ]
	then
		. "$TMPDIR"/rlock$$.tmp
		PARAM="$PARAM opentable=$MERGE_RLOCK_OPENTABLE_SIZE"
	fi

	grep "^MERGE_RLOCK_LOCKTABLE_SIZE" "$EDM" >"$TMPDIR"/rlock$$.tmp
	if [ $? -eq 0 ]
	then
		. "$TMPDIR"/rlock$$.tmp
		PARAM="$PARAM locktable=$MERGE_RLOCK_LOCKTABLE_SIZE"
	fi

	grep "^MERGE_RLOCK_HASHTABLE_SIZE" "$EDM" >"$TMPDIR"/rlock$$.tmp
	if [ $? -eq 0 ]
	then
		. "$TMPDIR"/rlock$$.tmp
		PARAM="$PARAM hashtable=$MERGE_RLOCK_HASHTABLE_SIZE"
	fi

	rm -f "$TMPDIR"/rlock$$.tmp
	if [ "x$PARAM" != "x" ]
	then
		# First remove the shared memory segment if it exists,
		# and then recreate it with the current settings.
		"$STATICMERGE"/rlockshm -r >/dev/null 2>&1
		"$STATICMERGE"/rlockshm $PARAM
	fi
}
SET_RLOCK_TABLES

# There are other non-rlock settings that might need adjusting for to allow
# more files to be used simultaneously:
# - Up the linux file-max parameter (/proc/sys/fs/file-max)
# - Up the linux inode limit (2.2 Kernels only) (/proc/sys/fs/inode-max)

exit 0
