#!/bin/sh

# find perl
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 this experiment's stashed scripts.  (Perl for advanced parsing)
EMUDIR="/usr/local/etc/emulab/"
PROJECT=`$EMUDIR/tmcc -b status | $PERL -ne '/ALLOCATED=([^\/]+)\/([^\s]+)/ && print "$1\n";'`
EXP=`$EMUDIR/tmcc -b status | $PERL -ne '/ALLOCATED=([^\/]+)\/([^\s]+)/ && print "$2\n";'`
CONFIGDIR="/proj/$PROJECT/exp/$EXP/tmp"
SCRIPTDIR="/usr/local/federation/bin"

# Very simple script to copy the global hosts file over the local one
# while preserving the localhost entry.  Run on DETER master clients

grep 127\\.0\\.0\\.1 /etc/hosts > /tmp/hosts
cat "$CONFIGDIR/hosts" >> /tmp/hosts
cp /tmp/hosts /etc/hosts && rm /tmp/hosts

# 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

# XXX: modified seer daemon to read a config file.  Not in the tarfile yet.  If
# we see one, and there's a /usr/seer/backend/daemon.py, replace that one with
# the local one.
if [ -e /usr/seer/backend/daemon.py ] && [ -e $SCRIPTDIR/daemon.py ]; then
	/bin/cp $SCRIPTDIR/daemon.py /usr/seer/backend/daemon.py 
fi

if [ -e /usr/seer/backend/experiment-setup.py ] && [ -e $SCRIPTDIR/experiment-setup.py ]; then
	/bin/cp $SCRIPTDIR/experiment-setup.py /usr/seer/backend/experiment-setup.py 
fi

# Copy the seer.conf to the local /tmp if there is one

if [ -e $CONFIGDIR/seer.conf ]; then
	/bin/cp $CONFIGDIR/seer.conf /tmp
fi


# SEER feels pain and confusion if times are wrong.  This lays a big lick on
# ntp sets the time directly and then restarts it.  XXX FreeBSD incantation is
# untested.

case `uname` in 
	"FreeBSD")
		/etc/rc.d/ntpd stop
		/usr/sbin/ntpdate boss
		/etc/rc.d/ntpd start
		;;
	"Linux")
		/etc/rc.d/init.d/ntpd stop
		/usr/sbin/ntpdate boss
		/etc/rc.d/init.d/ntpd start
		;;
esac

# If we need to configure an interface up for a DRAGON embedding, do it here.
if [ -e $SCRIPTDIR/config_from_tunnelip.pl ]; then
	$PERL $SCRIPTDIR/config_from_tunnelip.pl
fi
# 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 


# Execute any command passed in as a startcmd.  This basically daisy chains the
# startcmd.  This needs to become a perl script to have uniform behavior here.
echo "*$1* *$2*"
if [ ! -z "$1" ]; then 
	echo "calling *$1* *$2*"
	case `uname` in
		"FreeBSD")
			su -l "$1" -c "$2"
			;;
		"Linux")
			su "$1" --command="$2"
			;;
	esac
fi
