#!/bin/dash
# Description: bluealsa (bluez-alsa) dmix daemon (bmixd)
#

PIDFILE=/var/run/bmix.pid

start() {
	# load snd-aloop if not loaded
	! lsmod | grep -q snd_aloop && modprobe snd-aloop

	bmixd.bash &
	echo $! > $PIDFILE
}

stop() {
	if [ -e $PIDFILE ]; then
		read pid < $PIDFILE
		kill $pid
		rm -f $PIDFILE
	fi	
}

is_up() {
	if [ -e $PIDFILE ]; then
		read pid < $PIDFILE
		kill -0 $pid
	else
		return 1
	fi
}

case "$1" in
	start)
		! is_up &&
		start
		;;
		
	stop)
		stop
		;;
	
	reload|restart)
		stop
		sleep 1
		start
		;;
		
	status)
		is_up && echo "bmixd is running" || echo "bmixd is stopped."
		;;
esac
