#!/bin/bash
# handlearchived.sh - v.0.3
#
# source: burn-plugin
#
# add this lines to your reccmds.conf:
# folgende zeilen in die reccmds.conf eintragen:
#
# Clean archived recording   : /usr/local/bin/handlearchived.sh -clean
# Restore archived recording : /usr/local/bin/handlearchived.sh -restore
#
# history
# --------
# 20050318,ralf: first version

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

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

printNoMark() {
    if [ "$LANGUAGE" == "1" ]; then
        echo "Es wurde keine DVD-Archiv Markierung gefunden - Abbruch."
    elif [ "$LANGUAGE" == "8" ]; then
        echo "DVD-arkistomerkintää ei löytynyt - toiminto peruttu."
    else
        echo "Unable to find DVD archive mark - bailing out."
    fi
}

rmFile () {
    local FILE="$1"
    if [ -L "$FILE" ]; then
        REAL_FILE="$(readlink "$FILE")"
        if [ "${REAL_FILE##/tmp}" == "$REAL_FILE" ]; then
            rm -f -- "$REAL_FILE"
        fi
    fi
    rm -f -- "$FILE"  
}

case "${1}" in
    -clean)
        if [ -f "$2/dvd.vdr" ]; then
            if [ -r "$2/001.vdr" ]; then
                for FILE in "$2/"[0-9][0-9][0-9].vdr; do
                    rmFile "$FILE"
                done
            fi
            rmFile -f -- "$2/index.vdr" 
            if [ "$LANGUAGE" == "1" ]; then
                echo "Die Aufzeichnung wurde entfernt."
            elif [ "$LANGUAGE" == "8" ]; then
                echo "Tallenne on siivottu."
            else
                echo "Recording has been cleaned up."
            fi
        else
            printNoMark
        fi
        ;;
    -delmark)
        if [ -f "$2/dvd.vdr" ]; then
            if [ -f "$2/index.vdr" -a -f "$2/001.vdr" ]; then
                rm -f -- "$2/dvd.vdr"
            else
                if [ "$LANGUAGE" == "1" ]; then
                    echo "Die Aufzeichnung ist ausschließlich auf DVD - bitte zuerst Aufzeichnung wiederherstellen."
                elif [ "$LANGUAGE" == "8" ]; then
                    echo "Tallenne pitää ensin palauttaa DVD:ltä."
                else
                    echo "Recording is only on DVD - please restore recording first."
                fi
            fi
        else
            printNoMark
        fi
        ;;
    -restore)
        RECORDING="${2%/}"
        if [ "!" -f "$RECORDING/dvd.vdr" ]; then
            printNoMark
            exit 0
        fi
        RECMARK="${RECORDING##*/}"

        umount "$DVDDEVICE" >/dev/null 2>&1
        MOUNTDIR="/tmp/.restore.dvd.$$"
        mkdir -p "$MOUNTDIR"
        mount -o ro "$DVDDEVICE" "$MOUNTDIR"
        RESTORED=false
        while read DVDRECORDING; do
            DVDRECMARK="${DVDRECORDING##*/}"
            if [ "$RECMARK" == "$DVDRECMARK" ]; then
                cp -a -- "$DVDRECORDING/index.vdr" "$RECORDING" && RESTORED=true
                ( cp -a -- $DVDRECORDING/[0-9][0-9][0-9].vdr "$DVDRECORDING/index.vdr" "$RECORDING"; umount "$DVDDEVICE"; rmdir "$MOUNTDIR" ) >/dev/null 2>&1 &
                echo ""
            fi
        done < <(find "$MOUNTDIR" -type d -name "*.rec")

        if [ "$RESTORED" == "false" ]; then
            umount "$DVDDEVICE"
            rmdir "$MOUNTDIR"
            if [ "$LANGUAGE" == "1" ]; then
                echo "Wieder eingespielen fehlgeschlagen!"
            elif [ "$LANGUAGE" == "8" ]; then
                echo "Tallenteen palautus epäonnistui!"
            else
                echo "Restore of recording failed!"
            fi
            exit 0
        fi
 
        if [ "$LANGUAGE" == "1" ]; then
            echo "Die Aufzeichnung wird von DVD wieder eingespielt."
        elif [ "$LANGUAGE" == "8" ]; then
            echo "Tallenne palautetaan DVD:ltä"
        else
            echo "The recording will be restored from DVD."
        fi
        ;;
    *)
        echo "Illegal Parameter <$1>."
        ;;
esac


