#!/bin/bash
#
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
. /etc/emulab/paths.sh

#
# testbed       Do some testbed stuff,
#
# description:	On shutdown, it informs the testbed we are rebooting
#		and terminates any lingering daemons.

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
fi

# Source networking configuration.
if [ -f /etc/sysconfig/network ]; then
    . /etc/sysconfig/network
elif [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
    NETWORKING=yes
fi

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

RETVAL=0

# See how we were called.
case "$1" in
  start)
        #
        # Testbed Setup.
        #
	if [ -f /usr/local/etc/emulab/rc/rc.testbed ] ; then
	    echo -n 'testbed config: '
            /usr/local/etc/emulab/rc/rc.testbed
	    touch /var/lock/subsys/testbed
	fi
	;;
  stop)
  	rm -f /var/lock/subsys/testbed /var/emulab/boot/controlif
	echo "Shutting down testbed daemons (some may fail)..."
	echo -n "  trafgen: "
	killproc trafgen
	echo
	echo -n "  program-agent: "
	killproc program-agent
	echo
	echo -n "  slothd: "
	killproc slothd
	echo
	echo -n "  healthd: "
	killproc healthd
	echo
	echo -n "  watchdog: "
	killproc watchdog
	echo
	echo -n "  evproxy: "
	killproc evproxy
	echo
	echo -n "  linktest: "
	killproc linktest
	echo
	echo -n "  emulab-syncd: "
	killproc emulab-syncd
	echo
	echo -n "  tippty: "
	killproc tippty
	echo
        if [ ! -f /var/lock/subsys/tbprepare ]; then
	    echo "Informing the testbed we're rebooting ..."
	    $BINDIR/tmcc state SHUTDOWN
	    RETVAL=$?
	fi
	;;
  status)
	echo "Peachy"
	;;
  restart)
	echo "Done"
	;;
  reload)
	echo "Done"
	;;
  *)
	echo "Usage: testbed {start|stop|restart|reload|status}"
	exit 1
esac

exit $RETVAL
