name: antares
adopt-info: antares
summary: Open source SQL client made to be simple and complete.
description: |
  Antares is an SQL client that aims to become an useful and complete tool, especially for developers.
  The target is to support as many databases as possible, and all major operating systems, including the ARM versions.
base: core20

grade: stable
confinement: strict

architectures:
  - build-on: amd64
compression: lzo
layout:
  /etc/nsswitch.conf:
    bind-file: $SNAP/etc/nsswitch.conf

parts:
  antares:
    plugin: dump
    source: .
    override-build: |
      snapcraftctl build
      ARCHITECTURE=$(dpkg --print-architecture)
      if [ "${ARCHITECTURE}" = "amd64" ]; then
        FILTER="amd64.deb"
      else
        echo "ERROR! Antares only produces debs for amd64. Failing the build here."
        exit 1
      fi
      # Get the latest releases json
      echo "Get GitHub releases..."
      wget --quiet https://api.github.com/repos/fabio286/antares/releases/latest -O releases.json
      # Get the version from the tag_name and the download URL.
      VERSION=$(jq . releases.json | grep tag_name | cut -d'"' -f4 | sed s'/release-//')
      DEB_URL=$(cat releases.json | jq -r ".assets[] | select(.name | test(\"${FILTER}\")) | .browser_download_url")
      DEB=$(basename "${DEB_URL}")
      echo "Downloading ${DEB_URL}..."
      wget --quiet "${DEB_URL}" -O "${SNAPCRAFT_PART_INSTALL}/${DEB}"
      echo "Unpacking ${DEB}..."
      dpkg -x "${SNAPCRAFT_PART_INSTALL}/${DEB}" ${SNAPCRAFT_PART_INSTALL}
      rm -f releases.json 2>/dev/null
      rm -f "${SNAPCRAFT_PART_INSTALL}/${DEB}" 2>/dev/null
      echo $VERSION > $SNAPCRAFT_STAGE/version
      # Correct path to icon.
      sed -i 's|Icon=antares|Icon=/usr/share/icons/hicolor/256x256/apps/antares\.png|g' ${SNAPCRAFT_PART_INSTALL}/usr/share/applications/antares.desktop
      # Delete usr/bin/antares, it's a broken symlink pointing outside the snap.
      rm -f ${SNAPCRAFT_PART_INSTALL}/usr/bin/antares
      chmod -s ${SNAPCRAFT_PART_INSTALL}/opt/Antares/chrome-sandbox
      snapcraftctl set-version "$(echo $VERSION)"
    build-packages:
      - dpkg
      - jq
      - sed
      - wget
    stage-packages:
      - fcitx-frontend-gtk3
      - libappindicator3-1
      - libasound2
      - libcurl4
      - libgconf-2-4
      - libgtk-3-0
      - libnotify4
      - libnspr4
      - libnss3
      - libpcre3
      - libpulse0
      - libxss1
      - libsecret-1-0
      - libxtst6
      - libxkbfile1
      - gcc-10-base
      - libapparmor1
      - libblkid1
      - libbsd0
      - libcom-err2
      - libcrypt1
      - libdb5.3
      - libdbus-1-3
      - libexpat1
      - libffi7
      - libgcc-s1
      - libgcrypt20
      - libglib2.0-0
      - libgmp10
      - libgnutls30
      - libgpg-error0
      - libgssapi-krb5-2
      - libhogweed5
      - libidn2-0
      - libjson-c4
      - libk5crypto3
      - libkeyutils1
      - libkrb5-3
      - libkrb5support0
      - liblz4-1
      - liblzma5
      - libmount1
      - libnettle7
      - libp11-kit0
      - libpcre2-8-0
      - libselinux1
      - libsqlite3-0
      - libssl1.1
      - libstdc++6
      - libsystemd0
      - libtasn1-6
      - libudev1
      - libunistring2
      - libuuid1
      - libwrap0
      - libzstd1
      - zlib1g 
      - libx11-xcb1
      - libdrm2
      - libgbm1
      - libxcb-dri3-0
  cleanup:
    after: [antares]
    plugin: nil
    build-snaps: [gnome-3-38-2004]
    override-prime: |
      set -eux
      cd /snap/gnome-3-38-2004/current
      find . -type f,l -exec rm -f $SNAPCRAFT_PRIME/{} \;

  mdns-lookup:
    # Make resolution of ".local" host names (Zero-Conf/mDNS/DNS-SD)
    # working: Take the original nsswitch.conf file from the base
    # Snap and add "mdns4_minimal [NOTFOUND=return]" to its "hosts:" line
    # Also install corresponding mdns4_minimal plug-in
    # See: https://forum.snapcraft.io/t/no-mdns-support-in-snaps-should-core-have-a-modified-nsswitch-conf/
    plugin: nil
    stage-packages:
      - libnss-mdns
    override-prime: |
      set -eux
      sed -Ee 's/^\s*hosts:(\s+)files/hosts:\1files mdns4_minimal \[NOTFOUND=return\]/' /snap/core20/current/etc/nsswitch.conf > $SNAPCRAFT_STAGE/etc/nsswitch.conf
      snapcraftctl prime
    prime:
      - lib/$SNAPCRAFT_ARCH_TRIPLET/libnss_mdns4_minimal*
      - etc/nsswitch.conf

apps:
  antares:
    command: opt/Antares/antares --no-sandbox
    desktop: usr/share/applications/antares.desktop
    extensions: [gnome-3-38]
    environment:
      # Fallback to XWayland if running in a Wayland session.
      DISABLE_WAYLAND: 1
    plugs:
      - browser-support
      - cups-control
      - home
      - network
      - opengl
      - pulseaudio
      - removable-media
      - unity7
