#!/bin/bash
#
# IM - Infrastructure Manager
# Copyright (C) 2011 - GRyCAP - Universitat Politecnica de Valencia
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

### BEGIN INIT INFO
# Provides:             im
# Required-Start:       
# Required-Stop:        
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6 
# Short-Description:    IM - Infrastructure Manager
### END INIT INFO

# chkconfig: 2345 99 05
# description: IM - Infrastructure Manager


IM_LOG_PATH=/var/log/im
IMDAEMON=im_service.py

which ${IMDAEMON} > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
	echo "[Error] ${IMDAEMON} not in found"
	exit 1
fi

# the server will record its PID in this file
PIDFILE=/var/run/im.pid

function start() {
        echo -n "Starting IM Daemon: "
        test -d ${IM_LOG_PATH} || mkdir ${IM_LOG_PATH}
        if [ $? -ne 0 ]; then
                echo "[Error]"
                return
        fi
        ${IMDAEMON} 2> ${IM_LOG_PATH}/im.err &
        pid=$!
        echo ${pid} > ${PIDFILE}
        sleep 2
        alive=`ps ${pid} | grep ${pid} | wc -l`
        if [ ${alive} -eq 1 ]
        then
                # if there is some error msg en the err file
                if [ -s ${IM_LOG_PATH}/im.err ]
                then
                        echo "[Warn] (see im.err file for details)"
                else
                        echo "[OK]"
                fi
        else
                echo "[Error]"
        fi
}

function stop() {
        echo -n "Stopping IM Daemon: "
        if [ -f ${PIDFILE} ]
        then
                kill -s SIGINT `cat ${PIDFILE}`
                rm -f ${PIDFILE}
        fi
        echo "[OK]"
}

function status() {
        if ! [ -f ${PIDFILE} ]
        then
                echo "IM Daemon Stopped"
		exit 1
        else
                pid=`cat ${PIDFILE}`
                alive=`ps ${pid} | grep ${pid} | wc -l`
                if [ ${alive} -eq 0 ]
                then
                        echo "PID file exists but IM Daemon Stopped. Removing PID file"
                        rm -f ${PIDFILE}
			exit 2
                else
                        echo "IM Daemon Running (PID: ${pid})"
			exit 0
                fi
        fi
}

# See how we were called.
case "$1" in
  start)
        # check if the IM daemon is running
        if ! [ -f ${PIDFILE} ]
        then
                start
        else
                pid=`cat ${PIDFILE}`
                alive=`ps ${pid} | grep ${pid} | wc -l`
                if [ ${alive} -eq 0 ]
                then
                        rm -f ${PIDFILE}
                        start
                else
                        echo "IM Daemon Running (PID: ${pid})"
                fi
        fi
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 1s
        start
        ;;
  status)
        status
        ;;
  restart)
        $0 stop
        # give server time to die cleanly
        sleep 2
        $0 start
        ;;
  *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
esac

exit 0
