#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2016 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

oe_setup_addon service.multimedia.tvheadend

ADDON_SETTINGS="$ADDON_HOME/settings.xml"
XMLTV_FILE="$ADDON_DIR/bin/tv_grab_file"
XMLTV_SETTINGS_DIR="$ADDON_HOME/xmltv"
XMLTV_SETTINGS_FILE="$XMLTV_SETTINGS_DIR/config"
DVR_SETTINGS_DIR="$ADDON_HOME/dvr"
DVR_SETTINGS_FILE="$DVR_SETTINGS_DIR/config"
DVR_DIR="$HOME/recordings"
TIMESHIFT_SETTINGS_DIR="$ADDON_HOME/timeshift"
TIMESHIFT_SETTINGS_FILE="$TIMESHIFT_SETTINGS_DIR/config"
TIMESHIFT_DIR="$ADDON_HOME/cache/timeshift"

chmod a+x $ADDON_DIR/bin/*

if [ "$WORKAROUND_SLEEP" == "true" ] ; then
  sleep $WORKAROUND_SLEEP_TIME
fi

if [ ! -f "$XMLTV_SETTINGS_FILE" ]; then
  mkdir -p $XMLTV_SETTINGS_DIR
  if [ -f $ADDON_DIR/xmltv-config ]; then
    cp $ADDON_DIR/xmltv-config $XMLTV_SETTINGS_FILE
  fi
fi

if [ ! -f "$DVR_SETTINGS_FILE" ]; then
  mkdir -p $DVR_DIR
  mkdir -p $DVR_SETTINGS_DIR
  if [ -f $ADDON_DIR/dvr-config ]; then
    cp $ADDON_DIR/dvr-config $DVR_SETTINGS_FILE
  fi
fi

if [ ! -f "$TIMESHIFT_SETTINGS_FILE" ]; then
  mkdir -p $TIMESHIFT_DIR
  mkdir -p $TIMESHIFT_SETTINGS_DIR
  if [ -f $ADDON_DIR/timeshift-config ]; then
    cp $ADDON_DIR/timeshift-config $TIMESHIFT_SETTINGS_FILE
  fi
fi

if [ "$DEBUG" = "yes" ]; then
  TVHEADEND_ARG="-B -C -s -u root -g video -c $ADDON_HOME"
else
  TVHEADEND_ARG="-B -C -u root -g video -c $ADDON_HOME"
fi

# start userspace DVB driver/addon
for driver_dvb in $(find /storage/.kodi/addons/driver.dvb.*/bin/userspace-driver.sh -type f 2>/dev/null); do
  driver_dvb_name=$(echo $driver_dvb | awk 'BEGIN {FS="/"} {printf("%s", $5)}')
  logger -t Tvheadend "### Loading userspace DVB driver: $driver_dvb_name ###"
  # use ". " because of variable export
  . $driver_dvb
done

if [ "$WAIT_FOR_FEINIT" == "true" ] ; then
  while [ true ] ; do
    if [ -e /dev/dvb/adapter$((NUM_ADAPTERS-1))/frontend0 ] ; then
      break
    fi
    sleep 1
  done
fi

if [ "$PRELOAD_CAPMT_CA" == "true" ] ; then
  logger -t Tvheadend "### Preloading capmt_ca.so library ###"
  LD_PRELOAD="$ADDON_DIR/bin/capmt_ca.so $LD_PRELOAD" exec $ADDON_DIR/bin/tvheadend $TVHEADEND_ARG &>$ADDON_LOG_FILE
else
  exec $ADDON_DIR/bin/tvheadend $TVHEADEND_ARG &>$ADDON_LOG_FILE
fi
