#!/bin/sh
#
# alsasound	This shell script takes care of starting and stopping \
#		ALSA sound driver.
#
# 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 87 14
# description:	ALSA driver
#

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

driver_start()
{
  #
  # insert all sound modules
  #
  modprobe -c | awk '$1 == "alias" && $3 != "off" && ($2 ~ /^snd-card-[0-9]$/) {print $3}' | \
    while read line; do \
      msg_starting "sound driver: $line"
      busy
      /sbin/modprobe $line
      ok
    done
  modprobe -c | awk '$1 == "alias" && $3 != "off" && ($2 ~ /^sound-service-[0-9]-[0-9]+$/) {print $3}' | \
    while read line; do \
      msg_starting "sound driver: $line"
      busy
      /sbin/modprobe $line
      ok
    done
}

detect_stop()
{
  #
  # remove all sound modules
  #
  /sbin/lsmod | awk '/^(snd|ac97_bus)/ { print $1 }' | while read module; do \
     /sbin/rmmod $module
  done
}

driver_stop()
{
  #
  # remove all sound modules
  #
  detect_stop
}

detect_start()
{
  #
  # run only detect module
  #
  /sbin/modprobe snd-detect
}

# Start driver.
start() {
	if [ ! -d /proc/asound ]; then
		driver_start
		if [ -d /proc/asound ]; then
			touch /var/lock/subsys/alsasound
		else
			exit 1
		fi
	else
		if [ -f /proc/asound/detect ]; then
			show "Shutting down sound detect module"
			detect_stop
			ok
			driver_start
			if [ -d /proc/asound ]; then
				touch /var/lock/subsys/alsasound
			else
				exit 1
			fi
		else
			msg_already_running "ALSA driver"
		fi
	fi
	# "restore" is handled via udev if running; if not, do it manually
	if is_no "$START_UDEV" && [ -f /var/lib/alsa/asound.state ]; then
		sleep 1
		/sbin/alsactl restore
	fi
}

# Stop daemons.
stop() {
	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
		if [ -d /var/lock/subsys ]; then
			rm -f /var/lock/subsys/alsasound
		fi
 		ok
	else
		msg_not_running "ALSA driver"
	fi
}

condrestart() {
	if [ -f /var/lock/subsys/alsasound ]; then
		stop
		start
	else
		msg_not_running "ALSA driver"
		RETVAL=$1
	fi
}


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

exit 0
