# Copyright (C) Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

name: thunderbird
version: "128.8.0esr-5"
summary: Mozilla Thunderbird email application
description: Thunderbird is a free email application that’s easy to set up and customize - and it’s loaded with great features!
confinement: strict
grade: stable
base: core22
compression: lzo
contact: https://www.thunderbird.net/contact/
donation: https://www.thunderbird.net/donate/
issues: https://bugzilla.mozilla.org/enter_bug.cgi?product=Thunderbird&op_sys=Linux&keywords=snap
website: https://www.thunderbird.net/

apps:
  thunderbird:
    command-chain: [ bin/gpg-shim, bin/tmpdir ]
    command: thunderbird.launcher
    extensions: [gnome]
    environment:
      DICPATH: "$SNAP_COMMON/snap-hunspell"
      GTK_USE_PORTAL: 1
      HOME: "$SNAP_USER_COMMON"
    plugs:
      - avahi-observe
      - browser-sandbox
      - camera
      - cups-control
      - gsettings
      - gpg-keys
      - home
      - host-usr-share-hunspell
      - network
      - network-control
      - opengl
      - audio-playback
      - removable-media
      - u2f-devices
    slots:
      - dbus-daemon

plugs:
  browser-sandbox:
    interface: browser-support
    allow-sandbox: true
  etc-thunderbird:
    interface: system-files
    read: [/etc/thunderbird]
  dot-thunderbird:
    interface: personal-files
    write: [$HOME/.thunderbird]
  host-usr-share-hunspell:
    interface: system-files
    read: [/var/lib/snapd/hostfs/usr/share/hunspell]

