#!/bin/sh

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

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

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

if [ "${FORCED_TCPUDP}" = "yes" ]; then
    for i in `/usr/bin/seq 0 3`; do
      eval TCP_UDP_ENABLED_${i}="No"
      eval TCP_UDP_PEOL_${i}=""
      eval TCP_UDP_PAUSE_${i}="No"
      eval TCP_UDP_OHISERVIP_${i}="127.0.0.1"
      eval TCP_UDP_OHISERVPORT_${i}="$(( 3996 + ${i} ))"
      eval TCP_UDP_UDPIP_${i}=""
      eval TCP_UDP_RXUDP_${i}="$(( 3996 + ${i} ))"
      eval TCP_UDP_TXUDP_${i}="$(( 4000 + ${i} ))"
      eval TCP_UDP_DIRECTION_${i}="tcp2udp"
      eval TCP_UDP_KEEP_ALIVE_TIME_${i}="10"
      eval TCP_UDP_KEEP_ALIVE_INTERVAL_${i}="5"
      eval TCP_UDP_KEEP_ALIVE_COUNT_${i}="3"
      eval TCP_UDP_UDP_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
    TCP_UDP_ENABLED=$(eval /bin/echo \${"TCP_UDP_ENABLED_${i}"} | /usr/bin/tr [A-Z] [a-z] )
    TCP_UDP_PEOL=$(eval /bin/echo \${"TCP_UDP_PEOL_${i}"})
    TCP_UDP_PAUSE=$(eval /bin/echo \${"TCP_UDP_PAUSE_${i}"} | /usr/bin/tr [A-Z] [a-z] )
    TCP_UDP_OHISERVIP=$(eval /bin/echo \${"TCP_UDP_OHISERVIP_${i}"})
    TCP_UDP_OHISERVPORT=$(eval /bin/echo \${"TCP_UDP_OHISERVPORT_${i}"})
    TCP_UDP_UDPIP=$(eval /bin/echo \${"TCP_UDP_UDPIP_${i}"})
    TCP_UDP_RXUDP=$(eval /bin/echo \${"TCP_UDP_RXUDP_${i}"})
    TCP_UDP_TXUDP=$(eval /bin/echo \${"TCP_UDP_TXUDP_${i}"})
    TCP_UDP_DIRECTION=$(eval /bin/echo \${"TCP_UDP_DIRECTION_${i}"} | /usr/bin/tr [A-Z] [a-z] )
    TCP_UDP_KEEP_ALIVE_TIME=$(eval /bin/echo \${"TCP_UDP_KEEP_ALIVE_TIME_${i}"})
    TCP_UDP_KEEP_ALIVE_INTERVAL=$(eval /bin/echo \${"TCP_UDP_KEEP_ALIVE_INTERVAL_${i}"})
    TCP_UDP_KEEP_ALIVE_COUNT=$(eval /bin/echo \${"TCP_UDP_KEEP_ALIVE_COUNT_${i}"})
    TCP_UDP_UDP_PEERS=$(eval /bin/echo \${"TCP_UDP_UDP_PEERS_${i}"})
# ... Define selected status in XML lists
    if [ "${TCP_UDP_ENABLED}" = "yes" ]; then
        ENABLED_COUNT=$((${ENABLED_COUNT}+1));
        local SELECTED_ENABLED_YES=" selected=\"yes\"";
    else
        local SELECTED_ENABLED_NO=" selected=\"yes\"";
    fi
    case "${TCP_UDP_DIRECTION}" in
        "bidirectional") local SELECTED_BIDIRECTIONAL=" selected=\"yes\"" ;;
        "tcp2udp") local SELECTED_TCP2UDP=" selected=\"yes\"" ;;
        "udp2tcp") local SELECTED_UDP2TCP=" selected=\"yes\"" ;;
    esac
    if [ "${TCP_UDP_PAUSE}" = "yes" ]; then local SELECTED_PAUSE_YES=" selected=\"yes\""; else local SELECTED_PAUSE_NO=" selected=\"yes\""; fi
