#!/bin/sh
# Routines taken from original alsaconf and alsasound with mods for TC.

. /etc/init.d/tc-functions

ALSADRIVER=/opt/alsa/.alsadriver
> /opt/alsa/.alsadriver
> /tmp/.isaprobe

start() {
unset DRIVERINSTALLED

if ! ps | grep /bin/bash | grep alsaconf > /dev/null 2>&1 && ! grep "1" /opt/alsa/.alsaconf > /dev/null 2>&1; then  

	 for d1 in /sys/devices/pnp*; do
		for d2 in $d1/* ; do
			if [ -r $d2/card_id ]; then
	        		id=`cat $d2/card_id`
	        		cardname=`cat $d2/name`
	        		echo "'$cardname'" >> /tmp/.isaprobe
	   		fi
		done
	 done

	if [ ! -z /tmp/.isaprobe ] && [ ! "$DRIVERINSTALLED" == "1" ]; then

		for I in `find /lib/modules/*/kernel*/sound -name snd-* | sort | uniq`; do E=`basename "$I" .ko.gz`; 

			modprobe "$E" > /dev/null 2>&1
			NAME=`echo "$E" | cut -c5-`
			if dmesg | tail -n 1 | grep "$NAME" | grep "activated" > /dev/null 2>&1; then
				echo "${GREEN}Starting sound driver: $E ${NORMAL}" && echo "$I" >> "$ALSADRIVER"
				DRIVERINSTALLED=1
				break
			else
				lsmod | grep -E "^snd" | grep -Ev "(snd-page-alloc|snd_page_alloc)" | while read line; do \
     					/sbin/rmmod `echo $line | cut -d ' ' -f 1`; \
  				done
			fi
		done
   	 fi

  	 drivers=`lspci -k | grep -e "Kernel modules" | grep -e "snd" | cut -f 2 -d :`
 	 for i in $drivers; do
   	 	if [ "$i" != off ]; then
	 		echo -n "${GREEN}Starting sound driver: $i ${NORMAL}"
	 		echo " "
 	 		modprobe $i && module_loaded=1
 	 	fi
 	 done

else
	if [ -f /usr/local/tce.installed/alsa ]; then
		/usr/local/tce.installed/alsa > /dev/null 2>&1
	elif [ -f /usr/local/tce.installed/alsa-utils ]; then
		/usr/local/tce.installed/alsa-utils > /dev/null 2>&1
	fi
fi

modprobe snd-pcm-oss > /dev/null
modprobe snd-seq-oss > /dev/null
modprobe snd-mixer-oss > /dev/null

for I in `cut -d , -f 1 /proc/asound/seq/drivers`; do
	modprobe "$I" > /dev/null 2>&1
done

  if [ -f /usr/local/etc/alsa/asound.state ] || [ -f /etc/asound.state ]; then
    alsactl restore > /dev/null 2>&1
  else
amixer -s -q <<EOF
set Master 75% unmute
set Master -12dB
set 'Master Mono' 75% unmute
set 'Master Mono' -12dB
set Front 75% unmute
set Front -12dB
set PCM 90% unmute
set PCM 0dB
mixer Synth 90% unmute
mixer Synth 0dB
mixer CD 90% unmute
mixer CD 0dB
# mute mic
set Mic 0% mute
# ESS 1969 chipset has 2 PCM channels
set PCM,1 90% unmute
set PCM,1 0dB
# Trident/YMFPCI/emu10k1
set Wave 100% unmute
set Music 100% unmute
set AC97 100% unmute
# CS4237B chipset:
set 'Master Digital' 75% unmute
# Envy24 chips with analog outs
set DAC 90% unmute
set DAC -12dB
set DAC,0 90% unmute
set DAC,0 -12dB
set DAC,1 90% unmute
set DAC,1 -12dB
# some notebooks use headphone instead of master
set Headphone 75% unmute
set Headphone -12dB
set Playback 100% unmute
# turn off digital switches
set "SB Live Analog/Digital Output Jack" off
set "Audigy Analog/Digital Output Jack" off 
EOF
fi

udevadm settle

if [ ! -f /proc/asound/cards ] || grep "no soundcards" /proc/asound/cards > /dev/null 2>&1; then
	if grep "1" /opt/alsa/.alsaconf > /dev/null 2>&1 ; then
		echo "${YELLOW}Something went wrong, sound was not set up.  Since we are using the results of the last run of alsaconf \
make sure that your /etc/modprobe.conf file was properly backed up or has \
not been corrupted.  You need to run alsaconf again or restore a working /etc/modprobe.conf.${NORMAL}"
	else
		echo "${YELLOW}Something went wrong, sound was not set up.${NORMAL}"
	fi
fi
}

stop() {

  alsactl store

  lsmod | grep -E "^snd" | grep -Ev "(snd-page-alloc|snd_page_alloc)" | while read line; do \
     /sbin/rmmod `echo $line | cut -d ' ' -f 1`; \
  done

  /sbin/rmmod soundcore 2> /dev/null
  /sbin/rmmod gameport 2> /dev/null
 
}

status() {

	if [ ! -f /proc/asound/cards ] || grep "no soundcards" /proc/asound/cards > /dev/null 2>&1; then
		echo "Alsa is not running."
		exit 1
	else
		echo "Alsa is running."
		exit 0
	fi
}

case "$1" in
  start)
	start
        ;;
  stop)
        stop
        ;;
  status)
	status
	;;
  *)
        echo "Usage: alsasound {start|stop|status}"
        exit 1
esac
