#!/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


# runSets="U01 U02 U03 U04 U05 U06 U07 U08 U09 U10 U11 U12"
runSets="V00 V01 V03 V04 V05 V06 V07 V08 V09 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20"

for run in $runSets ; do

   M1ID="messung-disaster1A-$run"
   M2ID="messung-disaster1B-$run"
   PR="-loglevel=3 -distancestep=75"
   M1PE="-poolhandle=PoolOne -calcapp -rereginterval=3333 -policy=LeastUsedDPF:0.00001 -capmaxjobs=5 -loglevel=3"
   M2PE="-poolhandle=PoolTwo -calcapp -rereginterval=3333 -policy=LeastUsed -capmaxjobs=5 -loglevel=3"
   M1PU="-poolhandle=PoolOne -jobinterval=7.5 -jobsize=1000000 -loglevel=3"
   M2PU="-poolhandle=PoolTwo -jobinterval=7.5 -jobsize=1000000 -loglevel=3"


   killall ssh
   rm *.pid
   cat p?-*.hosts | xargs ./do-killall


   # ------ Registrars ------------------------------------------------------
   echo "`date` ========== $M1ID/$M2ID: Phase S1 - Starting PRs =========="
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start europe pr
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start america pr
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start asia pr
   sleep 60

   # ------ PEs -------------------------------------------------------------
   echo "`date` ========== $M1ID/$M2ID: Phase S2 - Starting PEs =========="
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start europe pe 5
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start america pe 5
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start asia pe 5

   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start europe pe 5
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start america pe 5
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start asia pe 5
   sleep 120

   # ------ PUs -------------------------------------------------------------
   echo "`date` ========== $M1ID/$M2ID: Phase S3 - Starting PUs =========="
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start europe pu 15
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start america pu 15
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start asia pu 15

   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start europe pu 15
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start america pu 15
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start asia pu 15

   # ========================================================================


   echo "`date` ========== $M1ID/$M2ID: Phase T1 - Normal operation =========="
   sleep 900

   echo "`date` ========== $M1ID/$M2ID: Phase T2 - Failure in Asia =========="
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop asia pe 2
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop asia pe 2
   sleep 900

   echo "`date` ========== $M1ID/$M2ID: Phase T3 - Activating backup capacity =========="
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start america pe 6
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start europe pe 6
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start america pe 6
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start europe pe 6
   sleep 900

   echo "`date` ========== $M1ID/$M2ID: Phase T4 - Recovered PEs in Asia =========="
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" start asia pe 5
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" start asia pe 5
   sleep 300

   echo "`date` ========== $M1ID/$M2ID: Phase T5 - Back to normal operation =========="
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop america pe 1
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop europe pe 1
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop america pe 1
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop europe pe 1
   sleep 900


   # ========================================================================

   # ------ Shutdown --------------------------------------------------------
   echo "`date` ========== $M1ID/$M2ID: Phase E1 - Stopping all components  =========="
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop europe pu
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop america pu
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop asia pu

   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop europe pu
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop america pu
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop asia pu
   sleep 10

   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop europe pe
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop america pe
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" stop asia pe

   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop europe pe
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop america pe
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop asia pe
   sleep 10

   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop europe pr
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop america pr
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" stop asia pr
   sleep 10

   # ------ Get results -----------------------------------------------------
   echo "`date` ========== $M1ID/$M2ID: Phase E2 - Fetching results  =========="
   rm -rf $M1ID/
   rm -rf $M2ID/
   ./three-regions-scenario "$M1ID" "$PR" "$M1PE" "$M1PU" results
   ./three-regions-scenario "$M2ID" "$PR" "$M2PE" "$M2PU" results

   echo "`date` ========== $M1ID/$M2ID: Completed! ============================"
   echo ""
   echo ""

done
