#!/bin/bash
set -e
configSource="GameData/Config/dedicated_cfg"
configFile="GameData/Config/live_config.xml"
settingsPath="/dedicated"
authLevelsPath="$settingsPath/authorization_levels/level"
accountPath="$settingsPath/masterserver_account"
serverPath="$settingsPath/server_options"
systemPath="$settingsPath/system_config"

TM_SUPERADMIN_PASSWORD="${TM_SUPERADMIN_PASSWORD:-$(openssl rand -base64 33)}"
TM_ADMIN_PASSWORD="${TM_ADMIN_PASSWORD:-$(openssl rand -base64 33)}"

echo "Checking whether the server is present…"

levels=(
    TM_SUPERADMIN_PASSWORD 1
    TM_ADMIN_PASSWORD 2
    TM_USER_PASSWORD 3
)

accountOption=(
    TM_SERVER_USER login
    TM_SERVER_PASSWORD password
    TM_SERVER_VALIDATION_KEY validation_key
)

serverOptions=(
    TM_TITLE name
    TM_COMMENT comment
    TM_HIDDEN hide_server
    \
    TM_MAX_PLAYERS max_players
    TM_PLAYER_PASSWORD password
    \
    TM_MAX_SPECTATORS max_spectators
    TM_SPECTATOR_PASSWORD password_spectator
    \
    TM_LADDER_MODE ladder_mode
    TM_LADDER_LIMIT_MIN ladder_serverlimit_min
    TM_LADDER_LIMIT_MAX ladder_serverlimit_max
    \
    TM_ENABLE_P2P_UPLOAD enable_p2p_upload
    TM_ENABLE_P2P_DOWNLOAD enable_p2p_download
    \
    TM_CALLVOTE_TIMEOUT callvote_timeout
    TM_CALLVOTE_RATIO callvote_ratio
    \
    TM_ALLOW_CHALLENGE_DOWNLOAD allow_challenge_download
    TM_AUTOSAVE_REPLAYS autosave_replays
    TM_AUTOSAVE_VALIDATION_REPLAYS autosave_validation_replays
    \
    TM_REFEREE_PASSWORD referee_password
    TM_REFEREE_VALIDATION_MODE referee_validation_mode
    \
    TM_USE_CHANGING_VALIDATION_SEED use_changing_validation_seed
)

systemOptions=(
    TM_BIND_IP bind_ip_address
    TM_BIND_PORT server_port
    TM_P2P_PORT server_p2p_port
    TM_CLIENT_PORT client_port
    TM_USE_NAT_UPNP use_nat_upnp
    \
    TM_XMLRPC_PORT xmlrpc_port
    TM_XMLRPC_ALLOWED_REMOTE xmlrpc_allowremote
    \
    TM_PACKMASK packmask
    \
    TM_CONNECTION_UPLOADRATE connection_uploadrate
    TM_CONNECTION_DOWNLOADRATE connection_downloadrate
    \
    TM_P2P_CACHE_SIZE p2p_cache_size
    \
    TM_BLACKLIST_URL blacklist_url
    TM_GUESTLIST_FILENAME guestlist_filename
    TM_BLACKLIST_FILENAME blacklist_filename
    \
    TM_ALLOW_SPECTATOR_RELAYS allow_spectator_relays
    \
    TM_USE_PROXY use_proxy
    TM_PROXY_LOGIN proxy_login
    TM_PROXY_PASSWORD proxy_password
)

if [[ ! -x "./TrackmaniaServer" ]] || [ ! -f "$configSource".* ]; then
    echo "Server not found!"
    echo "Downloading TrackMania Forever Dedicated Server…"
    file="$(mktemp)"
    dir="$(mktemp -d)"
    wget http://files2.trackmaniaforever.com/TrackmaniaServer_2011-02-21.zip -O "$file" && unzip -o "$file" -d .
fi

if [ -f "$configSource.xml" ]; then
    cp "$configSource.xml" "$configFile"
elif [ -f "$configSource.txt" ]; then
    cp "$configSource.txt" "$configFile"
fi

for i in $(seq 0 2 $((${#levels[@]} - 1))); do
    var="${levels[$i]}"
    index="${levels[$(($i + 1))]}"
    password="${!var}"

    if [ -n "$password" ]; then
        xmlstarlet edit --inplace --update "$authLevelsPath[$index]/password" --value "$password" "$configFile"
    fi
done

for i in $(seq 0 2 $((${#accountOption[@]} - 1))); do
    var="${accountOption[$i]}"
    option="${accountOption[$(($i + 1))]}"
    value="${!var}"

    if [ -n "$value" ]; then
        xmlstarlet edit --inplace --update "$accountPath/$option" --value "$value" "$configFile"
    fi
done

for i in $(seq 0 2 $((${#serverOptions[@]} - 1))); do
    var="${serverOptions[$i]}"
    option="${serverOptions[$(($i + 1))]}"
    value="${!var}"

    if [ -n "$value" ]; then
        xmlstarlet edit --inplace --update "$serverPath/$option" --value "$value" "$configFile"
    fi
done

for i in $(seq 0 2 $((${#systemOptions[@]} - 1))); do
    var="${systemOptions[$i]}"
    option="${systemOptions[$(($i + 1))]}"
    value="${!var}"

    if [ -n "$value" ]; then
        xmlstarlet edit --inplace --update "$systemPath/$option" --value "$value" "$configFile"
    fi
done

if [ -z "$TM_LOG_FILES" ]; then
    set -- "$@" /nologs
fi

unbuffer -p ./TrackmaniaServer /dedicated_cfg="$(basename "$configFile")" /nodaemon $@