#!/usr/bin/bash

# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/data_mgr/data_mgr-svc.sh 2576 2020-03-23 14:54:05Z depa $

if test $# -lt 1 ; then
    echo "usage: $me {start|stop} PIDFILE";
    exit -1
fi


me="data_mgr-svc"

pidfile=/opt/tan/var/run/$me.pid
logfile=/opt/tan/var/log/$me.log

case $1 in
    start)
        if test -e $pidfile; then
            oldpid=`cat $pidfile`; 
            oldrun=`lsof -t -p $oldpid`;
            if test "x$oldpid" = "x$oldrun"; then
                echo "$me already running with $oldpid"
                exit -1
            fi
        fi

        exec 2>&1 /opt/tan/bin/dataManagerService --Ice.Config="/opt/tan/etc/data_mgr/data_mgr-ice.cfg" $* 1>> $logfile &
        pid=$!
        run=`lsof -t -p $pid`
        sleep 0.1
        if test "x$pid" = "x$run"; then
            echo $! > $pidfile;
        else
	          echo "error: could not start #$me $*"
        fi
        ;;
    stop)
        kill -2 `cat $pidfile`;
        ii=20;
        while [ "$ii" -gt 0 ];
        do
            pid=`cat $pidfile`
            echo $ii
            if ! ps -p $pid>/dev/null; then 
                rm $pidfile;
                exit 0;
            fi
            sleep 0.1;
            ii=`expr $ii - 1`;
        done
        # kill -9 `cat $pidfile`; 
        rm $pidfile;
        ;;
    *)
        echo "usage: $me {start|stop} PIDFILE" ;;
esac
exit 0
