#!/bin/sh
# chkconfig: 2345 55 45
# description: jukeboxd is the jukebox network server.

# jukebox - http://raf.org/jukebox/
#
# Copyright (C) 2002 raf <raf@raf.org>
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or visit http://www.gnu.org/copyleft/gpl.html

# jukeboxd-init.d - jukebox network server init script
#
# 20021208 raf <raf@raf.org>

# Source networking configuration (if redhat).

[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

# Check that networking is up (if redhat).

[ "${NETWORKING}" = "no" ] && exit 0

# Get the user to run as

juke_user="jukebox"
[ -r /etc/jukebox.conf ] && . /etc/jukebox.conf

die() { logger -t "`basename $0`" -p daemon.err -s $*; exit 1; }

# Locate the jukeboxd executable

check()
{
	jukeboxd=""
	for dir in /opt/jukebox/sbin /opt/jukebox/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin
	do
		[ -x "$dir/jukeboxd" ] && jukeboxd="$dir/jukeboxd" && return
	done

	[ -z "$jukeboxd" ] && die "Failed to find jukeboxd"
}

# Get the currently running jukeboxd pid(s)

pid="`ps auxwww | awk '/[p]erl -Tw .*jukeboxd/ { print $2 }'`"

start()
{
	[ -n "$pid" ] && die 'jukeboxd is already running'
	echo -n 'Starting: jukeboxd...'
	check
	/bin/su "$juke_user" -c "$jukeboxd" && echo done. || echo failed.
}

stop()
{
	[ -z "$pid" ] && die 'jukeboxd is not running'
	echo -n 'Stopping: jukeboxd...'
	kill -15 "$pid" && echo done. || echo failed.
	pid=""
}

status()
{
	[ -n "$pid" ] && echo "jukeboxd is up (pid $pid)"
	[ -z "$pid" ] && echo "jukeboxd is down"
}

help()
{
	name="`basename $0`"
	echo "NAME"
	echo
	echo "$name - jukebox network server init script"
	echo
	echo "SYNOPSIS"
	echo
	echo "  $name [options] command"
	echo "  options:"
	echo "    -h, --help    - Show the help message then exit"
	echo "    -V, --version - Show the version message then exit"
	echo
	echo "  commands:"
	echo "    start         - Start the jukebox network server"
	echo "    stop          - Stop the jukebox network server"
	echo "    status        - Show whether the jukebox server is up or down"
	echo "    restart       - Stop then start the jukebox network server"
	echo "    reload        - Stop then start the jukebox network server"
	echo
	echo "DESCRIPTION"
	echo
	echo "$name is the init/boot script that launches the jukebox"
	echo "network server every time the system boots. If \"make install-server\""
	echo "was executed from the jukebox source directory, then this script will"
	echo "start the jukebox daemon whenever the host boots up into run levels"
	echo "2, 3, 4 or 5 and stop the jukebox daemon in run levels 0 (halt),"
	echo "1 (single user) and 6 (reboot). At least on systems with SVR4 style"
	echo "boot sequences. On systems with BSD style boot sequences, this"
	echo "script will be used to just start the jukebox daemon whenever the"
	echo "system boots."
	echo
	echo "Note: This command must not be used if inetd or xinetd has been"
	echo "set up to run jukeboxd."
	echo
	echo "FILES"
	echo
	echo "  /etc/jukebox.conf - System wide configuration file"
	echo "  ~/.jukeboxrc      - User specific configuration file"
	echo
	echo "SEE ALSO"
	echo
	echo "  rip(1), riptrack(1), mktoc(1), toc2names(1), toc2tags(1),"
	echo "  cdr(1), cdrw(1), burn(1), burnw(1), cdbackup(1), mp3backup(1),"
	echo "  jukebox(1), jukeboxc(1), jukeboxc.jar(1), jukeboxd(8),"
	echo "  jukeboxd-init.d(8), jukebox.conf(5),"
	echo "  http://raf.org/jukebox/Jukebox-HOWTO"
	echo
	echo "AUTHOR"
	echo
	echo "raf <raf@raf.org>"
	echo
	exit 0
}

version() { echo "jukeboxd-init.d-0.1"; exit 0; }

# Execute argument

case "$1" in
	start)     start;;
	stop)      stop;;
	status)    status;;
	restart)   stop; start;;
	reload)    stop; start;;
	-h|--help) help;;
	-V|--version) version;;
	*)         echo $"Usage: $0 {start|stop|status|restart|reload}"; exit 1;;
esac

exit $?

# vi:set ts=4 sw=4
