#!/bin/sh

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2016-present Team LibreELEC (https://libreelec.tv)

. /etc/profile

oe_setup_addon service.tvheadend43

ADDON_SETTINGS="$ADDON_HOME/settings.xml"

COMSKIP_DIR="$ADDON_HOME/comskip"
COMSKIP_SETTINGS_FILE="$COMSKIP_DIR/comskip.ini"

DVR_SETTINGS_DIR="$ADDON_HOME/dvr/config"
DVR_SETTINGS_FILE="$DVR_SETTINGS_DIR/8d0f5b7ae354d956d7fe5db25f5d0d24"

EPGGRAB_SETTINGS_DIR="$ADDON_HOME/epggrab"
EPGGRAB_SETTINGS_FILE="$EPGGRAB_SETTINGS_DIR/config"

TIMESHIFT_SETTINGS_DIR="$ADDON_HOME/timeshift"
TIMESHIFT_SETTINGS_FILE="$TIMESHIFT_SETTINGS_DIR/config"

TVH_SETTINGS_DIR="$ADDON_HOME"
TVH_SETTINGS_FILE="$TVH_SETTINGS_DIR/config"

XMLTV_SETTINGS_DIR="$ADDON_HOME/xmltv"
XMLTV_SETTINGS_FILE="$XMLTV_SETTINGS_DIR/config"

TIMESHIFT_DIR="$ADDON_HOME/cache/timeshift"
XMLTV_FILE="$ADDON_DIR/bin/tv_grab_file"

chmod a+x $ADDON_DIR/bin/*

# workaround to support old 4.1.x version upgrade for Tvh after 4.1.2369
if [ -f "$ADDON_HOME/dvr/config/dvr-config" ]; then
  rm $ADDON_HOME/dvr/config/dvr-config
fi

# workaround to create default Tvh directories without entering samba share
if [ ! -d "/storage/recordings" ]; then
  mkdir -p /storage/recordings /storage/picons/tvh /storage/picons/vdr
fi

if [ -f "$ADDON_HOME/channel/config/config.tmp" ]; then
  rm $ADDON_HOME/channel/config/config.tmp
fi

if [ -f "$ADDON_HOME/channel/config/tag.tmp" ]; then
  rm $ADDON_HOME/channel/config/tag.tmp
fi

# copy config files to userdata
if [ ! -f "$COMSKIP_SETTINGS_FILE" ]; then
  mkdir -p $COMSKIP_DIR
  cp $ADDON_DIR/defaults/comskip/comskip.ini $COMSKIP_SETTINGS_FILE
fi

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

if [ ! -f "$EPGGRAB_SETTINGS_FILE" ]; then
  mkdir -p $EPGGRAB_SETTINGS_DIR
  cp $ADDON_DIR/defaults/epggrab/config $EPGGRAB_SETTINGS_FILE
fi

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

if [ ! -f "$TVH_SETTINGS_FILE" ]; then
  mkdir -p $TVH_SETTINGS_DIR
  cp $ADDON_DIR/defaults/config $TVH_SETTINGS_FILE
fi

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

# delayed Tvh startup
if [ "$WORKAROUND_SLEEP" == "true" ]; then
  sleep $WORKAROUND_SLEEP_TIME
fi

# support FritzBox Sat>IP Server detection workaround for AVM-Repeater and AVM-6490
if [ "$AVM_SATIP_SERVER" == "true" ]; then
  if [ "$AVM_DEVICE" == "AVM_Repeater" ]; then
    AVM_SATIP="--satip_xml http://${AVM_ROUTER_IP}:49000/satipdesc.xml"
  fi
  if [ "$AVM_DEVICE" == "AVM_6490" ]; then
    AVM_SATIP="--satip_xml http://${AVM_ROUTER_FAKE_IP}:49000/satipdesc.xml"
    AVM_NUM="4"
  fi

  # add virtual ip as workaround for AVM limitation of one stream per ip
  # AVM-Repeater (2 tuners)
  ip address add ${AVM_IP1}/24 dev $AVM_ETH
  ip address add ${AVM_IP2}/24 dev $AVM_ETH

  # AVM-6490 (4 tuners)
  if [ "$AVM_NUM" = "4" ]; then
    ip address add ${AVM_IP3}/24 dev $AVM_ETH
    ip address add ${AVM_IP4}/24 dev $AVM_ETH
  fi
fi

# custom startup parameters
if [ "$CUSTOM_START" != "true" ]; then
  CUSTOM_ARGS=""
fi

# debug value generation
if [ "$DEBUG_LOG_TRACE_1" != "NONE" ]; then
  DEBUG_LOG_ARG="$DEBUG_LOG_TRACE_1"
fi

if [ "$DEBUG_LOG_TRACE_2" != "NONE" ]; then
  DEBUG_LOG_ARG="$DEBUG_LOG_ARG,$DEBUG_LOG_TRACE_2"
fi

if [ "$DEBUG_LOG_TRACE_3" != "NONE" ]; then
  DEBUG_LOG_ARG="$DEBUG_LOG_ARG,$DEBUG_LOG_TRACE_3"
fi

if [ "$DEBUG_LOG_TRACE_4" != "NONE" ]; then
  DEBUG_LOG_ARG="$DEBUG_LOG_ARG,$DEBUG_LOG_TRACE_4"
fi

if [ "$DEBUG_LOG_TRACE_5" != "NONE" ]; then
  DEBUG_LOG_ARG="$DEBUG_LOG_ARG,$DEBUG_LOG_TRACE_5"
fi

# if debuglog is activated and nothing selected to trace
if [ -z "$DEBUG_LOG_ARG" ];then
  DEBUG_LOG_ARG="all"
fi

# rename debuglog if already exist to prevent overwriting after crash
if [ -f "${DEBUG_LOG_PATH}" ]; then
  cp ${DEBUG_LOG_PATH} ${DEBUG_LOG_PATH}_$(date '+%Y-%m-%d_%H.%M.%S').txt
fi

# debug commandline
if [ "$DEBUG_LOG" = "true" ]; then
  TVHEADEND_ARG="-B -C -u root -g video -c $ADDON_HOME $AVM_SATIP $CUSTOM_ARGS -l ${DEBUG_LOG_PATH} --trace $DEBUG_LOG_ARG"
else
  TVHEADEND_ARG="-B -C -u root -g video -c $ADDON_HOME $AVM_SATIP $CUSTOM_ARGS"
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
