#!/bin/sh
#$Id: aksusbd.redhat,v 1.3 2005/06/17 07:27:40 axel 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
RETVAL2=0

start () {

	  echo -n $"Starting AKSUSB daemon: "
    daemon /usr/sbin/aksusbd
    RETVAL=$?
    echo

    echo -n $"Starting WINEHASP daemon: "
    pgrep winehasp >/dev/null
    RETVAL2=$?
    if [ $RETVAL2 != 0 ]; then
      daemon /usr/sbin/winehasp
      RETVAL2=$?
    else
      success $"winehasp running"
      RETVAL2=0
    fi
    echo
    [ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && touch /var/lock/subsys/aksusbd || RETVAL=1
    return $RETVAL
}

stop () {

    echo -n $"Stopping WINEHASP daemon: "

#    killproc winehasp
#    RETVAL=$?

    dpid="$(ps axh | grep winehasp | 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=$?
    	success $"winehasp stopped"
    else
    	RETVAL=0
    	failure $"winehasp not running"
    fi

    echo
    echo -n $"Stopping AKSUSB daemon: "

#    killproc aksusbd
#    RETVAL2=$?

    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
    	RETVAL2=$?
    	success $"aksusbd stopped"
    else
    	RETVAL2=0
    	failure $"aksusbd not running"
    fi

    [ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f /var/lock/subsys/aksusbd
    echo ""
    return $RETVAL
}

restart () {
    stop
    start
}

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

exit $?
