#!/bin/bash
############################################################################
#
# Interface script between a PVR and gshowtv. 
#
# This script performs no actions what so ever, it's only function is to 
# document the PVR interface.
#
# The PVR interface uses the following common parameters:
#
# Channel   - The channel name as understood by the PVR system. 
#             The user has to configure these to the application.
# 
# All times represented in the interface follow the "YYYYmmDDHHMMSS" 
# format. See "man date" for more info about the meaning of the string.
#

##
# List command lists all upcoming recordings and those already on disk.
# Parameters: NONE
#
# Response:
# Multiple entries of the following set of lines
#
# title     = <title>
# desc      = <description>
# channel   = <channel>
# start     = <starttime>
# stop      = <stoptime>
# category  = <category>
# file      = <filename>

#
# If the videofile is set, and the file it points to exists, then the
# entry is about an already recorded show.
# If the file does not exists, and the starttime is in the future, then
# the entry is an upcoming recording.
# 
# No data portion is allowed to contain CR/LF., and each line starting
# with title is considered as the beginning of a new entry.
# 
# - title is mandatory
# - channel is mandatory
# - start is mandatory

function list () {
    return 0
}

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

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

    return 0
}

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

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

    return 0
}

##
# 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
	# Do something to watch the 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
}

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

function filemanager () {
    # Open up a filemanager to the directory where the recorded shows are placed.
    return 0
}

##
# Info command
# Parameters: NONE
#
# Response: Information about this interface script. This is shown to the user
# when is selects the interface. The text can use the pango markup 
# http://developer.gnome.org/doc/API/2.0/pango/PangoMarkupFormat.html
# plus <br> tag.

function info () {
    echo "Document interface between gshowtv and a PVR backend.<br>"
    echo "<br>"
    echo "Interface version: $(interfaceversion)<br>"
    echo "CVS Version      : \$Id: pvr-interface,v 1.5 2006/03/27 06:50:54 pvakevai Exp $<br>"
}

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