# Maintainer: FWS
pkgname=fws-pacmanage
pkgver=1.6.2
pkgrel=1
pkgdesc="PacManager — gestionnaire de paquets graphique FWS (Qt6, sur pacman)"
arch=('x86_64')
url="https://gitea.nocode-var.tech/FWS/FWS-PacManage"
# FWS owns PacManager; the upstream LICENSE is source-available, not free.
license=('custom')
depends=('qt6-base' 'zlib' 'pacman')
makedepends=('cmake' 'gcc')
# Pinned to the commit the V1.6.2 tag points at instead of the tag name: a sha
# keeps the archive reproducible even if the tag is moved (V1.6.2).
_commit=d27792975ed572d7ba4c617d910d10e017fd0649
source=("${pkgname}-${pkgver}.tar.gz::https://gitea.nocode-var.tech/FWS/FWS-PacManage/archive/${_commit}.tar.gz")
sha256sums=('be6be117c7ac55a6d4ff8d95967eea80f972d4edaf2c3f4737673d58063b6c59')

# The whole app lives under /opt/fws-pacmanage because it updates itself: the
# self-update replaces the binary, the `version` marker and the plugins in the
# directory of the running executable. A real prefix keeps those files out of
# /usr/bin (a bare `bin` install would drop /usr/bin/version and /usr/bin/plugins).
build() {
  cmake -S "${srcdir}/${pkgname}" -B "${srcdir}/build" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/opt/fws-pacmanage \
    -DFWS_BUILD_FLATPAK_PLUGIN=ON \
    -DFWS_BUILD_AUR_PLUGIN=ON
  cmake --build "${srcdir}/build" -j"$(nproc)"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "${srcdir}/build"

  # The upstream CMakeLists declares its plugin install() rules ABOVE the
  # FWS_BUILD_*_PLUGIN option() calls, so both are undefined there and
  # `cmake --install` never ships the providers. Install the built modules by
  # hand into the first search path, which is also where the self-update
  # refreshes them (<prefix>/bin/plugins).
  install -d "${pkgdir}/opt/fws-pacmanage/bin/plugins"
  install -m755 "${srcdir}/build/plugins/"*.so \
    "${pkgdir}/opt/fws-pacmanage/bin/plugins/"

  # Entry point outside the self-updating tree: launchers and shells find it on
  # PATH while the update keeps rewriting /opt/fws-pacmanage/bin.
  install -d "${pkgdir}/usr/bin"
  ln -s /opt/fws-pacmanage/bin/PacManager "${pkgdir}/usr/bin/PacManager"

  # Launchers only read /usr/share; the prefix copies stay in /opt.
  install -Dm644 "${pkgdir}/opt/fws-pacmanage/share/applications/org.fws.PacManager.desktop" \
    "${pkgdir}/usr/share/applications/org.fws.PacManager.desktop"
  install -Dm644 "${pkgdir}/opt/fws-pacmanage/share/icons/hicolor/scalable/apps/org.fws.PacManager.svg" \
    "${pkgdir}/usr/share/icons/hicolor/scalable/apps/org.fws.PacManager.svg"

  # Restrictive upstream license (FWS, all rights reserved): the notice has to
  # travel with the redistributed binary.

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