#!/bin/bash

################################################################################
################################################################################
# Copyright 2023-2025 by NI SP Software GmbH, All rights reserved.
# Copyright 1999-2023 by Nice, srl., All rights reserved.
#
# This software includes confidential and proprietary information
# of NI SP Software GmbH ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with NI SP Software.
################################################################################
################################################################################

. "${EF_ROOT}/plugins/ef/lib/utils"
_my_awk=`ef_find_awk`
_my_hash=`ef_find_hash`

EF_PLUGIN_NAME="EnginFrame RSS Plug-in"
export EF_PLUGIN_NAME

FAILED_COMMAND="$0"

#-------------------------------------------------------------------------------
# _print_error
# Private utility function used to print errors
# Syntax: _print_error <message>
#  message: the error message
#-------------------------------------------------------------------------------
_print_error() {
  if [ -n "$RSS_AUTH" ]; then
    echo "<![CDATA[$1]]> Please contact your EnginFrame administrator."
    exit 1
  fi

  if [ -n "$RSS_GENERATE" ]; then
    printf "<efrss:metadata>\n"
    set_base_url "http://${SERVER_NAME}:${SERVER_PORT}/${EF_ROOT_CONTEXT}"
    set_channel_title "<![CDATA[${EF_PLUGIN_NAME} Error]]>"
    set_channel_description "<![CDATA[$1]]> Please contact your EnginFrame administrator."
    cat << EOF
      <!-- Error: $1 -->
  <ef:logout authority="rss" />
</efrss:metadata>
EOF
    exit 0
  fi

  cat << EOF
  <ef:error>
   <ef:title><![CDATA[${EF_PLUGIN_NAME} Error]]></ef:title>
  <ef:message><![CDATA[$1]]> Please contact your EnginFrame administrator.</ef:message>
  <ef:command><![CDATA[$FAILED_COMMAND]]></ef:command>
</ef:error>
EOF
  exit 1
}

#-------------------------------------------------------------------------------
# Set RSS plugin data file
#-------------------------------------------------------------------------------

RSS_DATA_FILE="${EF_DATA_ROOT}/plugins/rss/links.data"
export RSS_DATA_FILE

#-------------------------------------------------------------------------------
# Check the permissions of the RSS_DATA_FILE
#-------------------------------------------------------------------------------

if [ ! -f "${RSS_DATA_FILE}" ]; then
  _print_error "The file '${RSS_DATA_FILE}' does not exist."
fi

if [ ! -w "${RSS_DATA_FILE}" ]; then
  _print_error "The file '${RSS_DATA_FILE}' is not writable."
fi

if [ ! -r "${RSS_DATA_FILE}" ]; then
  _print_error "The file '${RSS_DATA_FILE}' is not readable."
fi

#-------------------------------------------------------------------------------
# add_feed
# This function add a RSS feed to the list of feeds provided by Enginframe. It
# must be used by the ef.rss.discovery scripts of plugins to register a new
# feed.
# Syntax: add_feed <service_id> <service_name> <provider_name>
#  service_id: the ID of the service provided by the feed
#  service_name: the name of the service provided by the feed
#  provider_name: the name of the feed provider
#
# See ${EF_ROOT}/plugins/lsf/bin/ef.rss.discovery for a sample of usage.
#-------------------------------------------------------------------------------
add_feed() {

  # RSS_PROVIDER_ID is a global variable assigned in
  # ${EF_ROOT}/plugins/rss/bin/rss.generate containing the directory of the
  # plugin adding the feed (it represent as a unique identifier for the plugin
  # providing the feed).

  _id_hash=`printf "${EF_USER}:${RSS_PROVIDER_ID}:$1" | "${_my_hash}" | sed 's/  -//'`

  # A part from potential race conditions, we are sure RSS_DATA_FILE is readable
  _found=`grep "^${_id_hash},${EF_USER},${RSS_PROVIDER_ID},$1$" "${RSS_DATA_FILE}" | wc -l | ${_my_awk} '{print $1}'`

  if [ $_found = "0" ]; then
    # A part from potential race conditions, we are sure RSS_DATA_FILE is writable
    printf "${_id_hash},${EF_USER},${RSS_PROVIDER_ID},$1\n" >> "${RSS_DATA_FILE}"
  fi

  # SERVER_NAME and SERVER_PORT are global variables defined by Enginframe
  # itself
  _link="/${EF_ROOT_CONTEXT}/rss/com.enginframe.rss.xml?_uri=//com.enginframe.rss/rss&_password=${_id_hash}&_id=${_id_hash}"

  _tr_class="EFEvenRow"

  cat << EOF
    <link rel="alternate" type="application/rss+xml" title="$(ef_xml_escape_attribute -i "$2")" href="$(ef_xml_escape_attribute -i "${_link}")" />
  <tr class="$_tr_class">
    <td><b>$(ef_xml_escape_content -i "$2")</b></td>
    <td align="right"><a href="$(ef_xml_escape_attribute -i "${_link}")"><img src="/$EF_ROOT_CONTEXT/rss/images/rss_icon.gif" border="0" /></a></td>
  </tr>
EOF
}

#-------------------------------------------------------------------------------
# set_channel_title ()
# Sets the RSS channel title
# Syntax: set_channel_title <title>
#  title: the channel title
#-------------------------------------------------------------------------------
set_channel_title()
{
  printf "<efrss:variable name=\"channel_title\">$(ef_xml_escape_content -i "$1")</efrss:variable>\n"
}

#-------------------------------------------------------------------------------
# set_channel_description ()
# Sets the RSS channel description
# Syntax: set_channel_description <description>
#  description: the channel description
#-------------------------------------------------------------------------------
set_channel_description()
{
  printf "<efrss:variable name=\"channel_description\">$(ef_xml_escape_content -i "$1")</efrss:variable>\n"
}

#-------------------------------------------------------------------------------
# set_base_url ()
# Sets the base URL that is used to generate the links associted with the
# channel and the RSS items.
# Syntax: set_base_uri <url>
#  url: the base URL
#-------------------------------------------------------------------------------
set_base_url()
{
  printf "<efrss:variable name=\"base_url\">$(ef_xml_escape_content -i "$1")</efrss:variable>\n"
}

#-------------------------------------------------------------------------------
# set_ttl ()
# Sets the TTL of the RSS channel
# Syntax: set_ttl <ttl>
#  url: the TTL
#-------------------------------------------------------------------------------
set_ttl()
{
  printf "<efrss:variable name=\"ttl\">$(ef_xml_escape_content -i "$1")</efrss:variable>\n"
}
