#!/bin/sh
############################################################################
#                                                                          #
# The contents of this file are subject to the WebStone Public License     #
# Version 1.0 (the "License"); you may not use this file except in         #
# compliance with the License. You may obtain a copy of the License        #
# at http://www.mindcraft.com/webstone/license10.html                      #
#                                                                          #
# Software distributed under the License is distributed on an "AS IS"      #
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See      #
# the License for the specific language governing rights and limitations   #
# under the License.                                                       #
#                                                                          #
# The Original Code is WebStone 2.5.                                       #
#                                                                          #
# The Initial Developer of the Original Code is Silicon Graphics, Inc.     #
# and Mindcraft, Inc.. Portions created by Silicon Graphics. and           #
# Mindcraft. are Copyright (C) 1995#1998 Silicon Graphics, Inc. and        #
# Mindcraft, Inc. All Rights Reserved.                                     #
#                                                                          #
# Contributor(s): ______________________________________.                  #
#                                                                          #
############################################################################

[ -n "$WEBSTONEROOT" ] || WEBSTONEROOT=`pwd`/../

# load configurations
. $WEBSTONEROOT/conf/testbed

# leaving the DEBUG variable unset or setting it to 0 turns off debugging
[ "$DEBUG" = '0' ] && DEBUG=''
[ -n "$DEBUG" ] && set -x

# check variables

[ -n "$FILELIST" ] || FILELIST="$WEBSTONEROOT/conf/filelist"
case $# in
	1)
		FILELIST=$1
		;;
esac
[ -n "$CLIENTOS" ] || CLIENTOS="UNIX"
[ -n "$ITERATIONS" ] || ITERATIONS="1"
[ -n "$MINCLIENTS" ] || MINCLIENTS="1"
[ -n "$MAXCLIENTS" ] || MAXCLIENTS="1"
[ -n "$CLIENTINCR" ] || CLIENTINCR="1"
[ -n "$TIMEPERRUN" ] || TIMEPERRUN="1"
# don't care about PROXYSERVER
[ -n "$SERVER" ] || SERVER="www"
[ -n "$HTTP_PORT" ] || HTTP_PORT="80"
[ -n "$HTTPS_PORT" ] || HTTPS_PORT="443"
# SERVERINFO
# OSTUNINGFILES
# WEBSERVERDIR
# WEBDOCDIR
# WEBSERVERTUNINGFILES
[ -n "$CLIENTS" ] || CLIENTS="localhost"
[ -n "$CLIENTACCOUNT" ] || CLIENTACCOUNT=$USER
[ -n "$CLIENTPASSWORD" ] || CLIENTPASSWORD=$CLIENTACCOUNT
# [ -n "$CLIENTINFO" ] || CLIENTINFO="uname -a"
# RCP
# RSH
if [ -z "$CLIENTPROGFILE" ]
then
	echo "CLIENTPROGFILE is not set in conf/testbed.  Exiting." 1>&2
	exit 1
fi
case $CLIENTOS in
# To send a backslash to webmaster, use two backslashes here
WIN32)	[ -n "$DEBUGFILE" ] || DEBUGFILE="c:\\webstone2.5\\debug"
		[ -n "$TMPDIR" ] || TMPDIR="c:\\webstone2.5"
		;;
*)	[ -n "$DEBUGFILE" ] || DEBUGFILE="/tmp/webstone-debug"
		[ -n "$TMPDIR" ] || TMPDIR="/tmp"
		;;
esac

export ITERATIONS MINCLIENTS MAXCLIENTS CLIENTINCR TIMEPERRUN 
export SERVER HTTP_PORT HTTPS_PORT SERVERINFO DEBUGFILE CLIENTPROGFILE
export CLIENTS CLIENTACCOUNT CLIENTPASSWORD CLIENTINFO
export TMPDIR RCP RSH

echo "Clients: " $CLIENTS

#
# Estimate run time
#
NUMCLIENTHOSTS=`echo $CLIENTS | wc -w`
TRIALS=`expr 1 + \( $MAXCLIENTS - $MINCLIENTS \) / $CLIENTINCR`
RUNTIME=`expr 60 \* $ITERATIONS \* $TRIALS \* $TIMEPERRUN`
RUNTIME=`expr $RUNTIME + $TRIALS \* \( $MAXCLIENTS + $MINCLIENTS \) \
 / \( 2 \* $NUMCLIENTHOSTS \)`

echo "Estimated run time:" `expr $RUNTIME / 3600` "hours" \
 `expr $RUNTIME % 3600 / 60` "minutes" 

#
# Checking for valid set of benchmark files to retrieve
#
#if [ -z "$PROXYSERVER" ]
#then
#  echo "Checking for a valid set of benchmark files"
#  CHECKLISTTMP=$TMPDIR/webstone-checkfilelist.$$
#  rm -f $CHECKFILELISTTMP
#  $WEBSTONEROOT/bin/checkfilelist $SERVER $PORTNO $FILELIST > $CHECKLISTTMP 2>&1
#  if [ `grep -c ' 4[0-9][0-9] '< $CHECKLISTTMP` -gt 0 ]
#  then
#    echo "ERROR: not all files in filelist are on server:"
#    echo "----- checkfilelist results -----"
#    cat $CHECKLISTTMP
#    echo "----- checkfilelist results -----"
#    rm  -f $CHECKLISTTMP
#    exit 1
#  else
#    echo "OK: All files found"
#  fi
#  rm -f $CHECKLISTTMP
#fi
# end if $PROXYSERVER

