#!/bin/bash
#
# Copyright (c) 2000-2018 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/>.
# 
# }}}
#
### BEGIN INIT INFO
# Provides: pubsubd
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Should-Start: 
# Required-Start: pubsubd
# Required-Stop: 
# Short-Description: Start and stop Emulab daemons
# Description: Start and stop Emulab daemons
### END INIT INFO

. /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

#
# Discover process kill function.  Older systems with sysv rc inits or
# upstart will provide /etc/rc.d/init.d/functions with killproc, but if
# not, fall back to pkill.
#
type killproc >/dev/null 2>&1
if [ $? -eq 0 ]; then
    KILLPROC=killproc
else
    KILLPROC=pkill
fi

# See how we were called.
case "$1" in
  start)
	# This stuff done from rc.local cause I cannot figure
	# out a way to log to the console instead of messages.
	# Note, /var/lock/subsys/testbed will be created at that time.
	# BUT: if systemd is our init, rc.local DOES NOT kick this off; it gets
	# done here!
	if [ -f /etc/emulab/uses-systemd -a -f $BINDIR/rc/rc.testbed ] ; then
	    mkdir -p /var/lock/subsys
	    echo -n 'testbed config: '
            $BINDIR/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
	echo -n "  capture-nossl: "
	$KILLPROC capture-nossl
	echo
	if [ -e $ETCDIR/genvmtype -a -x $BINDIR/bootvnodes ]; then
	    echo "  bootvnodes: halting vnodes..."
	    # Foreground mode.
	    $BINDIR/bootvnodes -f -h
	    echo "  bootvnodes: halted vnodes ($?)"
	fi
        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
