#!/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 Polish Linux Distribution:
# chkconfig: 2345 87 14
# description: ALSA driver
#

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

alsactl=/usr/sbin/alsactl

if [ -r /etc/modules.conf ]; then
MODULES_CONF=/etc/modules.conf
else
MODULES_CONF=/etc/conf.modules
fi

driver_start()
{
  #
  # insert all sound modules
  #
  if OLDLOGLEV=$(awk '{print $1}' < /proc/sys/kernel/printk 2> /dev/null); then
  	/sbin/loglevel 1
  fi
  awk '$1 == "alias" && $3 != "off" && ($2 ~ /^snd-card-[0-9]$/ || $2 ~ /^sound-service-[0-9]-[0-9]+$/) {print $2}' < $MODULES_CONF | \
    while read line; do \
      show "Starting sound driver: $line"
      busy
      /sbin/modprobe $line
      ok
    done
  #
  # set (non-)blocking state of pcm devices
  #
  for card in /proc/asound/[0-9]; do
    card=`echo $card | awk '{ sub(/^.*\//, ""); print $0 }'`
    for dev in /proc/asound/$card/pcmD[0-9]o; do
      dev=`echo $dev | awk '{ sub(/^.*pcmD/, ""); sub(/o$/, ""); print $0 }'`
      show "Setting up blocking state for card $card device $dev"
      busy
      echo "Playback erase" > /proc/asound/$card/pcmD${dev}o
      echo "Capture erase" > /proc/asound/$card/pcmD${dev}o
      awk -v CARD=$card '($1 ~ CARD || $1 ~ /\*/) { $1 = ""; sub(/^ /, ""); print $0}' < /etc/sysconfig/alsa-oss-pcm > /proc/asound/$card/pcmD${dev}o
      ok
    done
  done
  if [ -n "$OLDLOGLEV" ]; then
	/sbin/loglevel "$OLDLOGLEV"
  fi
  #
  # restore driver settings
  #
  if [ -x $alsactl ]; then
    $alsactl restore
  else
    show "ERROR: alsactl not found"; fail
  fi
}

detect_stop()
{
  #
  # remove all sound modules
  #
  /sbin/lsmod | awk '/^snd/ { print $0 }' | while read line; do \
     /sbin/rmmod `echo $line | cut -d ' ' -f 1`; \
  done
}

driver_stop()
{
  #
  # store driver settings
  #
  if [ -x $alsactl ]; then
    $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
}

# See how we were called.
case "$1" in
  start)
        # Start driver.
	if [ ! -d /proc/asound ]; then
	  driver_start
	  if [ -d /proc/asound ] && [ -d /var/lock/subsys ]; then
	    touch /var/lock/subsys/alsasound
	  fi
	else
	  if [ -f /proc/asound/detect ]; then
	    show "Shutting down sound detect module:"
	    detect_stop
	    ok
	    driver_start
	    if [ -d /proc/asound ] && [ -d /var/lock/subsys ]; then
	      touch /var/lock/subsys/alsasound
	    fi
          else
	    show "ALSA driver is already running"; fail
	  fi
	fi
        ;;
  stop)
        # Stop daemons.
	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
	  show "ALSA driver isn't running "; fail
	fi
        ;;
  restart)
	$0 stop
	$0 start
	;;
  detect)
	# Start driver only in detect mode.
	if [ -d /proc/asound ]; then
	  if [ -f /proc/asound/detect ]; then
	    show "ALSA is already running detection mode"; fail
	    exit 0
          else
	    show "Shutting down sound driver:"
	    driver_stop
	    ok
	  fi
	fi
	show "Starting sound detect module:"
	detect_start
	ok
	if [ -d /var/lock/subsys ]; then
	  touch /var/lock/subsys/alsasound
	fi
	;;
  *)
        echo "Usage: $0 {start|stop|restart|detect}"
        exit 1
esac

exit 0
