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

MAX_GOOD=999
PUs=15
PEs=6
PRs=1

GET_ALL_MAX_PARALLEL=64


get-hosts ()
{
   GH_OUTPUT=""
   i=0 ; while [ $i -lt $GH_AMOUNT ] ; do
      j=0;
      for host in $goodhosts ; do
         if [ $j -lt $MAX_GOOD ] ; then
            GH_OUTPUT="$GH_OUTPUT $host"
            let i=$i+1
            if [ $i -ge $GH_AMOUNT ]; then
               return
            fi
         fi
         let j=$j+1
      done
   done
}

test-hosts ()
{
   hosts=`echo $IN_HOSTS | xargs -n1 echo | sort -u`
   goodhosts=`echo $hosts | xargs -x ../exectime "./filter-hosts >/dev/null"`

   if [ "x$goodhosts" = "x" ] ; then
      echo "WARNING: No good hosts found!"
      rm -f pu-$HOSTSFILEMIDDLE.hosts
      rm -f pe-$HOSTSFILEMIDDLE.hosts
      rm -f pr-$HOSTSFILEMIDDLE.hosts
   else
      GH_GOODHOSTS=$goodhosts
      GH_AMOUNT=$PUs
      get-hosts
      OUT_PUs=$GH_OUTPUT

      GH_AMOUNT=$PEs
      get-hosts
      OUT_PEs=$GH_OUTPUT

      GH_AMOUNT=$PRs
      get-hosts
      OUT_PRs=$GH_OUTPUT

      echo "PRs: $OUT_PRs"
      echo "PEs: $OUT_PEs"
      echo "PUs: $OUT_PUs"
      write-hosts
   fi
}

write-hosts ()
{
   ( echo $OUT_PUs | xargs -n1 ) >pu-$HOSTSFILEMIDDLE.hosts
   ( echo $OUT_PEs | xargs -n1 ) >pe-$HOSTSFILEMIDDLE.hosts
   ( echo $OUT_PRs | xargs -n1 ) >pr-$HOSTSFILEMIDDLE.hosts
}

get-all-good-hosts ()
{
   echo "# Hosts list created on `date`" >$OUT_HOSTS
   hosts=`echo $IN_HOSTS | xargs -n1 echo | sort -u`
   count=0
   for host in $hosts ; do
      echo "Trying $host ..."
      ./filter-hosts -timeout $SERVER_TEST_TIMEOUT $host >try-$host.tmp &

      let count=$count+1
      if [ $count -ge $GET_ALL_MAX_PARALLEL ] ; then
         echo "Waiting for results ..."
         wait
         count=0
      fi
   done
   echo "Waiting for results ..."
   wait

   for host in $hosts ; do
      result=`cat try-$host.tmp`
      if [ "x$result" != "x" ] ; then
         echo $result >>$OUT_HOSTS
      fi
      rm -f try-$host.tmp
   done
}



# # Settings for "location":
# NorthAmerica=1
# Europe=2
# Australasia=3
# SouthAmerica=4
#
# UptimeOkay="(uptime >= 72000)"
#
# DNSWorking="(dns1udp<80 || dns2udp<80)"
# DNSFine="(dns1udp<20 && dns2udp<80)"
#
# DiskOkay="(gbfree>=5)"
# MemOkay="((freemem >= 100)  && (swapused <= 10))"
#
# TimeOkay="(drift <= 1000ms)"
# TimeFine="(drift <= 1ms)"
#
# LoadOkay="(5minload < 5)"
# LoadFine="(5minload < 1)"
#
#
# AliveHost="((resptime > 0) && (sshstatus < 1h) && $DNSWorking)"
# GoodHost="($UptimeOkay && $AliveHost && $DiskOkay && $MemOkay && $LoadOkay)"
# GreatHost="($GoodHost && $DNSFine && $LoadFine)"
#
# CandidateForPU="($GoodHost && $TimeOkay)"
# CandidateForPE="($GoodHost && $TimeOkay)"
# CandidateForPR="($GreatHost && $TimeFine)"

