#!/bin/sh

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

. /etc/profile
oe_setup_addon service.rsyslog

[ "$RSYSLOG_MANUAL" == "true" ] && exit

(
  if [ ! -z "$RSYSLOG_IP_NAME_FQDN" ]
  then
    cat "$ADDON_DIR/config/common.conf"
    [ "$RSYSLOG_PROTOCOL" == "RELP" ] && cat "$ADDON_DIR/config/relp.conf"
    [ "$RSYSLOG_JOURNAL_LOGGING" == "true" ] && cat "$ADDON_DIR/config/journal.conf"
    [ "$RSYSLOG_KODI_LOGGING" == "true" ] && cat "$ADDON_DIR/config/kodi.conf"
    [ "$RSYSLOG_PROTOCOL" == "UDP" ] && PROTOCOL="@"
    [ "$RSYSLOG_PROTOCOL" == "TCP" ] && PROTOCOL="@@"
    [ "$RSYSLOG_PROTOCOL" == "RELP" ] && PROTOCOL="omrelp"
    [ "$RSYSLOG_PROTOCOL" == "RELP" ] && ZLIB="0" || [ "$RSYSLOG_ZLIB_COMPRESSION" == "true" ] && ZLIB="$RSYSLOG_ZLIB_COMPRESSION_LEVEL" || ZLIB="0"
    [ "$RSYSLOG_PROTOCOL" == "RELP" ] && echo "action(type=\"$PROTOCOL\" target=\"$RSYSLOG_IP_NAME_FQDN\" port=\"$RSYSLOG_PORT\")" || echo "action(type=\"omfwd\" target=\"$RSYSLOG_IP_NAME_FQDN\" protocol=\"$RSYSLOG_PROTOCOL\" port=\"$RSYSLOG_PORT\")"
  fi
) > "$ADDON_HOME/rsyslog.conf"
