# Generated by rust2rpm 27
%bcond check 1

# prevent library files from being installed
%global __cargo_is_lib() 0

# Upstream application versions managed by the wrappers
%global bedrock_version {{bedrock_version}}
%global minecraft_java_version {{java_version}}
%global fabric_game_version {{fabric_game_version}}
%global fabric_loader_version {{fabric_loader_version}}
%global fabric_installer_version {{fabric_installer_version}}
%global geyser_version {{geyser_version}}
%global mcxboxbroadcast_version {{mcxboxbroadcast_version}}

Version:        {{version}}
Name:           minecraft-server
Release:        %autorelease
Summary:        Wrapper for the Java, Fabric, and Bedrock Minecraft servers that adds proper systemd support

# https://docs.fedoraproject.org/en-US/packaging-guidelines/Rust/#_license_tags
# Output of %%{cargo_license_summary}
# 0BSD OR MIT OR Apache-2.0
# Apache-2.0
# Apache-2.0 OR BSL-1.0
# Apache-2.0 OR ISC OR MIT
# Apache-2.0 OR MIT
# (Apache-2.0 OR MIT) AND BSD-3-Clause
# Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT
# BSD-3-Clause
# CC0-1.0
# MIT
# MIT OR Apache-2.0
# MIT OR Apache-2.0 OR Zlib
# MIT OR Zlib OR Apache-2.0
# MPL-2.0
# Unlicense OR MIT
# Zlib
# Zlib OR Apache-2.0 OR MIT
License:        0BSD OR MIT OR Apache-2.0 AND Apache-2.0 AND Apache-2.0 OR BSL-1.0 AND Apache-2.0 OR ISC OR MIT AND Apache-2.0 OR MIT AND (Apache-2.0 OR MIT) AND BSD-3-Clause AND Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT AND BSD-3-Clause AND CC0-1.0 AND MIT AND MIT OR Apache-2.0 AND MIT OR Apache-2.0 OR Zlib AND MIT OR Zlib OR Apache-2.0 AND MPL-2.0 AND Unlicense OR MIT AND Zlib AND Zlib OR Apache-2.0 OR MIT
# LICENSE.dependencies contains a full license breakdown

URL:            https://gitlab.com/kaidelorenzo/minecraft-server-rs
# Sources can be obtained by
# git clone https://gitlab.com/kaidelorenzo/minecraft-server-rs
# cd minecraft-server-rs
# tito build --tgz
Source:         %{name}-%{version}.tar.gz

BuildRequires:  cargo-rpm-macros >= 24
BuildRequires:  systemd-rpm-macros
BuildRequires:  java-25-openjdk-devel
BuildRequires:  javapackages-filesystem

%package -n minecraft-server-wrapper-native
Summary:        Shared native exec runtime for Minecraft server wrapper jars

%package java
Summary:        Wrapper for the Java Minecraft server that adds proper systemd support
# java-25-headless is provided by Oracle Java and OpenJDK Java
Requires:       java-25-headless
Requires:       javapackages-filesystem
Requires:       minecraft-java-server = %{version}-%{release}
Requires:       minecraft-server-wrapper-native = %{version}-%{release}
Provides:       wrapped(minecraft-java-server) = %{minecraft_java_version}

%package fabric
Summary:        Wrapper for the Fabric Minecraft server that adds proper systemd support
# java-25-headless is provided by Oracle Java and OpenJDK Java
Requires:       java-25-headless
Requires:       javapackages-filesystem
Requires:       minecraft-fabric-server = %{version}-%{release}
Requires:       minecraft-server-wrapper-native = %{version}-%{release}
Provides:       wrapped(minecraft-java-server) = %{fabric_game_version}
Provides:       wrapped(fabric-loader) = %{fabric_loader_version}
Provides:       wrapped(fabric-installer) = %{fabric_installer_version}

%package bedrock
Summary:        Wrapper for the Bedrock Minecraft server that adds proper systemd support
Provides:       wrapped(minecraft-bedrock-server) = %{bedrock_version}

