#!/bin/bash
#
# alsasound     This shell script takes care of starting and stopping
#               the ALSA sound driver.
#
# This script requires /usr/sbin/alsactl program from alsa-utils package.
#
# Copyright (c) by Jaroslav Kysela <perex@suse.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 RedHat 5.0+:
# chkconfig: 2345 87 14
# description: ALSA driver
#
# modified to visually fit into SuSE 6.0+ by Philipp Thomas <pthomas@suse.de>
# further improvements by Bernd Kaindl, Olaf Hering and Takashi Iwai.
# 
#

if [ -r /etc/rc.config ]; then
  . /etc/rc.config
  rc_warning="\033[m33m\033[1m"
else
  rc_done="done"
  rc_warning=""
  rc_reset=""
fi

config=/etc/modules.conf
alsactl=/usr/sbin/alsactl
asoundcfg=/etc/asound.conf

if [ ! -r $config ]; then
  if [ -r /etc/conf.modules ]; then
    config=/etc/conf.modules
  fi
fi

function start() {
  #
  # insert all sound modules
  #
  cat $config | \
    grep -E "^alias( |\t)+snd-card-[[:digit:]]" | \
    awk '{print $3}' | \
    while read line; do \
      echo -n "Starting sound driver: $line "; \
      /sbin/modprobe $line; \
      echo -e "$rc_done"; \
    done
  #
  # restore driver settings
  #
  if [ -d /proc/asound ]; then
    if [ ! -r /etc/asound.conf ]; then
      echo "No mixer config in /etc/asound.conf, you have to unmute your card!"
    else
      if [ -x $alsactl ]; then
        $alsactl restore
      else
        echo -e "${rc_warning}ERROR: alsactl not found${rc_reset}"
      fi
    fi
  fi
}

function stop() {
  #
  # store driver settings
  #
  if [ -x $alsactl ]; then
    $alsactl store
  else
    echo -n -e "${rc_warning}!!!alsactl not found!!!${rc_reset} "
  fi
  #
  # remove all sound modules
  #
  /sbin/lsmod | grep -E "^snd" | fgrep -v "snd-rme9652_mem" | while read line; do \
     /sbin/rmmod `echo $line | cut -d ' ' -f 1`; \
  done
  # remove the 2.2 soundcore module (if possible)
  /sbin/rmmod soundcore 2> /dev/null
}

# See how we were called.
case "$1" in
  start)
        # Start driver if it isn't already up.
	if [ ! -d /proc/asound ]; then
	  start
	  if [ -d /proc/asound ] && [ -d /var/lock/subsys ]; then
	    touch /var/lock/subsys/alsasound
	  fi
	else
	  echo "ALSA driver is already running."
	fi
        ;;
  stop)
        # Stop daemons.
	if [ -d /proc/asound ]; then
          echo -n "Shutting down sound driver: "
	  stop
	  if [ -d /var/lock/subsys ]; then
	    rm -f /var/lock/subsys/alsasound
	  fi
 	  echo -e "$rc_done"
	fi
        ;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        echo "Usage: alsasound {start|stop|restart}"
        exit 1
esac

exit 0
