#! /bin/sh

# For RedHat and cousins:
# chkconfig: 2345 20 80
# description: Userspace Xbox/Xbox360 Gamepad Driver Daemon
# processname: xboxdrvd

### BEGIN INIT INFO
# Provides: xboxdrvd
# Should-Start:
# Default-Start: 
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: Userspace Xbox/Xbox360 Gamepad Driver Daemon
# Description: This is a Xbox/Xbox360 gamepad driver for Linux that works in userspace.
#              It is an alternative to the xpad kernel driver and has support for 
#              Xbox1 gamepads, Xbox360 USB gamepads and Xbox360 wireless gamepads, 
#              both first and third party.
### END INIT INFO

##  Xbox360 USB Gamepad Userspace Driver
##  Copyright (C) 2011 Ingo Ruhnke <grumbel@gmail.com>
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.

XBOXDRVD_BIN=/usr/bin/xboxdrv

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

RETVAL=0
prog=xboxdrvd
pidfile=/var/run/xboxdrv.pid
config=/etc/sysconfig/default.xboxdrv
lockfile=/var/lock/subsys/$prog

 [ -c /etc/sysconfig/default.xboxdrv ] && . /etc/sysconfig/default.xboxdrv

start() {
    [ -x $XBOXDRVD_BIN ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon $XBOXDRVD_BIN
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    if [ -n "`pidfileofproc $XBOXDRVD_BIN`" ] ; then
        killproc $XBOXDRVD_BIN
		RETVAL=3
    else
        failure $"Stopping $prog"
    fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