%package -n minecraft-java-server
Summary:        Version-locked Kotlin wrapper for the Minecraft Java server
Requires:       java-25-headless
Requires:       javapackages-filesystem
Requires:       minecraft-server-wrapper-native = %{version}-%{release}
Provides:       wrapped(minecraft-java-server) = %{minecraft_java_version}

%package -n minecraft-fabric-server
Summary:        Version-locked Kotlin wrapper for the Minecraft Fabric server
Requires:       java-25-headless
Requires:       javapackages-filesystem
Requires:       minecraft-server-wrapper-native = %{version}-%{release}
Provides:       wrapped(minecraft-java-server) = %{fabric_game_version}
Provides:       wrapped(fabric-loader) = %{fabric_loader_version}
Provides:       wrapped(fabric-installer) = %{fabric_installer_version}

%package -n geyser-standalone
Summary:        Version-locked Kotlin wrapper for Geyser Standalone
Requires:       java-25-headless
Requires:       javapackages-filesystem
Requires:       minecraft-server-wrapper-native = %{version}-%{release}
Provides:       wrapped(geyser-standalone) = %{geyser_version}

%package -n mcxboxbroadcast-standalone
Summary:        Version-locked Kotlin wrapper for MCXboxBroadcast Standalone
Requires:       java-25-headless
Requires:       javapackages-filesystem
Requires:       minecraft-server-wrapper-native = %{version}-%{release}
Provides:       wrapped(mcxboxbroadcast-standalone) = %{mcxboxbroadcast_version}

%global _description %{expand:
minecraft-server is a wrapper for the Java, Fabric, and Bedrock Minecraft servers 
that adds proper systemd support. The core functionality includes proper 
signal handling for SIGINT and SIGTERM, systemd user services, support 
for the XDG Base Directory Specification, and a Fedora Copr package for 
seamless upgrades. The current upstream versions are Minecraft Java
%{minecraft_java_version}, Minecraft Bedrock %{bedrock_version}, and Minecraft
%{fabric_game_version} with Fabric Loader %{fabric_loader_version} and Installer
%{fabric_installer_version}.}

%global _java_description %{expand:
minecraft-server-java is a wrapper for the Java Minecraft server
that adds proper systemd support. The core functionality includes proper 
signal handling for SIGINT and SIGTERM, systemd user services, support 
for the XDG Base Directory Specification, and a Fedora Copr package for 
seamless upgrades. It currently launches Minecraft Java
%{minecraft_java_version}.}

%global _bedrock_description %{expand:
minecraft-server-bedrock is a wrapper for the Bedrock Minecraft server 
that adds proper systemd support. The core functionality includes proper 
signal handling for SIGINT and SIGTERM, systemd user services, support 
for the XDG Base Directory Specification, and a Fedora Copr package for 
seamless upgrades. It launches Minecraft Bedrock
%{bedrock_version}. The archive is downloaded with an application-specific HTTP
user agent, verified against its packaged SHA-256 checksum, and installed
through a staged filesystem update.}

%global _fabric_description %{expand:
minecraft-server-fabric is a wrapper for the Fabric Minecraft server 
that adds proper systemd support. The core functionality includes proper 
signal handling for SIGINT and SIGTERM, systemd user services, support 
for the XDG Base Directory Specification, and a Fedora Copr package for 
seamless upgrades. It currently launches Minecraft %{fabric_game_version} with
Fabric Loader %{fabric_loader_version} and Installer
%{fabric_installer_version}.}

%global _jar_launcher_description %{expand:
This package provides a runnable Kotlin jar that downloads its
version-locked Java application jar into the XDG data directory, verifies its
SHA-256 checksum, preserves its JVM and application arguments, and uses a small
Rust library through Java's Foreign Function and Memory API to replace itself
with the Java application process.}

%description %{_description}
%description -n minecraft-server-wrapper-native
This package contains the shared Rust native library used by the runnable wrapper
jars to replace their JVM process with the wrapped Java application or the
systemd-aware supervisor.

