# Maintainer: Jethro Jones <jethro@jethrojones.com>

pkgname=omapresent
pkgver=0.1.3
pkgrel=1
pkgdesc='Dead-simple, Markdown-only presentation app built with Qt Quick'
# Only x86_64 has ever been built and tested. aarch64 is plausible (the build
# is plain qmake6 + make) but unproven, so it is not claimed here.
arch=('x86_64')
url='https://github.com/jethrojones/omapresent'
license=('MIT')
# No .install file. On current Arch, gtk-update-icon-cache.hook and
# update-desktop-database.hook already refresh both caches, and namcap flags a
# scriptlet that duplicates them. Spec §15 milestone 8 wanted the agent skill
# installed from a scriptlet; it cannot be — that runs as root and the skill
# belongs in the invoking user's home — so Omapresent links it itself on first
# run (Backend::installAgentSkill) from the copy packaged below.
# The binary carries no useful debug symbols for a release package, and the
# split -debug package trips namcap's empty-package rule.
options=('!debug')
# Spec §12's list needed three corrections, each checked against the built
# binary rather than assumed:
#   * qt6-quickcontrols2 is not a package on Arch. Quick Controls 2 and Quick
#     Dialogs 2 ship inside qt6-declarative.
#   * qt6-multimedia is a runtime module. `QT += multimedia` in omapresent.pro
#     links libQt6Multimedia even though no QML file imports QtMultimedia, so it
#     is a runtime dependency as well as a build dependency.
#   * gst-plugins-{base,good,bad,ugly} are not in the playback path at all.
#     Local video is an HTML <video> element (src/renderer/render.js:335)
#     decoded by QtWebEngine's own Chromium stack, which Arch links against
#     ffmpeg and openh264 through qt6-webengine. GStreamer is never loaded.
# No yt-dlp, per the spec.
depends=(
  'hicolor-icon-theme'
  'qt6-base'
  'qt6-declarative'
  'qt6-webengine'
  'qt6-webchannel'
  'qt6-multimedia'
  # File dialogs and the light/dark system setting go through the portal.
  'xdg-desktop-portal'
  # Spec §12: depend on the iA Writer S family, never bundle it.
  'ttf-ia-writer'
)
# gcc and make come from base-devel, which makepkg requires. qmake6, moc and rcc
# come from qt6-base and qt6-declarative, which are runtime dependencies too.
makedepends=(
  'imagemagick' # rescales the icon master into the hicolor sizes
)

_srcdir="$pkgname-$pkgver"

# Release source: the GitHub tag tarball, checksum-verified.
#
# Local validation path: point OMAPRESENT_SRC at a checkout and makepkg builds
# that tree instead, downloading nothing. This is the same convention Omarchy's
# own packages use (see omarchy-dev's OMARCHY_SRC), so
#
#   OMAPRESENT_SRC="$PWD" makepkg -f --cleanbuild -C -p pkgbuild/PKGBUILD
#
# exercises the real prepare/build/package path against uncommitted work.
if [[ -n ${OMAPRESENT_SRC:-} ]]; then
  source=()
  sha256sums=()
else
  source=("$_srcdir.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
  # Filled in on the default branch after the immutable tag exists by
  # ./release-checksums; see pkgbuild/RELEASING.md. The zero placeholder is not
  # a valid checksum for anything, so makepkg refuses to build before that
  # release gate.
  sha256sums=('0bdc12a774f60c471f3f650b176774e99d8929401ab12ba2a64aeb250b6cf6e3')
fi

prepare() {
  [[ -n ${OMAPRESENT_SRC:-} ]] || return 0

  # Copy rather than build in place: makepkg owns $srcdir and may clean it.
  # The excludes matter twice over — object trees and history would add
  # hundreds of megabytes and are not in a release tarball, and makepkg's own
  # working directories live inside the checkout when the default
  # pkgbuild/ layout is used, so a plain copy would recurse into itself.
  rm -rf "$srcdir/$_srcdir"
  mkdir -p "$srcdir/$_srcdir"
  tar -C "$OMAPRESENT_SRC" -cf - \
    --exclude=./.git \
    --exclude=./build \
    --exclude=./build-tests \
    --exclude=./pkgbuild/src \
    --exclude=./pkgbuild/pkg \
    --exclude='./pkgbuild/*.pkg.tar*' \
    --exclude='./pkgbuild/*.log' \
    . | tar -C "$srcdir/$_srcdir" -xf -
}

build() {
  cd "$srcdir/$_srcdir"
  ./bin/build
}

package() {
  cd "$srcdir/$_srcdir"

  install -Dm755 build/omapresent "$pkgdir/usr/bin/omapresent"

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 NOTICE "$pkgdir/usr/share/licenses/$pkgname/NOTICE"
  # The renderer's vendored libraries are compiled into the binary, so their
  # licences have to ship with it.
  install -Dm644 src/renderer/vendor/LICENSES.md \
    "$pkgdir/usr/share/licenses/$pkgname/vendor/LICENSES.md"
  install -m644 -t "$pkgdir/usr/share/licenses/$pkgname/vendor" \
    src/renderer/vendor/LICENSE.*

  # First run links the skill into the agent skill directories itself
  # (src/backend.cpp), because a pacman hook must not write into a home.
  install -Dm644 skill/SKILL.md "$pkgdir/usr/share/$pkgname/skill/SKILL.md"
  install -d "$pkgdir/usr/share/$pkgname/skill/reference"
  install -m644 -t "$pkgdir/usr/share/$pkgname/skill/reference" \
    skill/reference/*.md

  # Backend installs this through `omarchy hook install theme-set`, which puts
  # the user copy in ~/.config/omarchy/hooks/theme-set.d/ with mode 755.
  install -Dm755 pkgbuild/omapresent-theme-refresh \
    "$pkgdir/usr/share/$pkgname/hooks/omapresent-theme-refresh"

  install -Dm644 welcome/welcome.md "$pkgdir/usr/share/$pkgname/welcome.md"

  # The icon master is a single 1024x1024 PNG. hicolor's index.theme lists no
  # 1024x1024 directory, so an icon installed only at its native size is never
  # found by a spec-compliant lookup; rescale it into the sizes the theme
  # actually indexes. The placeholder omapresent.svg is deliberately not
  # packaged — it is superseded by this artwork.
  local size
  for size in 16 22 24 32 48 64 128 256 512; do
    install -d "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps"
    magick pkgbuild/omapresent.png -filter Lanczos -resize "${size}x${size}" \
      -strip "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/omapresent.png"
    chmod 644 "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/omapresent.png"
  done

  install -Dm644 pkgbuild/omapresent.desktop \
    "$pkgdir/usr/share/applications/omapresent.desktop"
}
