#!/usr/bin/env bash
# $Id$
#
# Thomas Dreibholz's PlanetLab Script Collection
# Copyright (C) 2005-2022 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@iem.uni-due.de


. ./planetlab-config

TESTID=fractal-scenario1

PR_AUSTRALIA=`grep "^[a-Z0-9]" pr-australia.hosts`
PR_ASIA=`grep "^[a-Z0-9]" pr-asia.hosts`
PR_AMERICA=`grep "^[a-Z0-9]" pr-america.hosts`
PR_EUROPE=`grep "^[a-Z0-9]" pr-europe.hosts`
PR_ALL="$PR_AUSTRALIA $PR_ASIA $PR_AMERICA $PR_EUROPE"
PR_MISCOPT="-enrpannounce=off -asapannounce=off -loglevel=3"

PE_AUSTRALIA=`grep "^[a-Z0-9]" pe-australia.hosts`
PE_ASIA=`grep "^[a-Z0-9]" pe-asia.hosts`
PE_AMERICA=`grep "^[a-Z0-9]" pe-america.hosts`
PE_EUROPE=`grep "^[a-Z0-9]" pe-europe.hosts`
PE_ALL="$PE_AUSTRALIA $PE_ASIA $PE_AMERICA $PE_EUROPE"
PE_MISCOPT="-asapannounce=off -fractal -fgpfailureafter=0 -rereginterval=10000 -policy=LeastUsed -loglevel=3 "


resetAll ()
{
   ./stop-processes Registrar
   ./stop-processes PoolElement
}

startNetwork ()
{
   echo "PRs=$localPRs"
   echo "PEs=$localPEs"

   optPeers=""
   for peer in $PR_ALL ; do
      optPeers="$optPeers -peer=$peer:9901"
   done
   number=1
   for pr in $localPRs ; do
      echo "Starting PR on $pr ..."
      ./start-process "Registrar-$number" "./rootshell ./registrar $PR_MISCOPT -asap=0.0.0.0:3863 -enrp=0.0.0.0:9901 $optPeers $OPT_CSP >$TESTID-pr$number-$pr.log 2>&1" $pr &
      let number=$number+1
   done

   optRegistrars=""
   for pr in $localPRs ; do
      optRegistrars="$optRegistrars -registrar=$pr:3863"
   done
   number=1
   for pe in $localPEs ; do
      echo "Starting PE on $pe ..."
      ./start-process "PoolElement-$number" "./rootshell ./server $PE_MISCOPT $optRegistrars -capvector=$TESTID-pe$number-$pe.vec -capscalar=$TESTID-pe$number-$pe.sca -capobject=PE-$number-$pe $OPT_CSP >$TESTID-pe$number-$pe.log 2>&1" $pe &
      let number=$number+1
   done
}

stopNetwork ()
{
   number=1
   for pe in $localPEs ; do
      ./stop-process "PoolElement-$number" $pe
      let number=$number+1
   done
   number=1
   for pr in $localPRs ; do
      ./stop-process "Registrar-$number" $pr
      let number=$number+1
   done
}



startNetworkAustralia ()
{
   localPRs=$PR_AUSTRALIA
   localPEs=$PE_AUSTRALIA
   localPUs=$PU_AUSTRALIA
   startNetwork
}

startNetworkAsia ()
{
   localPRs=$PR_ASIA
   localPEs=$PE_ASIA
   localPUs=$PU_ASIA
   startNetwork
}

startNetworkAmerica ()
{
   localPRs=$PR_AMERICA
   localPEs=$PE_AMERICA
   localPUs=$PU_AMERICA
   startNetwork
}

startNetworkEurope ()
{
   localPRs=$PR_EUROPE
   localPEs=$PE_EUROPE
   localPUs=$PU_EUROPE
   startNetwork
}


stopNetworkAustralia ()
{
   localPRs=$PR_AUSTRALIA
   localPEs=$PE_AUSTRALIA
   localPUs=$PU_AUSTRALIA
   stopNetwork
}

stopNetworkAsia ()
{
   localPRs=$PR_ASIA
   localPEs=$PE_ASIA
   localPUs=$PU_ASIA
   stopNetwork
}

stopNetworkAmerica ()
{
   localPRs=$PR_AMERICA
   localPEs=$PE_AMERICA
   localPUs=$PU_AMERICA
   stopNetwork
}

stopNetworkEurope ()
{
   localPRs=$PR_EUROPE
   localPEs=$PE_EUROPE
   localPUs=$PU_EUROPE
   stopNetwork
}


if [ $# -lt 1 ] ; then
   echo "Usage: fractal-scenario1 [start|stop|reset] {all|europe|australia|asia|america}"
fi

if [ "x$1" = "xstart" ] ; then
   if [ $# -eq 2 -a "x$2" = "xaustralia" ] ; then
      echo "Starting components in Australia"
      startNetworkAustralia
   elif [ $# -eq 2 -a "x$2" = "xasia" ] ; then
      echo "Starting components in Asia"
      startNetworkAsia
   elif [ $# -eq 2 -a "x$2" = "xeurope" ] ; then
      echo "Starting components in Europe"
      startNetworkEurope
   elif [ $# -eq 2 -a "x$2" = "xamerica" ] ; then
      echo "Starting components in America"
      startNetworkAmerica
   elif [ $# -eq 2 -a "x$2" = "xall" ] ; then
      echo "Starting components worldwide"
      startNetworkAustralia
      startNetworkAsia
      startNetworkAmerica
      startNetworkEurope
   else
      echo "Usage: fractal-scenario1 start [all|europe|australia|asia|america]"
   fi
elif [ "x$1" = "xstop" ] ; then
   if [ $# -eq 2 -a "x$2" = "xaustralia" ] ; then
      echo "Stopping components in Australia"
      stopNetworkAustralia
   elif [ $# -eq 2 -a "x$2" = "xasia" ] ; then
      echo "Stopping components in Asia"
      stopNetworkAsia
   elif [ $# -eq 2 -a "x$2" = "xeurope" ] ; then
      echo "Stopping components in Europe"
      stopNetworkEurope
   elif [ $# -eq 2 -a "x$2" = "xamerica" ] ; then
      echo "Stopping components in America"
      stopNetworkAmerica
   elif [ $# -eq 2 -a "x$2" = "xall" ] ; then
      echo "Stopping components worldwide"
      stopNetworkAustralia
      stopNetworkAsia
      stopNetworkAmerica
      stopNetworkEurope
   else
      echo "Usage: fractal-scenario1 stop [all|europe|australia|asia|america]"
   fi
elif [ "x$1" = "xreset" ] ; then
   echo "Resetting everything!"
   resetAll
elif [ "x$1" = "xuser" ] ; then
   optRegistrars=""
   for pr in $PR_EUROPE ; do
      optRegistrars="$optRegistrars -registrar=$pr:3863"
   done
   shift
   cmd="../fractalpooluser $optRegistrars -loglevel=1 $PU_MISCOPT $OPT_CSP -configdir=../fgpconfig $@"
   echo "# $cmd"
   $cmd
else
   echo "ERROR: Bad parameter!"
fi