%description java %{_java_description}
%description fabric %{_fabric_description}
%description bedrock %{_bedrock_description}
%description -n minecraft-java-server
%{_jar_launcher_description}
The downloaded Minecraft Java server is version %{minecraft_java_version}.

%description -n minecraft-fabric-server
%{_jar_launcher_description}
The downloaded server uses Minecraft %{fabric_game_version}, Fabric Loader
%{fabric_loader_version}, and Fabric Installer %{fabric_installer_version}.

%description -n geyser-standalone
%{_jar_launcher_description}
The downloaded Geyser Standalone jar is version %{geyser_version}.

%description -n mcxboxbroadcast-standalone
%{_jar_launcher_description}
The downloaded MCXboxBroadcast Standalone jar is version
%{mcxboxbroadcast_version}.

%prep
%autosetup -n %{name}-%{version} -p1
# cargo2rpm evaluates every workspace member. The release updater is not part
# of the RPM build, so its dependencies are excluded from this buildroot.
sed -i '/    "update-checker",/d' Cargo.toml
%cargo_prep

%generate_buildrequires
%cargo_generate_buildrequires

%build
cd jar-launchers
./gradlew --no-daemon assemble testClasses resolveTestRuntimeDependencies
cd ..
%cargo_build -- --package jar-wrapper-exec --package minecraft-server
%{cargo_license_summary}
# Generate each binary package's dependency license inventory from its member.
cp -p Cargo.toml Cargo.toml.runtime-workspace
sed -i '/    "minecraft-server",/d' Cargo.toml
%{cargo_license} > LICENSE.native.dependencies
cp -p Cargo.toml.runtime-workspace Cargo.toml
sed -i '/    "jar-launchers\/native-exec",/d' Cargo.toml
%{cargo_license} > LICENSE.dependencies
mv Cargo.toml.runtime-workspace Cargo.toml

%install
install -Dpm 0644 jar-launchers/build/libs/minecraft-java-server-wrapper.jar -t %{buildroot}%{_jnidir}
install -Dpm 0644 jar-launchers/build/libs/minecraft-fabric-server-wrapper.jar -t %{buildroot}%{_jnidir}
install -Dpm 0644 jar-launchers/build/libs/minecraft-server-java.jar -t %{buildroot}%{_jnidir}
install -Dpm 0644 jar-launchers/build/libs/minecraft-server-fabric.jar -t %{buildroot}%{_jnidir}
install -Dpm 0644 jar-launchers/build/libs/geyser-standalone-wrapper.jar -t %{buildroot}%{_jnidir}
install -Dpm 0644 jar-launchers/build/libs/mcxboxbroadcast-standalone-wrapper.jar -t %{buildroot}%{_jnidir}
install -Dpm 0755 target/release/libjar_wrapper_exec.so \
    %{buildroot}%{_libdir}/libjar_wrapper_exec.so

install -Dpm 0644 systemd/minecraft-server-java.socket -t %{buildroot}%{_userunitdir}
install -Dpm 0644 systemd/minecraft-server-java.service -t %{buildroot}%{_userunitdir}
install -Dpm 0644 systemd/minecraft-server-fabric.socket -t %{buildroot}%{_userunitdir}
install -Dpm 0644 systemd/minecraft-server-fabric.service -t %{buildroot}%{_userunitdir}
install -Dpm 0644 systemd/minecraft-server-bedrock.socket -t %{buildroot}%{_userunitdir}
install -Dpm 0644 systemd/minecraft-server-bedrock.service -t %{buildroot}%{_userunitdir}

install -Dpm 0644 manuals/minecraft-server-java.1 -t %{buildroot}%{_mandir}/man1
install -Dpm 0644 manuals/minecraft-server-fabric.1 -t %{buildroot}%{_mandir}/man1
install -Dpm 0644 manuals/minecraft-server-bedrock.1 -t %{buildroot}%{_mandir}/man1

install -Dpm 0755 target/release/minecraft-server-bedrock -t %{buildroot}%{_bindir}
install -Dpm 0755 target/release/minecraft-server-java \
    %{buildroot}%{_libexecdir}/minecraft-server-java-supervisor
