#!/bin/sh
#
# cdemu-daemon: CDEmu userspace daemon
#
# /etc/init.d/cdemu-daemon
#
#   and symbolic its link
#
# /usr/sbin/rccdemu-daemon
#
### BEGIN INIT INFO
# Provides: cdemu-daemon
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the cdemu-daemon
### END INIT INFO

CDEMUD_BIN=/usr/bin/cdemud
test -x $CDEMUD_BIN || exit 5

CDEMUD_SYSCONFIG=/etc/sysconfig/cdemu-daemon
test -r $CDEMUD_SYSCONFIG || exit 6
. $CDEMUD_SYSCONFIG

CDEMUD_PIDFILE=/var/run/cdemud.pid

. /etc/rc.status

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset


start() {
    # Load module
    if [ -n "$MODULE" ]; then
        echo -n $"    Inserting kernel module ($MODULE): "
        /sbin/modprobe $MODULE >/dev/null 2>&1
        RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		:
	else
		return $RETVAL
	fi
    fi
    
    # Wait until control device is created...
    if [ -n "$CTL_DEVICE" ]; then
        echo -n "    Waiting for $CTL_DEVICE to be created: "
        until [ -c "$CTL_DEVICE" ]; do
            echo -n ""
        done
    fi
    
    RETVAL=0
    if [ $RETVAL -eq 0 ] ; then
    	:
    else
	return $RETVAL
    fi
    
    # Daemon arguments
    DAEMON_ARGS="-b system -l /var/log/cdemud.log"
    
    if [ -n "$DEVICES" ]; then
        DAEMON_ARGS="$DAEMON_ARGS --num-devices=$DEVICES"
    fi
    if [ -n "$CTL_DEVICE" ]; then
        DAEMON_ARGS="$DAEMON_ARGS --ctl-device=$CTL_DEVICE"
    fi
    if [ -n "$AUDIO_BACKEND" ]; then
        DAEMON_ARGS="$DAEMON_ARGS --audio-driver=$AUDIO_BACKEND"
    fi
    
    # Start daemon
    echo -n "    Starting daemon: "
    startproc -f $CDEMUD_BIN $DAEMON_ARGS
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        touch /var/lock/subsys/cdemu-daemon
    else
        return $RETVAL
    fi
    
    return $RETVAL
}

stop() {        
    # Kill daemon with 'cdemud -k'
    echo -n "    Killing daemon: "
    killproc -TERM $CDEMUD_BIN
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
	:
    else
	return $RETVAL
    fi	
    
    # Unload module
    if [ -n "$MODULE" ]; then
        echo -n "    Removing kernel module ($MODULE): "
        /sbin/rmmod $MODULE >/dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
	    :
        else
            return $RETVAL
        fi
    fi
    
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/cdemu-daemon
    fi
    
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
        echo -n "Starting CDEmu daemon: "
        start
        rc_status -v
        ;;
    stop)
        echo -n "Stopping CDEmu daemon: "
        stop
        rc_status -v
        ;;
    status)
	checkproc  $CDEMUD_BIN
        rc_status -v
        ;;
    restart)
	$0 stop
	$0 start
	rc_status
        ;;
    condrestart)
        if [ -f /var/lock/subsys/cdemu-daemon ]; then
            echo "Stopping CDEmu daemon: "
            stop
            RETVAL=$?
            if [ $RETVAL -eq 0]; then
                sleep 3
                echo "Starting CDEmu daemon: "
                start
            fi
        fi
	rc_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        ;;
esac
rc_exit
