#!/usr/bin/env bash
# $Id$
#
# Thomas Dreibholz's PlanetLab Script Collection
# Copyright (C) 2005-2023 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: thomas.dreibholz@gmail.com

. ./planetlab-config


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_MISCOPT="-asapannounce=off -enrpannounce=off"

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_MISCOPT="-asapannounce=off"

PU_AUSTRALIA=`grep "^[a-Z0-9]" pu-australia.hosts`
PU_ASIA=`grep "^[a-Z0-9]" pu-asia.hosts`
PU_AMERICA=`grep "^[a-Z0-9]" pu-america.hosts`
PU_EUROPE=`grep "^[a-Z0-9]" pu-europe.hosts`
PU_MISCOPT="-asapannounce=off"

MULTIPLIKATOR=1


# ---------------------------
puAustralia=$PU_AUSTRALIA
puAsia=$PU_ASIA
puAmerica=$PU_AMERICA
puEurope=$PU_EUROPE
peAustralia=$PE_AUSTRALIA
peAsia=$PE_ASIA
peAmerica=$PE_AMERICA
peEurope=$PE_EUROPE
i=1 ; while [ $i -lt $MULTIPLIKATOR ] ; do
   PU_AUSTRALIA="$PU_AUSTRALIA $puAustralia"
   PU_ASIA="$PU_ASIA $puAsia"
   PU_AMERICA="$PU_AMERICA $puAmerica"
   PU_EUROPE="$PU_EUROPE $puEurope"
   PE_AUSTRALIA="$PE_AUSTRALIA $peAustralia"
   PE_ASIA="$PE_ASIA $peAsia"
   PE_AMERICA="$PE_AMERICA $peAmerica"
   PE_EUROPE="$PE_EUROPE $peEurope"
   let i=$i+1
done
# ---------------------------


PR_ALL="$PR_ASIA $PR_AMERICA $PR_EUROPE" # $PR_AUSTRALIA
PE_ALL="$PE_ASIA $PE_AMERICA $PE_EUROPE" # $PE_AUSTRALIA
PU_ALL="$PU_ASIA $PU_AMERICA $PU_EUROPE" # $PU_AUSTRALIA



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


startNetwork ()
{
   # ------ Start PRs -------------------------------------------------------
   optPeers=""
   for peer in $PR_ALL ; do
      optPeers="$optPeers -peer=$peer:9901"
   done
   number=1
   for pr in $localPRs ; do
      if [ ! $number -gt $localPRLimit ] ; then
         echo "Starting PR on $pr ..."
         ./start-process "Registrar-$TESTID-$number" "./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
#       else
#          echo "Skipping PR $pr!"
      fi
   done

   # ------ Options for static PRs ------------------------------------------
   optRegistrars=""
   for pr in $localPRs ; do
      optRegistrars="$optRegistrars -registrar=$pr:3863"
   done

   # ------ Start PEs -------------------------------------------------------
   if [ ! "x$localPEs" = "x" ]; then
      number=1
      for pe in $localPEs ; do
         if [ ! $number -gt $localPELimit ] ; then
            echo "Starting PE on $pe ..."
            ./start-process "PoolElement-$TESTID-$number" "./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
#          else
#             echo "Skipping PE $pe!"
         fi
      done

      if [ ! "x$localPUs" = "x" ]; then
         sleep 30
      fi
   fi

   # ------ Start PUs -------------------------------------------------------
   if [ ! "x$localPUs" = "x" ]; then
      number=1
      for pu in $localPUs ; do
         if [ ! $number -gt $localPULimit ] ; then
            echo "Starting PU on $pu ..."
            ./start-process "PoolUser-$TESTID-$number" "./calcappclient $PU_MISCOPT $optRegistrars -vector=$TESTID-pu$number-$pu.vec -scalar=$TESTID-pu$number-$pu.sca -object=PU-$number-$pu $OPT_CSP >$TESTID-pu$number-$pu.log 2>&1" $pu &
            let number=$number+1
#          else
#             echo "Skipping PU $pu!"
         fi
      done
   fi
}