install -Dpm 0755 target/release/minecraft-server-fabric \
    %{buildroot}%{_libexecdir}/minecraft-server-fabric-supervisor

%if %{with check}
%check
cd jar-launchers
./gradlew --offline --no-daemon test -x buildNativeExec
cd ..
%cargo_test -- --package jar-wrapper-exec --package minecraft-server
%endif

# these systemd commands are miserably documented but this is a good reference
# https://github.com/systemd/systemd/blob/main/src/rpm/systemd-update-helper.in
# https://docs.fedoraproject.org/en-US/packaging-guidelines/Scriptlets/#_systemd
# we don't need the post ones because we aren't shipping a preset file but we'll add them anyway
# we do want the preun as to disable the service if the user had enabled it to remove symlinks
%post java
%systemd_user_post minecraft-server-java.socket
%systemd_user_post minecraft-server-java.service

%post fabric
%systemd_user_post minecraft-server-fabric.socket
%systemd_user_post minecraft-server-fabric.service

%post bedrock
%systemd_user_post minecraft-server-bedrock.socket
%systemd_user_post minecraft-server-bedrock.service

# disable systemd units on uninstall
%preun java
%systemd_user_preun minecraft-server-java.socket
%systemd_user_preun minecraft-server-java.service

%preun fabric
%systemd_user_preun minecraft-server-fabric.socket
%systemd_user_preun minecraft-server-fabric.service

%preun bedrock
%systemd_user_preun minecraft-server-bedrock.socket
%systemd_user_preun minecraft-server-bedrock.service

# mark systemd units for restart on upgrade
%postun java
%systemd_user_postun_with_restart minecraft-server-java.socket
%systemd_user_postun_with_restart minecraft-server-java.service

%postun fabric
%systemd_user_postun_with_restart minecraft-server-fabric.socket
%systemd_user_postun_with_restart minecraft-server-fabric.service

%postun bedrock
%systemd_user_postun_with_restart minecraft-server-bedrock.socket
%systemd_user_postun_with_restart minecraft-server-bedrock.service

%files java
%license LICENSE
%license LICENSE.dependencies
%doc README.md
%{_mandir}/man1/minecraft-server-java.1.gz
%{_jnidir}/minecraft-server-java.jar
%{_libexecdir}/minecraft-server-java-supervisor
%{_userunitdir}/minecraft-server-java.socket
%{_userunitdir}/minecraft-server-java.service

%files fabric
%license LICENSE
%license LICENSE.dependencies
%doc README.md
%{_mandir}/man1/minecraft-server-fabric.1.gz
%{_jnidir}/minecraft-server-fabric.jar
%{_libexecdir}/minecraft-server-fabric-supervisor
%{_userunitdir}/minecraft-server-fabric.socket
%{_userunitdir}/minecraft-server-fabric.service

%files bedrock
%license LICENSE
%license LICENSE.dependencies
%doc README.md
%{_mandir}/man1/minecraft-server-bedrock.1.gz
%{_bindir}/minecraft-server-bedrock
%{_userunitdir}/minecraft-server-bedrock.socket
%{_userunitdir}/minecraft-server-bedrock.service

%files -n minecraft-server-wrapper-native
%license LICENSE
%license LICENSE.native.dependencies
%{_libdir}/libjar_wrapper_exec.so

%files -n minecraft-java-server
%license LICENSE
%doc README.md
%{_jnidir}/minecraft-java-server-wrapper.jar

%files -n minecraft-fabric-server
%license LICENSE
%doc README.md
%{_jnidir}/minecraft-fabric-server-wrapper.jar

%files -n geyser-standalone
%license LICENSE
%doc README.md
%{_jnidir}/geyser-standalone-wrapper.jar

%files -n mcxboxbroadcast-standalone
%license LICENSE
%doc README.md
%{_jnidir}/mcxboxbroadcast-standalone-wrapper.jar

%changelog
%autochangelog

view changes at
https://gitlab.com/kaidelorenzo/minecraft-server-rs/-/releases
