#!/bin/sh
#
# aumix		Sound mixer setup.
#
# chkconfig:	345 85 15
#
# description:	Saves sound mixer's settings on system shutdown and restores \
#		them on system startup.

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

[ -f /etc/sysconfig/aumix ] && . /etc/sysconfig/aumix

start() {
	if [ ! -f /var/lock/subsys/aumix ]; then
		show "Setting up sound card mixer"
		busy
		if [ "$AUMIX_AUTO_INIT" = "yes" ]; then
			aumix -f /etc/aumixrc -L >/dev/null
			RETVAL=$?
		fi
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/aumix && ok || fail
	else
		nls "Sound card mixer is already set up"
	fi
}

stop() {
	if [ -f /var/lock/subsys/aumix ]; then
		show "Saving sound card mixer's settings"
		busy
		if [ "$AUMIX_AUTO_SAVE" = "yes" ]; then
			aumix -f /etc/aumixrc -S
		fi
		rm -f /var/lock/subsys/aumix >/dev/null 2>&1
		ok
	else
		nls "Sound card mixer hasn't been set up"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	if [ -f /var/lock/subsys/aumix ]; then
		stop
		start
	else
		nls "Sound card mixer hasn't been set up"
	fi
	;;
  save)
	run_cmd "Saving sound card mixer's settings" aumix -f /etc/aumixrc -S
	;;
  restore|reload|force-reload)
	show "Setting up sound card mixer"
	busy
	aumix -f /etc/aumixrc -L >/dev/null
	RETVAL=$?
	[ $RETVAL -eq 0 ] && ok || fail
	;;
  status)
	cat /proc/devices | grep -q "\(sparcaudio\|sound\)"
	if [ $? = 0 ]; then
		lsmod | grep -q "\(sound\|audio\)"
		if [ $? = 0 ]; then
			nls "Modular sound card detected."
		else
			nls "Monolithic sound card detected."
		fi

		cat /dev/sndstat 2> /dev/null | grep -A 1 "Midi devices:" | grep -q [0-9]:
		if [ $? = 0 ]; then
			nls "MIDI device present."
		else
			cat /dev/sndstat >/dev/null 2>&1
			if [ $? = 0 ]; then
				nls "MIDI device not detected."
			fi
		fi

	else
		nls "Sound card not configured."
	fi
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|save|restore|status}"
	exit 3
esac

exit $RETVAL
