#! /bin/sh

# A simple script to launch an automated test run.
# Highly dependant on the local setup.
# $Id$

ab_names=/infres/shalmaneser/quinot/projects/droopi/cos/naming/ab_names
serverp=/infres/shalmaneser/quinot/projects/droopi/examples/bbs/serverp
serverp_conf=/infres/shalmaneser/quinot/projects/droopi/examples/bbs/serverp.conf
evolutedp=/infres/ir10/astre/quinot/polyorb-sparc/build/examples/bbs/evolutedp
evolutedp_conf=/infres/shalmaneser/quinot/projects/droopi/examples/bbs/evolutedp.conf
output=/infres/shalmaneser/quinot/polyorb-output

Revolutedp_pids=""

sol8_hosts=valjean:cosette:fadette:roland:isengrin:candide:fracasse:donjuan:chimene:elvire:gavroche:lantier:folcoche:quasimodo:
#cyrano:

sol9_hosts=nadja:blizzard:aurelien:goriot:esmeralda:esmeralda2:rodrigue:vautrin:arsene:javert:

hosts=$sol8_hosts

nhosts=`echo $hosts | tr -cd : | wc -c`
size=100
count=100
cli_per_host=1
nmax=$nhosts
ulimit -c unlimited
kill=false
broadcast=""

while getopts bks:c:n:N: opt; do
  case "$opt" in
    k) kill=true ;;
    b) broadcast="-b" ;;
    s) size=$OPTARG ;;
    c) count=$OPTARG ;;
    N) cli_per_host=$OPTARG ;;
    n) nmax=$OPTARG ;;
    *) echo "invalid parameter"; exit 1;;
  esac
done

if [ "$nmax" -gt `expr "$nhosts" '*' "$cli_per_host"` ]; then
  echo "Too many total clients ($nhosts hosts, $cli_per_host clients each".
  exit 1
fi

if $kill; then
  IFS=:
  for h in $hosts; do
    if [ "$h" != "" ]; then
      echo $h
      rsh $h "/usr/ucb/ps axuw|grep 'quinot.*$evolutedp' | grep -v grep \
         | awk '{print \$2}' | xargs kill -9 2> /dev/null || echo ---"
    fi
  done
  ps axw|grep "tail -f /tmp/cli"|awk '{print $1}'|xargs kill 2> /dev/null
  exit 0
fi

now=`date +"%F_%T" | tr -d :`
output=$output/$now
mkdir $output

(
  echo "output=$output"
  echo "size=$size"
  echo "count=$count"
  echo "nmax=$nmax"
  echo "cli_per_host=$cli_per_host"
  if [ "$brodcast" = "-b" ]; then
    echo "broadcast=true"
  else
    echo "broadcast=false"
  fi
) > $output/00PARAMETERS

echo "000 init"
sed "s/^/    /" $output/00PARAMETERS
cp $ab_names_conf $serverp_conf $evolutedp_conf $output 2> /dev/null || true
trap 'echo kill $ab_names_pid $serverp_pid $Revolutedp_pids;
           kill -9 $ab_names_pid $serverp_pid $Revolutedp_pids;
           exit' 0 2 15

echo "001 Starting name server..."

touch $output/ab_names.out
POLYORB_CONF=$ab_names_conf $ab_names > $output/ab_names.out 2> /dev/null &
ab_names_pid=$!
while ! grep POLYORB_CORBA_NAMING_IOR $output/ab_names.out; do
  sleep 5
done
. $output/ab_names.out
export POLYORB_CORBA_NAMING_IOR

echo "002 Starting server partition..."
POLYORB_CONF=$serverp_conf $serverp > $output/serverp.out 2>&1 &
serverp_pid=$!
sleep 5

clinumber=0
hostnumber=0
while [ "$hostnumber" -lt "$nhosts" -a "$clinumber" -lt "$nmax" ]; do
  eval `echo $hosts | sed 's/^\([^:]*\):\(.*\)$/host=\1;hosts=\2/'`
  ping -c 2 $host > /dev/null || continue

  cli_thishost=0
  while [ "$cli_thishost" -lt "$cli_per_host" -a "$clinumber" -lt "$nmax" ]; do
    ctl=/tmp/cli${clinumber}_ctl
    rm -f $ctl
    mkfifo $ctl
    cliout=$output/cli$clinumber-$host 
    touch $cliout
    cliouts="$cliouts $cliout"
    echo `expr 100 + $clinumber`" Starting client $clinumber on $host"
    sh -c "tail -f $ctl | rsh $host \"ulimit -c unlimited;TERM=dumb \
      POLYORB_CORBA_NAMING_IOR=$POLYORB_CORBA_NAMING_IOR \
      POLYORB_CONF=$evolutedp_conf \
      LD_LIBRARY_PATH=/usr/local/lib \
      $evolutedp $broadcast -n $nmax -c $count -s $size TEST_$clinumber 2>&1 \" \
        >> $cliout" &
    Revolutedp_pids="$Revolutedp_pids $!"
    eval "cli_host_$clinumber=$h"
    clinumber=`expr $clinumber + 1`
    cli_thishost=`expr $cli_thishost + 1`
  done
done

echo "200 Waiting for clients to become ready."
retries=0
while [ "`grep -L Ready $cliouts`" != "" -a "$retries" -lt 60 ]; do
  retries=`expr $retries + 1`
  sleep 2
done
if [ "$retries" -ge 60 ]; then
  echo "222 Timed out"
  $0 -k
  exit 1
fi

echo "201 GO!"
date
i=0
while [ $i -lt $clinumber ]; do
  echo "" > /tmp/cli${i}_ctl &
  i=`expr $i + 1`
done
date

echo "202 Waiting for clients to complete."
while true; do
  if [ "`grep -L 'Elapsed :' $cliouts`" = "" ]; then
     echo "555 Success."
     echo OK > $output/98STATUS
     break
  fi

  if egrep -i 'Raised|STORAGE|ASSERT' $cliouts > /dev/null; then
    echo "666 Exception."
    echo FAIL > $output/98STATUS
    break
  fi
  sleep 5
done
date
echo "888 Completed, cleaning up."
$0 -k
