#!/bin/sh
#
#                Copyright (C) 2005 Joseph Coffland
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License as
#    published by the Free Software Foundation; either version 2 of
#    	 the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   	       General Public License for more details.
#
#         To receive a copy of the GNU General Public License write to
#         the Free Software #      Foundation, Inc., 59 Temple Place -
#         	Suite 330, Boston, MA 02111-1307, USA.
#
#     For more information regarding this program email the author at
#     			 joseph@coffland.com


# Defaults
USELAME=0
VIDEOBR=1000
AUDIOBR=196
SUBLANG=en
LANG=en
SOURCE="dvd://1"
OPTS=
MENCODER=mencoder
TEST=0


# Functions
requiresarg() {
  if [ "$2" == "" ]; then
    echo "Error option $1 requires and argument."
    exit 1
  fi

  CLEARARG=1
}

execute() {
  echo "@ $@"
  if [ $TEST -eq 0 ]; then
    $@
    return $?
  fi
  return 0
}

help() {
  echo "Syntax: $0 <filename> [options]"
  echo
  echo "Description:"
  echo "  This script copies a single title of a DVD (by default title 1) to an avi file by"
  echo "  reencoding with mencoder in two passes. See http://www.mplayerhq.hu/ for more information"
  echo "  about mencoder."
  echo
  echo "Options:"
  echo "    --source <source>     Set video source. (Default $SOURCE)"
  echo "    --use-lame            Reencode audio with lame."
  echo "    --sub-lang <code>     Set subtitle language. 0 for no subtities. (Default $SUBLANG)"
  echo "    --videobr <bitrate>   Set video bitrate. (Default $VIDEOBR)"
  echo "    --audiobr <bitrate>   Set audio bitrate. Only with lame. (Default $AUDIOBR)"
  echo "    --lang <code>         Set audio and subtitle language with two digit code. (Default $LANG)"
  echo "    --test                Just print the commands that would be run."
  echo "    --help                Print this screen."
  echo "    *                     All other options are passed on to mencoder."
}


# Parse Command Line
if [ "$1" == "" ]; then
  help
  exit 1
else
  NAME=$1
  shift
fi

CLEARARG=0
while [ "$1" != "" ]; do
  case "$1" in
    --source)
      requiresarg $@
      SOURCE=$2
    ;;
    
    --use-lame)
      USELAME=1
    ;;

    --sub-lang)
      requiresarg $@
      SUBLANG=$2
    ;;

    --videobr)
      requiresarg $@
      VIDEOBR=$2
    ;;

    --audiobr)
      requiresarg $@
      AUDIOBR=$2
    ;;
 
    --lang)
      requiresarg $@
      LANG=$2
    ;;

    --test)
      TEST=1
    ;;

    --help)
      help
      exit 0
    ;;

    *)
      OPTS="$OPTS $1"
    ;;
  esac

  shift
  if [ $CLEARARG -eq 1 ]; then
    shift
    CLEARARG=0
  fi
done


# Setup Variables
OPTS="$OPTS -alang $LANG"
LAVCOPTS="vcodec=mpeg4:vbitrate=$VIDEOBR"
LAMEOPTS="vbr=3:br=$AUDIOBR"

if [ "$SUBLANG" != "0" ]; then
  SUBS="-vobsubout $(dirname $NAME)/$(basename $NAME .avi) -vobsuboutindex 0 -slang $SUBLANG"
else
  SUBS=
fi

VIDEO="-ovc lavc -lavcopts $LAVCOPTS"

if [ $USELAME -eq 1 ]; then
  AUDIO="-oac mp3lame -lameopts $LAMEOPTS"
else
  AUDIO="-oac copy"
fi


# Check for mencoder
which $MENCODER >/dev/null
if [ $? -ne 0 ]; then
  echo "ERROR: Could not find $MENCODER.  Make sure it is installed and on your PATH."
  echo "  $MENCODER is free software available at http://www.mplayerhq.hu/"
  exit 1
fi


# Execute 2 Pass Encoding
execute $MENCODER $SOURCE $OPTS $VIDEO:vpass=1 $AUDIO $SUBS -o $NAME
if [ $? -eq 0 ]; then
  execute $MENCODER $SOURCE $OPTS $VIDEO:vpass=2 $AUDIO $SUBS -o $NAME
fi

