# Maintainer: Felitendo
# This PKGBUILD is updated automatically:
# https://git.felo.gg/Felitendo/PKGBUILDS

pkgname=modrinth-enhanced
pkgver=0.21.6
pkgrel=1
pkgdesc="Modrinth App without ads or telemetry, with offline and Ely.by accounts and Linux fixes"
arch=('x86_64')
url="https://git.felo.gg/Felitendo/Modrinth-Enhanced"
license=('GPL-3.0-only')
depends=('cairo' 'dbus' 'gdk-pixbuf2' 'glib2' 'glibc' 'gtk3' 'hicolor-icon-theme'
         'libdrm' 'libgcc' 'libsoup3' 'webkit2gtk-4.1'
         # loaded by Minecraft, not by the launcher
         'libgl' 'libpulse' 'libx11' 'libxcursor' 'libxext' 'libxxf86vm')
# git replays the patches the way upstream's scripts/prepare.sh does; the JDK
# builds the Java agent, whose Gradle toolchain asks for exactly 17
makedepends=('cargo' 'git' 'jdk17-openjdk' 'node-gyp' 'nodejs>=24.15.0' 'npm' 'pnpm')
optdepends=('xorg-xrandr: for older Minecraft versions')
# the release profile carries no debug info, so a -debug package would be empty
options=('!lto' '!debug')
# Modrinth Enhanced holds no forked source, only patches. Its release tag
# records the Modrinth App release it was built on (upstream.txt) and the one
# the patches were exported against (patches/base.txt); pkg.sh reads both.
# A new revision of the patches on the same release is tagged v0.21.2-2, which
# pkgver spells 0.21.2.r2.
_tag="v0.21.6"
_upstream="v0.21.6"
_base="v0.21.6"
# With the NVIDIA driver WebKitGTK paces the app with a 60 fps timer whatever
# the monitor's refresh rate. vblank-shim.c, preloaded by modrinth-enhanced.sh,
# paces it at the monitor's rate instead and keeps the app on X11, the only
# place that works; it is not part of upstream's release.
source=("${pkgname}-${_tag}.tar.gz::https://git.felo.gg/Felitendo/Modrinth-Enhanced/archive/${_tag}.tar.gz"
        "modrinth-code-${_upstream}.tar.gz::https://github.com/modrinth/code/archive/refs/tags/${_upstream}.tar.gz"
        "vblank-shim.c"
        "modrinth-enhanced.sh")
noextract=("modrinth-code-${_upstream}.tar.gz")
if [[ "$_base" != "$_upstream" ]]; then
  source+=("modrinth-code-${_base}.tar.gz::https://github.com/modrinth/code/archive/refs/tags/${_base}.tar.gz")
  noextract+=("modrinth-code-${_base}.tar.gz")
fi
sha256sums=('0bf2326e345ba2c6f25a6954538d20b4b63d7010ccdd54281186bac1196ef493' 'dd058ca164096a0f9aeeffec5dfa7905979719a33976643895806b0c82b6f409' '0190921b8ff2fb1deec3209cba71c01541ca1f013e3ba41680f1636e185fd455' 'b24872f82645c52ee4804599cd678d138876fb193c754403fa9c5cf92bd745b9')

