#!/bin/bash
#
# alsasettings  This shell script takes care of restoring the ALSA channel
#		settings on 2.6 kernel systems.
#
# This script requires /usr/sbin/alsactl and /usr/bin/aconnect
# programs from the alsa-utils package.
# 
# This script was taken and modified from the ALSA utils package 
# and featured the original disclaimer below:
# 
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
#
# For RedHat 5.0+:
# chkconfig: 2345 98 12
# 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.
# 
# Heavily modified for Lunar Linux by Moritz Heiber <moe@lunar-linux.org>
#

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

alsactl=/usr/sbin/alsactl
asoundcfg=/etc/asound.state
aconnect=/usr/bin/aconnect
alsascrdir=/etc/alsa.d

function start() {

#
# restore driver settings
#
  if [ -d /proc/asound ]; then
    if [ ! -r $asoundcfg ]; then
      echo "No mixer config in $asoundcfg, you have to unmute your card!"
    else
      if [ -x $alsactl ]; then
        $alsactl -F -f $asoundcfg restore
      else
        echo -e "${rc_warning}ERROR: alsactl not found${rc_reset}"
      fi
    fi
  fi
#
# touch lockfile if lockdir exists
#
  if [ -d /var/lock/subsys ] ; then
    touch /var/lock/subsys/alsasettings
  fi

}

function stop() {
  #
  # store driver settings
  #
  if [ -x $alsactl ]; then
    $alsactl -f $asoundcfg store
  else
    echo -n -e "${rc_warning}!!!alsactl not found!!!${rc_reset} "
  fi
  #
  # mute master to avoid clicks at unload
  #

  /usr/bin/amixer set Master mute >/dev/null 2>&1

  #
  # remove lockfile if lockdir exists
  #
  if [ -d /var/lock/subsys ] ; then
    rm -f /var/lock/subsys/alsasettings
  fi
}

# See how we were called.
case "$1" in
  start)
        # Start driver if it isn't already up.
	if [ -d /proc/asound ]; then
	  echo -n "Restoring ALSA sound settings: "
	  start
	  echo -e "$rc_done"
	else
	  echo "ALSA driver is is not in the kernel or not running!"
	fi
        ;;
  stop)
        # Stop daemons.
	if [ -d /proc/asound ]; then
          echo -n "Storing ALSA sound settings: "
	  stop
 	  echo -e "$rc_done"
	fi
        ;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
        echo "Usage: alsasettings {start|stop|restart}"
        exit 1
esac

exit 0
