#!/bin/sh
#
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
#
# goepg 1.1 2004.10.30 by tCC
# goepg 1.2 2004.10.31 changed by mirakels
#        - add -p to mkdir
#        - introduce a few vars to simplify configuration of script
#        - keep current stream info in var (e.g. no need for tmp file)
#        - only switch stream if needed
# goepg 1.3 2004.11.22 - mirakels
#        - determine time offset from current date
# goepg 1.4 2005.04.19 -  mirakels
#        - use dreambox status instead of streaminfo to get current stream
#          (this is no problem now since httpauth is assumed active)
# goepg 1.5 2005.06.30 - mirakels
#        - adapt to the streaminfo changes in enigma: uses xml know.
#


#
# Use Nederland 2 for EPG data
#
EPGCHAN="1:0:1:fac:451:35:c00000:0:0:0:"

#
# Hack: date output is like 0200 which sh interprets as octal.
#       to get GMToffset in hours, devide by octal 0100
#
let zone=`date +%z`/0100
let TIMEOFFSET=1-$zone

#
# location for MV files
#
# MVDIR should be set in environment.
# Evenbetter: we should get it from  the mv config files...
#
if [ -z "$MVDIR" ] ; then
    #
    # MVDIR is not configured, setup some default
    #
    MVDIR=/mnt/usb/mv   # default
    [ -d /mnt/cf/mv  ] && MVDIR=/mnt/cf/mv
    [ -d /mnt/usb/mv ] && MVDIR=/mnt/usb/mv
    [ -d /hdd/mv     ] && MVDIR=/hdd/mv
fi

#
# MHW provider dir
#
mhwdir=mhw-nl

#
# Getting the current stream information
# both xml/streaminfo or xml/currentservicedata work...
#
CURRCHAN=`wget -O - -q http://root:dreambox@127.0.0.1/xml/streaminfo | grep "<reference>" | sed s'/.*>\(.*\)<.*/\1/'`
#CURRCHAN=`wget -O - -q http://root:dreambox@127.0.0.1/xml/currentservicedata | grep "<reference>" | sed s'/.*>\(.*\)<.*/\1/'`
#CURRCHAN=`wget -O - -q http://root:dreambox@127.0.0.1/cgi-bin/streaminfo | grep '<!-- .*:-->' | sed 's/.*<!-- \(.*\)-->/\1/'`
#CURRCHAN=`wget -O - -q http://root:dreambox@127.0.0.1/cgi-bin/status | grep 'service reference' | sed 's:.*</td><td>\(.*\)</td></tr>:\1:'`

#
#  Make sure the mhv directory exists
#
[ ! -d "$MVDIR/$mhwdir" ] && mkdir -p $MVDIR/$mhwdir
[ ! -d "$MVDIR/$mhwdir" ] && echo "Failed to create MHW $MVDIR/$mhwdir directory.  Exitting..." && exit 1

#
# Switching to EPG channel
#
[ "$CURRCHAN" != "$EPGCHAN" ] && wget -O /tmp/goepg.log -q "http://root:dreambox@127.0.0.1/cgi-bin/zapTo?path=$EPGCHAN"

#
# Updating EPG information
#
# Should get info on standby status so we only show message when not in standby
# however, http://127.0.0.1/status needs authentication and I don't like to add
# passwords in a script. However the passowrtd is all over now so could as wel
# use it for messages. But then agian I run this script when the box is in
# standby most of the time so why bother with messages...
#
# Also when a recording is running we probably should delay getting EPG info...
#
### wget -O - -q "http://root:dreambox@127.0.0.1/cgi-bin/xmessage?timeout=0&caption=Please+wait&body=Updating+EPG+information..."

/var/bin/mhwepg -p $MVDIR/$mhwdir -t $TIMEOFFSET

#wget -O /tmp/goepg.log -q "http://root:dreambox@127.0.0.1/cgi-bin/rc?1"

#
# zapping back
#
[ "$CURRCHAN" != "$EPGCHAN" ] && wget -O /tmp/goepg.log -q http://root:dreambox@127.0.0.1/cgi-bin/zapTo?path=$CURRCHAN

### wget -O - -q "http://root:dreambox@127.0.0.1/cgi-bin/xmessage?timeout=5&caption=Ready&body=EPG+information+updated."

#
# Removing temporary files
#
rm /tmp/goepg.*

exit 0

#
# The End
#
