#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)

. /etc/profile

net_tuner_num_fix() {
  local num=$1

  [ -z "$num" ] && num=1
  num=$(( $num *1 ))
  [ $num -lt 1 ] && num=1
  num=$(( $num -1 ))
  echo $num
}

SUNDTEK_ADDON_DIR="$HOME/.kodi/addons/driver.dvb.sundtek-mediatv"
SUNDTEK_ADDON_HOME="$HOME/.kodi/userdata/addon_data/driver.dvb.sundtek-mediatv"
SUNDTEK_ADDON_SETTINGS="$SUNDTEK_ADDON_HOME/settings.xml"

mkdir -p $SUNDTEK_ADDON_HOME

# remove the entry
BUILD_DATE="#"
sed -i "s|\(id=\"BUILD_DATE\" .* values=\)\"[^\"]*\"|\1\"$BUILD_DATE\"|" $SUNDTEK_ADDON_DIR/resources/settings.xml

if [ ! -f "$SUNDTEK_ADDON_HOME/sundtek.conf" ]; then
  cp $SUNDTEK_ADDON_DIR/config/* $SUNDTEK_ADDON_HOME/
  
  # update driver on first run
  echo "update driver" >/tmp/ppp
  systemd-run $SUNDTEK_ADDON_DIR/bin/sundtek-update-driver.sh
  exit 0	# will be started later
else
  # in case of missing entries in addon home's sundtek.conf
  entry_set="$(grep use_hwpidfilter $SUNDTEK_ADDON_HOME/sundtek.conf 2>/dev/null)"
  if [ -z "$entry_set" ]; then
    sed -i 's|^device_attach=.*|# device_attach not used anymore\n\n# enable HW PID filter\nuse_hwpidfilter=off\n\n# enable listening on network\nenablenetwork=off|g' $SUNDTEK_ADDON_HOME/sundtek.conf
    sed -i 's|^#first_adapter=.*|first_adapter=0|g' $SUNDTEK_ADDON_HOME/sundtek.conf

    sed -i 's|.*network tuner IP address (OpenELEC specific).*||g' $SUNDTEK_ADDON_HOME/sundtek.conf
    sed -i 's|.*network_tuner_ip=.*||g' $SUNDTEK_ADDON_HOME/sundtek.conf
  fi
fi

if [ ! -f "$SUNDTEK_ADDON_SETTINGS" ]; then
  cp $SUNDTEK_ADDON_DIR/settings-default.xml $SUNDTEK_ADDON_SETTINGS
fi

[ ! -f $SUNDTEK_ADDON_HOME/rc_key_enter.map ] && mv $SUNDTEK_ADDON_HOME/rc_key_enter $SUNDTEK_ADDON_HOME/rc_key_enter.map
[ ! -f $SUNDTEK_ADDON_HOME/rc_key_ok.map ] && mv $SUNDTEK_ADDON_HOME/rc_key_ok $SUNDTEK_ADDON_HOME/rc_key_ok.map

mkdir -p /var/config

# check settings version
XML_SETTINGS_VER="$(xmlstarlet sel -t -m settings -v @version $SUNDTEK_ADDON_SETTINGS)"
if [ "$XML_SETTINGS_VER" = "2" ]; then
  xmlstarlet sel -t -m settings/setting -v @id -o "=\"" -v . -o "\"" -n "$SUNDTEK_ADDON_SETTINGS" > /var/config/sundtek-addon.conf
else
  xmlstarlet sel -t -m settings -m setting -v @id -o "=\"" -v @value -o "\"" -n "$SUNDTEK_ADDON_SETTINGS" > /var/config/sundtek-addon.conf
fi

. /var/config/sundtek-addon.conf

# check if there is new driver on web
if [ "$CHECK_BOOT" = "true" ]; then
  if [ ! -f /var/run/sundtek.checked ]; then
    # only once after boot
    touch /var/run/sundtek.checked

(
    # wait some time before checking
    sleep 20
    cd $SUNDTEK_ADDON_DIR

    wget -O version.latest http://sundtek.de/media/latest.phtml

    version_used=$(cat version.used)
    version_latest=$(cat version.latest)
    if [ "$version_used" != "$version_latest" ]; then
      # new version exist
      kodi-send -a "Notification(Sundtek, \"New driver version exist, update manually\", 4000, $SUNDTEK_ADDON_DIR/icon.png)"
    fi
)&
  fi
fi

if [ -z "$(pidof mediasrv)" ]; then
  rm -f /var/log/mediasrv.log
  rm -f /var/log/mediaclient.log

  SUNDTEK_CONF_TMP=/tmp/sundtek.conf
  cp $SUNDTEK_ADDON_HOME/sundtek.conf $SUNDTEK_CONF_TMP

  [ -z "$LOWEST_ADAPTER_NUM" ] && LOWEST_ADAPTER_NUM=0
  sed -i "s|^first_adapter=.*|first_adapter=$LOWEST_ADAPTER_NUM|g" $SUNDTEK_CONF_TMP

  # enable HW PID filter
  if [ "$ENABLE_HW_PID_FILTER" = "true" ]; then
    sed -i 's|^use_hwpidfilter=.*|use_hwpidfilter=on|g' $SUNDTEK_CONF_TMP
  else
    sed -i 's|^use_hwpidfilter=.*|use_hwpidfilter=off|g' $SUNDTEK_CONF_TMP
  fi

  # enable IR receiver
  if [ "$ENABLE_IR_RECEIVER" = "true" ]; then
    sed -i 's|^ir_disabled=.*|ir_disabled=0|g' $SUNDTEK_CONF_TMP
  else
    sed -i 's|^ir_disabled=.*|ir_disabled=1|g' $SUNDTEK_CONF_TMP
  fi

  if [ "$ALLOW_NET_USE" = "true" ]; then
    sed -i 's|^enablenetwork=.*|enablenetwork=on|g' $SUNDTEK_CONF_TMP
  else
    sed -i 's|^enablenetwork=.*|enablenetwork=off|g' $SUNDTEK_CONF_TMP
  fi

  [ "$DEVICE1_IP" = "0.0.0.0" ] && DEVICE1_IP=""
  [ "$DEVICE2_IP" = "0.0.0.0" ] && DEVICE2_IP=""
  [ "$DEVICE3_IP" = "0.0.0.0" ] && DEVICE3_IP=""
  [ "$DEVICE4_IP" = "0.0.0.0" ] && DEVICE4_IP=""
  [ "$DEVICE5_IP" = "0.0.0.0" ] && DEVICE5_IP=""
  DEVICE1_NUM=$(net_tuner_num_fix $DEVICE1_NUM)
  DEVICE2_NUM=$(net_tuner_num_fix $DEVICE2_NUM)
  DEVICE3_NUM=$(net_tuner_num_fix $DEVICE3_NUM)
  DEVICE4_NUM=$(net_tuner_num_fix $DEVICE4_NUM)
  DEVICE5_NUM=$(net_tuner_num_fix $DEVICE5_NUM)

  if [ "$USE_NET_TUNERS" = "true" -a -n "$DEVICE1_IP" ]; then
    # delete all network tuner entries
    awk '/^\[NETWORK\]/{flag=1; next} /^[^\[]|^$/{if (flag==1) next} /.*/{flag=0; print}' $SUNDTEK_CONF_TMP >${SUNDTEK_CONF_TMP}-net
    mv ${SUNDTEK_CONF_TMP}-net $SUNDTEK_CONF_TMP
    echo "" >>$SUNDTEK_CONF_TMP
    # remove empty lines at the end of file
    sed -i -e ':a' -e '/^\n*$/{$d;N;};/\n$/ba' $SUNDTEK_CONF_TMP
    # add entries
    echo -e "\n[NETWORK]" >>$SUNDTEK_CONF_TMP
    for dev in $(seq 0 $DEVICE1_NUM); do
      echo "device=$DEVICE1_IP:$dev" >>$SUNDTEK_CONF_TMP
    done
    if [ -n "$DEVICE2_IP" ]; then
      for dev in $(seq 0 $DEVICE2_NUM); do
        echo "device=$DEVICE2_IP:$dev" >>$SUNDTEK_CONF_TMP
      done
      if [ -n "$DEVICE3_IP" ]; then
        for dev in $(seq 0 $DEVICE3_NUM); do
          echo "device=$DEVICE3_IP:$dev" >>$SUNDTEK_CONF_TMP
        done
        if [ -n "$DEVICE4_IP" ]; then
          for dev in $(seq 0 $DEVICE4_NUM); do
            echo "device=$DEVICE4_IP:$dev" >>$SUNDTEK_CONF_TMP
          done
          if [ -n "$DEVICE5_IP" ]; then
            for dev in $(seq 0 $DEVICE5_NUM); do
              echo "device=$DEVICE5_IP:$dev" >>$SUNDTEK_CONF_TMP
            done
          fi
        fi
      fi
    fi
  else
    # delete all network tuner entries
    awk '/^\[NETWORK\]/{flag=1; next} /^[^\[]|^$/{if (flag==1) next} /.*/{flag=0; print}' $SUNDTEK_CONF_TMP >${SUNDTEK_CONF_TMP}-net
    mv ${SUNDTEK_CONF_TMP}-net $SUNDTEK_CONF_TMP
    echo "" >>$SUNDTEK_CONF_TMP
    # remove empty lines at the end of file
    sed -i -e ':a' -e '/^\n*$/{$d;N;};/\n$/ba' $SUNDTEK_CONF_TMP
  fi

  if [ "$ENABLE_TUNER_TYPES" = "true" ]; then
    # get tuner serial numbers
    SERIALS=$(cat /var/config/sundtek-addon.conf | sed -n 's|^ATTACHED_TUNER_\(.*\)_DVBMODE=.*|\1|gp' | sort | uniq)
    . /var/config/sundtek-addon.conf

    for SERIAL in $SERIALS; do
      DVBMODE=$(eval echo \$ATTACHED_TUNER_${SERIAL}_DVBMODE)
      IRPROT=$(eval echo \$ATTACHED_TUNER_${SERIAL}_IRPROT)
      KEYMAP=$(eval echo \$ATTACHED_TUNER_${SERIAL}_KEYMAP)

      if [ "$DVBMODE" = "DVB-T" ]; then
        # only set DVB-T because default is DVB-C (and DVB-S/S2 is not set either)
        DVBMODE="DVBT"
      else
        DVBMODE=""
      fi

      [ "$IRPROT" = "NEC" -o "$IRPROT" = "auto" ] && IRPROT=""

      [ ! -f $KEYMAP ] && KEYMAP=""

      # remove setttings for this tuner
      awk -v val="[$SERIAL]" '$0 == val {flag=1; next} /^[^\[]|^$/{if (flag==1) next} /.*/{flag=0; print}' $SUNDTEK_CONF_TMP >${SUNDTEK_CONF_TMP}-types
      mv ${SUNDTEK_CONF_TMP}-types $SUNDTEK_CONF_TMP
      echo "" >>$SUNDTEK_CONF_TMP
      # remove empty lines at the end of file
      sed -i -e ':a' -e '/^\n*$/{$d;N;};/\n$/ba' $SUNDTEK_CONF_TMP

      echo "" >>$SUNDTEK_CONF_TMP
      echo "[$SERIAL]" >>$SUNDTEK_CONF_TMP

      [ -n "$DVBMODE" ] && echo "initial_dvb_mode=$DVBMODE" >>$SUNDTEK_CONF_TMP

      if [ "$ENABLE_IR_RECEIVER" = "true" ]; then
        echo "ir_disabled=0" >>$SUNDTEK_CONF_TMP
      else
        echo "ir_disabled=1" >>$SUNDTEK_CONF_TMP
      fi

      [ -n "$IRPROT" ] && echo "ir_protocol=$IRPROT" >>$SUNDTEK_CONF_TMP
      [ -n "$KEYMAP" ] && echo "rcmap=$KEYMAP" >>$SUNDTEK_CONF_TMP

      echo "" >>$SUNDTEK_CONF_TMP
    done
  fi

  md5_1=$(md5sum -b $SUNDTEK_CONF_TMP | awk '{print $1}')
  md5_2=$(md5sum -b $SUNDTEK_ADDON_HOME/sundtek.conf | awk '{print $1}')
  if [ "$md5_1" != "$md5_2" ]; then
    # file changed - copy to addon home
    cp $SUNDTEK_CONF_TMP $SUNDTEK_ADDON_HOME/sundtek.conf
  fi

  chmod +x $SUNDTEK_ADDON_DIR/bin/*
  mediasrv --wait-for-devices -p $SUNDTEK_ADDON_DIR/bin -c $SUNDTEK_ADDON_HOME/sundtek.conf -d

  # wait few seconds
  [ -z "$SETTLE_TIME" ] && SETTLE_TIME=0
  SETTLE_TIME=$(( $SETTLE_TIME *1 ))
  if [ $SETTLE_TIME -gt 0 ]; then
    logger -t Sundtek "### Settle for $SETTLE_TIME sec ###"
    sleep $SETTLE_TIME
  fi

  if [ "$RUN_USER_SCRIPT" = "true" -a -f "$SUNDTEK_ADDON_HOME/userscript.sh" ]; then
    logger -t Sundtek "### Running user script $SUNDTEK_ADDON_HOME/userscript.sh ###"
    cat $SUNDTEK_ADDON_HOME/userscript.sh | dos2unix >/var/run/sundtek-userscript.sh
    sh /var/run/sundtek-userscript.sh
  fi

  # save adapter serial number
  serial_number_old=$(cat $SUNDTEK_ADDON_HOME/adapters.txt 2>/dev/null)
  serial_number_new=$(mediaclient -e | awk '/device / {print $0} /SERIAL/ {id=1} /ID:/ {if (id==1) print $2}')
  if [ "$serial_number_old" != "$serial_number_new" ]; then
    echo "$serial_number_new" >$SUNDTEK_ADDON_HOME/adapters.txt
  fi
fi

# change build date in addon settings
BUILD_DATE=$(mediasrv --build 2>&1)
sed -i "s|\(id=\"BUILD_DATE\" .* values=\)\"[^\"]*\"|\1\"$BUILD_DATE\"|" $SUNDTEK_ADDON_DIR/resources/settings.xml

logger -t Sundtek "### Sundtek ready ($BUILD_DATE) ###"
