#!/bin/sh

# Set up a federated environment on a client.  Unmount all local file systems,
# erase all local accounts and then bring in accounts and file systems from the
# master testbed.  Once all that's done, restore client communication to the
# local bossnode and execute any startup command passed in as arguments to this
# script.


# The file containing the DNS name of the current boss
BOSSNODE="/usr/local/etc/emulab/bossnode"

# Ports that the master gateway will forward
PORTS="139 7777"
# network cat command (to listen for the gateway)
NC="/usr/bin/nc"
# Emulab rc scripts directory
RCDIR="/usr/local/etc/emulab/rc"

# Emulab rc script to reset accounts and federation srcipt to start the smb
# automount process.
RCACCT="rc.accounts"
SMBMOUNT="smbmount.pl"

# Find somewhere safe to stop
cd /tmp

if [ -f $BOSSNODE ]
then
	rm -f $BOSSNODE
fi

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 Scripts are not where they should be.  NB, this is checked by the
# bootstrapper, too, so a failure here is unlikely.
if [ -z "$SCRIPTDIR" ] ; then
    echo "Can't find federation scripts.  Tmcc status says:"
    $EMUDIR/tmcc -b status
    exit 1;
fi

# Copy the global hostnames file into /etc/hosts so this node can address
# others in the experiment on other testbeds.  The extra steps preserves teh
# localhost entry that's aliased to this host's name.
grep 127\\.0\\.0\\.1 /etc/hosts > /tmp/hosts
cat "$SCRIPTDIR/hosts" >> /tmp/hosts
cp /tmp/hosts /etc/hosts && rm /tmp/hosts

# Get our gateway, share, and mount user from the configuration file.
# There's probably a way to get all three at once, but this works.
GATEWAY=`perl -ne '/ControlGateway:\s+(.*)/i && print "$1\n";' $SCRIPTDIR/client.conf`
SHARE=`perl -ne '/SMBShare:\s+(.*)/i && print "$1\n";' $SCRIPTDIR/client.conf`
SMBUSER=`perl -ne '/ProjectUser:\s+(.*)/i && print "$1\n";' $SCRIPTDIR/client.conf`

#
# Spin on our gateway!
#

echo "Waiting for the tunnel to come online."

for port in $PORTS
do
        until $NC -z $GATEWAY $port
        do
                sleep 5
        done
done

#
# Setup TMCC to use our gateway
#

echo "Configuring TMCC."
echo $GATEWAY > $BOSSNODE


#
# Unmount stuff before messing with accounts
#
umount -A -f -t nfs,smbfs


#
# Setup new accounts
#
$RCDIR/rc.accounts reconfig

#
# Call the smbmount perl script, this invokes the automounter to mount teh
# needed smb filesystems.
#
echo "Mounting via SMB."
"/tmp/$SMBMOUNT" $SHARE $GATEWAY $SMBUSER 

echo "Restoring old bossnode"
# remove bossnode override
/bin/rm -f $BOSSNODE
# Also clear any cached data that might interfere with rourte construction.
/bin/rm -f /var/emulab/boot/tmcc/ifconfig

# I remain unclear why ospfd seems screwed up after the resetting of accounts
# and mounts.  This seems to fix it, but it's not very satisfying.  At this
# point the federated topology is complete, so any problems with routing in a
# partial topology -e.g. failed or partial bridging - that might have confused
# the router should be gone.
/bin/pkill ospf
/bin/pkill gated
/usr/local/etc/emulab/rc/rc.route boot

# Execute any command passed in as a startcmd.  This basically daisy chains the
# startcmd.
if [ ! -z "$1" ]; then 
	su -l "$1" -c "$2"
fi
