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

echo "### Configuring MPlayer's v4l2 support ###"

if [ -f /var/tvcard -a -c /dev/video0 ]; then
  . /etc/tvcard

  # configure mplayer v4l2 driver
  if [ -n "$TV_WIDTH" -a -n "$TV_HEIGHT" ]; then
    TV_OPTIONS=":width=$TV_WIDTH:height=$TV_HEIGHT"
  fi
  if [ -n "$TV_BRIGHTNESS" ]; then
    TV_OPTIONS="$TV_OPTIONS:brightness=$TV_BRIGHTNESS"
  fi
  if [ -n "$TV_CONTRAST" ]; then
    TV_OPTIONS="$TV_OPTIONS:contrast=$TV_CONTRAST"
  fi
  if [ -n "$TV_HUE" ]; then
    TV_OPTIONS="$TV_OPTIONS:hue=$TV_HUE"
  fi
  if [ -n "$TV_SATURATION" ]; then
    TV_OPTIONS="$TV_OPTIONS:saturation=$TV_SATURATION"
  fi
  if [ -c /dev/vbi0 ]; then
    TV_OPTIONS="$TV_OPTIONS:tdevice=/dev/vbi0"
    [ -n "$TELETEXT_LANG" ] && TV_OPTIONS="$TV_OPTIONS:tlang=$TELETEXT_LANG"
  fi

  if [ `grep -c ^CHAN /etc/tvcard` -gt 0 ]; then
    if [ `grep -c "tv=channels" /etc/mplayer/mplayer.conf` -eq 0 ]; then
      for CHAN in `sed -n 's/^CHAN="\(.*\):\(.*\)"/\1-\2/p' /etc/tvcard | sed 'y/ /_/' `; do
        mp_set_option tv=channels "$CHAN" concat
      done
    fi
  else
    sed "s/.*set_menu tv_chan.*//" /etc/mplayer/menu.conf
  fi

  echo "tv=driver=v4l2:norm=$TVIN_STANDARD:chanlist=$CHANLIST$TV_OPTIONS" >> /etc/mplayer/mplayer.conf

  IFS='
'
  # set mplayer TV channels list
  echo "<cmdlist name=\"tv_chan\" title=\"TV Channels\" ptr=\"\" item-bg=\"-1\" title-bg=\"-1\" ptr-bg=\"164\" auto-close=\"yes\" >" >> /etc/mplayer/menu.conf
  for CHNAME in `sed -n 's/^tv=channels=//p' /etc/mplayer/mplayer.conf | sed -e 's/,/\n/g' -e 's/_/ /g' | sed 's/\(.*\)-\(.*\)/\2/'`; do
    echo "<e name=\"$CHNAME\" ok=\"loadfile tv://$((++n))\"/>" >> /etc/mplayer/menu.conf
  done
  echo "</cmdlist>" >> /etc/mplayer/menu.conf

  # Configure external input names
  EXT_INPUTS=`mplayer tv:// -really-quiet -msglevel tv=4 -vo null -ao null -frames 0 2>/dev/null | sed -n "s/^ inputs: \(.*\)/\1/p"`
  COMPOSITE_NUM=`echo $EXT_INPUTS | sed -n "s/\([0-9*]\) = Composite.*/\1/p" | sed "s/.*; //"`
  SVIDEO_NUM=`echo $EXT_INPUTS | sed -n "s/\([0-9*]\) = S-Video.*/\1/p" | sed "s/.*; //"`
  [ -z $COMPOSITE_NUM ] && sed "s#.*tv:///COMPOSITE_NUM.*##" -i /etc/mplayer/menu.conf || sed "s#tv:///COMPOSITE_NUM#tv:///$COMPOSITE_NUM#" -i /etc/mplayer/menu.conf
  [ -z $SVIDEO_NUM ] && sed "s#.*tv:///SVIDEO_NUM.*##" -i /etc/mplayer/menu.conf || sed "s#tv:///SVIDEO_NUM#tv:///$SVIDEO_NUM#" -i /etc/mplayer/menu.conf

else
  # remove tv options from menu
  sed -i 's/.*ok="set_menu tv_settings".*//' /etc/mplayer/menu.conf
  sed -i 's/.*ok="set_menu tv_norm".*//' /etc/mplayer/menu.conf
fi


[ -f /var/digimatrix ] && sed -i 's/set_menu radio_settings/set_menu digimatrix_radio/' /etc/mplayer/menu.conf

[ -f /etc/radio ] && . /etc/radio

if [ "$RADIO" = yes ]; then
  # set mplayer menu radio stations list
  if [ `grep -c ^CHAN /etc/radio` -gt 0 ]; then
    for CHAN in `sed -n 's/^CHAN="\(.*\):\(.*\)"/\1-\2/p' /etc/radio | sed 'y/ /_/' `; do
      mp_set_option radio=channels "$CHAN" concat
    done
  else
    sed "s/.*set_menu radio_chan.*//" /etc/mplayer/menu.conf
    sed -i 's/^radio=.*//' /etc/mplayer/menu.conf
  fi

  # set mplayer Radio channels list
  n=0
  echo "<cmdlist name=\"radio_chan\" title=\"Radio Stations\" ptr=\"\" item-bg=\"-1\" title-bg=\"-1\" ptr-bg=\"164\" auto-close=\"yes\" >" >> /etc/mplayer/menu.conf
  for CHNAME in `sed -n 's/^radio=channels=//p' /etc/mplayer/mplayer.conf | sed -e 's/,/\n/g' -e 's/_/ /g' | sed 's/\(.*\)-\(.*\)/\2/'`; do
    echo "<e name=\"$CHNAME\" ok=\"loadfile radio://$((++n))\"/>" >> /etc/mplayer/menu.conf
  done
  echo "</cmdlist>" >> /etc/mplayer/menu.conf
else
  # no supported radio card specified
  sed -i 's/.*ok="set_menu radio.*//' /etc/mplayer/menu.conf
  sed -i 's/^radio=.*//' /etc/mplayer/menu.conf
fi

exit 0
