#!/bin/bash
#
# sound - sound configuration plugin for postplug
# Copyright (c) 2005-2007 by Davide Madrisan <davide.madrisan@gmail.com>
# Copyright (c) 2005-2013 by Silvan Calarco <silvan.calarco@mambasoft.it>

# Note: Some code has been stolen from /usr/sbin/alsaconf version 1.0.13

if [ $UID != 0 ]; then
   echo "$0: must be superuser." >&2
   exit 1
fi

me="sound"

[ -r /etc/sysconfig/machine ] && . /etc/sysconfig/machine
. /etc/postplug/postplug.defs
[ -r /etc/sysconfig/postplug ] && . /etc/sysconfig/postplug
SYSCONFIG_SOUNDCARDS=/etc/sysconfig/soundcards

# set default mixer volumes
function set_mixers() {
   [ -x /usr/bin/amixer ] || return
   channel=$1
   [ "$channel" ] || channel=0

   /usr/bin/amixer -s -c $channel -q <<_EOF
# Playback channels
set 'Master' 90% unmute
# some notebooks use headphone instead of master
set 'Headphone' 75% unmute
set 'Playback' 100% unmute
set 'Master Mono' 75% unmute
set 'Front' 90% unmute
set 'Speaker' 90% unmute
set 'PCM' 90% unmute
set 'Surround' 90% unmute
set 'Center' 90% unmute
set 'LFE' 90% unmute
set 'Line' 75% unmute
set 'CD' 90% unmute
set 'Synth' 90% unmute
# mute mic
set 'Front Mic' 75% mute
set 'Mic' 75% mute
# ESS 1969 chipset has 2 PCM channels
set 'PCM,1' 90% unmute
# 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,0' 90% unmute
set 'DAC,1' 90% unmute
# Capture channels
set 'Capture' 90% unmute
set 'Capture 1' 90% unmute
set 'Capture 2' 90% unmute
# virtual channels
set 'Capture Gain' 50% unmute
set 'Output Gain' 75% unmute
# turn off digital switches
set 'SB Live Analog/Digital Output Jack' off
set 'Audigy Analog/Digital Output Jack' off
_EOF
}

if [ -e /proc/bus/pci ]; then
   SOUND_DEV="`lspci -n 2>/dev/null| grep " 04[0-9][0-9]: "`"
fi
if [ ! "$SOUND_DEV" ]; then
   if [ -e /proc/asound/cards ]; then
      SOUND_DEV="`md5sum /proc/asound/cards 2>/dev/null`"
   else
      SOUND_DEV="missing"
   fi
fi

if [ "$SOUND_FORCE_RESCAN" != "on" -a "$SOUND_FORCE_RESCAN" != "1" ]; then
   if [ -e $postpluglibdir/sound.cache ]; then
      SOUND_DEV_CACHED="`cat $postpluglibdir/sound.cache`"
      [ "$SOUND_DEV" = "$SOUND_DEV_CACHED" ] && exit 0
      logmsg "$me" "warning: audio device appears to have changed; re-probing"
      mv $SYSCONFIG_SOUNDCARDS $SYSCONFIG_SOUNDCARDS.postplug.save
   fi
fi

if [ -e /sys/module/spi_bcm2708 ]; then
   # Raspberry PI
   update-alternatives --set asound.conf /etc/asound.conf.dummy
elif [ -e /proc/asound/MidasWM1811 ]; then
   # Galaxy Note 10.1
   /usr/sbin/alsactl restore -f /usr/share/alsa/ucm/Midas_WM1811/init.state
   # enable speaker for output and builtin mic for input
   /usr/bin/alsaucm -c Midas_WM1811 set _verb HiFi set _enadev Speaker
   /usr/bin/alsaucm -c Midas_WM1811 set _verb HiFi set _enadev Mic
fi

for c in /proc/asound/card[0-9]*; do
   id=`cat $c/id`
   codec=`grep Codec: $c/codec* 2>/dev/null | head -n1`
   logmsg "$me" $"setting volumes for $id sound card ($codec)"
   index=${d1/*card}
   set_mixers $index
done

if [ -x /usr/sbin/alsactl ]; then
   logmsg "$me" $"saving the mixer setup (/var/lib/sound/asound.state)"
   /usr/sbin/alsactl store >/dev/null
fi

echo "$SOUND_DEV" > $postpluglibdir/sound.cache

exit 0
