#!/bin/sh

# /etc/init.d/alsa: Starts and stops the ALSA sound driver.

set -e

if [ $(id -u) -ne 0 ]; then
	echo "Refusing to $1 ALSA, since you don't have root privs."
	exit 1
fi 

if [ -e /etc/alsa/alsa-base.conf ]; then
    . /etc/alsa/alsa-base.conf
fi

if ! /sbin/modprobe snd >/dev/null 2>&1 ; then
    alsa_version="none"
else
    alsa_version=$(head -1 /proc/asound/version | cut -d\  -f7 | cut -d. -f -3)
fi
/sbin/modprobe -r snd >/dev/null 2>&1 || true

case "$1" in
    start)
	echo -n "Starting ALSA sound driver (version $alsa_version):"
	found_driver="false"
	for i in $(grep -E '^((alias)|(probe))[[:space:]]+snd-card-[0-9]' \
	    /etc/modules.conf | sort | grep -Ev '^(off|null)$' | \
	    awk '{ print $3 }'); do
		desc=$(echo $i | cut -d- -f2-)
		    if /sbin/modprobe $i >/dev/null 2>&1 ; then
		    echo -n " $desc"
		    found_driver="true"
		    else
		    echo -n " $desc-failed"
		    fi
	done
	case "$found_driver" in
	    true)
		echo "."
	    ;;
	    false)
		if [ "$alsa_version" = none ]; then
			    echo " no driver installed."
		else
			    echo " no sound cards defined."
		fi
		    exit 0
	    ;;
	esac
	if [ -f /dev/.devfsd -a ! -e /dev/snd ]; then
		    echo " no devfs available (but kernel has devfs)."
		    exit 0
	elif [ -d /proc/asound/dev ];  then
		    if [ ! -e /dev/snd ]; then
		    ln -s /proc/asound/dev /dev/snd
		    fi
		    if [ ! -L  /dev/sndstat ]; then
		    ln -sf /proc/asound/oss/sndstat /dev/sndstat
		    fi
	    fi
	if [ "$startosslayer" = true ]; then
		    for i in mixer pcm seq ; do 
		    /sbin/modprobe snd-$i-oss >/dev/null 2>&1 || true
		    done
	fi
	if [ -d /proc/asound ]; then
	    if [ "$alsactl_store_on_shutdown" = true ]; then
		echo -n "Restoring ALSA mixer settings... "
		if alsactl restore >/dev/null 2>&1; then
				echo "done."
		else
				echo "failed."
		fi
	    fi
	fi
	;;
    stop)
	if [ -d /proc/asound ]; then
	    if [ "$alsactl_store_on_shutdown" = true ]; then
		echo -n "Storing ALSA mixer settings... "
		if alsactl store >/dev/null 2>&1; then
		    sleep 1
		    echo "done."
		else
		    echo "failed."
		fi
	    fi
	    echo -n "Shutting down ALSA sound driver (version $alsa_version): "
	    
	    procs_using_sound="$(echo $({ find /dev -print0 | xargs -0 stat -c '%t:%n' | grep '^e:' | cut -d: -f2-; find /proc/asound/dev -type c; } | while read; do fuser "$REPLY" || true; done | cut -f2- -d:))"
	    if [ "$procs_using_sound" != "" ]; then
	    	if [ "$ALSA_KILL_MODE" = force ]; then
				kill $procs_using_sound
	    	else
		    	echo "aborting. (sound used by pid $procs_using_sound)"
		    	exit 0
			fi
	    fi
	    for i in seq mixer pcm ; do
		/sbin/modprobe -r snd-$i-oss >/dev/null 2>&1 || true
	    done
	    for i in $(/sbin/lsmod | grep -E '^snd' | cut -d\  -f1) ; do
		/sbin/modprobe -r $i >/dev/null 2>&1 || true
	    done
	    /sbin/modprobe -r soundcore >/dev/null 2>&1 || true
	    echo "done."
	else
	    echo "ALSA driver isn't running."
	fi
	;;
    restart|reload)
	$0 stop && $0 start
	;;
    force-*)
	ALSA_KILL_MODE="force" $0 ${1##*-}
	;;
    *)
	echo "Usage: /etc/init.d/alsa {start|stop|restart|reload|force-stop|force-restart|force-reload}"
	exit 1
esac

