pkgname=strata
pkgver=0.20.1
pkgrel=1
pkgdesc='Fast, keyboard-first file manager for modern Linux desktops'
arch=('x86_64' 'aarch64')
url='https://github.com/lgse/strata'
license=('MIT')

depends=(
  'bash'
  'bubblewrap'
  'cairo'
  'ffmpeg'
  'ffmpegthumbnailer'
  'fontconfig'
  'gcc-libs'
  'gdk-pixbuf2'
  'glib2'
  'glibc'
  'graphene'
  'gst-libav'
  'gst-plugins-good'
  'gvfs'
  'gtk4'
  'gtksourceview5'
  'hicolor-icon-theme'
  'pango'
  'poppler-glib'
  'util-linux'
  'xdg-terminal-exec'
)
makedepends=('cargo' 'git' 'glib2-devel' 'pkgconf')
# The example actions' tests convert images with ImageMagick, WebP included.
checkdepends=('imagemagick' 'libwebp')
optdepends=(
  'gvfs-smb: browse SMB network shares'
  'imagemagick: additional camera RAW preview support'
  'libraw: additional camera RAW preview support'
)
conflicts=('strata-git')
options=('!debug' '!lto')

source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
sha256sums=('cdbe7f196970cbaa1fa41c427833899e35c2dcaa8bb68a864c3afa6428827e2f')

prepare() {
  cd "$pkgname-$pkgver"

  local commit
  if ! commit=$(git get-tar-commit-id < <(gzip -dc "$srcdir/$pkgname-$pkgver.tar.gz")) ||
    [[ ! $commit =~ ^[0-9a-f]{40}$ ]]; then
    printf 'Could not recover the release commit from the source archive\n' >&2
    return 1
  fi
  printf '%s\n' "$commit" >.build-commit

  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
  cd "$pkgname-$pkgver"

  export CARGO_TARGET_DIR=target
  export STRATA_BUILD_COMMIT
  STRATA_BUILD_COMMIT=$(<.build-commit)
  export STRATA_RELEASE_TAG="v$pkgver"
  export STRATA_BUILD_KIND=stable
  cargo build --frozen --release
}

check() {
  cd "$pkgname-$pkgver"

  # Upstream's release profile is fat LTO with one codegen unit, which
  # compiles the final crate on a single thread. That is the right trade
  # for the binary users run, and build() keeps it. The test harness is a
  # second compile of the same crate under the same profile, thrown away
  # afterwards; build it in parallel, in its own target directory so the
  # shipped binary is untouched. Measured on the x86_64 builder: 708 s
  # serial, 223 s with thin LTO and 16 units.
  export CARGO_TARGET_DIR=target-check
  export CARGO_PROFILE_RELEASE_LTO=thin
  export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=16
  export STRATA_BUILD_COMMIT
  STRATA_BUILD_COMMIT=$(<.build-commit)
  export STRATA_RELEASE_TAG="v$pkgver"
  export STRATA_BUILD_KIND=stable
  # Search fixtures depend on directory enumeration order and fail on overlayfs.
  # Run them on tmpfs; other suites execute fixtures, so cannot use noexec /dev/shm.
  local search_tmp
  search_tmp=$(mktemp -d /dev/shm/strata-tests.XXXXXXXX) || return 1
  # rename_refresh_rescores_every_session_sharing_the_index asserts that
  # every Results event carries the query it set, but the index worker can
  # publish its first walk before query() lands. Natively query() always
  # wins; under the QEMU emulation aarch64 builds run in, the worker does.
  local -a search_skip=()
  [[ $CARCH == aarch64 ]] && search_skip+=(--skip services::search::tests::refresh::rename_refresh_rescores_every_session_sharing_the_index)
  local test_status=0
  TMPDIR="$search_tmp" cargo test --frozen --release --all-targets --all-features \
    services::search::tests:: -- --test-threads=1 "${search_skip[@]}" || test_status=$?
  rm -rf -- "$search_tmp"
  (( test_status == 0 )) || return "$test_status"

  # restart_waiter_* run `sh -c 'while kill -0 "$1"; do sleep; done'` with
  # the pid u32::MAX. Arch's sh is bash, and bash's kill builtin in POSIX
  # mode prints "not a pid or valid job spec" for that number but exits 0,
  # so the loop never ends and the job hits its 180-minute timeout.
  # Upstream CI runs on Ubuntu, where sh is dash and rejects the number.
  # Skip until upstream waits on a real pid.
  local skip=(--skip services::search::tests:: --skip ui::settings::tests::restart_waiter_)
  # checksum_example_handles_native_names_... hands the SHA-256 example a
  # file named with a raw 0xff byte. The action's log() print()s that name,
  # and under a UTF-8 locale Python's stdout refuses the surrogate it
  # decodes to, so the action dies with UnicodeEncodeError. The builder sets
  # LANG=en_US.UTF-8; upstream's test container sets no locale, and in the
  # C locale Python's stdout escapes the byte instead. A real
  # bug in the example for users with UTF-8 locales; skip until upstream
  # logs names with errors="surrogateescape".
  skip+=(--skip adapters::local_jobs::tests::examples::checksum_example_handles_native_names_and_refuses_existing_files_and_links)
  # ownership_probe_errors_disable_in_place_updates points the pacman path
  # at a directory and expects Command::output() to fail. aarch64 builds
  # run under QEMU user-mode emulation, where glibc's posix_spawn cannot
  # observe the child's failed execve (natively it returns EACCES); the
  # spawn "succeeds" with exit 127, the probe reads that as "not owned",
  # and the assertion fails. Passes natively.
  # a_missing_working_directory_fails_instead_of_using_stratas_cwd is the
  # same blind spot: the child's failed chdir never reaches the parent.
  # in_place_retirement_signals_and_waits_for_an_owned_process finds its
  # target by /proc/<pid>/exe, which under QEMU user-mode names the
  # emulator rather than `sleep`, so nothing is signalled.
  [[ $CARCH == aarch64 ]] && skip+=(
    --skip services::update_install::tests::ownership_probe_errors_disable_in_place_updates
    --skip adapters::local_jobs::tests::lifecycle::a_missing_working_directory_fails_instead_of_using_stratas_cwd
    --skip services::update_install::tests::in_place_retirement_signals_and_waits_for_an_owned_process
  )
  cargo test --frozen --release --all-targets --all-features \
    -- --test-threads=1 "${skip[@]}"
}

package() {
  cd "$pkgname-$pkgver"

  install -Dm755 target/release/strata "$pkgdir/usr/bin/strata"
  install -Dm644 data/io.github.lgse.Strata.desktop \
    "$pkgdir/usr/share/applications/io.github.lgse.Strata.desktop"
  install -Dm644 data/icons/scalable/apps/io.github.lgse.Strata.svg \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/io.github.lgse.Strata.svg"
  # Payload for the app's per-user FileManager1 opt-in; upstream's own
  # packaging stages it here, not in /usr/share/dbus-1.
  install -Dm644 data/io.github.lgse.Strata.FileManager1.service \
    "$pkgdir/usr/share/strata/io.github.lgse.Strata.FileManager1.service"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 THIRD_PARTY_LICENSES.md \
    "$pkgdir/usr/share/licenses/$pkgname/THIRD_PARTY_LICENSES.md"
  install -Dm644 README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
}