# ... Define peers in edit lists
    PEER_EDIT_LIST=""
    for peer in ${TCP_UDP_UDP_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 No.$(( ${1}+1 ))\">
        <list name=\"TCP_UDP_ENABLED_${1}\" label=\"Instance No.$(( ${1}+1 )): Enabled\" mandatory=\"yes\">
            <description>Enables/disables this instance of TCP-UDP</description>
            <value${SELECTED_ENABLED_YES}>Yes</value>
            <value${SELECTED_ENABLED_NO}>No</value>
        </list>
        <section name=\"Instance No.$(( ${1}+1 )) Configuration\">
            <list name=\"TCP_UDP_DIRECTION_${1}\" label=\"Data Flow\" mandatory=\"yes\">
                <description>Direction of data flow</description>
                <value${SELECTED_BIDIRECTIONAL}>bidirectional</value>
                <value${SELECTED_TCP2UDP}>tcp2udp</value>
                <value${SELECTED_UDP2TCP}>udp2tcp</value>
            </list>
            <textbox name=\"TCP_UDP_OHISERVIP_${1}\" label=\"TCP Server Host\" syntax=\"ipv4-address\" mandatory=\"yes\">
                <description>TCP Server host name or IP address. Example: {IP} 172.29.50.1</description>
                <value>${TCP_UDP_OHISERVIP}</value>
            </textbox>
            <textbox name=\"TCP_UDP_OHISERVPORT_${1}\" label=\"TCP Server Port\" syntax=\"integer\" mandatory=\"yes\">
                <description>TCP Server port number or service name. Example: {port} 8080</description>
                <value>${TCP_UDP_OHISERVPORT}</value>
            </textbox>
            <editlist name=\"TCP_UDP_UDP_PEERS_${1}\" label=\"UDP Peers\" syntax=\"udp-peer\" mandatory=\"yes\">
                <description>List of UDP peers where data must be transmitted. Example: {IP} {port} 172.29.50.2 8080</description>${PEER_EDIT_LIST}
            </editlist>
            <textbox name=\"TCP_UDP_TXUDP_${1}\" label=\"UDP TX Port\" syntax=\"integer\" mandatory=\"no\">
                <description>Messages are sent to this port on the UDP hosts, (when not specified in Peers List). Example: {port} 8082</description>
                <value>${TCP_UDP_TXUDP}</value>
            </textbox>
            <textbox name=\"TCP_UDP_RXUDP_${1}\" label=\"UDP RX Port\" syntax=\"integer\" mandatory=\"no\">
                <description>Messages are received from UDP hosts on this port. Example {port} 8081</description>
                <value>${TCP_UDP_RXUDP}</value>
            </textbox>
            <textbox name=\"TCP_UDP_PEOL_${1}\" label=\"PEOL\" syntax=\"byte\" mandatory=\"no\">
                <description>Primary end-of-line delimiter (decimal or hex). Example: {1 hexbyte} 0xAA</description>
                <value>${TCP_UDP_PEOL}</value>
            </textbox>
            <list name=\"TCP_UDP_PAUSE_${1}\" label=\"Pause\" mandatory=\"no\">
                <description>Insert a pause after the end of line delimiter</description>
                <value${SELECTED_PAUSE_YES}>Yes</value>
                <value${SELECTED_PAUSE_NO}>No</value>
            </list>
            <textbox name=\"TCP_UDP_KEEP_ALIVE_TIME_${1}\" label=\"Keep Alive Time\" syntax=\"byte\" mandatory=\"no\">
                <description>Time (seconds) after which keep-alive messages are sent. Example: {seconds} 10</description>
                <value>${TCP_UDP_KEEP_ALIVE_TIME}</value>
            </textbox>
            <textbox name=\"TCP_UDP_KEEP_ALIVE_INTERVAL_${1}\" label=\"Keep Alive Interval\" syntax=\"byte\" mandatory=\"no\">
                <description>Interval (seconds) between each keep-alive message. Example: {seconds} 5</description>
                <value>${TCP_UDP_KEEP_ALIVE_INTERVAL}</value>
            </textbox>
            <textbox name=\"TCP_UDP_KEEP_ALIVE_COUNT_${1}\" label=\"Keep Alive Count\" syntax=\"byte\" mandatory=\"no\">
                <description>Number of keep-alive messages to send. Example: {seconds} 3</description>
                <value>${TCP_UDP_KEEP_ALIVE_COUNT}</value>
            </textbox>
        </section>
    </section>"
}

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

exit 0