stopNetwork ()
{
   # ------ Stop PUs --------------------------------------------------------
   number=1
   for pu in $localPUs ; do
      if [ ! $number -gt $localPULimit ] ; then
         ./stop-process "PoolUser-$TESTID-$number" $pu
         let number=$number+1
#       else
#          echo "Skipping PU $pu!"
      fi
   done

   # ------ Stop PEs --------------------------------------------------------
   number=1
   for pe in $localPEs ; do
      if [ ! $number -gt $localPELimit ] ; then
         ./stop-process "PoolElement-$TESTID-$number" $pe
         let number=$number+1
#       else
#          echo "Skipping PE $pe!"
      fi
   done

   # ------ Stop PUs --------------------------------------------------------
   number=1
   for pr in $localPRs ; do
      if [ ! $number -gt $localPRLimit ] ; then
         ./stop-process "Registrar-$TESTID-$number" $pr
         let number=$number+1
#       else
#          echo "Skipping PR $pr!"
      fi
   done
}


action ()
{
   localPRLimit=999999999
   localPELimit=999999999
   localPULimit=999999999
   if [ "x$2" = "xaustralia" ] ; then
      # echo "Components in Australia"
      localPRs=$PR_AUSTRALIA
      localPEs=$PE_AUSTRALIA
      localPUs=$PU_AUSTRALIA
   elif [ "x$2" = "xasia" ] ; then
      # echo "Components in Asia"
      localPRs=$PR_ASIA
      localPEs=$PE_ASIA
      localPUs=$PU_ASIA
   elif [ "x$2" = "xeurope" ] ; then
      # echo "Components in Europe"
      localPRs=$PR_EUROPE
      localPEs=$PE_EUROPE
      localPUs=$PU_EUROPE
   elif [ "x$2" = "xamerica" ] ; then
      # echo "Components in America"
      localPRs=$PR_AMERICA
      localPEs=$PE_AMERICA
      localPUs=$PU_AMERICA
   elif [ "x$2" = "xall" ] ; then
      # echo "Components in all regions"
      action $1 asia $3 $4 $5 $6
      action $1 america $3 $4 $5 $6
      action $1 europe $3 $4 $5 $6
      return
   else
      echo "ERROR: Invalid region specified!"
      exit
   fi

   if [ "x$3" = "xpe" ] ; then
      # echo "- PEs only"
      localPUs=
      localPRLimit=0
      if [ "x$4" != "x" ] ; then
         localPELimit=$4
         # echo "- PE limit is $localPELimit"
      fi
   elif [ "x$3" = "xpu" ] ; then
      # echo "- PUs only"
      localPEs=
      localPRLimit=0
      if [ ! "x$4" = "x" ] ; then
         localPULimit=$4
         # echo "- PU limit is $localPULimit"
      fi
   elif [ "x$3" = "xpr" ] ; then
      # echo "- PRs only"
      localPEs=
      localPUs=
   else
      # echo "- PRs, PEs and PUs"
      if [ ! "x$4" = "x" ] ; then
         localPRLimit=$4
         # echo "- PR limit is $localPRLimit"
      fi
      if [ ! "x$5" = "x" ] ; then
         localPELimit=$5
         # echo "- PE limit is $localPELimit"
      fi
      if [ ! "x$6" = "x" ] ; then
         localPULimit=$6
         # echo "- PU limit is $localPULimit"
      fi
   fi

   if [ "x$1" = "xstart" ] ; then
      # echo "=> Start"
      startNetwork
   elif [ "x$1" = "xstop" ] ; then
      # echo "=> Stop"
      stopNetwork
   fi
}



if [ $# -lt 5 ] ; then
   echo "Usage: test5 [Test ID] [PR Parameters] [PE Parameters] [PU Parameters] [start|stop|reset] {all|europe|australia|asia|america} {pr|pu|pe|all} {amount limit|{PRs PEs PUs}}"
fi

TESTID=$1
PR_MISCOPT="$PR_MISCOPT $2"
PE_MISCOPT="$PE_MISCOPT $3"
PU_MISCOPT="$PU_MISCOPT $4"

if [ "x$5" = "xstart" -o "x$5" = "xstop" ] ; then
   cmd=$5
   region="$6"
   component="$7"
   climit=""
   if [ $# -ge 8 ] ; then
      climit=$8
   fi
   a1=""
   if [ $# -ge 9 ] ; then
      a1=$9
   fi
   a2=""
   if [ $# -ge 9 ] ; then
      a2=$10
   fi
   action $cmd $region $component $climit $a1 $a2
elif [ "x$5" = "xreset" ] ; then
   echo "Resetting everything!"
   resetAll
elif [ "x$5" = "xresults" ] ; then
   echo "Fetching results!"
   echo $PU_ALL $PE_ALL $PR_ALL | sort -u | xargs ./get-results $TESTID
   ./combine-vectors $TESTID
   tar czf $TESTID.tar.gz $TESTID p?-*.hosts
else
   echo "ERROR: Bad parameter!"
fi
