#!/bin/sh
#
# alsasound	This shell script takes care of starting and 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 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

  # restore driver settings
  #
  if [ -x /usr/sbin/alsactl ]; then
    if [ -f /etc/asound.state ]; then
      if [ "$(kernelver)" -lt "002006012" ]; then
	for i in 1 2 3 4; do
	  [ -a /dev/snd/controlC0 ] && break
	  sleep 1
	done
      fi
      /usr/sbin/alsactl restore
    fi
  else
    show "ERROR: alsactl not found"; fail
  fi
}

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

driver_stop()
{
  #
  # store driver settings
  #
  if [ -x /usr/sbin/alsactl ]; then
    /usr/sbin/alsactl store
  else
    show '!!!alsactl not found!!!'; fail
  fi
  #
  # 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
}

# 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
}


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

exit 0
