#!/bin/sh
set -eu

if [ -z "${ACEVO_SERVER_EXE:-}" ]; then
    ACEVO_SERVER_EXE="/opt/acevo/server/ServerLauncher.exe"
fi

if [ -z "${ACEVO_LOG_FILE:-}" ]; then
    ACEVO_LOG_FILE="$(dirname "$0")/../logs/acevo-server.log"
fi

if [ ! -f "$ACEVO_SERVER_EXE" ]; then
    echo "Server executable wurde nicht gefunden: $ACEVO_SERVER_EXE"
    exit 1
fi

server_dir=$(dirname "$ACEVO_SERVER_EXE")
server_file=$(basename "$ACEVO_SERVER_EXE")
log_dir=$(dirname "$ACEVO_LOG_FILE")

mkdir -p "$log_dir"

cd "$server_dir"

if echo "$server_file" | grep -qi '\.exe$'; then
    wine_binary="${WINE_BINARY:-wine}"
    nohup "$wine_binary" "$ACEVO_SERVER_EXE" >> "$ACEVO_LOG_FILE" 2>&1 &
else
    nohup "$ACEVO_SERVER_EXE" >> "$ACEVO_LOG_FILE" 2>&1 &
fi

echo $! > acevo-server.pid

exit 0
