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


SERVER=$1
if [ x$SERVER = "x" ]; then
   SERVER=$PLANETLAB_COMPILEHOST
   echo "NOTE: Using compile host $SERVER."
fi


echo -e "\x1b[34m`date`: Contacting $SERVER ...\x1b[0m"
ssh -C -i $PLANETLAB_KEY -oPasswordAuthentication=no $PLANETLAB_USER@$SERVER "\
echo \"\`date\`: Cleaning up $SERVER ...\" && \
rm -rf sctplib* && \
rm -rf socketapi* && \
rm -rf rsplib* && \
sudo rm -f /usr/local/libsctpsocket* && \
sudo rm -f /usr/local/libcppsocket* && \
sudo rm -f /usr/local/librsplib* && \
sudo /sbin/ldconfig" || exit 1

echo -e "\x1b[34m`date`: Preparing sctplib/socketapi distributions ...\x1b[0m"
if [ ! -e "$SCTPLIB_DIRECTORY" ] ; then
   echo "ERROR: sctplib directory \"$SCTPLIB_DIRECTORY\" does not exist!"
   exit 1
fi
cd "$SCTPLIB_DIRECTORY"
rm -f *.tar.gz
make dist
scp -i $PLANETLAB_KEY -oPasswordAuthentication=no sctplib*.tar.gz $PLANETLAB_USER@$SERVER: || exit 1


if [ ! -e "$SOCKETAPI_DIRECTORY" ] ; then
   echo "ERROR: socketapi directory \"$SOCKETAPI_DIRECTORY\" does not exist!"
   exit 1
fi
cd "$SOCKETAPI_DIRECTORY"
rm -f *.tar.gz
make dist
scp -i $PLANETLAB_KEY -oPasswordAuthentication=no socketapi*.tar.gz $PLANETLAB_USER@$SERVER: || exit 1


echo -e "\x1b[34m`date`: Contacting $SERVER ...\x1b[0m"
ssh -C -i $PLANETLAB_KEY -oPasswordAuthentication=no $PLANETLAB_USER@$SERVER "\
tar xzvf sctplib*.tar.gz && \
tar xzvf socketapi*.tar.gz && \
cd sctplib* && \\
./configure $SCTPLIB_OPT && \
make && \
sudo make install && \
cd .. && \
cd socketapi* && \\
./configure $SOCKETAPI_OPT && \
make && \
sudo make install && \
sudo /sbin/ldconfig" || exit 1
