# Maintainer: GM <gianmarcomorales@icloud.com>

pkgname=flea
pkgver=0.2.1
pkgrel=1
pkgdesc='Fast, keyboard-first file manager for Omarchy'
arch=('x86_64' 'aarch64')
url='https://github.com/thisisgm/flea'
license=('MIT')

depends=(
  'bubblewrap'
  'expect'
  'gcc-libs'
  'glib2'
  'glibc'
  'gvfs'
  'gvfs-dnssd'
  'gvfs-nfs'
  'gvfs-smb'
  'hicolor-icon-theme'
  'kimageformats'
  'libheif'
  'omarchy'
  'python'
  'python-gobject'
  'quickshell'
  'qt6-multimedia'
  'qt6-webengine'
  'shared-mime-info'
  'util-linux'
  'wl-clipboard'
  'xdg-terminal-exec'
  'xdg-utils'
)
makedepends=('cargo')
optdepends=(
  '7zip: 7z archive support'
  'dropbox-cli: Dropbox browsing and share links'
  'ffmpeg: media metadata previews'
  'imagemagick: image conversion'
  'libarchive: archive listing and extraction'
  'tailscale: Taildrop sharing'
)
conflicts=('flea-git')
options=('!debug')

source=(
  "$url/releases/download/v$pkgver/$pkgname-v$pkgver.tar.gz"
)
sha256sums=('75f9ac0274a09a0d55cf7d9187943983c1b78a9e443465738b3ac8af8f2a77e9')

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

  export CARGO_TARGET_DIR=target
  cargo build --frozen --release
}

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

  export CARGO_TARGET_DIR=target

  local -a test_args=()
  if ! /usr/bin/prlimit --cpu=30 --as=1073741824 \
    /usr/bin/bwrap --unshare-all --die-with-parent --new-session --clearenv \
    --ro-bind /usr /usr --ro-bind /etc /etc \
    --symlink usr/lib /lib --symlink usr/lib /lib64 \
    --symlink usr/bin /bin --symlink usr/bin /sbin \
    --proc /proc --dev /dev --tmpfs /tmp /usr/bin/true >/dev/null 2>&1; then
    printf 'Skipping sandbox integration tests: this builder cannot create an unprivileged namespace\n'
    test_args+=(
      --skip backend::archiveops::tests::a_silent_failure_names_the_operation_that_was_running
      --skip tui::job::tests::preview_worker_reads_held_source_in_readonly_sandbox
      --skip backend::archiveops::tests::a_tool_that_exits_zero_writing_nothing_is_caught_by_the_predicate
      --skip backend::archiveops::tests::an_empty_archive_extracts_to_an_empty_directory_and_that_is_success
      --skip backend::archiveops::tests::only_the_archive_root_extracts_to_nothing_while_nested_directories_do_not
      --skip backend::archiveops::tests::the_emptiness_check_reads_the_destination_and_the_index_separately
      --skip backend::thumbs::tests::a_cancelled_job_never_runs
      --skip backend::thumbs::tests::a_real_file_round_trips_to_a_stamped_cache_entry
      --skip backend::thumbs::tests::cancel_all_empties_the_queue
      --skip backend::thumbs::tests::the_queue_is_bounded_and_drops_the_oldest_rather_than_growing
    )
  fi

  if [[ ! -x /usr/bin/evince-thumbnailer ]] ||
    [[ ! -x /usr/bin/ffmpegthumbnailer ]] ||
    [[ ! -x /usr/bin/glycin-thumbnailer ]]; then
    printf 'Skipping thumbnailer fixture tests: this builder lacks their optional host programs\n'
    test_args+=(
      --skip backend::thumbspec::tests::a_type_two_files_declare_goes_to_the_first_in_path_order
      --skip backend::thumbspec::tests::the_heaviest_shipped_field_parses
    )
  fi

  # Recovery-lock tests are unreliable when run concurrently with other suites.
  cargo test --frozen --release -- --test-threads=1 "${test_args[@]}"
  ./tests/js.sh
  ./tests/keymap-gen.sh
}

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

  install -Dm755 target/release/flea "$pkgdir/usr/bin/flea"
  install -Dm755 tools/flea-gio-auth "$pkgdir/usr/lib/flea/flea-gio-auth"
  install -Dm755 tools/flea-portal "$pkgdir/usr/lib/flea/flea-portal"
  install -Dm644 packaging/flea.portal \
    "$pkgdir/usr/share/xdg-desktop-portal/portals/flea.portal"
  install -Dm644 packaging/org.freedesktop.impl.portal.desktop.flea.service \
    "$pkgdir/usr/share/dbus-1/services/org.freedesktop.impl.portal.desktop.flea.service"
  install -Dm755 tools/flea-filemanager1 "$pkgdir/usr/lib/flea/flea-filemanager1"
  install -Dm644 packaging/com.thisisgm.flea.FileManager1.service \
    "$pkgdir/usr/share/dbus-1/services/com.thisisgm.flea.FileManager1.service"
  install -Dm644 packaging/com.thisisgm.flea.desktop \
    "$pkgdir/usr/share/applications/com.thisisgm.flea.desktop"
  install -Dm644 packaging/com.thisisgm.flea.svg \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/com.thisisgm.flea.svg"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  install -Dm644 ui/qmldir ui/*.qml -t "$pkgdir/usr/share/flea/ui"
  install -Dm644 ui/js/*.js -t "$pkgdir/usr/share/flea/ui/js"
  ln -s /usr/share/omarchy/shell/Commons "$pkgdir/usr/share/flea/ui/Commons"
  ln -s /usr/share/omarchy/shell/Ui "$pkgdir/usr/share/flea/ui/Ui"
}