prepare() {
  # What upstream's scripts/prepare.sh does with two shallow tags: apply the
  # patches to the release they were exported against, where they always fit,
  # then rebase them onto the release this version was built on, so that a
  # change upstream made near a patch is merged instead of failing to apply.
  export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
  export GIT_AUTHOR_NAME="Modrinth Enhanced" GIT_AUTHOR_EMAIL="patches@modrinth-enhanced.invalid"
  export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"

  rm -rf app
  mkdir app
  cd app
  git init -q
  # reset --hard checks the tree out again with upstream's .gitattributes
  # applied, as a clone of the tag would have it
  bsdtar -xf "$srcdir/modrinth-code-${_base}.tar.gz" --strip-components 1
  git add -Af
  git commit -qm "Modrinth App ${_base}"
  git reset -q --hard
  git tag base
  if [[ "$_base" != "$_upstream" ]]; then
    git rm -rqf .
    bsdtar -xf "$srcdir/modrinth-code-${_upstream}.tar.gz" --strip-components 1
    git add -Af
    git commit -qm "Modrinth App ${_upstream}"
    git reset -q --hard
    git tag upstream
    git checkout -q -b enhanced base
  fi
  git am -q --3way --whitespace=nowarn "$srcdir/modrinth-enhanced/patches/"*.patch
  if [[ "$_base" != "$_upstream" ]]; then
    git rebase -q --onto upstream base enhanced
  fi

  # upstream's scripts/set-version.sh: the sources carry 1.0.0-local, the app
  # reports the Modrinth App release it is built on
  sed -i "s/1.0.0-local/${_upstream#v}/" \
    apps/app/Cargo.toml packages/app-lib/Cargo.toml apps/app-frontend/package.json Cargo.lock

  # the production API endpoints, as upstream's scripts/build.sh selects them
  cp packages/app-lib/.env.prod packages/app-lib/.env

  # node-gyp for the native modules
  export NODE_PATH=/usr/lib/node_modules
  export TURBO_TELEMETRY_DISABLED=1
  pnpm install --frozen-lockfile

  export RUSTUP_TOOLCHAIN=stable
  cargo fetch --locked --target host-tuple
}

build() {
  # GLib for its headers only: the shim looks GTK and GLib up at run time
  gcc $CPPFLAGS $CFLAGS $LDFLAGS -shared -fPIC -o libwebkit-vblank-shim.so vblank-shim.c \
    $(pkg-config --cflags glib-2.0) $(pkg-config --cflags --libs libdrm)

  cd app

  export RUSTUP_TOOLCHAIN=stable
  export NODE_PATH=/usr/lib/node_modules
  export TURBO_TELEMETRY_DISABLED=1
  export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
  # keep Gradle's cache inside the build tree instead of ~/.gradle
  export GRADLE_USER_HOME="$srcdir/gradle-home"

  # Without tauri-release.conf.json, as upstream builds it: that config only
  # adds Modrinth's own updater. app-lib's build script runs Gradle, which
  # downloads itself and the agent's dependencies, so build() needs network.
  pnpm --filter=@modrinth/app run tauri build --no-bundle
}

package() {
  cd app

  # the same files, under the same names, as modrinth-enhanced-bin: the binary
  # sits behind the script that preloads the shim
  install -Dm755 target/release/ModrinthEnhanced -t "$pkgdir/usr/lib/modrinth-enhanced/"
  install -Dm755 "$srcdir/libwebkit-vblank-shim.so" -t "$pkgdir/usr/lib/modrinth-enhanced/"
  install -Dm755 "$srcdir/modrinth-enhanced.sh" "$pkgdir/usr/bin/ModrinthEnhanced"
  ln -s ModrinthEnhanced "$pkgdir/usr/bin/modrinth-enhanced"

  install -Dm644 apps/app/icons/128x128.png \
    "$pkgdir/usr/share/icons/hicolor/128x128/apps/ModrinthEnhanced.png"
  install -Dm644 apps/app/icons/128x128@2x.png \
    "$pkgdir/usr/share/icons/hicolor/256x256@2/apps/ModrinthEnhanced.png"

  install -d "$pkgdir/usr/share/applications"
  cat > "$pkgdir/usr/share/applications/Modrinth Enhanced.desktop" << EOF
[Desktop Entry]
Categories=Game;
Exec=ModrinthEnhanced
Icon=ModrinthEnhanced
Name=Modrinth Enhanced
Terminal=false
Type=Application
MimeType=application/x-modrinth-modpack+zip;x-scheme-handler/modrinth
EOF
}
