#!/usr/bin/env bash
#
# Thomas Dreibholz's PlanetLab/NorNet Script Collection
# Copyright (C) 2005-2025 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

# Bash options:
set -e

. ./planetlab-config


if [ x$1 = "x" ]; then
   SERVER_LIST=$PLANETLAB_COMPILEHOST
   echo "NOTE: Using compile host $SERVER_LIST."
else
   SERVER_LIST=`\
   ( \
      while [ x$1 != "x" ] ; do \
         echo $1 && \
         shift ; \
      done \
   ) | sort -u`
fi


for SERVER in $SERVER_LIST; do
   echo -e "\x1b[34m`env LANG=en date`: Contacting $SERVER ...\x1b[0m"

   cmd="echo -e \"\x1b[33m\`env LANG=en date\`: Updating repositories on $SERVER ...\x1b[30m\""
   
   # yum extremely sucks by trying really slow or even not working mirrors.
   # yum-plugin-fastestmirror solves this issue. Therefore, install it first.
   cmd="$cmd ; sudo yum -y install $PLANETLAB_YUMOPTS yum-plugin-fastestmirror"
   cmd="$cmd ; sudo yum $PLANETLAB_YUMOPTS clean all ; sudo yum -y $PLANETLAB_YUMOPTS update"

   cmd="$cmd ; echo -e \"\x1b[33m\`env LANG=en date\`: Installing packages on $SERVER ...\x1b[30m\""
   cmd="$cmd ; sudo yum $PLANETLAB_YUMOPTS -y install"
   for package in $PLANETLAB_COMPILEPKGS ; do
      cmd="$cmd $package"
   done

   cmd="$cmd ; echo -e \"\x1b[33m\`env LANG=en date\`: Cleaning up on $SERVER ...\x1b[30m\""
   cmd="$cmd ; sudo yum -y $PLANETLAB_YUMOPTS upgrade"
   cmd="$cmd ; sudo /usr/bin/updatedb"
   
   ssh -C -i $PLANETLAB_KEY -oPasswordAuthentication=no -oStrictHostKeyChecking=no $PLANETLAB_USER@$SERVER "$cmd" &
done

wait
