#!/bin/bash

. /etc/colorrc

CfgFile="/tmp/autoconfig.txt"
AlsaWork="n"

Embtool="/opt/aldccp/bin/embtool"

CfgFileWrite() {
  if test -x $Embtool; then
    $Embtool shdata -w -I $1 -D "\"$2\"" $CfgFile
  else
    echo "$1=$2" >>$CfgFile
  fi
}

chk_play() {
aplay -q /usr/share/sounds/alsa/KDE_Beep_RimShot.wav 2>/dev/null
if test $? = 0; then AlsaWork="y"; fi
}

#
# set default mixer volumes
#
set_mixers() {
    amixer -s -q <<EOF
set Master 75% unmute
set Master -9dB
set 'Master Mono' 75% unmute
set 'Master Mono' -9dB
set Front 75% unmute
set Front -9dB
set PCM 90% unmute
set PCM 0dB
set 'PC Speaker' 33% unmute
set Beep 33% unmute
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

chk_play
}

set_card() {
if test -z $2 ; then
cat > /etc/asound.conf <<EOF
defaults.pcm.card $1
defaults.ctl.card $1
EOF
else
cat > /etc/asound.conf <<EOF
defaults.pcm.card $1
defaults.pcm.device $2
defaults.ctl.card $1
EOF
fi
}

chk_alsa_hdmi() {
local v1 card ldat dev dname hdmitxt hdmi_idx
  dd if=/dev/zero of=/tmp/alsa_xxx.wav count=2 &>/dev/null

  aplay -l | while read v1 card ldat ; do
    if test "$v1" = "card" ; then
      card=${card%:}
      ldat=${ldat#*device}
      dev=${ldat%:*}
      dev=${dev# }

      ldat=${ldat#*:}
      dname=${ldat%[*}
      dname=${dname% }
      dname=${dname# }
      hdmitxt=${dname% *}
      hdmi_idx=${dname#* }

      ldat=${ldat#*[}
      ldat=${ldat%]*}

      if test "$hdmitxt" = "HDMI"; then
# echo "$card $dev $hdmitxt \"$hdmi_idx\" $ldat"
	if test "$dname" != "$ldat"; then
	  set_card $card $dev
	  CfgFileWrite "Alsa_HDMI" "$dname"
	  CfgFileWrite "Alsa_HDMI_mon" "$ldat"
	  CfgFileWrite "Alsa_HDMI_hw" "hw$card,$dev"
	  aplay -q aplay -fS16_BE -c2 -r44100 2>/dev/null
	  amixer -q -c$card set IEC958,$hdmi_idx unmute 2>/dev/null
	  amixer -q -c$card set PCM 80% unmute 2>/dev/null
	  echo "ok"
	  exit 0
	fi
      fi
    fi
  done
  rm /tmp/alsa_xxx.wav
}

case "$1" in
    start)
	if test -e /tmp/rpm_install_error; then
	  exit 0;
	fi

	if test -d /proc/asound; then
	  hdmi=`chk_alsa_hdmi`
	  if test ! -z $hdmi; then
	    sleep 1
	    chk_play
	  fi
	  CfgFileWrite "Alsa_HDMI_Work" "$AlsaWork"

	  if test "$AlsaWork" = "n"; then
	    set_card 0
	    set_mixers
	  fi
#	  if test "$AlsaWork" = "n"; then
#	    set_card 0 3
#	    set_mixers
#	  fi
	  if test "$AlsaWork" = "n"; then
	    set_card 1
	    set_mixers
	  fi
	  if test "$AlsaWork" = "n"; then
	    rm /etc/asound.conf
	    cp /etc/asound.conf- /etc/asound.conf
	    set_mixers
	  fi

	  if test "$AlsaWork" = "n"; then
	    rm /etc/asound.conf
	    echo ${rcC3}"ALSA sound not running \"/etc/asound.conf not work\""${rcLF}
	  else
	    alsactl store
	  fi
	fi

	CfgFileWrite "AlsaWork" "$AlsaWork"
	;;
    stop)
	;;
    *)
	echo "Usage: {start|stop|reload|restart}"
	exit 1
	;;
esac
