#! /bin/sh
#
# Copyright (c) 2013-2014, 2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#

#
# chkconfig: 2345 95 95
#
### BEGIN INIT INFO
# Provides:          pmon
# Required-Start: $null
# Required-Stop: $null
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: process Monitor daemon 
### END INIT INFO

# echo "7:3:respawn:/usr/local/bin/pmond" >> /etc/inittab

. /etc/init.d/functions

DAEMON_NAME="pmond"
DAEMON="/usr/local/bin/${DAEMON_NAME}"
IFACE=""

if [ ! -e "$DAEMON" ] ; then
    logger "$DAEMON is missing"
    exit 1
fi

RETVAL=0

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
export PATH

case "$1" in
    start)
        echo -n "Starting $DAEMON_NAME: "
        # Prevent multipe starts
        if [ -n "`pidof ${DAEMON_NAME}`" ] ; then
            echo "OK"
            exit $RETVAL
        fi
        start-stop-daemon --start -b -x ${DAEMON} --
        RETVAL=$?
        if [ $RETVAL -eq 0 ] ; then
            echo "OK"
        else
            echo "FAIL"
        fi
        ;;

    stop)
        echo -n "Stopping ${DAEMON_NAME}: "
        if [ -n "`pidof ${DAEMON_NAME}`" ] ; then
            killproc ${DAEMON_NAME}
        fi
        echo "OK"
        ;;

    restart)
        $0 stop
        $0 start
        ;;

    status)
        pid=`pidof ${DAEMON_NAME}`
        RETVAL=$?
        if [ ${RETVAL} -eq 0 ] ; then
            echo "${DAEMON_NAME} is running"
        else
            echo "${DAEMON_NAME} is NOT running"
        fi
        ;;

    condrestart)
        [ -f /var/lock/subsys/${DAEMON_NAME} ] && $0 restart
        ;;

    *)
        echo "usage: $0 { start | stop | status | restart | condrestart | status }"
        ;;
esac

exit $RETVAL
