# Reference:
# https://appimage-builder.readthedocs.io/en/latest/reference/version_1.html

version: 1

script: |
  set -eu
  # Clean up directories created by the recipe
  rm "$TARGET_APPDIR" -rf || true
  rm "$REPO_DIR" -rf || true
  # Create a temporary Debian repository folder to put the generated Debian
  # packages on, so we can install it like any other packages
  mkdir -p "$REPO_DIR"
  _deb="$(find "${TARGET_APPIMAGE_DIR}/../deb" -type f -name '*.deb' | sort -t '_' -k '2,2' -V | tail -n 1)"
  cp -f "$_deb" "$REPO_DIR"
  CWD="$PWD" && cd "$REPO_DIR" && apt-ftparchive packages . > Packages && cd "$CWD"
  # The following two commands are a workaround for the local APT package not
  # being saved to the archives directory, as expected by appimage-builder
  mkdir -p appimage-build/apt/archives
  cp -f "$_deb" appimage-build/apt/archives

AppDir:
  path: !ENV '${TARGET_APPDIR}'

  app_info:
    id: com.spacedrive
    name: Spacedrive
    icon: spacedrive
    version: !ENV '${VERSION}'
    exec: usr/bin/spacedrive
    exec_args: $@

  apt:
    arch: !ENV ${TARGET_APPIMAGE_APT_ARCH}
    sources:
      - sourceline: !ENV 'deb [arch=${TARGET_APPIMAGE_APT_ARCH}] http://deb.debian.org/debian bookworm main'
        key_url: 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x1f89983e0081fde018f3cc9673a4f27b8dd47936'
      - sourceline: !ENV 'deb [arch=${TARGET_APPIMAGE_APT_ARCH}] http://security.debian.org/debian-security bookworm-security main'
        key_url: 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x1f89983e0081fde018f3cc9673a4f27b8dd47936'
      - sourceline: !ENV 'deb [arch=${TARGET_APPIMAGE_APT_ARCH}] http://deb.debian.org/debian bookworm-updates main'
        key_url: 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xac530d520f2f3269f5e98313a48449044aad5c5d'
      - sourceline: !ENV 'deb [trusted=yes] file:${REPO_DIR}/ ./'

    include:
      - spacedrive
      - gstreamer1.0-plugins-good
      - gstreamer1.0-plugins-ugly
      - gstreamer1.0-packagekit
      # This package is necessary to avoid this: https://github.com/AppImageCrafters/appimage-builder/pull/191
      - shared-mime-info

    exclude:
      - systemd
      - systemd-sysv
      - util-linux
      - util-linux-extra
      - xkb-data
      - fdisk
      - dmsetup
      - mount
      - dbus-daemon
      - perl-base
      - readline-common
      - libpam-modules
      - avahi-daemon
      - adwaita-icon-theme
      - humanity-icon-theme
      - sensible-utils
      - dconf-service
      - ubuntu-mono
      - fonts-*-core
      - bubblewrap
      - gstreamer1.0-plugins-bad
      - gstreamer1.0-libav

  files:
    exclude:
      # Development, localization, documentation and configuration files
      - usr/include
      - usr/share/bug
      - usr/share/man
      - usr/share/doc
      - usr/share/doc-base
      - usr/share/lintian
      - usr/share/sounds
      - usr/share/bash-completion
      - usr/share/zsh
      - usr/share/applications
      - usr/share/pkgconfig
      - usr/share/pam
      - usr/share/pam-configs
      - usr/share/polkit-1
      - usr/share/util-linux
      - usr/share/initramfs-tools
      - usr/share/locale
      - usr/share/systemd
      - usr/lib/systemd
      - usr/lib/*-linux-gnu/systemd
      - usr/lib/udev
      - usr/lib/*.d
      - usr/lib/kernel
      - usr/lib/tmpfiles.d
      # using our own ALSA can cause issues, and the API is pretty stable anyway
      - usr/lib/*/libasound.so.*
      # produced by library compilation
      - usr/lib/*.a
      # produced by library compilation
      - usr/lib/cmake
      # produced by library compilation
      - usr/lib/pkgconfig
      - etc
      - var
      # systemd, pam, lsb, modprobe.d, udev and misc daemon files
      - lib/systemd
      - lib/*-linux-gnu/security
      - lib/udev
      - lib/lsb
      - lib/modprobe.d
      # Unneeded utility programs and libraries
      - usr/sbin
      # Remove any binary but spacedrive
      - usr/bin/[!s]*
      - usr/bin/s[!p]*
      - usr/bin/sp[!a]*
      - usr/bin/spa[!c]*
      - usr/bin/spac[!e]*
      - sbin
      - bin

  runtime:
    path_mappings:
      - !ENV '/usr/lib/${TARGET_APPIMAGE_ARCH}-linux-gnu/webkit2gtk-4.0/injected-bundle/libwebkit2gtkinjectedbundle.so:$APPDIR/usr/lib/${TARGET_APPIMAGE_ARCH}-linux-gnu/webkit2gtk-4.0/injected-bundle/libwebkit2gtkinjectedbundle.so'

  # Patch webkit2gtk so it works under appimage
  after_bundle: |
    set -eux

    # libwebkit2gtk Patch for WebKit binaries
    mv "${TARGET_APPDIR}/usr/lib/${TARGET_APPIMAGE_ARCH}-linux-gnu/webkit2gtk-4.0/WebKitWebProcess" "${TARGET_APPDIR}/usr/bin/WebKitWebProcess"
    mv "${TARGET_APPDIR}/usr/lib/${TARGET_APPIMAGE_ARCH}-linux-gnu/webkit2gtk-4.0/WebKitNetworkProcess" "${TARGET_APPDIR}/usr/bin/WebKitNetworkProcess"
    if [ "$TARGET_APPIMAGE_ARCH" == 'aarch64' ]; then
      find "${TARGET_APPDIR}/usr/lib/aarch64-linux-gnu" -type f -name 'libwebkit2gtk-4.0.so*' -exec sed -i -e "s|/usr/lib/aarch64-linux-gnu/webkit2gtk-4.0\([^/][^i][^n]\)|../../././././././././././././././usr/bin\1|g" '{}' \;
    else
      find "${TARGET_APPDIR}/usr/lib/x86_64-linux-gnu" -type f -name 'libwebkit2gtk-4.0.so*' -exec sed -i -e "s|/usr/lib/x86_64-linux-gnu/webkit2gtk-4.0\([^/][^i][^n]\)|../.././././././././././././././usr/bin/\1|g" '{}' \;
    fi

    # libwebkit2gtk Patch for gstreamer binaries
    mv "${TARGET_APPDIR}/usr/libexec/pk-gstreamer-install" "${TARGET_APPDIR}/usr/bin/pk-gstreamer-install"
    ln -sf pk-gstreamer-install "${TARGET_APPDIR}/usr/bin/gst-install-plugins-helper"
    mv "${TARGET_APPDIR}/usr/lib/${TARGET_APPIMAGE_ARCH}-linux-gnu/gstreamer1.0/gstreamer-1.0/gst-ptp-helper" "${TARGET_APPDIR}/usr/bin/gst-ptp-helper"
    mv "${TARGET_APPDIR}/usr/lib/${TARGET_APPIMAGE_ARCH}-linux-gnu/gstreamer1.0/gstreamer-1.0/gst-plugin-scanner" "${TARGET_APPDIR}/usr/bin/gst-plugin-scanner"
    find "${TARGET_APPDIR}/usr/lib/${TARGET_APPIMAGE_ARCH}-linux-gnu" -type f -name 'libwebkit2gtk-4.0.so*' -exec sed -i -e "s|/usr/libexec/gstreamer-1.0|../../././././././usr/bin/|g" '{}' \;

  after_runtime: |
    set -eux

    find "$TARGET_APPDIR" -type d -empty -delete

  # Tests don't work due to Tauri security checks we can't work around:
  # https://github.com/tauri-apps/tauri/blob/35264b4c1801b381e0b867c1c35540f0fbb43365/core/tauri-utils/src/lib.rs#L202

AppImage:
  arch: !ENV '${TARGET_APPIMAGE_ARCH}'
  comp: xz
  sign-key: None
  update-information: None
