#!/bin/sh
#
# configure MPlayer's audio settings
#
# runlevels: geexbox, debug

echo "### Configuring MPlayer's audio settings ###"

# include audio configuration file
. /etc/audio

echo $ALSA_CARD | grep -q ".." || ALSA_CARD="0$ALSA_CARD"

case $SOUNDCARD_MODE in
  SPDIF)  ALSA_REAL_MODE=IEC958 ;;
  analog) ALSA_REAL_MODE=DAC    ;;
esac

ALSA_DEVICE=`sed -n "s/^$ALSA_CARD-\(..\): .*$ALSA_REAL_MODE.*/\1/p" /proc/asound/pcm | tail -n 1`
[ -z "$ALSA_DEVICE" ] && ALSA_DEVICE=`grep -iv Modem /proc/asound/pcm | sed -n "s/^$ALSA_CARD-\(..\): .*playback.*/\1/p" | tail -n 1`
[ -z "$ALSA_DEVICE" ] && ALSA_DEVICE=`sed -n "s/^$ALSA_CARD-\(..\): .*playback.*/\1/p" /proc/asound/pcm | tail -n 1`

[ -n "$ALSA_DEVICE" ] && AO_DEVICE=":device=hw=$ALSA_CARD.$ALSA_DEVICE"

[ -x /usr/bin/mencoder ] && echo ":forceaudio=1:alsa=1:adevice=hw.$ALSA_CARD:audiorate=32000" > /var/recorder_alsa

# set alsa as audio output isn't forced already.
mp_set_option ao "alsa$AO_DEVICE"

# enable hardware AC3 output via S/PDIF if audio codec isn't forced already.
if [ "$SOUNDCARD_MODE" = SPDIF -a "$AC3_DECODER" = hardware ]; then
  mp_set_option ac "hwac3,"
  CHANNELS=2
fi

# don't use Master mixer channel when not available
if [ -z "`amixer -c $ALSA_CARD simple | grep "'Master'"`" ]; then
  sed -i 's/mixer-channel=Master//' /etc/mplayer/mplayer.conf
fi

# set number of playback channels if isn't forced already.
mp_set_option channels "$CHANNELS"

# set DVD default language
echo "alang=`sed 's/^\(..\).*/\1/' /etc/lang | head -n 1`,en" >> /etc/mplayer/mplayer.conf

# Set suitable background movie when playing audio-only.
for i in background-audio.avi background.avi; do
  if [ -f /usr/share/mplayer/$i ]; then
    mp_set_option bgvideo "/usr/share/mplayer/$i"
    break
  fi
done

# set softvol for VIA 8235 rev80 and SiS SI7012 rev16 cards
if grep -q -e 'Class 0401:.*1106:3059 (rev 80)' -e 'Class 0401:.*13f6:0111 (rev 16)'  /proc/pci; then
  mp_set_option softvol "yes"
fi

exit 0
