#!/bin/bash
############################################################################
#
# Interface script between a PVR and gshowtv. 
#
# This script interfaces between the gshowtv record/cancel scripts from 
# gshowtv versions 0.9.2 and earlier. 
# This script is provided for easier upgrade only.
#
# Before using this script, the record and cancel commands have to be
# specified. If you do not know what they are, then please do not use
# this interface. This is for legacy support only.

# The configuration of these two scripts is done in 
# ~/.gshowtv/legacy-pvr-interface.conf
#
# THIS FILE CONTAINS NOTHING TO CONFIGURE.

CONF="$HOME/.gshowtv/legacy-pvr-interface.conf"

if [ -e $CONF ] ; then
	source $CONF
else
	echo "# Please configure the following to point to your recording scripts." > $CONF
	echo "RECORD=" >>$CONF
	echo "CANCEL=" >>$CONF
	echo "# The dir where the recorded shows are" >> $CONF
	echo "SHOWDIR=" >> $CONF
fi

# The locations

DIR=/var/lib/gshowtv
ERROR=0

# Check if the configuration is ok

if [ "$RECORD" == "" ] ; then
	ERROR=1
elif [ "$CANCEL" == "" ] ; then
	ERROR=1
elif [ ! -e $RECORD ] ; then
	ERROR=1
elif [ ! -e $CANCEL ] ; then
	ERROR=1
fi

# Implemantation of the gshowtv interface

function list () {
    for file in $(ls $DIR/*/* | sort) ; do
	RESULT=$(basename $file | perl -pe 's/.(\d+)\.(\d+)/ $1 $2/')
	set $RESULT
	CID=$1
	START=$2
	STOP=$3

	VIDEO=$(head -n 1 $file)
	TITLE=$(head -n 2 $file | tail -n 1)

	LINES=$(($(wc -l $file | perl -pe 's/\s+.*//') - 2 ))
	DESC=$(tail -n $LINES $file | perl -pe 's/[\r\n]/ /g')

	echo "title    = $TITLE"
	echo "desc     = $DESC"
	echo "channel  = $CID"
	echo "start    = $START"
	echo "stop     = $STOP"

	echo $file | grep "ondisk" > /dev/null

	if [ $? == 0 ] ; then
	    echo "file    = $VIDEO"
	elif [ -e "$VIDEO" ] ; then
	    echo "file    = $VIDEO"

	    echo $file | grep "ondisk" > /dev/null

	    if [ $? == 1 ] ; then
		mv -f $file $DIR/ondisk/
	    fi
	fi

	echo
    done
}

##
# Record command
# Parameters: Channel, Start, Stop, Title
#
# Response: 
# Exit with 0 if success
# Exit with 1 if overlapping programs
# Exit with 2 if unknown channel
# Exit with 3 or above are considered other errors.

function record () {
    CHANNEL=$1
    START=$2
    STOP=$3
    TITLE=$4
    DESC=$5

    FILE="$DIR/recording/$CHANNEL.$START.$STOP"

    if [ $ERROR == 1 ] ; then
    	exit 3
    fi
    
    $RECORD "$CHANNEL" "$START" "$STOP" "$TITLE" "$FILE"

    if [ $? != 0 ] ; then
	exit $?
    fi

    echo "$TITLE" >> $FILE
    echo "$DESC"  >> $FILE
}

##
# Cancel command
# Parameters: Channel, Start
#
# Response: 
# Exit with 0 if success
# Exit with non-zero if failure

function cancel () {
    CHANNEL=$1
    START=$2

    if [ $ERROR == 1 ] ; then
    	exit 1
    fi

    $CANCEL "$CHANNEL" "$START"

    rm -f $DIR/recording/$CHANNEL.$START.*
}

##
# Watch command
# Parameters: Channel, Start, Filename
# 
# Response:
# Exit with 0 if success
# Exit with 1 if file does not exists
# Exit with 2 or above are considered other errors.

function watch () {
    CHANNEL=$1
    START=$2
    FILE=$3

    if [ -e "$FILE" ] ; then
	gnome-open "$FILE"
	return 0
    else
	return 1
    fi
}

##
# Delete command
# Parameters: Channel, Start, Filename
# 
# Response:
# Exit with 0 if success
# Exit with non-zero if failure

function delete () {
    CHANNEL=$1
    START=$2
    FILE=$3

    rm -f "$FILE"
    rm -f $DIR/ondisk/$CHANNEL.$START.*
}

##
# Launch File Manager
# Parameters: NONE
#
# Response: NONE

function filemanager () {
    if [ -e $SHOWDIR ] ; then
	nautilus $SHOWDIR &
    fi
}

##
# Info command
# Parameters: NONE
#
# Response: Information about this interface script.

function info () {
    echo "Legacy PVR interface<br>"
    echo "<br>"
    echo "This interface provides support for the PVR interface used in gshowtv 0.9.2 "
    echo "and earlier. This interface implements the old interface and can be used as "
    echo "a drop-in replacement. Actual recording features require configuration, the "
    echo "record and cancel scripts have to be configured for this interface. The old "
    echo "scripts are used.<br>"
    echo "<br>"
    echo "The configuration has to be done to the file:<br>"
    echo "<br>"
    echo "$CONF<br>"
    echo "<br>"
    echo "Please open up the file and set the record and cancel scripts.<br>"
    echo "<br>"
    echo "Interface version: $(interfaceversion)<br>"
    echo "CVS Version      : \$Id: legacy-interface,v 1.10 2006/04/05 09:04:03 pvakevai Exp $"
}

##
# Interface version
# Parameters: NONE
#
# Response: Return the interface version number. For now, this is "1.0"
#

function interfaceversion () {
    echo "1.0"
}

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

# Just for debugging purposes, log all calls to this script

echo "$(date) $(basename $0) $@" >> /tmp/gshowtv-interface.log

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

# See what should be done


case "$1" in
    list)
       list
       ;;

    record)
       record "$2" "$3" "$4" "$5" "$6"
       ;;
    
    cancel)
        cancel "$2" "$3"
	;;
	
    watch)
        watch "$2" "$3" "$4"
        ;;

    delete)
        delete "$2" "$3" "$4"
        ;;

    filemanager)
        filemanager
	;;

    info)
        info
	;;

    interfaceversion)
        interfaceversion
	;;
    
    *)
        echo "Usage: $0 {list|record|cancel|watch|delete|filemanager|info|interfaceversion}"
	exit 2
	;;
esac
