# Maintainer: Philipp Wagner <philipp@wagnersnetz.de>
pkgname=kst4contest
pkgver=1.44.0
pkgrel=1
pkgdesc="ON4KST Chat Client for VHF/UHF contest operation"
arch=('x86_64')
url="https://github.com/praktimarc/kst4contest"
license=('GPL-3.0-only')
depends=('gst-plugins-base' 'gst-plugins-good')
makedepends=('java-environment=21' 'gradle')
provides=('kst4contest')
conflicts=('kst4contest-bin' 'kst4contest-git')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/praktimarc/kst4contest/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('e702df24a29e6f914a43934df0095bc4adbd00a9f02df3a632816c832272e914')

build() {
    cd "${srcdir}/kst4contest-${pkgver}"

    export JAVA_HOME=$(find /usr/lib/jvm -maxdepth 1 -name 'java-21-*' -type d | head -n 1)
    export PATH="${JAVA_HOME}/bin:${PATH}"

    ./gradlew --offline -S :app-desktop:collectRuntime

    mkdir -p dist
    # From the Gradle build onwards the list lives in gradle.properties. Older
    # tarballs still carry it in pom.xml or derive it via packaging/AddModules.java.
    if [ -f gradle.properties ]; then
        ADD_MODULES="$(grep -m1 '^jpackageAddModules=' gradle.properties | cut -d= -f2)"
    elif [ -f packaging/AddModules.java ]; then
        ADD_MODULES="$(java packaging/AddModules.java)"
    else
        ADD_MODULES="$(sed -n 's:.*<addmodule>\(.*\)</addmodule>.*:\1:p' pom.xml | paste -sd,)"
    fi
    # Same story for the packaging icon: without --icon jpackage silently ships
    # its own Duke placeholder, but tarballs older than v1.42.0 have no icon to
    # point at, so only pass the flag when the file is actually there.
    ICON_ARGS=()
    if [ -f packaging/icons/kst4contest.png ]; then
        ICON_ARGS=(--icon packaging/icons/kst4contest.png)
    fi
    jpackage \
        --type app-image \
        --name KST4Contest \
        "${ICON_ARGS[@]}" \
        --input app-desktop/build/dist-libs \
        --main-jar app.jar \
        --main-class kst4contest.view.Main \
        --add-modules "$ADD_MODULES" \
        --dest dist
}

package() {
    cd "${srcdir}/kst4contest-${pkgver}"

    install -dm755 "${pkgdir}/usr/lib/KST4Contest"
    cp -a dist/KST4Contest/. "${pkgdir}/usr/lib/KST4Contest/"

    install -dm755 "${pkgdir}/usr/bin"
    printf '#!/bin/sh\nexec /usr/lib/KST4Contest/bin/KST4Contest "$@"\n' \
        > "${pkgdir}/usr/bin/KST4Contest"
    chmod 755 "${pkgdir}/usr/bin/KST4Contest"

    install -dm755 "${pkgdir}/usr/share/applications"
    cat > "${pkgdir}/usr/share/applications/KST4Contest.desktop" << 'EOF'
[Desktop Entry]
Type=Application
Name=KST4Contest
Comment=ON4KST Chat Client for VHF/UHF contest operation
Exec=KST4Contest
Icon=KST4Contest
Categories=Network;HamRadio;
Terminal=false
EOF

    if [[ -f "${pkgdir}/usr/lib/KST4Contest/lib/KST4Contest.png" ]]; then
        install -Dm644 "${pkgdir}/usr/lib/KST4Contest/lib/KST4Contest.png" \
            "${pkgdir}/usr/share/icons/hicolor/256x256/apps/KST4Contest.png"
    fi

    install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}
