#!/bin/bash
#
# UNICORE TSI
#

### BEGIN INIT INFO                                                                                                                                                     
# Provides:          unicore-tsi
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       UNICORE TSI
### END INIT INFO

# Source function library.
. /lib/lsb/init-functions

NAME="UNICORE TSI"

#This must be equal to this file name
SERVICE=unicore-tsi
START=/usr/share/unicore/tsi_selected/bin/start_tsi
STOP=/usr/share/unicore/tsi_selected/bin/kill_tsi
LASTPID=/var/run/unicore/tsi.pid
CONF=/etc/unicore/tsi


[ -f /etc/default/$SERVICE ] && . /etc/default/$SERVICE

start() {
	log_daemon_msg "Starting $NAME: "
        start-stop-daemon --start --exec "$START" "$CONF"
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch "/var/lock/$SERVICE"
	log_end_msg $RETVAL
	return $RETVAL
}
stop() {
	log_daemon_msg $"Stopping $NAME: "
	$STOP $CONF
	log_end_msg $?
	rm -f "/var/lock/$SERVICE"
	echo
}

status() {                                                                                                                                                               
        if [ -a "$LASTPID" ]; then
                 status_of_proc -p "$LASTPID" "$SERVICE"
        else
                 log_failure_msg "$SERVICE is not running"
        fi
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status
	;;
  restart)
	stop
	start
	;;
  setup)
	# Setup the TSI, i.e. run Install.sh from the installation directory
	cd /usr/share/unicore/tsi
	./Install.sh "" /usr/share/unicore/tsi_selected
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|help}"
	exit 1
esac

exit $RETVAL
