#!/bin/sh 

#set -x


#sleep 2;

#myip=`/sbin/ifconfig | sed -n 's/inet addr:192\.168\.2\.\([0-9][0-9]*\).*/\1/p'`

myhost=`/bin/hostname -f | sed 's/.*\(0[0-9]\+\).*/\1/'`

if [ $myhost == "master.g4cluster.prv" ]; then
  myhost="01"
fi

echo Myhost =  $myhost

tmphost=`expr $myhost + 1`
if [ $tmphost -le 9 ]; then
  tmphost=0$tmphost
fi
nexthost=slave0$tmphost.g4cluster.prv

echo Myhost: $myhost
echo Nexthost: $nexthost

#myip=`expr $myip + 1`

while (! scp ./copyme.sh $nexthost:); do
  tmphost=`expr $tmphost + 1`
  if [ $tmphost -le 9 ]; then
    tmphost=0$tmphost
  fi
  nexthost=slave0$tmphost.g4cluster.prv
#  myip=`expr $myip + 1`
  echo Nexthost to try: $nexthost
  if [ $tmphost -ge 70 ]; then
    tmphost=2
    nexthost=slave$tmphost.g4cluster.prv
  fi
done
# file is now successfully copied to remove host

# execute file on remote host
#ssh 192.168.2.$myip "(chmod 755 ./copyme.sh; ./copyme.sh) &>/dev/null &"
ssh $nexthost "(chmod 755 ./copyme.sh; ./copyme.sh) &>/dev/null &"

# play a sound.
cat /root/Bart*au > /dev/audio
