#!/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.`uname`.pl"

# Find the right version of pkill.  For killing.
if [ -x "/usr/bin/pkill" ] ; then 
	PKILL="/usr/bin/pkill"
else
	if [ -x "/bin/pkill" ] ; then 
		PKILL="/bin/pkill"
	else
		PKILL="echo"
	fi
fi

if [ -x "/usr/bin/perl" ]; then
    PERL="/usr/bin/perl"
else
    if [ -x "/usr/local/bin/perl" ]; then
	PERL="/usr/local/bin/perl"
    else
	# Wing it...
	PERL="perl"
    fi
fi

# 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`
SMBPROJECT=`$PERL -ne '/ProjectName:\s+(.*)/i && print "$1\n";' $SCRIPTDIR/client.conf`

# XXX:
# The standard emulab rc.accounts script won't replace existing user accounts
# with our new ones, so we have to patch it to do so.  This is obviously pretty
# fragile and needs to change.  To make matters worse, our FBSD62-SMB images
# contain the patched rc.accounts, so this patching step will fail there.
cd "$EMUDIR/rc"
patch -f < /tmp/rc.accounts.patch
cd /tmp

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

case `uname` in
	"FreeBSD")
		umount -A -f -t nfs,smbfs,cifs
		;;
	"Linux")
		# Linux doesn't believe in -A so pull the filesystems out of
		# /etc/mtab and umount them individually
		for f in `$PERL -nae 'print "$F[1]\n" if $F[2] =~ /(nfs|cifs|smbfs)/;' /etc/mtab`; do
			umount -f $f
		done
		;;
esac

#
# Setup new accounts
#

# Clear the cache, just in case:
rm -f /var/emulab/boot/tmcc/accounts

$RCDIR/rc.accounts reconfig

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

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.
$PKILL ospf
$PKILL gated
# XXX Danger, hack ahead.  Apparently our FC6-SMB image and emulab disagree
# about where gated lives.  This will alias it.
if [ ! -x /usr/sbin/gated ] ; then 
	if [ -x /sbin/gated ] ; then
		ln /sbin/gated /usr/sbin/gated
	fi
fi
/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
