#!/bin/sh

################################################################################
#      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.sd4tvh"
ADDON_DIR="$HOME/.kodi/addons/script.module.sd4tvh"
ADDON_SETTINGS="$ADDON_HOME/settings.xml"
if [ $# -lt 1 ]
then
  CD=`pwd`
  cd $ADDON_HOME
  USERID=`grep id=\"sd.username\" $ADDON_SETTINGS | awk '{print $2 }' | sed -e "s,id=\"sd.username\">,," -e "s,</setting>,,g"`
  PASSWORD=`grep id=\"sd.password\" $ADDON_SETTINGS | awk '{print $2 }' | sed -e "s,id=\"sd.password\">,," -e "s,</setting>,,g"`
  NUMDAYS=`grep id=\"days\" $ADDON_SETTINGS | awk '{print $2 }' | sed -e "s,id=\"days\">,-d ," -e "s,</setting>,,g"`
  HDHOME=`grep id=\"hdhomerun\" $ADDON_SETTINGS | awk '{print $2 }' | sed -e "s,id=\"hdhomerun\",," -e "s,default=\"true\",," -e "s,1, --hdhomerun discover," -e "s,0,," -e "s,</setting>,,g" -e "s, ,," -e "s,>,," `
  if [ "$HDHOME" == "2" ]
  then
    HDHOMEIP=`grep id=\"homerunip\" $ADDON_SETTINGS | awk '{print $2 }' | sed -e "s,id=\"homerunip\", --hdhomerun ," -e "s,default=\"true\",," -e "s,</setting>,,g" -e "s, ,," -e "s,>,,"`
  else
    HDHOMEIP=''
  fi
  CHFILTER=`grep id=\"cfilter\" $ADDON_SETTINGS | awk '{print $2 }' | sed -e "s,id=\"cfilter\">,," -e "s,true,--filter," -e "s,false,," -e "s,</setting>,,g"`
#  printf "$HDHOME" > sd4tvh.log 2>&1
  python $ADDON_DIR/sd4tvh.py -u "$USERID" -p "$PASSWORD" "$NUMDAYS" "$CHFILTER""$HDHOME""$HDHOMEIP" > sd4tvh.log 2>&1
  cat xmltv.xml
  cd $CD
  exit 0
fi

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

   -v | --version )
     printf "0.2.2\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
