#! /bin/bash
#120323 npierce: remove OK button if no cards detected. refer: http://murga-linux.com/puppy/viewtopic.php?t=76182&start=90
#120815 rodin.s: i18n
#130216 01micko, BK: fix retrovol.
#150630 rg66, Geoffrey, change GUI uses buttons, only non active cards selectable, save retrovol mixer settings for each sound card

GTKDIALOG="gtkdialog"

export TEXTDOMAIN=multiple_soundcard
export OUTPUT_CHARSET=UTF-8
. gettext.sh

ICON="/usr/share/icons/hicolor/48x48/devices/audio-card.png"

DEVICES="`aplay -l | grep card | grep -v pcspeaker`"
[ "$DEVICES" = "" ] && gtkdialog-splash -close never -timeout 5 -icon $ICON -text "$(gettext 'Multiple Sound Card Wizard
No Sound Devices Detected')" && exit

[ ! -f /etc/asound.conf -a "$DEVICES" != "" ] && echo "defaults.pcm.card 0
defaults.pcm.device 0" > /etc/asound.conf 

ACARD=`awk 'FNR == 1 {print $2}' /etc/asound.conf`
ADEVICE=`awk 'FNR == 2 {print $2}' /etc/asound.conf`
ACTIVE=`aplay -l | grep card | grep -w "card $ACARD" | grep -w "device $ADEVICE"`
CURRENT=`echo "$ACTIVE" | cut -f1,2 -d'['`

#150623 rg66, change gui
echo "export MAIN_DIALOG='
<window title=\"$(gettext 'Multiple Sound Card Wizard')\" image-name=\"$ICON\" resizable=\"false\">
  <vbox>
    <text use-markup=\"true\"><label>\"<b>$(gettext 'Choose default sound card')</b>\"</label></text>
    <text wrap=\"false\" justify=\"2\" use-markup=\"true\"><label>\"$(gettext "<b>Current:</b> $CURRENT")\"</label></text>" > /tmp/mscw.tmp

aplay -l | grep card | grep -v pcspeaker | grep -xvF "$ACTIVE" | cut -f1,2 -d'[' | while read line; do
echo "<button>
      <label>$line</label>     
      </button>" >> /tmp/mscw.tmp ;
done   
echo "<hbox homogeneous=\"true\">
       <button cancel></button>
    </hbox>
  </vbox>
</window>
'" >> /tmp/mscw.tmp

. /tmp/mscw.tmp

I=$IFS; IFS=""
  for STATEMENTS in  $($GTKDIALOG --center --program MAIN_DIALOG); do
    eval $STATEMENTS
  done
IFS=$I
     
[ "$EXIT" = "Cancel" -o "$EXIT" = "abort" ] && exit   

Card="$(echo $EXIT | cut -d ":" -f 1 | sed -e 's/\(^.*\)\(.$\)/\2/')"
Device="$(echo $EXIT | cut -d ":" -f 2 | sed -e 's/\(^.*\)\(.$\)/\2/')"
        
echo "defaults.pcm.card "$Card > /etc/asound.conf
echo "defaults.pcm.device "$Device >> /etc/asound.conf

#130216 01micko, BK: fix retrovol...
RETROVOL=$(which retrovol)
if [ "$RETROVOL" ];then
  killall retrovol
 
#150630 Geoffrey, rg66, fix retrovol mixer settings
cp -f $HOME/.retrovolrc $HOME/.retrovolrc-$ACARD-$ADEVICE

if [ -f $HOME/.retrovolrc-$Card-$Device ]; then
rm -f $HOME/.retrovolrc
mv $HOME/.retrovolrc-$Card-$Device $HOME/.retrovolrc 
else
rm -f $HOME/.retrovolrc
fi

[ -f $HOME/.retrovolrc ] &&  sed -i -e "/^card=hw/d;/#card=hw:0/a card=hw:${Card}" $HOME/.retrovolrc || echo "card=hw:${Card}" > $HOME/.retrovolrc
   
  retrovol &   
 
  TEXT="$(gettext 'Retrovol has been set to use this card'): hw:${Card}"
else
  TEXT="$(gettext 'Configure your audio mixer as') hw:${Card}"   
fi

gtkdialog-splash -close never -timeout 5 -icon $ICON -text "Multiple Sound Card Wizard
$TEXT" && exit
