#!/bin/bash

# Length in minutes of each chapter when DVD without menues is created
CHAPTER_LEN="${CHAPTER_LEN:-5}"

# Start Command with nice ? If not wished then set NICE=""
NICE="${NICE:-nice -n 19}"

# Use cut function of vdrsync (yes|no)
USE_CUT="${USE_CUT:-yes}"

# Use "tcrequant" or plain "requant" command (yes|no)
USE_TCREQUANT="${USE_TCREQUANT:-yes}"

# Use "tcmplex" or plain "mplex" command (yes|no)
USE_TCMPLEX="${USE_TCMPLEX:-no}"

# tcmplex command to use (if USE_TCMPLEX is yes)
#TCMPLEX="${TCMPLEX:-tcmplex}"
# plain "tcmplex" is availale in transcode < 1.0.0 only
TCMPLEX="${TCMPLEX:-tcmplex-panteltje}"

# Script version
PLUGIN_VERSION="0.0.6i"

# when script is called out of $PATH then add directory of script to $PATH
# there may be other needed programs in this directory
export PATH=$PATH:$(dirname $0)

logger -s "<$*>"

ExecCmd() {
    logger -s "Starting <$NICE $*>"
    $NICE "$@"
}

CheckCmd() {
    type "$1" >/dev/null 2>&1
    local retVal="$?"
    if [ "$retVal" -ne 0  ]; then
        echo "Unable to find '$1'."
        return 1
    fi
    return 0
}

# debug script execution
#set -x

# Sync Syntax: TempDir Number FileName
case "${1}" in
    SYNC)
        if [ "$USE_CUT" == "yes" ]; then
            CUTCMD="-cut"
        fi
        ExecCmd vdrsync.pl $CUTCMD -o "$2/VDRSYNC.$3" "$4"
        ;;

    # Requant Syntax: TempDir Number Filename Faktor
    REQUANT)
        if [ "$USE_TCREQUANT" == "yes" ]; then
            ExecCmd tcrequant -f "$5" -i "$4" -o "$2/VDRSYNC.$3/requant.mpv"
        else
            ExecCmd requant "$5" < "$4" > "$2/VDRSYNC.$3/requant.mpv"
        fi
        ;;

    # Mplex Syntax: TempDir FileName1 FileName2 FileName3
    MPLEX)
        if [ "$USE_TCMPLEX" == "yes" ]; then
            if [ "$5" != "" ]; then
                ExecCmd $TCMPLEX -i "$3" -p "$4" -s "$5" -m d -o "$2/movie.mpg"
            else
                ExecCmd $TCMPLEX -i "$3" -p "$4" -m d -o "$2/movie.mpg"
            fi
        else
            if [ "$5" != "" ]; then
                ExecCmd mplex -f 8 -vbr -v 1 -o "$2/movie.mpg" "$3" "$4" "$5"
            else
                ExecCmd mplex -f 8 -vbr -v 1 -o "$2/movie.mpg" "$3" "$4"
            fi
        fi
        ;;

    MKMENU)
        if [ "$USE_TCMPLEX" == "yes" ]; then
            convert "$2/menu-bg-$3.png" pnm:- |
                ppmtoy4m -n 1 -F25:1 -A 59:54 -I t -L -r -v 0 |
                y4mscaler -O chromass=420_MPEG2 |
                mpeg2enc -q 2 -a 2 -n p -f 8 -v 0 -o "$2/menu-bg-$3.m2v" &&
                $TCMPLEX -i "$2/menu-bg-$3.m2v" -p "$4/menu-silence.mp2" -m d -o /dev/stdout |
                spumux -v 2 "$2/menu-$3.xml" > "$2/menu-bg-$3.mpg"
        else
            convert "$2/menu-bg-$3.png" pnm:- |
                ppmtoy4m -n 1 -F25:1 -A 59:54 -I t -L -r -v 0 |
                y4mscaler -O chromass=420_MPEG2 |
                mpeg2enc -q 2 -a 2 -n p -f 8 -v 0 -o "$2/menu-bg-$3.m2v" &&
                mplex -f 8 -vbr -v 1 -o /dev/stdout "$2/menu-bg-$3.m2v" "$4/menu-silence.mp2" |
                spumux -v 2 "$2/menu-$3.xml" > "$2/menu-bg-$3.mpg"
        fi
        ;;

    # Author Syntax: TempDir or XML-File
    AUTHOR)
        if [ -d "$2" ]; then
            cd "$2"
            for i in $(seq 0 99) ; do
                if [ -f "VDRSYNC.$i/movie.mpg" ] ; then
                    echo "Found: VDRSYNC.$i"
                    CS="-c 0"
                    for j in $(seq $CHAPTER_LEN $CHAPTER_LEN 300) ; do
                        SH=$(($j / 60))
                        SM=$(($j % 60))
                        CS="$CS,$SH:$SM:0"
                    done
                    ExecCmd dvdauthor -o "$2/DVDAUTHOR" $CS -f "$2/VDRSYNC.$i/movie.mpg"
                else
                    break
                fi
            done
            ExecCmd dvdauthor -T -o "$2/DVDAUTHOR"
        else
            ExecCmd dvdauthor -x "$2"
        fi
        ;;

    # MkIso Syntax: TempDir IsoDir JobTitle
    MKISO)
        ExecCmd mkisofs -V "${4:0:32}" -dvd-video "$2/DVDAUTHOR" > "$3/$4.iso"
        ;;

    # Burn Syntax: TempDir Device JobTitle
    BURN)
        ExecCmd growisofs -use-the-force-luke=tty -dvd-compat -Z "$3" -V "${4:0:32}" -dvd-video "$2/DVDAUTHOR"
        ;;

    # MkIso-Burn Syntax: TempDir Device IsoDir JobTitle FifoPath
    MKISO_BURN)
        ExecCmd growisofs -use-the-force-luke=tty -dvd-compat -Z "$3=$6" | mkisofs -V "${5:0:32}" -dvd-video "$2/DVDAUTHOR" | tee "$4/$5.iso" > "$6"
        ;;

    CHECK)
#        if [ "$2" != "$PLUGIN_VERSION" ]; then
#            echo "Invalid plugin version $2 != $PLUGIN_VERSION."
#            exit
#        fi  
        CheckCmd growisofs &&
        CheckCmd mkisofs &&
        CheckCmd dvdauthor &&
        CheckCmd ppmtoy4m &&
        CheckCmd y4mscaler &&
        CheckCmd mpeg2enc &&
        CheckCmd spumux &&
        CheckCmd vdrsync.pl
        if [ "$?" -eq 0 ]; then
            if [ "$USE_TCMPLEX" == "yes" ]; then
                CheckCmd $TCMPLEX
            else
                CheckCmd mplex
            fi
            if [ "$?" -eq 0 ]; then
                echo "Ok"
            fi
        fi
        ;;
    *)
        echo "Illegal Parameter <$1>"
        ;;
esac
