#!/bin/bash
################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC 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.
#
#  OpenELEC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License$
#  along with OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

. /etc/profile

ADDON_HOME="$HOME/.kodi/userdata/addon_data/script.module.zap2xml"
ADDON_DIR="$HOME/.kodi/addons/script.module.zap2xml"
ADDON_SETTINGS="$ADDON_HOME/settings.xml"
XMLTV_OUTPUT="$ADDON_HOME/xmltv.xml"
if [ $# -lt 1 ]
then
  CD=`pwd`
  cd $ADDON_HOME
  USERID=`grep id=\"User\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,\",,g"`
  PASSWORD=`grep id=\"Pass\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,\",,g"`
  NUMDAYS=`grep id=\"days\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value,-d," -e "s,=, ," -e "s,\",,g"`
  EPGSOURCE=`grep id=\"source\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,1,-z," -e "s,0,," -e "s,\",,g"`
  CHFIRST=`grep id=\"cfirst\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,true,-F," -e "s,false,," -e "s,\",,g"`
  CHANNELICONS=`grep id=\"cicons\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,true,-i\ icons," -e "s,false,," -e "s,\",,g"`
  ALLCHANNELS=`grep id=\"allc\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,true,-a," -e "s,false,," -e"s,\",,g"`
  PROGRAMICONS=`grep id=\"picons\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,true,-I," -e "s,false,," -e "s,\",,g"`
  EXTRADETAILS=`grep id=\"xdetails\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,true,-D," -e "s,false,," -e "s,\",,g"`
  EXTRADESCRIPTION=`grep id=\"xdesc\" $ADDON_SETTINGS | awk '{print $3 }' | sed -e "s,value=,," -e "s,true,-X," -e "s,false,," -e "s,\",,g"`
  DESCSORT=`grep id=\"desc\"* $ADDON_SETTINGS | awk -v ORS=, '{gsub(/"/, "");print $3}' | tr -s 'value=' ' ' | sed 's/ //g' | echo "[$(cat)]" `
  python $ADDON_DIR/zap2xml.py -u $USERID -p $PASSWORD -o $XMLTV_OUTPUT $EPGSOURCE $NUMDAYS $CHFIRST $ALLCHANNELS $CHANNELICONS $PROGRAMICONS $EXTRADETAILS $EXTRADESCRIPTION -V $DESCSORT > zap2xml.log 2>&1
  cat $XMLTV_OUTPUT
  cd $CD
  exit 0
fi

while [ $# -gt 0 ]
do case "$1" in
   -d | --description )
     printf "tv_grab_zap2xml is a simple wrapper for zap2xml.py\n"
     ;;

   -v | --version )
     printf "0.6.6\n"
     ;;

   -c | --capabilities )
     printf "baseline\n"
     ;;

    -* )
      printf "unknown option: %s\n" "$1"
      printf "Usage: %s: [--description] [--version] [--capabilities]\n" ${0##*/}
      exit 2
      ;;

esac; shift
done

exit 0
