#!/bin/sh
set -eu

pid_file="${ACEVO_SERVER_PID_FILE:-acevo-server.pid}"

if [ -f "$pid_file" ]; then
    pid=$(cat "$pid_file")
    if kill -0 "$pid" 2>/dev/null; then
        kill "$pid"
        rm -f "$pid_file"
        exit 0
    fi
    rm -f "$pid_file"
fi

if [ -z "${ACEVO_SERVER_PROCESS:-}" ]; then
    ACEVO_SERVER_PROCESS="ServerLauncher.exe"
fi

if [ -n "$ACEVO_SERVER_PROCESS" ]; then
    pkill -f "$ACEVO_SERVER_PROCESS"
    exit $?
fi

echo "Kein laufender Server ueber PID-Datei gefunden und ACEVO_SERVER_PROCESS ist nicht gesetzt."
exit 1
