#!/bin/sh
#
# icecast:       Starts the Icecast media streamer
#
# chkconfig: 2345 90 10
# description: Starts and stops the Icecast media streamer at boot time and shutdown.
#
# processname: icecast
# config: /etc/icecast/icecast.conf
# hide: true
### BEGIN INIT INFO
# Provides: icecast
# Required-Start: $local_fs $network $remote_fs
# Required-Stop:  $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: start and stop Icecast
# Description:  Icecast is an Internet based broadcasting system 
#		based on the Mpeg Layer III streaming technology.
#		It is, however, not limited to streaming mp3 files.
### END INIT INFO

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

CONFFILE=/etc/icecast.xml
PIDFILE=/var/run/icecast/icecast.pid
LOCKFILE=/var/lock/subsys/icecast
RETVAL=0

#===== adjust chroot environment ======
adjust()
{
    if [ -f "$CONFFILE" ] && grep -q '<chroot>1</chroot>' "$CONFFILE"; then
        action "Adjusting environment for icecast:" /etc/chroot.d/icecast.all || exit
    fi
}

start()
{
	adjust
	echo -n "Starting Icecast media streamer: "
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user icecast -- icecast -b -c "$CONFFILE"
	RETVAL=$?
	echo	
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	return $RETVAL
}

stop()
{
	echo -n "Shutting down Icecast media streamer: "
        stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user icecast -- icecast
		
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	adjust
	echo -n "Reloading Icecast media streamer configuration: "
	stop_daemon --pidfile "$PIDFILE" --expect-user icecast -HUP icecast
	RETVAL=$?
	echo
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status icecast
		RETVAL=$?
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|status|reload|restart|condstop|condrestart}"
		RETVAL=1
esac

exit $RETVAL
