#!/bin/sh
#
# cdemu-daemon: CDEmu userspace daemon
#
# chkconfig:    345 98 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: "
    daemon --check $servicename $processname $DAEMON_ARGS >/dev/null 2>&1
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        success $"$base startup" 
        touch /var/lock/subsys/$servicename
        echo
    else
        failure $"$base startup"
        echo
        return $RETVAL
    fi
    
    return $RETVAL
}

stop() {        
    # Kill daemon with 'cdemud -k'
    echo -n $"    Killing daemon: "
    $processname -k >/dev/null 2>&1
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        success $"$base shutdown"
        echo
    else
        failure $"$base shutdown"
        echo
        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
}

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