#!/bin/bash
#
# logitechmediaserver	Startup script for the Logitech Media Server
#
# chkconfig:		345 80 30
# description:		Logitech Media Server powers the Squeezebox, Transporter and SLIMP3 network music \
#			players and is the best software to stream your music to any software MP3 \
#			player. It supports MP3, AAC, WMA, FLAC, Ogg Vorbis, WAV and more! \
#			As of version 7.7 it also supports UPnP clients, serving pictures and movies too!
# processname:		squeezeboxserver
# config: 		/etc/squeezeboxserver/squeezeboxserver.conf
# config:		/etc/sysconfig/squeezeboxserver

#
### BEGIN INIT INFO
# Provides:		logitechmediaserver
# Required-Start:	$syslog $remote_fs
# Should-Start:		$time ypbind sendmail
# Required-Stop:	$syslog $remote_fs
# Should-Stop:		$time ypbind sendmail
# Default-Start:	3 5
# Default-Stop:		0 1 2 6
# Short-Description:	Startup script for the Logitech Media Server
# Description:		Logitech Media Server powers the Squeezebox, Transporter and SLIMP3 network music \
#			players and is the best software to stream your music to any software MP3 \
#			player. It supports MP3, AAC, WMA, FLAC, Ogg Vorbis, WAV and more! \
#			As of version 7.7 it also supports UPnP clients, serving pictures and movies too!
### END INIT INFO
# 

if [ -f /etc/redhat-release ] ; then

	# Source function library.
	. /etc/rc.d/init.d/functions
	# Source networking configuration.
	. /etc/sysconfig/network
	# Check that networking is up.
	[ ${NETWORKING} = "no" ] && exit 0

	# Check for existence of needed config file and read it
	SQUEEZEBOX_CONFIG=/etc/sysconfig/squeezeboxserver
	test -r $SQUEEZEBOX_CONFIG || { echo "$SQUEEZEBOX_CONFIG not existing";
		if [ "$1" = "stop" ]; then exit 0;
		else exit 6; fi; }

	# Read config	
	. $SQUEEZEBOX_CONFIG

	# Check for missing binaries (stale symlinks should not happen)
	# Note: Special treatment of stop for LSB conformance
	SQUEEZEBOX_BIN="$SQUEEZEBOX_HOME/squeezeboxserver"
	test -x $SQUEEZEBOX_BIN || { echo "$SQUEEZEBOX_BIN not installed"; 
		if [ "$1" = "stop" ]; then exit 0;
		else exit 5; fi; }

	LOCKFILE="/var/lock/subsys/squeezeboxserver"
	RETVAL=0

	start() {
		echo -n "Starting Squeezebox Server: "
		daemon --user $SQUEEZEBOX_USER $SQUEEZEBOX_BIN $SQUEEZEBOX_ARGS
		RETVAL=$?
		echo
		[ $RETVAL -eq 0 ] && touch $LOCKFILE
		return $RETVAL
	}

	stop() {
		echo -n "Stopping Squeezebox Server: "
		# Support old versions of RHEL and still fix bug 5620
		KILLPROC_ARGS=""
		if [ `grep -c 'Usage: killproc.*-d delay' /etc/init.d/functions` -ge 1 ]; then
			KILLPROC_ARGS="-d 6"
		fi
		killproc $KILLPROC_ARGS $SQUEEZEBOX_BIN
		RETVAL=$?
                if [ $RETVAL -ne 0 ]; then
                        # If killproc fails, try killall (fix for RHEL4)
                        killall squeezeboxserver
                        RETVAL=$?
                        [ $RETVAL -eq 0 ] && echo_success
                        [ $RETVAL -ne 0 ] && echo_failure
                fi
		echo
		[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
		return $RETVAL
	}

	restart() {
		stop
		sleep 5
		start
	}

	condrestart() {
		[ -f $LOCKFILE ] && restart || :
	}

	fdr_status() {
        	status $SQUEEZEBOX_BIN
       		RETVAL=$?
	}

elif [ -f /etc/SuSE-release ] ; then

	# Check for existence of needed config file and read it
	SQUEEZEBOX_CONFIG=/etc/sysconfig/squeezeboxserver
	test -r $SQUEEZEBOX_CONFIG || { echo "$SQUEEZEBOX_CONFIG not existing";
		if [ "$1" = "stop" ]; then exit 0;
		else exit 6; fi; }

	# Read config	
	. $SQUEEZEBOX_CONFIG

	# Check for missing binaries (stale symlinks should not happen)
	# Note: Special treatment of stop for LSB conformance
	SQUEEZEBOX_BIN="$SQUEEZEBOX_HOME/squeezeboxserver"
	test -x $SQUEEZEBOX_BIN || { echo "$SQUEEZEBOX_BIN not installed"; 
		if [ "$1" = "stop" ]; then exit 0;
		else exit 5; fi; }

	# Source rc.status and reset
	. /etc/rc.status
	rc_reset

	start() {
		echo -n "Starting Squeezebox Server: "
		export HOME=$SQUEEZEBOX_HOME
		startproc -u $SQUEEZEBOX_USER $SQUEEZEBOX_BIN $SQUEEZEBOX_ARGS
		rc_status -v
	}

	stop() {
		echo -n "Stopping Squeezebox Server: "
		killproc -TERM $SQUEEZEBOX_BIN
		rc_status -v
	}

	restart() {
		$0 stop
		$0 start
		rc_status
	}

	condrestart() {
		$0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset
		fi
		rc_status
	}

	fdr_status() {
		/sbin/checkproc $SQUEEZEBOX_BIN
		rc_status -v
	}

else
	exit 1
fi

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload|force-reload)
		restart
		;;
	condrestart|try-restart)
		condrestart
		;;
	status)
       		fdr_status
		;;
	*)
		echo "Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
		exit 1
esac

[ -f /etc/redhat-release ] && exit $RETVAL
[ -f /etc/SuSE-release ] && rc_exit

