#!/bin/sh
#
# chkconfig: 345 35 56
# description: Starts and stops the icecast daemon which provides \
# streaming MP3 support.

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

RETVAL=0

start() {
	export TMPDIR="/var/tmp"
	gprintf "Starting icecast: "
	daemon icecast -c /etc/icecast/icecast.xml &
	RETVAL=$?;
	echo

	if [ -f /usr/bin/ices-lame ]; then
		gprintf "Starting ices-lame: "
		daemon ices-lame -c /etc/ices.conf &
		RETVAL=$?
		echo
	elif [ -f /usr/bin/ices ]; then
		gprintf "Starting ices: "
		daemon ices -c /etc/ices.conf &
		RETVAL=$?
		echo
	fi
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/icecast || \
	   RETVAL=1
	return $RETVAL
}	
stop() {
	if [ -f /usr/bin/ices-lame ]; then
		gprintf "Shutting down ices-lame: "
		killproc ices-lame
		RETVAL=$?;
		echo
	elif [ -f /usr/bin/ices ]; then
		gprintf "Shutting down ices: "
		killproc ices
		RETVAL=$?;
		echo
	fi

	gprintf "Shutting down icecast: "
	killproc icecast
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/icecast
	return $RETVAL
}	

restart() {
	stop
	start
}	

reload() {
	export TMPDIR="/var/tmp"

        gprintf "Reloading icecast: "
	killproc icecast -HUP
	RETVAL=$?
	echo

        gprintf "Reloading ices: "
	killproc ices -HUP	
	RETVAL=$?
	echo

	return $RETVAL
}

mdkstatus() {
	status icecast
}	

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  reload)
  	reload
	;;
  status)
  	mdkstatus
	;;
  condrestart)
  	[ -f /var/lock/subsys/icecast ] && restart || :
	;;
  *)
	gprintf "Usage: %s {start|stop|restart|status|condrestart}\n" "$0"
	exit 1
esac

exit $?
