#!/bin/bash

#Author: <mihai-radu.dumitru.ext@unify.com>
#
# Daemon for starting inventory client
#
#HOC:
# 2015.04.08 - initial creation
# 2015.04.20 - mihai
#   - changed start and stop to return 0 in case already started or stopped
# 2015.11.12 - jano
#   - Change logging - log is opened from client script itself - to enable log rotation.


# /etc/init.d/inventoryd
#
### BEGIN INIT INFO
# Provides: inventoryd
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Inventory Client Daemon
### END INIT INFO

INVENTORY_CLIENT_PIDFILE=/var/run/inventory-client.pid

. /etc/profile
. /etc/rc.status

rc_reset
case "$1" in
    start)
        if checkproc -p $INVENTORY_CLIENT_PIDFILE /bin/bash; then
            echo -n "Service Inventory Client already running "
            true
            rc_status -v
        else
            echo -n "Starting Inventory Client daemon "
            0</dev/null 1>/dev/null 2>&1 startproc -f -p $INVENTORY_CLIENT_PIDFILE /bin/bash \
                /opt/inventory/inventory-client.sh
            rc_status -v
        fi
	;;
    stop)
        if checkproc -p $INVENTORY_CLIENT_PIDFILE /bin/bash; then
            echo -n "Shutting down Inventory Client daemon "
            killproc -p $INVENTORY_CLIENT_PIDFILE /bin/bash
            rc_status -v
            rm -f /dev/.inventory.connection.status
        else
            echo -n "Service Inventory Client not running "
            true
            rc_status -v
        fi
	;;
    restart)
        $0 stop
        $0 start

        rc_status
        ;;
    status)
        echo -n "Checking for Inventory Client daemon "
        checkproc -p $INVENTORY_CLIENT_PIDFILE /bin/bash
        rc_status -v
        ;;
    *)
	echo "Usage: $0 {start|stop|status}"
	exit 1
	;;
esac
rc_exit