#
# distribute webclient binary
#
if [ -n "$RCP" ] && [ "$CLIENTOS" != "WIN32" ]
then
	for i in $CLIENTS
	do
		$RCP $WEBSTONEROOT/bin/webclient $i:$CLIENTPROGFILE
	done
fi

# BEGIN iterations
J=1
while [ $J -le $ITERATIONS ]
do
	NUMCLIENTS=$MINCLIENTS
	while [ $NUMCLIENTS -le $MAXCLIENTS ]
	do
		echo "***** Iteration $J, Total clients "`expr $NUMCLIENTS`" **********"
		date
		# bodge to keep date format safe from SCCS
		TIMESTAMP=`date +"%y%m%d_%H"`
		TIMESTAMP=$TIMESTAMP`date +"%M"`
		LOGDIR=$WEBSTONEROOT/bin/runs/$TIMESTAMP
		
		#
		# nuke debug files
		#
		if [ -n "$RSH" ] && [ "$CLIENTOS" != "WIN32" ]
		then
			for client in $CLIENTS
			do
				$RSH $client "rm ${DEBUGFILE}*" > /dev/null 2>&1
			done
		fi

		mkdir -p $LOGDIR
		rm -f $LOGDIR/config
		touch $LOGDIR/config
		CLIENTSPERHOST=`expr $NUMCLIENTS / $NUMCLIENTHOSTS`
		EXTRACLIENTS=`expr $NUMCLIENTS % $NUMCLIENTHOSTS`
		
		for i in $CLIENTS
		do
			# Special case to support clients on multiple networks:
            #  - The server's host number must be the same on each net
			#  - Set SERVER to the server's host number
			#  - Give the CLIENT IDs as decimal Internet numbers
			case "$SERVER" in
			[0-9]|[0-9][0-9]|[12][0-9][0-9])
				CLIENTNET=`expr $i \: "\(.*\)\..*"`
				SERVERNAME=${CLIENTNET}.${SERVER}
				;;
			*)
				SERVERNAME=${SERVER}
				;;
			esac
			if [ $EXTRACLIENTS -gt 0 ]
			then
				echo "$i $CLIENTACCOUNT $CLIENTPASSWORD \
					`expr $CLIENTSPERHOST + 1` \
					${SERVERNAME}" >> $LOGDIR/config
				EXTRACLIENTS=`expr $EXTRACLIENTS - 1`
			else
				echo "$i $CLIENTACCOUNT $CLIENTPASSWORD $CLIENTSPERHOST \
					${SERVERNAME}" >> $LOGDIR/config
			fi
		done
		cp $FILELIST $LOGDIR/`basename $FILELIST`

		if [ -n "$RCP" ]
		then
			for i in "$OSTUNINGFILES" "$WEBSERVERTUNINGFILES"
			do
				if [ -n "$i" ]
				then
						$RCP $SERVERNAME:$i $LOGDIR
				fi
			done
		fi

		if [ -n "$RSH" ] && [ "$CLIENTOS" != "WIN32" ]
		then
			for i in $CLIENTS
			do
				$RSH $i "$CLIENTINFO" > $LOGDIR/hardware.$i 2>&1
			done
		fi
		
		#
		# Run benchmark
		#
		date
		CMD="$WEBSTONEROOT/bin/webmaster -v -W -C $CLIENTPROGFILE"
		CMD="$CMD -f $LOGDIR/config -t $TIMEPERRUN"

		# SSL stuff (ngm)
		[ -n "$SSL_VERSION" ] && CMD=$CMD" -V $SSL_VERSION"
		[ -n "$SSL_CIPHER" ] && CMD=$CMD" -c $SSL_CIPHER"
		[ -n "$SSL_CACHE_MODE" ] && CMD=$CMD" -m $SSL_CACHE_MODE"
		[ -n "$SSL_MIX" ] && CMD=$CMD" -x $SSL_MIX"
		[ -n "$SSL_HANDSHAKES" ] && CMD=$CMD" -r $SSL_HANDSHAKES"

		[ -n "$CLIENTFILELIST" ] && FILELIST="" && CMD=$CMD" -U $CLIENTFILELIST"
		[ -n "$FILELIST" ] && CMD=$CMD" -u $FILELIST"
		[ -n "$HTTP_PORT" ] && CMD=$CMD" -p $HTTP_PORT"
		[ -n "$HTTPS_PORT" ] && CMD=$CMD" -y $HTTPS_PORT"
		[ -n "$PROXYSERVER" ] && CMD=$CMD" -P $PROXYSERVER"
		[ -n "$DEBUG" ] && CMD=$CMD" -d -D $DEBUGFILE"
		[ "$FIXED_RANDOM_SEED" = "true" ] && CMD="$CMD -S"

		echo $CMD

		# dump environment into $LOGDIR
		rm -rf $LOGDIR/controller.env
		env > $LOGDIR/controller.env
		
		eval `echo $CMD` | tee $LOGDIR/run
		
		#
		# Get ending configuration and stats from each participant
		#
		date
		#for i in $SERVER $CLIENTS
		#do
		#$WEBSTONEROOT/bin/getstats $i > $LOGDIR/end.$i 2>&1
		#done
		#date
		NUMCLIENTS=`expr $NUMCLIENTS + $CLIENTINCR`
	done
	# while NUMCLIENTS
	J=`expr $J + 1`
done
# while J
# end
