#!/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.4a 2005.02.23
#        - Changed by Redneck Eyeball to work with EPGdb 2.0
# goepg 1.4b 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.
# getepg 1.6 2005.07.03
#        - Changed by Redneck Eyeball to work with EPGdb 2.2
# getepg 1.7 2005.07.03 - Redneck Eyeball
#        - Check if recording is running
#	 - added epg.dat creation for Dreamvcr compatibility
#	 - removed temp file usage


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

# Is Recording Active ?
RECORDINGSTATUS=`wget -O- -q http://root:dreambox@127.0.0.1/cgi-bin/status | grep "Recording:" | sed 's/.*<td>\(.*\)<\/td.*/\1/' | sed 's/ON/On/g' | sed 's/OFF/Off/g'`
if [ $RECORDINGSTATUS = "Off" ]
then
# Not recording, so download EPG

#
# Getting the current stream information
#
CURRCHAN=`wget -O- -q http://root:dreambox@127.0.0.1/xml/streaminfo | grep "<reference>" | sed s'/.*>\(.*\)<.*/\1/'`

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


#
# Updating EPG information
#
/var/tuxbox/plugins/mhwepg2 -l > `grep -e '/epgui.dat' /var/tuxbox/config/epgui.conf | cut -c 13-40`

#Copy the EPGUI.DAT file to the EPG.DAT file
EPGFILE=`grep -e '/epgui.dat' /var/tuxbox/config/epgui.conf | cut -c 13-40`
VCRFILE=`grep -e '/epgui.dat' /var/tuxbox/config/epgui.conf | cut -c 13-17` 
VCRFILE="$VCRFILE/epg.dat"
cp $EPGFILE $VCRFILE

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

#End check recording
fi

exit 0

#
# The End
#