# ./comon-query alive-planetlab.hosts "$AliveHost" name name nameonly quiet
# ./comon-query good-planetlab.hosts "$GoodHost" name name nameonly quiet
# ./comon-query great-planetlab.hosts "$GreatHost" name name nameonly quiet


if [ ! -e alive-planetlab.hosts ] ; then
   echo >&2 "ERROR: alive-planetlab.hosts not found! Create it first!"
   exit 1
fi


echo "Removing old SSH keys ..."
cat alive-planetlab.hosts | xargs -n1 ssh-keygen -R >/dev/null 2>&1


echo "###### Trying European Hosts ##########################################"
IN_HOSTS="`grep ".de$" alive-planetlab.hosts`"
#  `grep ".at$" alive-planetlab.hosts` `grep ".ch$" alive-planetlab.hosts` `grep ".fr$" alive-planetlab.hosts` `grep ".be$" alive-planetlab.hosts` `grep ".nl$" alive-planetlab.hosts` `grep ".pl$" alive-planetlab.hosts` `grep ".cz$" alive-planetlab.hosts` `grep ".dk$" alive-planetlab.hosts` `grep ".se$" alive-planetlab.hosts` `grep ".fi$" alive-planetlab.hosts` `grep ".uk$" alive-planetlab.hosts` `grep ".no$" alive-planetlab.hosts` `grep ".it$" alive-planetlab.hosts` `grep ".es$" alive-planetlab.hosts` `grep ".pt$" alive-planetlab.hosts`"
HOSTSFILEMIDDLE="europe"
test-hosts

echo "###### Trying American Hosts ##########################################"
IN_HOSTS="`grep ".edu$" alive-planetlab.hosts`"
# "`grep ".berkeley.edu$" alive-planetlab.hosts` `grep ".ucla.edu$" alive-planetlab.hosts` `grep ".ucsd.edu$" alive-planetlab.hosts`"
HOSTSFILEMIDDLE="america"
test-hosts

echo "###### Trying Asian Hosts #############################################"
IN_HOSTS="`grep ".jp$" alive-planetlab.hosts` `grep ".cn$" alive-planetlab.hosts` `grep ".th$" alive-planetlab.hosts` `grep ".sg$" alive-planetlab.hosts` `grep ".kr$" alive-planetlab.hosts` `grep ".tw$" alive-planetlab.hosts` `grep ".in$" alive-planetlab.hosts`"
HOSTSFILEMIDDLE="asia"
test-hosts

echo "###### Trying Australian Hosts ########################################"
IN_HOSTS="`grep ".au$" alive-planetlab.hosts` `grep ".nz$" alive-planetlab.hosts`"
let SERVER_TEST_TIMEOUT=5*$SERVER_TEST_TIMEOUT
HOSTSFILEMIDDLE="australia"
test-hosts
touch pr-australia.hosts
touch pe-australia.hosts
touch pu-australia.hosts

echo "###### Trying Chinese Hosts ###########################################"
IN_HOSTS="`grep ".cn$" alive-planetlab.hosts`"
HOSTSFILEMIDDLE="china"
test-hosts

echo "###### Trying Essen Hosts #############################################"
IN_HOSTS="`grep ".uni-essen.de$" alive-planetlab.hosts`"
HOSTSFILEMIDDLE="essen"
test-hosts


echo "###### Trying ALL Hosts ###############################################"
. ./planetlab-config

IN_HOSTS="`cat alive-planetlab.hosts`"
OUT_HOSTS=great-planetlab.hosts
let SERVER_TEST_TIMEOUT=1*$SERVER_TEST_TIMEOUT
get-all-good-hosts

IN_HOSTS="`cat alive-planetlab.hosts`"
OUT_HOSTS=good-planetlab.hosts
let SERVER_TEST_TIMEOUT=10*$SERVER_TEST_TIMEOUT
get-all-good-hosts
