#!/bin/sh

BOSSNODE="/usr/local/etc/emulab/bossnode"
# GATEWAY="detertunnel"

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

EMUDIR="/usr/local/etc/emulab/"
GATEWAY=`$EMUDIR/tmcc -b syncserver | awk ' { match($0, /SERVER=[^[:space:]]+/); print substr($0,RSTART+8,RLENGTH-9);}'`

PORTS="139 7777"
NC="/usr/bin/nc"
RCDIR="/usr/local/etc/emulab/rc"

RCACCT="rc.accounts"
SMBMOUNT="smbmount.pl"

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

$EMUDIR/sethostname

echo $GATEWAY > $BOSSNODE

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


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

#
# Copy and exec the smbmount perl script
#

echo "Mounting via SMB."

exec $EMUDIR/$SMBMOUNT
