#!/bin/sh

CONFIG_FILE="/etc/3d-p/osi-ohiservice/osi-ohiservice"

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-ohiservice 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
}

CONFIG_FILE_MODTIME=0

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

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

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

# ... For each instance
for i in `/usr/bin/seq 0 3`; do
    PEERS_FILE_MODTIME=0
    PEERS_FILE="/etc/3d-p/osi-ohiservice/osi-ohiservice.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_OHI_SERVICE_ENABLED="No"
    DFLT_OHI_SERVICE_PORT=$(( 3996 + ${i} ))
    DFLT_OHI_SERVICE_QLEN="16"
    DFLT_OHI_SERVICE_TTL="1800"
    DFLT_OHI_SERVICE_KEEP_ALIVE_TIME="1800"
    DFLT_OHI_SERVICE_KEEP_ALIVE_INTERVAL="12"
    DFLT_OHI_SERVICE_KEEP_ALIVE_COUNT="5"
    DFLT_OHI_SERVICE_UDP_PEERS=""

    # ... Handle command line directive
    case $1 in
        "--help")
            usage
        ;;
        "--force")  # Force default values and write to config file
            OHI_SERVICE_ENABLED="${DFLT_OHI_SERVICE_ENABLED}"
            OHI_SERVICE_PORT="${DFLT_OHI_SERVICE_PORT}"
            OHI_SERVICE_QLEN="${DFLT_OHI_SERVICE_QLEN}"
            OHI_SERVICE_TTL="${DFLT_OHI_SERVICE_TTL}"
            OHI_SERVICE_KEEP_ALIVE_TIME="${DFLT_OHI_SERVICE_KEEP_ALIVE_TIME}"
            OHI_SERVICE_KEEP_ALIVE_INTERVAL="${DFLT_OHI_SERVICE_KEEP_ALIVE_INTERVAL}"
            OHI_SERVICE_KEEP_ALIVE_COUNT="${DFLT_OHI_SERVICE_KEEP_ALIVE_COUNT}"
            OHI_SERVICE_UDP_PEERS="${DFLT_OHI_SERVICE_UDP_PEERS}"

        ;;
        *) # VARIABLE = Newest Value (if exists) OR Config File Value (if exists) OR Default Value
            OHI_SERVICE_ENABLED="$(eval /bin/echo \${EXPORT_OHI_SERVICE_ENABLED_${i}-\${OHI_SERVICE_ENABLED_${i}-${DFLT_OHI_SERVICE_ENABLED}}})"
            OHI_SERVICE_PORT="$(eval /bin/echo \${EXPORT_OHI_SERVICE_PORT_${i}-\${OHI_SERVICE_PORT_${i}-\${DFLT_OHI_SERVICE_PORT}}})"
            OHI_SERVICE_QLEN="$(eval /bin/echo \${EXPORT_OHI_SERVICE_QLEN_${i}-\${OHI_SERVICE_QLEN_${i}-\${DFLT_OHI_SERVICE_QLEN}}})"
            OHI_SERVICE_TTL="$(eval /bin/echo \${EXPORT_OHI_SERVICE_TTL_${i}-\${OHI_SERVICE_TTL_${i}-\${DFLT_OHI_SERVICE_TTL}}})"
            OHI_SERVICE_KEEP_ALIVE_TIME="$(eval /bin/echo \${EXPORT_OHI_SERVICE_KEEP_ALIVE_TIME_${i}-\${OHI_SERVICE_KEEP_ALIVE_TIME_${i}-${DFLT_OHI_SERVICE_KEEP_ALIVE_TIME}}})"
            OHI_SERVICE_KEEP_ALIVE_INTERVAL="$(eval /bin/echo \${EXPORT_OHI_SERVICE_KEEP_ALIVE_INTERVAL_${i}-\${OHI_SERVICE_KEEP_ALIVE_INTERVAL_${i}-${DFLT_OHI_SERVICE_KEEP_ALIVE_INTERVAL}}})"
            OHI_SERVICE_KEEP_ALIVE_COUNT="$(eval /bin/echo \${EXPORT_OHI_SERVICE_KEEP_ALIVE_COUNT_${i}-\${OHI_SERVICE_KEEP_ALIVE_COUNT_${i}-${DFLT_OHI_SERVICE_KEEP_ALIVE_COUNT}}})"
            OHI_SERVICE_UDP_PEERS="$(eval /bin/echo \${EXPORT_OHI_SERVICE_UDP_PEERS_${i}-\${PEERS_IN_FILE-\${OHI_SERVICE_UDP_PEERS_${i}-${DFLT_OHI_SERVICE_UDP_PEERS}}}})"
        ;;
    esac

    # ... write to file
    /bin/echo "OHI_SERVICE_ENABLED_${i}=\"${OHI_SERVICE_ENABLED}\"
    OHI_SERVICE_PORT_${i}=\"${OHI_SERVICE_PORT}\"
    OHI_SERVICE_QLEN_${i}=\"${OHI_SERVICE_QLEN}\"
    OHI_SERVICE_TTL_${i}=\"${OHI_SERVICE_TTL}\"
    OHI_SERVICE_KEEP_ALIVE_TIME_${i}=\"${OHI_SERVICE_KEEP_ALIVE_TIME}\"
    OHI_SERVICE_KEEP_ALIVE_INTERVAL_${i}=\"${OHI_SERVICE_KEEP_ALIVE_INTERVAL}\"
    OHI_SERVICE_KEEP_ALIVE_COUNT_${i}=\"${OHI_SERVICE_KEEP_ALIVE_COUNT}\"
    OHI_SERVICE_UDP_PEERS_${i}=\"${OHI_SERVICE_UDP_PEERS}\"
    " >> ${CONFIG_FILE}
    build_peers_file "${OHI_SERVICE_UDP_PEERS}" "${PEERS_FILE}"
done

exit 0
