#! /bin/sh
#
# chkconfig: 345 92 08
# description: The WNoDeS HyperVisor, used to instantiate and manage VM.
# processname: wnodes_hypervisor
#-------------------------------------------------------------------------------
# Copyright 2008-2012 Istituto Nazionale di Fisica Nucleare (INFN)
# 
# Licensed under the EUPL, Version 1.1 only (the "Licence").
# You may not use this work except in compliance with the Licence. 
# You may obtain a copy of the Licence at:
# 
# http://joinup.ec.europa.eu/system/files/EN/EUPL%20v.1.1%20-%20Licence.pdf
# 
# Unless required by applicable law or agreed to in 
# writing, software distributed under the Licence is 
# distributed on an "AS IS" basis, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. 
# See the Licence for the specific language governing 
# permissions and limitations under the Licence.
#-------------------------------------------------------------------------------
# Source function library.
. /etc/rc.d/init.d/functions

PRG="wnodes_hypervisor"
STP="wnodes_hypervisor_stop"
DIR="/usr/bin/wnodes_hypervisor"
CFG="/etc/wnodes/hypervisor/$PRG.cfg"

start() {
    bash -c "echo -n 'Starting $PRG:'"
    $DIR/$PRG -d
    RETVAL=$?
    [ "$RETVAL" = 0 ] && touch /var/lock/$PRG &&  echo_success
    echo
}

stop () {
    bash -c "echo -n 'Stopping $PRG:'"
    pid=`cat /var/run/"$PRG".pid`
    $DIR/$STP localhost
    echo "Killing the process $PRG"
    pid=`cat /var/run/"$PRG".pid`
    if [ "$pid" != "" ] ; then
        kill $pid
        rm -f /var/run/$PRG.pid
        bash -c "echo -n ' (Killed $pid)'"
    fi;
    RETVAL=$?
    [ "$RETVAL" = 0 ] && rm -f /var/lock/wnodes_bait && echo_success
    echo
}

status () {
    running=`ps aux | awk "/$PRG"'/ && !/awk/ && !/status/ {print $2}'`
    if [ "$running" = "" ]; then
        if [ -e /var/run/$PRG.pid ] ; then
            echo "$PRG not running but stale file found (/var/run/$PRG.pid)"
        else
            echo "$PRG not running"
        fi;
    else
        echo "$PRG (pid $running) is running"
    fi;
}

restart () {
    stop
    start
}


# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status
	;;
  restart|reload)
	restart
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload}"
esac

