#!/bin/sh

#------------------------------------------------------------------------------
# osi-udpproxy-xml
#
# Generates an XML file fo use with the Enpoint Manager.
#

NUM_SESSIONS=8
# ... Source config file
FORCED_UDPPROXY=""
if [ ! -f "/etc/3d-p/osi-udpproxy/osi-udpproxy" ]; then
    if [ "${1}" != "--force" ]; then
      /bin/echo "/etc/3d-p/osi-udpproxy/osi-udpproxy" >&2
      exit 1
   fi
   FORCED_UDPPROXY="yes"
else
    . /etc/3d-p/osi-udpproxy/osi-udpproxy
fi

# ... Get version
if [ -z "${XML_DESCRIPTION_VERSION}" ]; then
    echo "Error! Version number not defined" >&2
    exit 1
fi

if [ "${FORCED_UDPPROXY}" = "yes" ]; then
    for i in `/usr/bin/seq 0 $((NUM_SESSIONS-1))`; do
      eval UDP_PROXY_ENABLED_${i}="No"
      eval UDP_PROXY_RELAY_${i}="On"
      eval UDP_PROXY_SRC_PORT_${i}=""
      eval UDP_PROXY_DST_PORT_${i}=""
      eval UDP_PROXY_IP1_${i}=""
      eval UDP_PROXY_IP2_${i}=""
      eval UDP_PROXY_PEERS_${i}=""
    done
fi

# ... Keep track of ports enabled
ENABLED_COUNT=0
# ... Keep a buffer of all XML text to be printed out
XML_TXT=""

# ... Appends XML data for the given port
append_serial_port_xml() { # ${1}: port index
    IFS_SAV=${IFS}
    IFS=","
# ... Get variables
    UDP_PROXY_ENABLED=$(eval /bin/echo \${"UDP_PROXY_ENABLED_${1}"} | /usr/bin/tr [A\-Z] [a\-z] )
    UDP_PROXY_RELAY=$(eval /bin/echo \${"UDP_PROXY_RELAY_${1}"} | /usr/bin/tr [A\-Z] [a\-z] )
    UDP_PROXY_SRC_PORT=$(eval /bin/echo \${"UDP_PROXY_SRC_PORT_${1}"} )
    UDP_PROXY_DST_PORT=$(eval /bin/echo \${"UDP_PROXY_DST_PORT_${1}"} )
#     UDP_PROXY_IP1=$(eval /bin/echo \${"UDP_PROXY_IP1_${1}"} )
#     UDP_PROXY_IP2=$(eval /bin/echo \${"UDP_PROXY_IP2_${1}"} )
    UDP_PROXY_PEERS=$(eval /bin/echo \${"UDP_PROXY_PEERS_${1}"} )
# ... Define selected status in XML lists
    if [ "${UDP_PROXY_ENABLED}" = "yes" ]; then
        ENABLED_COUNT=$((${ENABLED_COUNT}+1));
        local SELECTED_ENABLED_YES=" selected=\"yes\"";
    else
        local SELECTED_ENABLED_NO=" selected=\"yes\"";
    fi
    if [ "${UDP_PROXY_RELAY}" = "on" ]; then local SELECTED_RELAY_ON=" selected=\"yes\""; else local SELECTED_RELAY_OFF=" selected=\"yes\""; fi
# ... Define peers in edit lists
    PEER_EDIT_LIST=""
    for peer in ${UDP_PROXY_PEERS}; do
        PEER_EDIT_LIST="${PEER_EDIT_LIST}
                <value>${peer}</value>"
    done
    IFS=${IFS_SAV}
# ... Append new XML text to existing XML_TXT variable
    XML_TXT="${XML_TXT}
    <section name=\"Instance $(( ${1}+1 ))\">
        <list name=\"UDP_PROXY_ENABLED_${1}\" label=\"Instance $(( ${1}+1 )): Enabled\" mandatory=\"yes\">
            <description>Enables/disables this instance of UDP-PROXY</description>
            <value${SELECTED_ENABLED_YES}>Yes</value>
            <value${SELECTED_ENABLED_NO}>No</value>
        </list>
        <editlist name=\"UDP_PROXY_PEERS_${1}\" label=\"UDP Peers\" syntax=\"ipv4-address\" mandatory=\"yes\">
            <description>List of IP address where data must be transmitted. Example: {IP} 192.168.1.1</description>${PEER_EDIT_LIST}
        </editlist>
        <textbox name=\"UDP_PROXY_SRC_PORT_${1}\" label=\"RX Port\" syntax=\"integer\" mandatory=\"yes\">
            <description>Port to listen to for UDP messages from the host being proxied. Example: {port} 8080</description>
            <value>${UDP_PROXY_SRC_PORT}</value>
        </textbox>
        <textbox name=\"UDP_PROXY_DST_PORT_${1}\" label=\"TX Port\" syntax=\"integer\" mandatory=\"no\">
            <description>Messages are sent to all UDP Peers on this port if specified. Otherwise, the RX Port value is used. Example: {port} 8081</description>
            <value>${UDP_PROXY_DST_PORT}</value>
        </textbox>
    </section>"
}

# ... Build XML string
for i in `/usr/bin/seq 0 $((NUM_SESSIONS-1))`; do
    append_serial_port_xml ${i}
done
# ... Print to file
/bin/echo \
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<feature name=\"osi-udpproxy\" version=\"$XML_DESCRIPTION_VERSION\" label=\"UDP-PROXY\" enabled=\"$ENABLED_COUNT\">
    <about>Intelligent Endpoint&#8482; is a Trademark of 3D-P</about>${XML_TXT}
</feature>"

exit 0
