#!/bin/bash
#
# This script is called from VDR to start MPlayer
#
# argument 1: the file to play
# argument 2: (optional) the phrase SLAVE if SlaveMode is enabled
# argument 3: (optional) the phrase AID x to select audio stream x

# where to find mplayer
MPLAYER="mplayer"

# mplayer options, -vc will be added below
# add "-lircconf <lircrc>" to enable LIRC support
OPTS="-vo mpegpes"
if [ "aa$DVB_DEVICE" != "aa" ]; then
  OPTS="-vo mpegpes:card=$DVB_DEVICE -ao mpegpes:card=$DVB_DEVICE"
fi

# mplayer options for SlaveMode
SLAVE="-slave -quiet -nolirc"

#####################

FILE=$1
case "$FILE" in
*.pls | *.m3u)
  popt="-playlist"
  first=`grep -v -m1 "^#" $FILE`
  type=`file "$first"`
  ;;
*)
  type=`file "$FILE"`
  ;;
esac

while shift; do
  if [ "$1" = "SLAVE" ]; then
    sopt=$SLAVE
  elif [ "$1" = "AID" ]; then
    aopt="-aid $2"
    shift
  fi
done

case "$type" in
*AVI*)
  VC="ffdivx"
  ;;
*MPEG*)
  VC="mpegpes"
  ;;
*)
  echo "Unknown video file format $type"
  echo "Edit mplayer.sh to support this file type"
  exit 1
  ;;
esac

exec $MPLAYER $OPTS -vc $VC $sopt $aopt $popt "$FILE"

