#!/bin/sh
#
# alsasave	This shell script takes care of stopping ALSA sound driver.
#
# This script requires /usr/sbin/alsactl program from alsa-utils package.
#
# Copyright (c) by Jaroslav Kysela <perex@jcu.cz> 
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# For PLD Linux Distribution:
# chkconfig:	2345 80 14
# description:	ALSA driver
#

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

alsactl=/usr/sbin/alsactl

detect_stop()
{
  #
  # remove all sound modules
  #
  /sbin/lsmod | awk '/^snd/ { print $0 }' | while read line; do \
     /sbin/rmmod `echo $line | cut -d ' ' -f 1`; \
  done
}


driver_stop()
{
  #
  # store driver settings
  #
  if [ -x $alsactl ]; then
    $alsactl store
  else
    show '!!!alsactl not found!!!'; fail
  fi
}

# See how we were called.
case "$1" in
  start)
	#Not Needed, its for udev
	;;
  stop)
	# Stop daemons.
	if [ -d /proc/asound ]; then
		show "Shutting down sound driver:"
		busy
		if [ -f /proc/asound/detect ]; then
			detect_stop
		else
			driver_stop
		fi
		(rmmod isapnp; rmmod soundcore) 2> /dev/null
 		ok
	else
		msg_not_running "ALSA driver"
	fi
	;;
  save)
	driver_stop
	;;
  status)
	#TODO
	;;
  *)
	msg_usage "$0 {start|stop|save|status}"
	exit 3
esac

exit 0
