#!/bin/bash -e
#
# 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> 
#
# Slightly modified for Debian GNU/Linux by Wichert Akkerman.
#
#  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 RedHat 5.0+:
# chkconfig: 2345 87 14
# description: ALSA driver
#

alsactl=/usr/sbin/alsactl

function start() {
  #
  # insert all sound modules
  #
  # Note: configuration actually lives in /etc/modutils/alsa,
  # however, since modprobe knows nothing about that, we use
  # /etc/conf.modules instead.
    awk '/^alias +snd-card-[0-9]/ {print $3}' /etc/modules.conf | \
    while read line; do
      echo -n "Starting sound driver: $line "
      if /sbin/modprobe $line >/dev/null 2>&1; then
	echo "done."
      else
        echo "failed."; exit 0
      fi
    done
  #
  # restore driver settings
  #
  if [ -x $alsactl ]; then
    $alsactl restore >/dev/null 2>&1 || true
  else
    echo "alsactl not found, mixer settings will not be restored."
  fi
}

function detect_stop() {
  #
  # remove all sound modules
  #
  /sbin/lsmod | grep -E "^snd" | while read line; do \
     /sbin/modprobe -r `echo $line | cut -d ' ' -f 1` >/dev/null 2>&1 || true; \
  done
  # remove the 2.2 soundcore module (if possible)
  /sbin/modprobe -r soundcore >/dev/null 2>&1 || true
}

function stop() {
  #
  # store driver settings
  #
  if [ -x $alsactl ]; then
    $alsactl store >/dev/null 2>&1 || true
  else
    echo -n "alsactl not found "
  fi
  #
  # remove all sound modules
  #
  detect_stop
}

function detect_start() {
  #
  # run only detect module
  #
  /sbin/modprobe snd-detect >/dev/null 2>&1 || true
}

# See how we were called.
case "$1" in
  start)
        # Start driver.
	if [ ! -d /proc/asound ]; then
	  start
	else
	  if [ -f /proc/asound/detect ]; then
	    echo -n "Shutting down sound detect module: "
	    detect_stop
	    echo "done."
	    start
          else
	    echo "ALSA driver is already running."
	  fi
	fi
        ;;
  stop)
        # Stop daemons.
	if [ -d /proc/asound ]; then
          echo -n "Shutting down sound driver: "
	  if [ -f /proc/asound/detect ]; then
	    detect_stop
	  else
	    stop
	  fi
 	  echo "done."
	else
	  echo "ALSA driver isn't running."
	fi
        ;;
  restart)
	$0 stop
	$0 start
	;;
  force-reload)
  	$0 restart
	;;
  *)
        echo "Usage: /etc/init.d/alsa {start|stop|restart|force-reload}"
        exit 1
esac

exit 0
