#!/bin/bash
#
# start/stop axia driver.
#
#    LiveWire Driver for Advanced Linux Sound Architecture
#    Copyright (C) 2005 Telos Systems <support@axiaaudio.com>
#

### BEGIN INIT INFO
# Provides: axia-alsa
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Start the Axia/LiveWire Audio Driver
### END INIT INFO

#
# If the LSB Init functions exist, use them, otherwise roll our own
#
if test -f /lib/lsb/init-functions ; then
  . /lib/lsb/init-functions
#else
#  function log_success_msg() {
#    echo "$1 ... done."
#  }
#  function log_failure_msg() {
#    echo "$1 ... done."
#  }
fi

axialwrd_exec="/usr/sbin/axialwrd"
axialwrd_pid="/var/run/axialwrd.pid"
axiaadvd_exec="/usr/sbin/axiaadvd"
axiaadvd_pid="/var/run/axiaadvd.pid"
axiagpr_exec="/usr/sbin/axiagpr"
axiagpr_pid="/var/run/axiagpr.pid"
device="axia"
module="axialivewire"
PACKAGE_NAME="LiveWire Audio Driver"

case "$1" in
    start)
	if /sbin/modprobe snd-axia ; then
	    rm -f /dev/${device}0
	    major=$(awk "/axialivewire/ {print \$1}" /proc/devices)
	    mknod /dev/${device}0 c $major 0
	    log_success_msg "Starting $PACKAGE_NAME"
	    $axialwrd_exec
	    $axiaadvd_exec
	    $axiagpr_exec
	    exit 0
        else
	    log_failure_msg "Starting $PACKAGE_NAME"
	    exit 1
        fi
	;;
    stop)
	killall $axialwrd_exec
	killall $axiaadvd_exec
	killall $axiagpr_exec
	/sbin/rmmod snd_axia
	$0 silent-status
	EXIT_CODE=$?
	if test $EXIT_CODE -eq 0 ; then
	    log_failure_msg "Stopping $PACKAGE_NAME"
	    exit 1
	else
	    log_success_msg "Stopping $PACKAGE_NAME"
	    rm -f /dev/${device}0
	    exit 0
	fi
	;;
    restart)
	killall $axialwrd_exec
	killall $axiaadvd_exec
	killall $axiagpr_exec
	/sbin/rmmod snd_axia
	if /sbin/modprobe snd-axia ; then
	    rm -f /dev/${device}0
	    major=$(awk "/axialivewire/ {print \$1}" /proc/devices)
	    mknod /dev/${device}0 c $major 0
	    log_success_msg "Restarting $PACKAGE_NAME"
	    $axialwrd_exec
	    $axiaadvd_exec
	    $axiagpr_exec
	    exit 0
        else
	    log_failure_msg "Restarting $PACKAGE_NAME"
	    exit 1
        fi
	exit 0;
        ;;
    force-restart)
        $0 restart
        ;;
    status)
	$0 silent-status
	EXIT_CODE=$?
	if test $EXIT_CODE -eq 0 ; then
	    echo "$PACKAGE_NAME running."
	else 
	    echo "$PACKAGE_NAME stopped."
	fi
	exit $EXIT_CODE
	;;
    silent-status)
	if test `cat /proc/modules | grep -c snd_axia` -ne 0 ; then
	    if test -f $axialwrd_pid ; then
		if test -d /proc/$(cat $axialwrd_pid) ; then
		    if test -f $axiaadvd_pid ; then
			if test -d /proc/$(cat $axiaadvd_pid) ; then
		   	    if test -f $axiagpr_pid ; then
			        if test -d /proc/$(cat $axiagpr_pid) ; then
			            exit 0
			        fi
		            fi
			fi
		    fi
		fi
	    fi
	fi
	exit 3
	;;
    *)
	echo "Usage: $0 {start|stop|restart|force-restart|status}"
	exit 1
esac

exit 0

