# Maintainer: GM <gianmarcomorales@icloud.com>

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

depends=(
  'bubblewrap'
  'gcc-libs'
  'glib2'
  'glibc'
  'hicolor-icon-theme'
  'omarchy'
  'quickshell'
  'qt6-multimedia'
  'qt6-webengine'
  'shared-mime-info'
  'util-linux'
  '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'
  'wl-clipboard: copy Dropbox share links to the clipboard'
)
conflicts=('flea-git')
options=('!debug')

_security_patches=(
  '2bd7cc207c110ae3b3ea61a4d4e42336f1815111'
  '27d19ca4a38ef4a0a920fb78441ce662a0101327'
  '23290ce1917b0d7f0392dd09d04bf2a7504ec93a'
  'b4b7ee47244b52457eec2874e21a8656d8ace647'
)
source=(
  "$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz"
  "security-2bd7cc2.patch::$url/commit/${_security_patches[0]}.patch"
  "security-27d19ca.patch::$url/commit/${_security_patches[1]}.patch"
  "security-23290ce.patch::$url/commit/${_security_patches[2]}.patch"
  "security-b4b7ee4.patch::$url/commit/${_security_patches[3]}.patch"
)
sha256sums=(
  '3599ab76253c444dea027f1ebe85b32612d9a174e60eb8aca33ee21d9e2d6973'
  'c610a9f44294b67341940203943c9c567003b65cc436d6013cd36754379183d8'
  'e457ef17e26f70057b1184eeb938fccc5906f48a7c77f1df573d55d13a845425'
  'f8381456a3b5f39d341cc6610bf27beb8354892b17a1c8c6d7882db4e40824c3'
  '46bdbe43f135a052c893b1987f8b0928736445616a84b0c65d2181ee74b21b25'
)

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

  security_patch_present() {
    case "$1" in
      2bd7cc207c110ae3b3ea61a4d4e42336f1815111)
        grep -Fq 'a.push("--".to_string());' src/backend/archive.rs &&
          grep -Fq 'let input = std::fs::canonicalize(input)' src/backend/archiveops.rs &&
          grep -Fq 'if op != "compress" && op != "extract"' \
            src/backend/run.rs src/backend/archivereq.rs
        ;;
      27d19ca4a38ef4a0a920fb78441ce662a0101327)
        grep -Fq 'the sandbox is unavailable: bwrap or prlimit is not on PATH' src/backend/archivework.rs &&
          grep -Fq 'if !sandbox::available()' src/backend/mediaprobe.rs &&
          grep -Fq 'if !sandbox::available()' src/backend/metareq.rs
        ;;
      23290ce1917b0d7f0392dd09d04bf2a7504ec93a)
        grep -Fq 'copyToClipboard.command = ["wl-copy", url]' ui/ShareLink.qml
        ;;
      b4b7ee47244b52457eec2874e21a8656d8ace647)
        grep -Fq '.custom_flags(O_NOFOLLOW)' src/backend/copyfile.rs
        ;;
      *)
        return 1
        ;;
    esac
  }

  local commit patch_file
  for commit in "${_security_patches[@]}"; do
    patch_file="$srcdir/security-${commit:0:7}.patch"

    if patch --batch --forward --dry-run -Np1 -i "$patch_file" >/dev/null 2>&1; then
      patch --batch --forward -Np1 -i "$patch_file"
    elif security_patch_present "$commit"; then
      printf 'Security patch %s is already present upstream\n' "$commit"
    else
      printf 'Security patch %s no longer applies and is not present upstream\n' "$commit" >&2
      return 1
    fi
  done
}

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_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

  cargo test --frozen --release -- "${test_args[@]}"
  ./tests/js.sh
  ./tests/keymap-gen.sh
}

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

  install -Dm755 target/release/flea "$pkgdir/usr/bin/flea"
  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"
}
