#!/bin/sh
#
# icecast	This shell script takes care of starting and stopping icecast
#
# chkconfig:	345 96 24
# description:	Icecast is an Internet audio broadcasting system based on \
#		MPEG audio technology.

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

# Get network config
. /etc/sysconfig/network


# Get service config - may override defaults
[ -f /etc/sysconfig/icecast ] && . /etc/sysconfig/icecast

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down icecast
		exit 1
	fi
else
	exit 0
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/icecast ]; then
		msg_starting icecast
		busy
		# Our daemon function doesn't have --shell option!
		su icecast -s /bin/sh -c 'setsid icecast -c /etc/icecast/icecast.xml &' > /dev/null
		RETVAL=$?
		[ $RETVAL -eq 0 ] && ok || fail
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/icecast
	else
		msg_already_running Icecast
	fi
	;;
  stop)
	# Stop daemons.
	if [ -f /var/lock/subsys/icecast ]; then
		msg_stopping icecast
		killproc icecast
		rm -f /var/lock/subsys/icecast >/dev/null 2>&1
	else
		msg_not_running Icecast
	fi	
	;;
  restart)
	$0 stop
	$0 start
	;;
  reload|force-reload)
	if [ -f /var/lock/subsys/icecast ]; then
		msg_reloading Icecast
		killproc icecast -HUP
		RETVAL=$?
	else
		msg_not_running Icecast >&2
		exit 7
	fi
	;;
  status)
	status icecast
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
