#!/bin/sh

# Bootstrap cleint federation set up.  Move federation scripts to /tmp and
# execute them from there.  Because those scripts unmount significant hunks of
# the file system - including the directories in which the scripts are
# initially stored - unpredictable behavior can result unless we're careful not
# to be depending on any of those directories.

# Emulab scripts directory
EMUDIR="/usr/local/etc/emulab/"

# Find this experiment's stashed scripts.  (Perl for advanced parsing)
SCRIPTDIR=`$EMUDIR/tmcc -b status | perl -ne '/ALLOCATED=([^\/]+)\/([^\s]+)/ && print "/proj/$1/exp/$2/tmp\n";'`


# Die if federation scripts are not where they're supposed to be.
if [ -z "$SCRIPTDIR" ] ; then
    echo "Can't find federation scripts.  Tmcc status says:"
    $EMUDIR/tmcc -b status
    exit 1;
fi

# The copy
for f in federate.sh smbmount.pl; do 
	cp $SCRIPTDIR/$f /tmp && chmod 755 /tmp/$f
done

# Move to a safe directory and overwrite this process with the actual
# federation script, and run it.
cd /tmp
exec /tmp/federate.sh
