#!/bin/sh
#
# chkconfig: 345 41 60
#
# the template file of init.d script
# start or stop services

############
# CHANGELOG
#===========
# 2010/06/04    Creation	Thanh lam NGUYEN
################


. /etc/init.d/rc.config

bin=/usr/sbin/send_inventory
pidfile=/var/run/inventory.pid
lockfile=
args="-p $pidfile"

start()
{
    local RET=0

    : "Already running ?"
    [[ -f $pidfile ]] && [[ -e /proc/$(cat $pidfile) ]] && RET=1 && return $RET

    : "Forward the xflag to $bin"
    if [[ "${-//x}" != "$-" ]]; then
        sh -x $bin $args
    else
        $bin $args
    fi
    RET=$?
    if [[ $RET -ne 0 ]]; then
        [[ -f $pidfile ]] && [[ -e /proc/$(cat $pidfile) ]] && kill $(cat $pidfile)
        rm -f $pidfile >/dev/null 2>&1
    fi
    return $RET
}

stop()
{
    local RET=0

    if [[ -f $pidfile ]]; then
        kill $(cat $pidfile)
        RET=$?
        if [[ $RET -eq 0 ]]; then
            rm -f $pidfile $lockfile >/dev/null 2>&1
        fi
    fi
    return $RET
}

condrestart()
{
    local RET=0

    if [ -f $lockfile ]; then
        stop
        start
        RET=$?
    fi
    return $RET
}

restart()
{
    local RET=0

    stop
    start
    RET=$?
    return $RET
}


param=$1
shift
case "$param" in
    start|"") : "$param"
        start "$@"
        RETVAL=$?
    ;;
    stop) : "$param"
        stop "$@"
        RETVAL=$?
    ;;
    restart) : "$param"
        restart "$@"
        RETVAL=$?
    ;;
    condrestart) : "$param"
        condrestart "$@"
        RETVAL=$?
    ;;
    *) : "$param"
        echo "Usage $0 {start|stop|restart|condrestart}" >&2
        RETVAL=1
    ;;
esac
exit $RETVAL
