#!/bin/sh
#$Id: aksusbd.redhat,v 1.4 2002/09/20 10:33:44 chris Exp $
#
# chkconfig: 2345 27 73
# description: AKSUSB daemon
# processname: /usr/sbin/aksusbd

# Source function library.
. /etc/rc.d/init.d/functions

[ -x /usr/sbin/aksusbd ] || exit 0

RETVAL=0

start () {
    echo -n "Starting AKSUSB daemon: "
    # run checkpc to fix whatever lpd would complain about
    /usr/sbin/checkpc -f
    # start daemon
    daemon /usr/sbin/aksusbd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/aksusbd
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n "Stopping AKSUSB daemon: "
    dpid="$(ps axh | grep aksusbd | grep -v grep | sed -e 's/ *\(.*\)/\1/' -e 's/ \+/ /g' | grep -v " /bin/sh " | grep -v "^$$ " |  cut -f1 -d\  | head -1)"
    if [ ! -z "$dpid" ]; then
    	kill $dpid
    	RETVAL=$?
    else
    	RETVAL=0
    fi
    [ $RETVAL != 0 ] && failure $"aksusbd shutdown" || success $"aksusbd shutdown"
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/aksusbd
    return $RETVAL
}

restart () {
    stop
    start
    RETVAL=$?
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	status /usr/sbin/aksusbd
	RETVAL=$?
	;;
    restart)
	restart
	;;
    condrestart)
	# only restart if it is already running
	[ -f /var/lock/subsys/aksusbd ] && restart || :
	;;
    reload)
	echo -n "Reloading AKSUSB daemon: "
	killproc /usr/sbin/aksusbd -HUP
	RETVAL=$?
	echo
	;;
    *)
        echo "Usage: aksusbd {start|stop|restart|condrestart|reload|status}"
        RETVAL=1
esac

exit $RETVAL
