#!/bin/bash
#
# $1 is name of profile which must exist in the $AUTODIR/sources/ffsbnew directory
# $2 ... are any key=value pairs of valid profile entries or numdevs=n wher n is
#	the number of devices to rn on
# Other key assumption is that FS(s) to benchmark are already mounted on /mnt/tmp1 ../mnt/tmpn

. /etc/autobench.conf || . functions

# Process the arguments ######################################################
if [ $# -lt 1 ]; then
  echo "You must specify the profile to run "
  exit 1
fi

# Install support files ######################################################
if [ ! \( -d $AUTODIR/sources/ffsbnew -a -x $AUTODIR/sources/ffsbnew/ffsb \) ];
then
  $AUTODIR/scripts/benchmarks/install/ffsbnew || exit 2
fi

getcommand doprofilers
doprofilers install

pushd $AUTODIR/sources/ffsbnew


numfs=1
unset profile
unset location

i=0
for arg in $@
do
   	echo "arg $i is $arg"
	equal=$(expr index $arg "=")
	if [ "$equal" -eq "0" ] ; then # this arg should be the profile
		if [ ! -s "$arg" ]; then
	    		wget -O $arg http://ausgsa.austin.ibm.com/projects/l/ltcperformance/benchmarks/ffsb/resources/profiles/$arg
			if [ ! -s "$arg" ]; then
			    echo "Error retrieving profile, aborting"
			    popd
			    exit 1;
			fi
		fi
		
		let i=i+1
		profile=$arg
		#sed -e "s/location=.*/location=\/mnt\/tmp${i}/" $base > $LOGDIR/benchmark/profile${i}
		cp $profile $LOGDIR/benchmark/profile
		continue
	elif [ "$equal" != "0" ]; then #if there is an "=" in the arg
		key=${arg%%=*}              #strip off everything after the =
		echo debug key is $key

		#make sure profile has been set
		if [ -z "$profile" ]; then
		    log "Error profile must be the first argument to ffsbnew"
		    popd
		    exit 1;
		fi


		if [ $key == "numfs" ]; then 
	  		numfs_requested=${arg##*=}
			echo numfs_requested is $numfs_requested
			
			# if I detect more than one "location=" in the file, abort
			NUMLOC=$(grep "location=" $LOGDIR/benchmark/profile | wc -l)
			if [ $NUMLOC -gt 1  ] ; then 
			    log "more than one filesystem clause detected in profile, aborting"
			    popd
			    exit 1
			fi

			numfs=$numfs_requested
			
			# change the num_filesystems= parameter
			perl -p -i -e "s/num_filesystems=.*/num_filesystems=${numfs}/" $LOGDIR/benchmark/profile
			
			# make sure that the first location clause is "/mnt/tmp0"
			perl -p -i -e "s/location=.*/location=\/mnt\/tmp0/" $LOGDIR/benchmark/profile
			
			# this is going to be a major hackjob because we have to insert the new filesystem
			# clauses inbetween the filesystem0 clause and the first threadgroup :-)
			
			unset FS_STRING
			# first build up a temp file with all the new fs clauses, then slice up the profile and rebuild it
			rm -f $LOGDIR/benchmark/tmpfile
			for (( j=1 ; j< $numfs ; j++))
			do	
			        echo "[filesystem${j}]" >> $LOGDIR/benchmark/tmpfile
				echo "        location=/mnt/tmp${j}" >> $LOGDIR/benchmark/tmpfile
				echo "[end${j}" >> $LOGDIR/benchmark/tmpfile
				echo "" >> $LOGDIR/benchmark/tmpfile
			done

			#cut out everything before the end of of filesystem0 into "top"
			rm -f $LOGDIR/benchmark/top
			grep -m 1 -B 100 'end0' $LOGDIR/benchmark/profile >> $LOGDIR/benchmark/top

			#cut out everything below threadgroup0 into "bottom"
			rm -f $LOGDIR/benchmark/bottom
			grep -A 100 'threadgroup0' $LOGDIR/benchmark/profile >> $LOGDIR/benchmark/bottom
			
			cat $LOGDIR/benchmark/top > $LOGDIR/benchmark/profile
			cat $LOGDIR/benchmark/tmpfile >> $LOGDIR/benchmark/profile
			cat $LOGDIR/benchmark/bottom >> $LOGDIR/benchmark/profile
			
			rm -f $LOGDIR/benchmark/top $LOGDIR/benchmark/tmpfile $LOGDIR/benchmark/bottom
			
			continue
		fi

		#echo debug sed args $key $arg $profile
		# do in-place editing of the profile 
		perl -p -i -e "s/${key}=.*/${arg}/" $LOGDIR/benchmark/profile 

	fi
done 

if [ $numfs -gt 1 ]; then 
    for ((j=0; j< $numfs; j++))
      do
      mp="/mnt/tmp${j}"
      fst=`mount | grep $mp`
      fst=${fst##*type}
      echo $fst
      fst=${fst%%(*}
      echo fstype is $fst
      echo "fstype for /mnt/tmp${j} is $fst" >> $LOGDIR/benchmark/fstype.$RUN_NUMBER
      if [ "$fst" == "" ]; then
		log "No filesystem mounted to run test on, aborting!"
		exit
      fi
    done 
fi

watchfor "Starting Actual Benchmark" startprofilers
watchfor "Results" 'stopprofilers; doprofilers report ; doprofilers postprocess'

$AUTODIR/sources/ffsbnew/ffsb $LOGDIR/benchmark/profile | tee $LOGDIR/benchmark/ffsbout.$RUN_NUMBER-1 #&
popd
