#!/bin/sh
### BEGIN INIT INFO
# Provides:	iguanair
# Required-Start:	$remote_fs
# Required-Stop:	$remote_fs
# Short-Description: IguanaWorks USB IR Device Daemon
# Description: IguanaWorks USB IR Device Daemon
# Should-Start:
# Should-Stop:
# X-Start-Before: lirc
# X-Stop-After: lirc
# Default-Start: 2 3 4 5 
# Default-Stop: 0 1 6
### END INIT INFO
#
# Startup script for the Iguanaworks USB IR daemon.
#
# Two lines for Fedora's chkconfig
# chkconfig: 2345 80 20
# description: Enables the driver for Iguanaworks USB IR devices.
#
######################

# load the default settings
[ -f /etc/sysconfig/iguanaIR ] && . /etc/sysconfig/iguanaIR
# combine some variables into a command line
IGUANAIR_OPTIONS="$OPTIONS --log-level=$LOGLEVEL --send-timeout=$SENDTIMEOUT --receive-timeout=$RECEIVETIMEOUT $DRIVERS"
SOCKDIR=/var/run/iguanaIR

# We need to know what helper functions are legal, so detect the
# distro.  Override this in /etc/defaults/iguanaIR
# First try lsb, if it exists, if not then use our
# /etc/issue hack.

    . /etc/rc.d/init.d/functions

    # fedora needs these variables
    LOCKFILE=/var/lock/subsys/iguanaIR
    PIDFILE=/var/run/igdaemon.pid

    # definitions to make a common shell script    
    log_begin_msg()
    {
        echo -n $*
    }
    log_end_msg()
    {
        echo
    }

# common defaults
IGPATH=/usr/bin/igdaemon
LOGFILE=/var/log/iguanaIR.log
RETVAL=0

# check for the executable exists
if [ ! -x $IGPATH ]; then
    echo "Failed to find igdaemon executable."
    exit 1
fi

# I like the way gentoo does startup scripts, but no dependencies for
# me.
depends()
{
    RETVAL=0
}

start()
{
    # figure out what command to run to start the daemon
    if [ "$LOCKFILE" != "" ]; then 
        if [ ! -e $LOCKFILE ]; then
            START="daemon --user=iguanair $IGPATH $IGUANAIR_OPTIONS -l $LOGFILE"
        fi
    else
        START="start-stop-daemon --start --chuid iguanair --group iguanair --exec $IGPATH -- $IGUANAIR_OPTIONS -l $LOGFILE"
    fi

    if [ "$START" != "" ]; then
        # make sure the log file exists/is writable
        touch $LOGFILE
        chown iguanair:iguanair $LOGFILE

        # make sure the socket directory exists and is writable
        mkdir -p $SOCKDIR
        chown iguanair:iguanair $SOCKDIR

        # start the daemon
        log_begin_msg "Starting Iguanaworks USB IR daemon..."
        $START
        RETVAL=$?
        log_end_msg $RETVAL

        # touch the PIDFILE
        if [ "$PIDFILE" != "" -a $RETVAL = 0 ]; then
            echo $(pidofproc $IGPATH) > $PIDFILE
            touch $LOCKFILE
        fi
    fi

    return $RETVAL
}

stop()
{
    log_begin_msg "Stopping Iguanaworks USB IR daemon..."
    if [ "$LOCKFILE" != "" ]; then
        killproc $IGPATH
    else
        start-stop-daemon --stop --oknodo --retry 2 --exec $IGPATH
    fi
    RETVAL=$?
    log_end_msg $RETVAL

    if [ "$LOCKFILE" != "" ]; then
        [ $RETVAL = 0 ] && rm -f $LOCKFILE $PIDFILE
    fi

    # remove any previous /var/run/iguanaIR if it's empty
	rmdir /var/run/iguanaIR 2>/dev/null

    return $RETVAL
}

restart()
{
    stop && start
}

usage()
{
    if [ "$LOCKFILE" != "" ]; then
        echo "Usage: $0 {start|stop|status|restart|condrestart|force-restart}"
    else
        echo "Usage: $0 {start|stop|status|restart|force-restart}"
    fi
    RETVAL=1
}


forcereload()
{
    restart
}



# See how we were called.
case "$1" in
    start)
# NOTE: was necessary on old versions of udev.... why? but now causes weird
#       keyboard and similar issues on F9 (and F8 has been reported)
#        if type udevtrigger > /dev/null 2>&1; then
#            udevtrigger
#        fi
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    force-reload)
        forcereload
        ;;
    status)
        if [ "$LOCKFILE" != "" ]; then
            status $IGPATH
        else
		if [ -z $(pidofproc $IGPATH) ]; then
		echo "IguanaIR Daemon is not running"
		else
		echo "IguanaIR Daemon is running (pid $(pidofproc $IGPATH) )"
		fi            
        fi
        ;;
    condrestart)
        if [ "$LOCKFILE" != "" ]; then
            [ -e $LOCKFILE ] && restart
        else
            usage
        fi
        ;;
    *)
        usage
        ;;
esac

exit $RETVAL
