#!/bin/sh

CONFIG_FILE="/etc/3d-p/osi-udpproxy/osi-udpproxy"
NUM_SESSIONS=8

backup() { # $1: file to backup
    if [ -z ${1} ]; then return 1; fi
    if [ ! -f ${1} ]; then return 1; fi
    BACKUPS="${BACKUPS} ${1}"
    /bin/mv ${1} ${1}.b || return 1
    return 0
}

# ... Restore backups on error
error_trap() {
    for f in ${BACKUPS}; do
        /bin/mv ${f}.b ${f}
    done
    exit 1
}
# ... Delete backups on successful exit
at_exit() {
    RC="$?" # ... make sure the proper exit code is returned
    for f in ${BACKUPS}; do
        /bin/rm -f ${f}.b
    done
    exit ${RC}
}
trap error_trap HUP INT ILL ABRT FPE
trap at_exit EXIT

usage() {
    echo ""
    echo "Sets up the config file(s) used by the osi-udpproxy init.d script"
    echo ""
    echo "Usage: ${0} [option]"
    echo ""
    echo "Options:"
    echo "   --force      Sets the config file(s) to default values."
    echo "   --help       Shows this help message."
    exit 0
}

build_peers_file() { # $1 "peer list", $2 peers file name
    IFS_SAV=${IFS}
    IFS=","
    LIST=${1}
    FILE=${2}
    backup ${FILE}
    /bin/echo -n "" > ${FILE} # in case there are no peers, create empty file
    for peer in ${LIST}; do
        /bin/echo ${peer} >> ${FILE}
    done
    IFS=${IFS_SAV}
}

read_peers_file() { # $1: file
    RET=""
    if [ ! -f ${1} ]; then /bin/echo "Error! ${1} does not exist"; return 1; fi
    for peer in `/bin/cat ${1}`; do
        if [ -z ${RET} ]; then
            RET="${peer}"
        else
            RET="${RET},${peer}"
        fi
    done
    return 0
}

# ... Source + backup the config file if it exists
CONFIG_FILE_MODTIME=0
if [ -e ${CONFIG_FILE} ]; then
    . ${CONFIG_FILE}
    CONFIG_FILE_MODTIME=`/usr/bin/stat -c %Y ${CONFIG_FILE}`
    backup ${CONFIG_FILE}
fi

# ... Write top header
UDP_PROXY_VERSION=`/bin/cat /usr/share/3d-p/osi-udpproxy/version`
/bin/echo "# Default settings for osi-udpproxy
#
# This file is sourced by /bin/bash from /etc/init.d/osi-udpproxy
#
#

UDP_PROXY_VERSION=\"${UDP_PROXY_VERSION}\"" >> ${CONFIG_FILE}

