# Maintainer: Yurzi <yurzi@foxmail.com>

pkgname="recaf"
pkgver=4.0.0
pkgrel=2
pkgdesc='A modern Java bytecode editor'
url='https://github.com/Col-E/Recaf'
license=('MIT')
# We bundle Recaf's own JavaFX (org.openjfx 23.0.2, pulled by its Gradle build)
# into the all.jar instead of depending on the now-unmaintained java-openjfx.
# The bundled JavaFX carries platform-specific native libraries, so this is no
# longer arch-independent.
arch=('x86_64')

# Recaf 4.0.0-alpha pins its Jasm dependency to an unreleased JitPack snapshot
# commit. JitPack's build of that commit errored, so the artifact is unavailable
# from any public Maven repo. Build that exact Jasm commit from source and publish
# it into the local Maven repo (consumed via Recaf's mavenLocal()) before building.
_jasm_commit=c5a2c9050312497f53378f8c6a397ec3d3f02ef6
_jasm_ver=c5a2c90503

depends=('java-environment>=25' 'ttf-font')
makedepends=('jdk25-openjdk' 'gradle' 'git')

conflicts=("$pkgname-bin" "$pkgname-git")

source=(
  "$pkgname-$pkgver-alpha.tar.gz::$url/archive/refs/tags/$pkgver-alpha.tar.gz"
  "jasm::git+https://github.com/jumanji144/Jasm.git#commit=$_jasm_commit"
  "LICENSE::https://raw.githubusercontent.com/Col-E/Recaf/$pkgver-alpha/LICENSE"
  "$pkgname"
  "$pkgname.desktop"
)

sha256sums=('ef3dc4884802173816ac104d61f56e58ff86d2f094cf369385795a2e6d569478'
            '33fd83b42f7ba75be1633b3562b1d4de79a411aed126af689d02106efe1ff3b6'
            'a98659971ead7e75b30f8d4ef8c27d13338c6187a49fee181379e74b89d4a16f'
            '6991aa0bd7b735d6a29abedf8f11040e016ba988e8f6cd195cba5a57cc1d470f'
            '5ffc7c63deb1935ffe19298a15b525bcc08a1e7e9de7146ea5785b21b12045d4')

prepare() {
  cd "$srcdir/jasm"

  # Republish under the JitPack coordinates Recaf expects:
  # com.github.jumanji144.Jasm:<module>:<short-commit>.
  sed -i "s/group 'me.darknet'/group 'com.github.jumanji144.Jasm'/" build.gradle
  sed -i "s/version '2.9.0'/version '$_jasm_ver'/" build.gradle

  # Build Jasm with the same JDK 25 LTS toolchain as Recaf (it pins 21 upstream).
  sed -i 's/JavaLanguageVersion.of(21)/JavaLanguageVersion.of(25)/' build.gradle

  # Drop the unused CLI module; its pinned shadow plugin can break newer Gradle.
  sed -i "/include 'jasm-cli'/d" settings.gradle
}

build() {
  # Build the pinned Jasm snapshot and install it into the local Maven repo so
  # Recaf's mavenLocal() can resolve it.
  cd "$srcdir/jasm"
  JAVA_HOME="/usr/lib/jvm/java-25-openjdk" gradle --no-daemon \
    :jasm-core:publishToMavenLocal :jasm-composition-jvm:publishToMavenLocal

  # Build Recaf against the JDK 25 LTS. Recaf's Gradle build selects the JDK via a
  # toolchain (languageVersion=$TARGET_VERSION, default 22) and won't auto-download
  # a missing JDK, so the matching JDK must be installed and pointed at explicitly.
  #
  # Do NOT pass -Dskip_jfx_bundle=true: leaving it unset makes recaf-ui's Gradle
  # build (via the org.openjfx plugin) bundle JavaFX into the all.jar, so we no
  # longer rely on the system java-openjfx package.
  cd "$srcdir/Recaf-$pkgver-alpha"
  export TARGET_VERSION=25
  JAVA_HOME="/usr/lib/jvm/java-25-openjdk" gradle --no-daemon \
    assemble -x compileTestJava
}

package() {
  cd "$srcdir/Recaf-$pkgver-alpha"

  install -Dm755 "recaf-ui/build/libs/recaf-ui-$pkgver-SNAPSHOT-all.jar" "$pkgdir/usr/share/java/$pkgname/$pkgname.jar"
  install -Dm644 "recaf-ui/src/main/resources/icons/logo-full.png" "$pkgdir/usr/share/pixmaps/$pkgname.png"

  install -Dm755 "$srcdir/$pkgname" "$pkgdir/usr/bin/recaf"
  install -Dm644 "$srcdir/$pkgname.desktop" "$pkgdir/usr/share/applications/$pkgname.desktop"
  install -Dm644 "$srcdir/LICENSE" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
