#!/bin/sh
#
# pulseaudio	PulseAudio system-wide sound server	
#
# chkconfig:	345 86 13
#
# description:	System mode startup script for the PulseAudio sound server.
#
# processname:	pulseaudio
# config:	/etc/sysconfig/pulseaudio
# pidfile:	/var/run/pulse/pid
#
# $Id$

# Set defaults
PULSEAUDIO_SYSTEM_START=0
DISALLOW_MODULE_LOADING=1

# Get service config - may override defaults
[ -f /etc/sysconfig/pulseaudio ] && . /etc/sysconfig/pulseaudio
[ "$PULSEAUDIO_SYSTEM_START" != "1" ] && exit 0

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

# Get network config
. /etc/sysconfig/network

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/pulseaudio ]; then
		msg_already_running "PulseAudio"
		return
	fi

	msg_starting "PulseAudio"
	daemon /usr/bin/pulseaudio --system --daemonize --high-priority --log-target=syslog --disallow-module-loading=$DISALLOW_MODULE_LOADING
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pulseaudio
}

stop() {
	if [ ! -f /var/lock/subsys/pulseaudio ]; then
		msg_not_running "PulseAudio"
		return
	fi

	# Stop daemons.
	msg_stopping "PulseAudio"
	killproc --pidfile /var/run/pulse/pid pulseaudio -TERM
	rm -f /var/lock/subsys/pulseaudio
}

reload() {
	if [ ! -f /var/lock/subsys/pulseaudio ]; then
		msg_not_running "PulseAudio"
		RETVAL=7
		return
	fi

	msg_reloading "PulseAudio"
	killproc --pidfile /var/run/pulse/pid pulseaudio -HUP
	RETVAL=$?
}

condrestart() {
	if [ ! -f /var/lock/subsys/pulseaudio ]; then
		msg_not_running "PulseAudio"
		RETVAL=$1
		return
	fi

	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload)
  	reload
	;;
  status)
	status pulseaudio
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