# ... For each instance
for i in `/usr/bin/seq 0 $((NUM_SESSIONS-1))`; do
    PEERS_FILE_MODTIME=0
    PEERS_FILE="/etc/3d-p/osi-udpproxy/osi-udpproxy.peers.${i}"
    if [ -e ${PEERS_FILE} ] ; then
        PEERS_FILE_MODTIME=`/usr/bin/stat -c %Y ${PEERS_FILE}`
    fi

    # ... If the peer list in the peer file is newer than the config file,
    #     it must prevail (unless EXPORT_TCP_UDP_UDP_PEERS_${i} is defined...)
    unset PEERS_IN_FILE
    if [ ${PEERS_FILE_MODTIME} -gt ${CONFIG_FILE_MODTIME} ]; then
        read_peers_file ${PEERS_FILE}
        PEERS_IN_FILE=${RET}
    fi

    # ... Define default parameters
    DFLT_UDP_PROXY_ENABLED="No"
    DFLT_UDP_PROXY_RELAY="On"
    DFLT_UDP_PROXY_SRC_PORT=""
    DFLT_UDP_PROXY_DST_PORT=""
    DFLT_UDP_PROXY_IP1=""
    DFLT_UDP_PROXY_IP2=""
    DFLT_UDP_PROXY_PEERS=""

    # ... Handle command line directive
    case $1 in
        "--help")
            usage
        ;;
        "--force")  # Force default values and write to config file
            UDP_PROXY_ENABLED="${DFLT_UDP_PROXY_ENABLED}"
            UDP_PROXY_RELAY="${DFLT_UDP_PROXY_RELAY}"
            UDP_PROXY_SRC_PORT="${DFLT_UDP_PROXY_SRC_PORT}"
            UDP_PROXY_DST_PORT="${DFLT_UDP_PROXY_DST_PORT}"
            UDP_PROXY_IP1="${DFLT_UDP_PROXY_IP1}"
            UDP_PROXY_IP2="${DFLT_UDP_PROXY_IP2}"
            UDP_PROXY_PEERS="${DFLT_UDP_PROXY_PEERS}"
        ;;
        *) # VARIABLE = Newest Value (if exists) OR Config File Value (if exists) OR Default Value
            UDP_PROXY_ENABLED="$(eval /bin/echo \${EXPORT_UDP_PROXY_ENABLED_${i}-\${UDP_PROXY_ENABLED_${i}-${DFLT_UDP_PROXY_ENABLED}}})"
            UDP_PROXY_RELAY="$(eval /bin/echo \${EXPORT_UDP_PROXY_RELAY_${i}-\${UDP_PROXY_RELAY_${i}-${DFLT_UDP_PROXY_RELAY}}})"
            UDP_PROXY_SRC_PORT="$(eval /bin/echo \${EXPORT_UDP_PROXY_SRC_PORT_${i}-\${UDP_PROXY_SRC_PORT_${i}-${DFLT_UDP_PROXY_SRC_PORT}}})"
            UDP_PROXY_DST_PORT="$(eval /bin/echo \${EXPORT_UDP_PROXY_DST_PORT_${i}-\${UDP_PROXY_DST_PORT_${i}-${DFLT_UDP_PROXY_DST_PORT}}})"
            UDP_PROXY_IP1="$(eval /bin/echo \${EXPORT_UDP_PROXY_IP1_${i}-\${UDP_PROXY_IP1_${i}-${DFLT_UDP_PROXY_IP1}}})"
            UDP_PROXY_IP2="$(eval /bin/echo \${EXPORT_UDP_PROXY_IP2_${i}-\${UDP_PROXY_IP2_${i}-${DFLT_UDP_PROXY_IP2}}})"
            UDP_PROXY_PEERS="$(eval /bin/echo \${EXPORT_UDP_PROXY_PEERS_${i}-\${PEERS_IN_FILE-\${UDP_PROXY_PEERS_${i}-${DFLT_UDP_PROXY_PEERS}}}})"
        ;;
    esac
    # ... Get new version
    /bin/echo "UDP_PROXY_ENABLED_${i}=\"${UDP_PROXY_ENABLED}\"
UDP_PROXY_RELAY_${i}=\"${UDP_PROXY_RELAY}\"
UDP_PROXY_SRC_PORT_${i}=\"${UDP_PROXY_SRC_PORT}\"
UDP_PROXY_DST_PORT_${i}=\"${UDP_PROXY_DST_PORT}\"
UDP_PROXY_PEERS_${i}=\"${UDP_PROXY_PEERS}\"" >> ${CONFIG_FILE}
    build_peers_file "${UDP_PROXY_PEERS}" "${PEERS_FILE}"
done

# ... Generate the XML file
if [ -f /usr/sbin/osi-udpproxy-xml ]; then
    export XML_DESCRIPTION_VERSION="`/bin/cat /usr/share/3d-p/osi-udpproxy/version`"
    XML_FILE="/etc/3d-p/osi-udpproxy/osi-udpproxy_${XML_DESCRIPTION_VERSION}.xml"
    backup ${XML_FILE}
    if ! /usr/sbin/osi-udpproxy-xml > ${XML_FILE}; then
        /bin/echo "Error! Command failed: /usr/sbin/osi-udpproxy-xml" >&2
        error_trap
    fi
fi

exit 0
