name: simple-scan
adopt-info: simple-scan
summary: Document Scanning Application
description: |
  A really easy way to scan both documents and photos from a scanner (e.g. a flatbed scanner).

grade: stable # must be 'stable' to release into candidate/stable channels
confinement: strict
base: core22

# this is not used or needed for anything other than to trigger automatic
# installation of the cups snap via "default-provider: cups"
plugs:
  foo-install-cups:
    interface: content
    content: foo
    default-provider: cups
    target: $SNAP_DATA/foo

slots:
  # for GtkApplication registration
  simple-scan:
    interface: dbus
    bus: session
    name: org.gnome.SimpleScan

# Make our libsane find the SANE backends (scanner drivers)
layout:
  /etc:
    bind: $SNAP/etc
  /usr/lib/sane:
    bind: $SNAP/usr/lib/sane


apps:
  simple-scan:
    plugs:
      - network
      - home
      - cups
      - raw-usb   #This will need manual connection
      - hardware-observe
      - avahi-observe
    command: usr/bin/simple-scan
    extensions: [ gnome ]
    desktop: usr/share/applications/simple-scan.desktop
    environment:
      GSETTINGS_SCHEMA_DIR: $SNAP/share/glib-2.0/schemas

parts:
  libsane:
# ext: updatesnap
    source: https://gitlab.com/sane-project/backends.git
    source-type: git
    source-tag: '1.3.1'
    source-depth: 1
    plugin: autotools
    autotools-configure-parameters:
      - --prefix=/usr
      - --sysconfdir=/etc
      - --with-api-spec=no
      - --with-avahi
    build-packages:
      # Everything of the build dependencies of the sane-backends
      # Debian package ...
      - libavahi-client-dev
      - libcups2-dev
      - libcurl4-gnutls-dev
      - libgphoto2-dev
      - libieee1284-3-dev
      - libjpeg-dev
      - libltdl3-dev
      - libpoppler-glib-dev
      - libsnmp-dev
      - libtiff-dev
      - libusb-1.0-0-dev
      - libxml2-dev
      - python3-minimal
    stage-packages:
      - libexif12
      - libgphoto2-6
      - libgphoto2-port12
      - libsnmp40
    organize:
      snap/simple-scan/current/usr: usr

  libsane-airscan:
# ext: updatesnap
    after: [libsane]
    source: https://github.com/alexpevzner/sane-airscan
    source-type: git
    source-tag: '0.99.29'
    source-depth: 1
    plugin: make
    make-parameters:
      - prefix=/usr
      - sysconfdir=/etc
      - libdir=/usr/lib
    build-packages:
      # Everything of the build dependencies of the sane-airscan
      # Debian package ...
      - libavahi-client-dev
      - libavahi-common-dev
      - libjpeg-turbo8-dev
      - libpng-dev
      - libxml2-dev

  simple-scan:
    after: [libsane]
    source: https://gitlab.gnome.org/GNOME/simple-scan.git
    source-type: git
    source-tag: '46.0'
    source-depth: 1
    plugin: meson
    parse-info: [usr/share/metainfo/simple-scan.appdata.xml]
    meson-parameters:
      - --prefix=/usr
    override-build: |
      sed -i.bak -e 's|Icon=org.gnome.SimpleScan$|Icon=${SNAP}/meta/gui/org.gnome.SimpleScan.svg|g' $CRAFT_PART_SRC/data/simple-scan.desktop.in
      mkdir -p $CRAFT_PART_INSTALL/meta/gui/
      cp $CRAFT_PART_SRC/data/icons/hicolor/scalable/org.gnome.SimpleScan.svg $CRAFT_PART_INSTALL/meta/gui/
      craftctl default
    build-packages:
      - desktop-file-utils
      - gnome-common
      - intltool
      - libcolord-dev
      - libgusb-dev
      - libpackagekit-glib2-dev

  libs:
    plugin: nil
    stage-packages:
      - libgusb2
      - libpackagekit-glib2-18
      - libieee1284-3

  cleanup:
    after: [ libs ]
    plugin: nil
    build-snaps: [core22, gtk-common-themes, gnome-42-2204]
    override-prime: |
      set -eux
      for snap in "core22" "gtk-common-themes" "gnome-42-2204"; do
        cd "/snap/$snap/current" && find . -type f,l -name *.so.* -exec rm -f "$CRAFT_PRIME/{}" \;
      done