#!/bin/bash
#
#	/etc/rc.d/init.d/vortexbox
#
# Starts the VortexBox auto-ripping daemon
#
# chkconfig: 345 99 10
# description: runs the VortexBox auto-ripping daemon
# processname: vortexbox

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

# Eject the disc if one is in the drive
CDDEVICE=`/opt/vortexbox/get-cddevice`
CDSTATUS=`/opt/vortexbox/checkcd $CDDEVICE`
if [ ! "$CDSTATUS" = "nodisk" ]; then
	/usr/sbin/eject
fi

start() {
    # Check if it is already running
	echo -n $"Starting xo|one auto-ripping daemon: "
    if [ ! `pgrep -f docd.sh` ]; then
	 daemon daemonize /opt/vortexbox/docd.sh
	 echo
	else
	 status
	fi 
}

stop() {
	echo -n $"Stopping xo|one auto-ripping daemon: "
	echo_success
	for i in `ps ax | grep -v grep | grep ripit| cut -c 1-6`
	 do
	  kill $i
	 done
	killproc docd.sh	 
	echo
}

checkstatus() {
	if [ ! `pgrep -f docd.sh` ]; then
	 echo -n $"xo|one auto-ripping daemon is stopped"
	 echo
	else
	  echo "xo|one auto-ripping daemon (pid `pgrep -f docd.sh`) is running..."
	fi 
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
status)
	checkstatus
	;;
*)
	echo $"Usage: $0 {start|stop|status}"
	exit 1
esac

exit 0