#! /bin/sh
# gets xmltv stuff from net and removes the Uusinta.* to get re-run
# marking working. There is 7 days offset, since finnish epg data is
# for 7 days and epg is more accurate in timings.

FETCH_PROGRAMS=0
FILL_DB=0

for PARAM
do
       case "$PARAM" in
       --feed)
               FEED_DB=1
               ;;
       --nofeed)
               FEED_DB=0
               ;;
       --fetch)
               FETCH_PROGRAMS=1
               ;;
       --nofetch)
               FETCH_PROGRAMS=0
               ;;
       *)
               echo "Invalid parameters!"
               exit 1;
               ;;
       esac
done

mkdir /tmp/channels

if [ "${FETCH_PROGRAMS}" = 1 ]
then
       /usr/bin/tv_grab_fi --quiet --config-file /stuff/videot/mythtv/.mythtv/HTV-telkku.xmltv --offset 7 --output /tmp/channels/xmltv_channels.tmp
       if [ "$?" != 0 ]
       then
               echo "Program list grabbing failed!"
               exit 1;
       fi
fi

sed  -e '/\<desc lang=.*\>/,/\<\/desc\>/s/\ Uusinta[a-zA-Z ]*\.//' \
            -e '/\<desc lang=.*\>/,/\<\/desc\>/s/\([uU]\)$//' \
            -e '/\<desc lang=.*\>/,/\<\/desc\>/s/\ [a-zA-Z ]*\ sarja\.//' \
            /tmp/channels/xmltv_channels.tmp > /tmp/channels/xmltv_channels.tmp2

if [ "${FEED_DB}" = 1 ]
then
       mythfilldatabase --file 1 /tmp/channels/xmltv_channels.tmp2 --update
fi
