#!/bin/bash
#
# chkconfig: - 50 01
#
# p2 This shell script enables the automatic use of P2
#

# source function library
. /etc/rc.d/init.d/functions

MASTER=$2
PORT=$3
TIMEPORT=$4
SLICE=$5
NODEPORT=$6
OVERLOG=$7

start() {
	export PYTHONPATH=/usr/local/lib:/usr/local/bin
        if [ -f /home/${SLICE}/${OVERLOG} ]; then
		sudo python /home/${SLICE}/planetlab.py -t $TIMEPORT -m $MASTER -p $PORT -o /home/${SLICE}/${OVERLOG} -n ${SLICE} ${NODEPORT} >  /home/${SLICE}/planetlab.out 2>&1 &
	else
		sudo python /home/${SLICE}/planetlab.py -t $TIMEPORT -m $MASTER -p $PORT -n ${SLICE} ${NODEPORT} > /home/${SLICE}/planetlab.out 2>&1 &
        fi
}

stop() {
	sudo /bin/kill -9 `ps -ef | grep planetlab.py | grep -v grep | awk '{print $2}'` > /dev/null 2>&1
	echo `ps -ef | grep -v grep | grep planetlab.py`
}

restart() {
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop) 
	stop
	;;
  restart)
	restart
	;;
  *)
	echo $"Usage: $0 {start|stop|restart}"
	exit 1
esac
