#!/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="W01 W02 W03 W04 W05 W06 W07 W08 W09 W10 W11 W12 W13 W14 W15 W16 W17 W18 W19 W20 W21 W22 W23 W24 W25 W26 W27 W28 W29 W30 W31 W32 W33 W34 W35 W36 W37 W38 W39 W40 W41 W42 W43 W44 W45 W46 W47 W48 W49 W50 W51 W52 W53 W54 W55 W56 W57 W58 W59 W60 W61 W62 W63 W64"
for run in $runSets ; do

   # ===== Neue Hosts-Dateien für jeden Run!
   ./create-hosts-files
   ./install-binaries

   M1ID="messung-disaster2A-$run"
   M2ID="messung-disaster2B-$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"


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