name: core20
# version: "20"
adopt-info: bootstrap
summary: Runtime environment based on Ubuntu 20.04
description: |
  The base snap based on the Ubuntu 20.04 release.
confinement: strict
type: base
build-base: core20
assumes: [snapd2.61]

parts:
  consoleconf-deb:
    plugin: nil
    source: https://github.com/CanonicalLtd/subiquity.git
    source-type: git
    source-branch: core/focal
    override-pull: |
      snapcraftctl pull
      # install build dependencies
      export DEBIAN_FRONTEND=noninteractive
      export DEBCONF_NONINTERACTIVE_SEEN=true
      sudo -E apt-get build-dep -y ./
    override-build: |
      # unset the LD_FLAGS and LD_LIBRARY_PATH vars that snapcraft sets for us
      # as those will point to the $SNAPCRAFT_STAGE which on re-builds will
      # contain things like libc and friends that confuse the debian package
      # build system
      # TODO: should we unset $PATH to not include $SNAPCRAFT_STAGE too?
      unset LD_FLAGS
      unset LD_LIBRARY_PATH
      # run the real build (but just build the binary package, and don't
      # bother compressing it too much)
      dpkg-buildpackage -b -uc -us -Zgzip -zfast
      cp ../console-conf_*.deb ../subiquitycore_*.deb $SNAPCRAFT_PART_INSTALL
    stage:
      - -console-conf_*.deb
      - -subiquitycore_*.deb
    # XXX: Dirty hacks to enable building core20 on non-focal systems.
    # See below for details.
    override-prime: |
      unset LD_LIBRARY_PATH;
      export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
      # ensure snapcraftctl is found, see
      # https://github.com/snapcore/snapcraft/pull/2251
      export PATH="$PATH:/snap/snapcraft/current/bin/scriptlet-bin"
      snapcraftctl prime
  avahi-daemon-equiv-deb:
    plugin: nil
    build-packages:
      - equivs
    override-build: |
      cat > avahi-daemon.control << \EOF
      Section: misc
      Priority: optional
      Standards-Version: 3.9.2
      Architecture: all

      Package: avahi-daemon
      Version: 1000
      Multi-Arch: foreign
      Description: A dummy version of avahi-daemon

      EOF
      unset LD_FLAGS LD_LIBRARY_PATH
      equivs-build avahi-daemon.control
      cp avahi-daemon_*.deb $SNAPCRAFT_PART_INSTALL
    stage:
      - -avahi-daemon_*.deb
  bootstrap:
    after:
      - consoleconf-deb
      - avahi-daemon-equiv-deb
    plugin: make
    source: .
    build-packages:
      - shellcheck
      - wget
      - distro-info
      - python3-debian
      - python3-requests
      - python3-yaml
    # XXX: Dirty hacks to enable building core20 on non-focal systems.
    # Without these overrides both the PATH and LD_LIBRARY_PATH contain paths
    # in the part's install directory which binaries can be incompatible with
    # the ones running on our system.  We don't need those while running stage
    # and prime anyway.
    override-pull: |
      unset LD_LIBRARY_PATH;
      snapcraftctl set-version "$(/bin/date +%Y%m%d)"
      snapcraftctl pull
    override-stage: |
      unset LD_LIBRARY_PATH;
      export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
      # ensure snapcraftctl is found, see
      # https://github.com/snapcore/snapcraft/pull/2251
      export PATH="$PATH:/snap/snapcraft/current/bin/scriptlet-bin"
      snapcraftctl stage
    override-prime: |
      unset LD_LIBRARY_PATH;
      export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
      # ensure snapcraftctl is found, see
      # https://github.com/snapcore/snapcraft/pull/2251
      export PATH="$PATH:/snap/snapcraft/current/bin/scriptlet-bin"
      snapcraftctl prime
      # ensure build-in tests are run
      cd ${SNAPCRAFT_PART_SRC} && make test TESTDIR=${SNAPCRAFT_PRIME}
