#!/bin/bash

# chkconfig: 345 85 15
# description: Saves and restores sound card mixer settings at \
#	       boot time and shutdown.

HOME=/etc

case "$1" in
  start)
	echo -n "Starting sound configuration: "

	# loads mixer settings from /etc/.aumixrc
	if [ -f /etc/.aumixrc ]; then
  	  cat /proc/devices | grep -q "\(sparcaudio\|sound\)"
	  if [ $? = 0 ]; then  
	      /usr/bin/aumix -f /etc/.aumixrc -L > /dev/null
	  fi
	fi
	
	echo -n sound
	echo
	touch /var/lock/subsys/sound
	;;
  stop)
	echo -n "Saving sound configuration: "

	# Save mixer settings to /etc/.aumixrc
  	cat /proc/devices | grep -q "\(sparcaudio\|sound\)"
	if [ $? = 0 ]; then
	    /usr/bin/aumix -f /etc/.aumixrc -S
	fi

	rm -f /var/lock/subsys/sound
	echo -n sound
	echo
	;;
  restart)
	$0 stop
        $0 start
	;;
  status)
  	cat /proc/devices | grep -q "\(sparcaudio\|sound\)"
	if [ $? = 0 ]; then
	    lsmod | grep -q "\(sound\|audio\)"
	    if [ $? = 0 ]; then
		echo "Modular sound card detected."
	    else
		echo "Monolithic sound card detected."
	    fi
 
	  cat /dev/sndstat 2> /dev/null | grep -A 1 "Midi devices:" | grep -q [0-9]:
	  if [ $? = 0 ]; then
	    echo "MIDI device present."
	  else
	    cat /dev/sndstat >/dev/null 2>&1
	    if [ $? = 0 ]; then
	    	echo "MIDI device not detected."
	    fi
	  fi	

	else
	  echo "Sound card not configured."
	fi
	;;
  *)
	echo "Usage: sound {start|stop|status|restart}"
	exit 1
esac

exit 0
