#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          utm
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      pcscd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: CenterInform Transport
# Description:       CenterInform Transport
### END INIT INFO
### ver. 1.0.5


DAEMON_NAME=utm
DAEMON_PATH=/opt/utm/transport
DAEMON=$DAEMON_PATH/bin/transport.sh
DAEMON_OPTS=""
DAEMON_USER=root
LOGFILE=$DAEMON_PATH/$DAEMON_NAME.log

export PIDFILE=$DAEMON_PATH/$DAEMON_NAME.pid

do_start () {
    chmod 777 /opt/utm/jre/bin/*
    start-stop-daemon --start --quiet --background \
        --pidfile $PIDFILE --make-pidfile \
        --user $DAEMON_USER --chuid $DAEMON_USER \
        --startas $DAEMON \
        -- $DAEMON_OPTS >$LOGFILE 2>&1 

    chown $DAEMON_USER $PIDFILE
    chown $DAEMON_USER $LOGFILE

}

do_stop () {

     if [ -f $PIDFILE ]; then
        start-stop-daemon --stop --quiet --pidfile $PIDFILE --retry 10
        pkill -f es.programador.transport.Transport

        rm -f $PIDFILE
     fi
}

case "$1" in

    start|stop)
        do_${1}
        ;;

    restart|reload|force-reload)
        do_stop
        do_start
        ;;

    status)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%-50s\n" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
        fi
        ;;

    *)
        echo "Usage: service $DAEMON_NAME {start|stop|restart|status}"
        exit 1
        ;;

esac
exit 0
