#!/bin/sh
#
# Support:      linux-ha-dev@lists.tummy.com
# License:      GNU General Public License (GPL)
#
# The purpose of this script is to break or fix the communication in the cluster.
#
HADIR=/etc/ha.d
HBSCRIPT=/etc/ha.d/init.d/heartbeat.sh

LIBDIR=/usr/local/lib/
HBLIB=$LIBDIR/heartbeat
TESTFILE=OnlyForTesting

USAGE="Usage: 'TestHeartbeatComm break-communication allow-nodes-list|reduce-communication  xmit_loss recv_loss allow-nodes-list|fix-communication|delete-testingfile|save-testingfile testingfile|restore-communication testingfile'"

if 
  [ $# -lt 1 ]
then
  echo "$USAGE";
  exit 1;
fi

cd /etc/ha.d

# Create OnlyForTesting File. It is invoked by ParseTestOpts() in heartbeat.c

GenerateTestingFile(){
save_IFS=$IFS
IFS=';'
  cat <<EOF >$TESTFILE
xmitloss=$1
rcvloss=$2
EOF
shift
shift
  cat <<EOF >>$TESTFILE
allownodes=$*;
EOF

IFS=$save_IFS
}

DeleteTestingFile(){
  rm -f $TESTFILE
  echo "DeleteTestFileOK"
}

RestoreTestingFile(){
  if [ -f $1 ]
  then 
      cp $1 $TESTFILE
      exit $?
  fi
}
SaveTestingFile(){
  cp $TESTFILE $1 
  exit $?
}

HBReload(){
  $HBSCRIPT reload
}


OPT=$1
case "$OPT" in
  break-communication)
	shift
	GenerateTestingFile 1 1 $@
	HBReload
	;;
  reduce-communication)
	shift
	GenerateTestingFile $@
	HBReload
	;;
  fix-communication)
	DeleteTestingFile
	HBReload
	;;
  delete-testingfile)
	DeleteTestingFile
	;;
  save-testingfile)
	shift
	SaveTestingFile $1
	;;
  restore-communication)
	shift
	RestoreTestingFile $1
	HBReload
	;;
  *)
	echo "$USAGE"
	;;
esac

exit $?
