#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          pedClient
# Required-Start:    $syslog
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Descrption: PED client service
# Descrption: PED client service
### END INIT INFO
# Copyright (c) 2004-2017 Gemalto NV. All rights reserved.
#
# All rights reserved.  This file contains information that is
# proprietary to Gemalto NV, and may not be distributed
# or copied without written consent from Gemalto, NV.
#

PEDCLIENT_NAME="pedClient"

case `uname -s` in
    HP-UX)
        OS="hpux"
        PEDCLIENT_CMD="/opt/safenet/lunaclient/bin/$PEDCLIENT_NAME"
        ;;

    AIX)
        OS="aix"
        PEDCLIENT_CMD="/usr/safenet/lunaclient/bin/$PEDCLIENT_NAME"
        ;;

    [sS]unOS)
        OS="solaris"
        PEDCLIENT_CMD="/opt/safenet/lunaclient/bin/$PEDCLIENT_NAME"
        ;;

    [lL]inux)
        OS="linux"
        PEDCLIENT_CMD="/usr/safenet/lunaclient/bin/$PEDCLIENT_NAME"
        ;;

    *)
        # Should never happen since we went through it in the client install
        OS=""
        ;;

esac


if [ "$OS" = "linux" ] ; then
    REDHAT=""
    DEBIAN=""
    SUSE=""
    if [ -e /etc/redhat-release ] ; then
        REDHAT="yes"
    elif [ -e /etc/debian_version ] ; then
        DEBIAN="yes"
    elif [ -e /etc/SuSE-release ] ; then
        SUSE="yes"
    fi

    if [ "$REDHAT" != "" ] ; then
        LOCKFILE="/var/lock/subsys/$PEDCLIENT_NAME"
        . /etc/init.d/functions
    elif [ "$DEBIAN" != "" ] ; then
        LOCKFILE="/var/lock/$PEDCLIENT_NAME"
        . /lib/lsb/init-functions
    elif [ "$SUSE" != "" ] ; then
        LOCKFILE="/var/lock/subsys/$PEDCLIENT_NAME"
        . /etc/rc.status
    fi
fi


RETVAL=0
PROCCOUNT=0

check_proc()
{
    PEDCLIENT_P=`pgrep -f $PEDCLIENT_CMD`
    if [ "$PEDCLIENT_P" = "" ] ; then
        PROCCOUNT=0
    else
        PROCCOUNT=1
    fi
}


start()
{
    echo -n $"Starting $PEDCLIENT_NAME:"
    check_proc

    if [ "$PROCCOUNT" -eq 1 ]; then
	echo "OK"
          if [ "$LOCKFILE" != "" ] && [ "$OS" = "linux" ] ; then
              touch $LOCKFILE
          fi
    else
        # Start pedClient
        $PEDCLIENT_CMD -m start -logfilename /dev/null
	sleep 3
        check_proc

        if [ "$PROCCOUNT" -eq 1 ]; then
            echo "OK"
            if [ "$LOCKFILE" != "" ] && [ "$OS" = "linux" ] ; then
                touch $LOCKFILE
            fi
        else
            echo "FAILED"
            RETVAL=1
        fi
    fi
}

stop()
{
    echo -n $"Stopping $PEDCLIENT_NAME:"
    check_proc

    if [ "$PROCCOUNT" -ne 0 ]; then 
        PEDCLIENT_P=`pgrep -f $PEDCLIENT_CMD`
        if [ "$PEDCLIENT_P" != "" ] ; then
            # Stop pedClient
            $PEDCLIENT_CMD -m stop -logfilename /dev/null

            # If it is still running we now force kill it
            PEDCLIENT_P=`pgrep -f $PEDCLIENT_CMD`
            if [ "$PEDCLIENT_P" != "" ] ; then
                # try three times just in case
                kill -9 $PEDCLIENT_P
                kill -9 $PEDCLIENT_P
                kill -9 $PEDCLIENT_P
            fi
        fi

        check_proc
        if [ "$PROCCOUNT" -eq 0 ]; then
            echo "OK"
            if [ "$LOCKFILE" != "" ] && [ "$OS" = "linux" ] ; then
                rm -f $LOCKFILE
            fi
        else
            echo "FAILED"
            RETVAL=1
        fi

    else
	echo "OK"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        PEDCLIENT_P=`pgrep -f $PEDCLIENT_CMD`
        if [ "$PEDCLIENT_P" != "" ] ; then
            echo "$PEDCLIENT_NAME (pid $PEDCLIENT_P) is running..."
        else
            echo "$PEDCLIENT_NAME is stopped"
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=1
        ;;
esac

exit $RETVAL
