#!/bin/bash
# burnmark.sh - v.0.3
#
# source: burn-plugin
#
# add this lines to your reccmds.conf:
# folgende zeilen in die reccmds.conf eintragen:
#
# Mark for DVD-Writer: /usr/local/bin/burnmark.sh -add
# Delete Mark        : /usr/local/bin/burnmark.sh -del
#
# history
# --------
# 20050318,ralf: added setup.conf check
# 20041206,randy: added osd text for deletions

SETUPCONF=/etc/vdr/setup.conf
if [ "!" -f "$SETUPCONF" ]; then
    echo "Unable to find '$SETUPCONF' - please setup correct path in"
    echo "'$0'."
    exit 1
fi

IFS="="
read KEY LANGUAGE < <(grep "^OSDLanguage" "$SETUPCONF")
unset IFS
LANGUAGE="${LANGUAGE//[ 	]/}"

#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)


getBits() {
    local BASE=1
    local INCREMENT=256
    local BITS=0
    while [ $(( $BASE * $INCREMENT / $INCREMENT )) -eq $BASE ]; do
        BASE=$(( $BASE * $INCREMENT ))
        BITS=$(( $BITS + 8 ))
    done
    echo $BITS
}

div1024() {
    BITS=${BITS:-$(getBits)}
    if [ "$BITS" -gt 48 ]; then
        echo $(( $1 / 1024 ))
    else 
        echo "$1 / 1024 " | bc
    fi
}

getCutSize() {
    TMP="$1/~vdrburn"
    rm -f "$1/size.vdr" "$1/size_cut.vdr"
    vdrsync.pl -i "$1" > "$TMP"
    if [ -f "$1/marks.vdr" ] ; then
        CC=$(grep -c ":" "$1/marks.vdr")
        # fix for vdrsync ( last Mark must be set )
        if [ $(($CC % 2)) = 1 ] ; then
            grep "^movie_length :" "$TMP" | cut -f 3 -d " " >> "$1/marks.vdr"
            vdrsync.pl -i "$1" > "$TMP"
        fi
    fi
    local DIR_SIZE=$(grep "^movie_size_cut :" "$TMP" | cut -f 2 -d ":")
    if [ "$DIR_SIZE" != "" ] ; then
        div1024 "$DIR_SIZE" > "$1/size_cut.vdr"
    fi
    DIR_SIZE=$(grep "^movie_size :" "$TMP" |cut -f 2 -d ":")
    if [ "$DIR_SIZE" != "" ] ; then
        div1024 "$DIR_SIZE" > "$1/size.vdr"
    else
        DIR_SIZE=$(du -k "$1")
        echo "${DIR_SIZE%%[ 	]*}" > "$1/size.vdr"
    fi
    rm "$TMP"
}

case "${1}" in
    -add)
        touch "$2/burnmark.vdr"
        nohup "$0" -cutsize "$2" </dev/null >/dev/null 2>/dev/null
        if [ "$LANGUAGE" == "1" ]; then
            echo "Die Aufzeichnung wurde markiert..."
            echo ""
            echo "Bitte wechseln Sie nun in das Menü DVD-Brenner oder markieren Sie weitere Aufnahme."
        elif [ "$LANGUAGE" == "3" ]; then
            echo "La registrazione e' stata selezionata..."
            echo ""
            echo "Seleziona altre registrazioni e poi entra nel menu 'Crea DVD' per passare alla creazione del disco."   
        elif [ "$LANGUAGE" == "8" ]; then
            echo "Tallenne on lisätty polttolistaan."
            echo ""
            echo "Voit lisätä uuden tallenteen tai aloittaa DVD-polton päävalikosta."
        elif [ "$LANGUAGE" == "10" ]; then
            echo "La grabación ha sido seleccionada..."
            echo ""
            echo "Por favor cambie ahora al menu de escribir DVDs, o seleccione mas grabaciones."
        else
            echo "The recording has been marked..."
            echo ""
            echo "Please change now into the DVD burn menu, or mark further recordings."
        fi
        ;;
    -del)
        rm -f "$2/burnmark.vdr" "$2/size.vdr" "$2/size_cut.vdr"
        if [ "$LANGUAGE" == "1" ]; then
            echo "Die Aufzeichnung wurde aus der Warteschlange entfernt."
        elif [ "$LANGUAGE" == "3" ]; then
            echo "La registrazione e' stata rimossa dalla lista..."
        elif [ "$LANGUAGE" == "8" ]; then
            echo "Tallenne on poistettu DVD-polttojonosta."
        elif [ "$LANGUAGE" == "10" ]; then
            echo "La grabación ha sido borrada de la lista."
        else
            echo "The recording was removed from the queue."
        fi
        echo ""
        ;;
    # Do not use "-cutsize" this is for internal use only
    -cutsize)
        getCutSize "$2"
        ;;
    *)
        echo "Illegal Parameter <$1>."
        ;;
esac