parts:
  # This is a temporary workaround to including the hunspell content
  # snap, which would cause breakage in the Ubuntu desktop image build
  # because of the Ubuntu policy.  See:
  # https://bugzilla.mozilla.org/show_bug.cgi?id=1792006
  #
  # The definition of this part is essentially a copy of the
  # corresponding part in hunspell-dictionaries-1-7-2004 by
  # Buo-ren, Lin.
  hunspell:
    plugin: nil
    override-build: |
      set -eu
      craftctl default
      apt download $(apt-cache search '^hunspell-.*$' |
        awk '!/myspell|dbgsym|tools|transitional|dependency/{printf "%s ", $1}')
      find . -name "*.deb" -exec dpkg-deb -x {} "$CRAFT_PART_INSTALL" \;
    prime:
      - usr/share/hunspell

  rust:
    plugin: nil
    build-packages:
      - wget
    override-pull: |
      # Do not use rustup to work around https://forum.snapcraft.io/t/armhf-builds-on-launchpad-timing-out/31008
      REQUIRED_RUST_VERSION=1.78.0
      ROOT=https://static.rust-lang.org/dist/rust-$REQUIRED_RUST_VERSION
      if [ $CRAFT_TARGET_ARCH = "amd64" ]; then
        BINARIES_SUFFIX=x86_64-unknown-linux-gnu
      elif [ $CRAFT_TARGET_ARCH = "armhf" ]; then
        BINARIES_SUFFIX=armv7-unknown-linux-gnueabihf
      elif [ $CRAFT_TARGET_ARCH = "arm64" ]; then
        BINARIES_SUFFIX=aarch64-unknown-linux-gnu
      fi
      wget -O - $ROOT-$BINARIES_SUFFIX.tar.gz | tar -x -z --strip-components=1
      ./install.sh --prefix=/usr --destdir=$CRAFT_STAGE
    override-prime: ''

  # For thunderbird beta, the dump-syms git informations is pulled from the
  # mozilla-beta trees, because there seems to be no mozilla/ in the beta tree,
  dump-syms:
    plugin: nil
    after:
      - rust
    build-packages:
      - curl
      - git
      - jq
      - python3-yaml
    override-pull: |
      if [ $CRAFT_ARCH_BUILD_FOR = "amd64" ] || [ $CRAFT_ARCH_BUILD_FOR = "arm64" ]; then
        VERSION=$(craftctl get version | cut -d- -f1)
        BUILD=$(craftctl get version | cut -d- -f2)
        TBINFO=$(curl -s https://ftp.mozilla.org/pub/thunderbird/candidates/$VERSION-candidates/build$BUILD/linux-x86_64/en-US/thunderbird-$VERSION.json)
        TB_SOURCE_REPO=$(echo $TBINFO | jq -r .moz_source_repo)
        TB_SOURCE_STAMP=$(echo $TBINFO | jq -r .moz_source_stamp)
        GECKO_REV=$TB_SOURCE_REPO/raw-file/${TB_SOURCE_STAMP}/.gecko_rev.yml
        GECKO_REVS=$(curl -sSL "${GECKO_REV}" | grep -v "#")
        MOZ_SOURCE_REPO=$(echo "$GECKO_REVS" | grep GECKO_HEAD_REPOSITORY | sed -e 's/GECKO_HEAD_REPOSITORY: //g')
        MOZ_SOURCE_STAMP=$(echo "$GECKO_REVS" | grep GECKO_HEAD_REV | sed -e 's/GECKO_HEAD_REV: //g')
        FETCHES=$MOZ_SOURCE_REPO/raw-file/$MOZ_SOURCE_STAMP/taskcluster/kinds/fetch/toolchains.yml
        TOOLCHAINS=$(curl -sSL "${FETCHES}")
        unset PYTHONPATH
        REPO=$(echo "${TOOLCHAINS}" | /usr/bin/python3 -c 'import yaml, sys; fetches = yaml.safe_load(sys.stdin); print("{}".format(fetches["dump-syms"]["fetch"]["repo"]))')
        SHA1=$(echo "${TOOLCHAINS}" | /usr/bin/python3 -c 'import yaml, sys; fetches = yaml.safe_load(sys.stdin); print("{}".format(fetches["dump-syms"]["fetch"]["revision"]))')
        git clone "${REPO}" . && git checkout "${SHA1}"
      fi
    override-build: |
      craftctl default
      if [ $CRAFT_ARCH_BUILD_FOR = "amd64" ] || [ $CRAFT_ARCH_BUILD_FOR = "arm64" ]; then
        $CRAFT_STAGE/usr/bin/cargo build --release
        mkdir -p $CRAFT_STAGE/usr/bin/
        cp target/release/dump_syms $CRAFT_STAGE/usr/bin/
      fi

  cbindgen:
    plugin: nil
    after:
      - rust
    override-build: |
      craftctl default
      cargo install cbindgen
    override-prime: ''
  mozconfig:
    plugin: nil
    override-stage: |
      cp $CRAFT_PROJECT_DIR/mozconfig.in $CRAFT_STAGE/mozconfig
    override-prime: ''

  # Launchpad builders have a timeout for how long they are allowed to access
  # the internet (through a proxy) starting from the start of the build.
  # Since the thunderbird part takes a long time to build, we need to ensure
  # that all other parts that need to access the internet (to e.g. fetch build
  # or stage packages) are built before it (before the proxy authentication is
  # revoked).
  thunderbird:
    plugin: nil
    after:
      - cbindgen
      - dump-syms
      - mozconfig
      - rust
      - thunderbird-langpacks
    build-packages:
      - cargo
      - clang-15
      - cmake
      - coreutils
      - file
      - git
      - libasound2-dev
      - libclang-15-dev
      - libdbus-glib-1-dev
      - llvm-15-dev
      - libpython3-dev
      - libx11-xcb-dev
      - libxt-dev
      - m4
      - make
      - nasm
      - nodejs
      - quilt
      - rustc
      - unzip
      - wget
      - xvfb
      - zip
    override-pull: |
      VERSION=$(craftctl get version | cut -d- -f1)
      BUILD=$(craftctl get version | cut -d- -f2)
      ROOT=https://ftp.mozilla.org/pub/thunderbird/candidates/$VERSION-candidates/build$BUILD
      wget -O - $ROOT/source/thunderbird-$VERSION.source.tar.xz | tar -x --xz --strip-components=1
      mkdir -p $CRAFT_STAGE/debug-symbols
      cp toolkit/crashreporter/tools/upload_symbols.py $CRAFT_STAGE/debug-symbols/
    override-build: |
      craftctl default
      export PATH=$CRAFT_STAGE/usr/bin:/usr/lib/llvm-15/bin/:$PATH
      export MOZCONFIG="$CRAFT_STAGE/mozconfig"
      QUILT_PATCHES=$CRAFT_PROJECT_DIR/patches quilt push -a
      GNOME_SDK_SNAP=/snap/gnome-42-2204-sdk/current
      if [ $CRAFT_TARGET_ARCH = "amd64" ]; then
        # "clang -dumpmachine" returns "x86_64-unknown-linux-gnu" on
        # amd64 (at least the binaries they distribute), but what we
        # really need is "x86_64-pc-linux-gnu"; so let's hard-code it.
        export TARGET_TRIPLET="x86_64-pc-linux-gnu"
      else
        export TARGET_TRIPLET=$(clang-15 -dumpmachine)
      fi
      export LDFLAGS="-Wl,-rpath-link=$GNOME_SDK_SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_ON -Wl,-rpath-link=$GNOME_SDK_SNAP/usr/lib"
      export LDFLAGS="-Wl,-rpath-link=$CRAFT_PART_BUILD/obj-$TARGET_TRIPLET/dist/bin${LDFLAGS:+ $LDFLAGS}"
      export LD_LIBRARY_PATH="$CRAFT_PART_BUILD/obj-$TARGET_TRIPLET/dist/bin${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
      export MOZBUILD_STATE_PATH=$CRAFT_PART_BUILD/.mozbuild
      unset PYTHONPATH
      if [ $CRAFT_TARGET_ARCH = "amd64" ]; then
        # Needed for PGO-enabled builds that execute the built binaries. This should mirror the link paths in $LDFLAGS.
        export LD_LIBRARY_PATH="$GNOME_SDK_SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_ON:$GNOME_SDK_SNAP/usr/lib"
        # Ensure the instrumented binary is run against the right version of libssl3.so and libnss3.so
        # (locally-built versions are more recent than the ones in the gnome platform snap)
        export LD_LIBRARY_PATH="$CRAFT_PART_BUILD/obj-$TARGET_TRIPLET/instrumented/dist/bin${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
      fi
      BUILD_DBGSYMS=true
      if [ $BUILD_DBGSYMS = "true" ]; then
        echo "ac_add_options --enable-debug-symbols" >> $MOZCONFIG
      else
        echo "ac_add_options --disable-debug-symbols" >> $MOZCONFIG
      fi
      MACH="/usr/bin/python3 ./mach"
      $MACH configure --prefix=$CRAFT_PART_INSTALL/usr
      $MACH uniffi generate # workaround for https://bugzilla.mozilla.org/1797714
      $MACH build -j$CRAFT_PARALLEL_BUILD_COUNT
      $MACH install
      DISTRIBUTION=$CRAFT_PART_INSTALL/usr/lib/thunderbird/distribution
      mkdir -p $DISTRIBUTION/extensions
      mkdir -p $CRAFT_PART_INSTALL/bin/
      cp -p $CRAFT_PROJECT_DIR/gpg-shim $CRAFT_PART_INSTALL/bin/
      cp -p $CRAFT_PROJECT_DIR/tmpdir $CRAFT_PART_INSTALL/bin/
      cp -pr $CRAFT_PART_SRC/* $CRAFT_PART_INSTALL
      if [ $BUILD_DBGSYMS = "true" ]; then
        DUMP_SYMS=$CRAFT_STAGE/usr/bin/dump_syms $MACH buildsymbols
      fi
      if [ $BUILD_DBGSYMS = "true" ]; then
        cp obj-*/dist/thunderbird-*.crashreporter-symbols.zip $CRAFT_STAGE/debug-symbols/
      fi
    override-stage: |
      # Workaround for LP: #2016358: create mount points for the gnome
      # content interface, while a proper fix is implemented in snapd.
      # Thanks to James Henstridge.
      mkdir $CRAFT_PART_INSTALL/{gnome-platform,data-dir,data-dir/{icons,sounds,themes}}
      craftctl default
    prime:
      - bin
      - usr/lib/thunderbird
      # Workaround for LP: #2016358 (see the 'override-stage' above).
      - gnome-platform
      - data-dir/icons
      - data-dir/sounds
      - data-dir/themes

  thunderbird-langpacks:
    plugin: nil
    build-packages:
      - coreutils
      - sed
      - wget
    override-pull: |
      VERSION=$(craftctl get version | cut -d- -f1)
      BUILD=$(craftctl get version | cut -d- -f2)
      SERVER=https://ftp.mozilla.org
      ROOT=$SERVER/pub/thunderbird/candidates/$VERSION-candidates/build$BUILD
      XPIS=$(wget -O - $ROOT/linux-x86_64/xpi/ | sed -n 's/.* href="\(.*\.xpi\)".*/\1/p')
      for XPI in $XPIS; do
        wget $SERVER$XPI
      done
    override-prime: |
      INSTALLDIR=$CRAFT_PRIME/usr/lib/thunderbird/distribution/extensions
      mkdir -p $INSTALLDIR
      for XPI in $(ls $CRAFT_PART_SRC/*.xpi); do
        LANGCODE=$(basename $XPI .xpi)
        mkdir $INSTALLDIR/locale-$LANGCODE
        cp $XPI $INSTALLDIR/locale-$LANGCODE/langpack-$LANGCODE@thunderbird.mozilla.org.xpi
      done

  thunderbird-staged:
    plugin: nil
    stage-packages:
      - desktop-file-utils
      - gnupg # Needed to use Thunderbird >78 with smartcards.
      - gnupg-agent # Needed to use Thunderbird >78 with smartcards.
      - libblkid1
      - libcurl4 # Needed for the crash reporter (LP: #1983502)
      - libgcrypt20
      - libgpg-error0
      - libgpgme11 # Needed to use Thunderbird >78 with smartcards.
      - liblzma5
      - libudev1
      - libuuid1
      - zlib1g
      - liblz4-1
      - libxt6
    prime:
      - -usr/lib/*/libgio*
      - -usr/lib/*/libglib*
      - -usr/lib/*/libgm*

  debug-symbols:
    plugin: nil
    build-packages:
      - python3
      - python3-virtualenv
    after:
      - thunderbird
    override-build: |
      export SYMBOLS_ARCHIVE=$(find $CRAFT_STAGE/debug-symbols/ -type f -name "thunderbird-*.crashreporter-symbols.zip")
      if [ -f "$SYMBOLS_ARCHIVE" ]; then
        cp $SYMBOLS_ARCHIVE $CRAFT_PROJECT_DIR/$CRAFT_PROJECT_NAME_$(craftctl get version)_$CRAFT_ARCH_BUILD_FOR.debug
      fi

  launcher:
    plugin: nil
    override-prime: |
      cp "$CRAFT_PROJECT_DIR/thunderbird.launcher" "$CRAFT_PRIME/"
      cp "$CRAFT_PROJECT_DIR/patch-default-profile.py" "$CRAFT_PRIME/"

  # Find files provided by the base and platform snap and ensure they aren't
  # duplicated in this snap
  cleanup:
    after: [thunderbird]
    plugin: nil
    build-snaps: [core22, gnome-42-2204]
    override-prime: |
      set -eux
      for snap in "core22" "gnome-42-2204"; do
        cd "/snap/$snap/current" && find . -type f,l -exec rm -f "$CRAFT_PRIME/{}" \;
      done

slots:
  dbus-daemon:
    interface: dbus
    bus: session
    name: org.mozilla.thunderbird
hooks:
  post-refresh:
    plugs: [host-usr-share-hunspell]