#!/bin/sh
# 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 changed by mirakels
#        - determine time offset from current date
# goepg 1.4 2005.04.19 by mirakels
#        - use dreambox status instead of streaminfo to get current stream
#          (this is no problem now since httpauth is assumed active)

#
# 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
#
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 MV 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 want to add
# passwords in a script
### wget -O - -q "http://root:dreambox@127.0.0.1/cgi-bin/xmessage?timeout=0&caption=Please+wait&body=Updating+EPG+information..."
echo "epg.dat file moet hoer komen: $MVDIR/$mhwdir"
/var/bin/mhwepg -p $MVDIR/$mhwdir -t $TIMEOFFSET
#/var/bin/mhwepg -p $MVDIR/$mhwdir -t -1
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
#
