#!/usr/bin/bash
set -euo pipefail

project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)"
test_dir="$(mktemp -d)"
trap 'rm -rf -- "$test_dir"' EXIT
runtime_dir="$test_dir/runtime"
install -d -m 0700 "$runtime_dir"

install -pm 0644 \
    "$project_dir/systemd/minecraft-server-java.service" \
    "$project_dir/systemd/minecraft-server-java.socket" \
    "$project_dir/systemd/minecraft-server-fabric.service" \
    "$project_dir/systemd/minecraft-server-fabric.socket" \
    "$project_dir/systemd/minecraft-server-bedrock.socket" \
    "$project_dir/example systemd unit files/mcxboxbroadcast-standalone.service" \
    "$project_dir/example systemd unit files/geyser.service" \
    "$project_dir/example systemd unit files/offline-upgrade.service" \
    "$project_dir/example systemd unit files/offline-upgrade.timer" \
    -t "$test_dir"

# The packaged Bedrock executable is unavailable in a source-tree check, so
# substitute a known executable while preserving and validating the rest of
# the unit exactly as shipped.
sed 's|^ExecStart=/usr/bin/minecraft-server-bedrock$|ExecStart=/usr/bin/true|' \
    "$project_dir/systemd/minecraft-server-bedrock.service" \
    > "$test_dir/minecraft-server-bedrock.service"

XDG_RUNTIME_DIR="$runtime_dir" systemd-analyze verify --user \
    "$test_dir/minecraft-server-java.service" \
    "$test_dir/minecraft-server-java.socket" \
    "$test_dir/minecraft-server-fabric.service" \
    "$test_dir/minecraft-server-fabric.socket" \
    "$test_dir/minecraft-server-bedrock.service" \
    "$test_dir/minecraft-server-bedrock.socket" \
    "$test_dir/mcxboxbroadcast-standalone.service" \
    "$test_dir/geyser.service"

systemd-analyze verify \
    "$test_dir/offline-upgrade.service" \
    "$test_dir/offline-upgrade.timer"
