#! /bin/sh
#
# Startup script for the Linux Audio Backstop daemon
#
#   (C) Copyright 1996-2004 Fred Gleason <fredg@paravelsystems.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as
#   published by the Free Software Foundation; either version 2 of
#   the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#

### BEGIN INIT INFO
# Provides:          labd
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Description:       Start the Linux Audio Backstop capture/playback daemon
### END INIT INFO

. /lib/lsb/init-functions

#
# Package Definitions
#
PACKAGE_BIN=/usr/sbin/labd
PACKAGE_CONFIG=/etc/lab.conf
PACKAGE_PID=/var/run/labd.pid
PACKAGE_NAME="Linux Audio Backstop daemon"

# Check for missing binaries
if [ ! -x $PACKAGE_BIN ] ; then
  echo "$PACKAGE_BIN not installed"
  exit 5
fi

# Check for existence of needed config file and read it
if [ ! -r $PACKAGE_CONFIG ] ; then
  echo "Missing $PACKAGE_CONFIG"
  exit 6}
fi

case "$1" in
    start)
	if test ! -f $PACKAGE_PID ; then
	    $PACKAGE_BIN
	    sleep 1
	else 
	    if test ! -d /proc/`cat $PACKAGE_PID` ; then
		$PACKAGE_BIN
		sleep 1
	    fi
	fi
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_success_msg "Starting $PACKAGE_NAME"
		exit 0
	    else
		log_failure_msg "Starting $PACKAGE_NAME"
		exit 1
	    fi
	else
	    log_failure_msg "Starting $PACKAGE_NAME"
	    exit 1
	fi
	;;
    stop)
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		kill `cat $PACKAGE_PID`
		sleep 1
	    fi
	fi
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_failure_msg "Shutting down $PACKAGE_NAME"
		exit 1
	    else
		log_success_msg "Shutting down $PACKAGE_NAME"
		exit 0
	    fi
	else
	    log_success_msg "Shutting down $PACKAGE_NAME"
	    exit 0
	fi
	;;
    restart)
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		kill `cat $PACKAGE_PID`
		sleep 1
	    fi
	fi
	$PACKAGE_BIN
	sleep 1
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_success_msg "Restarting $PACKAGE_NAME"
		exit 0
	    else
		log_success_msg "Restarting $PACKAGE_NAME"
		exit 1
	    fi
	else
	    echo "failed."
	    exit 1
	fi
	;;
    force-reload)
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		kill `cat $PACKAGE_PID`
		sleep 1
	    fi
	fi
	$PACKAGE_BIN
	sleep 1
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_success_msg "Reloading $PACKAGE_NAME"
		exit 0
	    else
		log_failure_msg "Restarting $PACKAGE_NAME"
		exit 1
	    fi
	else
	    log_failure_msg "Restarting $PACKAGE_NAME"
	    exit 1
	fi
	;;
    status)
	echo -n "Checking for $PACKAGE_NAME..."
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		echo "running."
		exit 0
	    else
		echo "stale PID file."
		exit 1
	    fi
	else
	    echo "stopped."
	    exit 3
	fi
	;;
esac


# End of rc.labd
