#!/bin/sh
#
# cdemu-daemon: CDEmu userspace daemon
#
# chkconfig:    345 92 02 
# description:  This is CDEmu userspace daemon that controls CDEmu block devices \
#               and services device requests that are passed from kernel space
#
# processname:  cdemud
# pidfile:      /var/run/cdemud.pid
# config:       /etc/sysconfig/cdemu-daemon
#

# so we can rearrange this easily
processname=/usr/bin/cdemud
servicename=cdemu-daemon

# Sanity checks.
if [ ! -x $processname ]; then
    echo "Daemon binary $processname not found!"
    exit 1
fi

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

# Source config
if [ -f /etc/sysconfig/cdemu-daemon ] ; then
    . /etc/sysconfig/cdemu-daemon
fi

RETVAL=0

start() {
    # Load module
    if [ -n "$MODULE" ]; then
        echo -n $"Inserting kernel module ($MODULE): "
        /sbin/modprobe $MODULE >/dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL = 0 ]; then
            success $"$base startup"
            echo
        else
            failure $"$base startup"
            echo
            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
        success $"$base startup"
        echo
    else 
        failure $"$base startup"
        echo
        return $RETVAL
    fi
    
    # Daemon arguments
    DAEMON_ARGS="--daemonize"
    
    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: "
    start_daemon -- $processname $DAEMON_ARGS
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        touch /var/lock/subsys/$servicename
    else
        return $RETVAL
    fi
    
    return $RETVAL
}

stop() {        
    stop_daemon -- $processname 
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
        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
            success $"$base shutdown"
            echo
        else
            failure $"$base shutdown"
            echo
            return $RETVAL
        fi
    fi
    
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/$servicename
    fi
    
    return $RETVAL
}

restart() {
    stop
    start
}

# See how we were called.
case "$1" in
    start)
        start
        RETVAL=$?
        ;;
    stop)
        stop
        RETVAL=$?
        ;;
    status)
        status $processname
        RETVAL=$?
        ;;
    restart)
        restart
        ;;
    condstop)
        if [ -e /var/lock/subsys/$servicename ]; then
          stop
        fi
        ;;
    condrestart)
        if [ -e /var/lock/subsys/$servicename ]; then
          restart
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        ;;
esac
exit $RETVAL
