makedepends=(rustc autoconf2-13 icu alsa-lib gtk3 zip unzip libevent libwebp nspr nss yasm dbus-glib doxygen libnotify openjdk startup-notification hunspell sqlite wireless-tools xorg-libx11 xorg-libxt xorg-libxrandr desktop-file-utils hicolor-icon-theme shared-mime-info cbindgen pulseaudio node libvpx mesa xorg-libxcomposite xorg-libxdamage)
run=(startup-notification hunspell)

description="Stand-alone mail/news client based on the Mozilla codebase"
url="http://www.mozilla.org/thunderbird"

packager="Great-OS Team"
maintainer="Great-OS Team"

name=thunderbird
version=140.10.1
release=1

source=(https://ftp.mozilla.org/pub/mozilla.org/thunderbird/releases/${version}esr/source/thunderbird-${version}esr.source.tar.xz
        $name.desktop)

build() {

  ARCH=$(uname -m)

  for lang in da de en-US es-ES fi fr it nb-NO nl pt-PT sv-SE tr
  do
    wget --prefer-family=IPv4 http://download.cdn.mozilla.net/pub/$name/releases/${version}esr/linux-$ARCH/xpi/$lang.xpi
  done

  cd $name-$version

  # fix error
  # error: failed to calculate checksum of: /tmp/work/src/thunderbird-140.8.1/comm/third_party/rust/minimal-lexical/.gitmodules
  find -name .cargo-checksum.json | \
    xargs sed -ri 's|"[^"]*.gitmodules":"[^"]*",?||'

  cat > mozconfig << "EOF"
# If you have a multicore machine, all cores will be used by default.

# If you have installed (or will install) wireless-tools, and you wish
# to use geolocation web services, comment out this line
ac_add_options --disable-necko-wifi

# API Keys for geolocation APIs - necko-wifi (above) is required for MLS
# Uncomment the following line if you wish to use Mozilla Location Service
#ac_add_options --with-mozilla-api-keyfile=$PWD/mozilla-key

# Uncomment the following line if you wish to use Google's geolocaton API
# (needed for use with saved maps with Google Maps)
#ac_add_options --with-google-location-service-api-keyfile=$PWD/google-key

# Uncomment the following option if you have not installed PulseAudio and
# want to use alsa instead
#ac_add_options --enable-audio-backends=alsa

# Comment out following options if you have not installed
# recommended dependencies:

#disable system icu to avoid bugs
# see: https://bugzilla.mozilla.org/show_bug.cgi?id=1989158
#ac_add_options --with-system-icu

ac_add_options --with-system-libevent
ac_add_options --with-system-libvpx
ac_add_options --with-system-nspr
ac_add_options --with-system-nss
ac_add_options --with-system-webp

# libdavid (av1 decoder) requires nasm. Uncomment this if nasm
# has not been installed.
#ac_add_options --disable-av1

# You cannot distribute the binary if you do this
ac_add_options --enable-official-branding

# Stripping is now enabled by default.
# Uncomment these lines if you need to run a debugger:
#ac_add_options --disable-strip
#ac_add_options --disable-install-strip

# Disabling debug symbols makes the build much smaller and a little
# faster. Comment this if you need to run a debugger. Note: This is
# required for compilation on i686.
ac_add_options --disable-debug-symbols

# The elf-hack is reported to cause failed installs (after successful builds)
# on some machines. It is supposed to improve startup time and it shrinks
# libxul.so by a few MB - comment this if you know your machine is not affected.
ac_add_options --disable-elf-hack

# The BLFS editors recommend not changing anything below this line:
ac_add_options --prefix=/usr
ac_add_options --enable-application=comm/mail
ac_add_options --disable-crashreporter
ac_add_options --disable-updater
# enabling the tests will use a lot more space and significantly
# increase the build time, for no obvious benefit.
ac_add_options --disable-tests

# The default level of optimization again produces a working build with gcc.
ac_add_options --enable-optimize

ac_add_options --enable-system-ffi
ac_add_options --enable-system-pixman

ac_add_options --with-system-jpeg
ac_add_options --with-system-zlib

# Using sandboxed wasm libraries has been moved to all builds instead
# of only mozilla automation builds. It requires extra llvm packages
# and was reported to seriously slow the build. Disable it.
ac_add_options --without-wasm-sandboxed-libraries

unset MOZ_TELEMETRY_REPORTING

mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/thunderbird-build-dir

EOF

  sed -i 's/icu-i18n/icu-uc &/' js/moz.configure

  #mountpoint /dev/shm >/dev/null || mount -t tmpfs devshm /dev/shm

  export CC=clang CXX=clang++ AR=llvm-ar NM=llvm-nm RANLIB=llvm-ranlib

  export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=none
  export MOZBUILD_STATE_PATH=${PWD}/mozbuild
  
  SHELL=/bin/sh ./mach build

  export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=none
  SHELL=/bin/sh DESTDIR=$PKG ./mach install

  unset MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE
  unset MOZBUILD_STATE_PATH

  for i in 16 22 24 32 48 64 128 256; do
    install -Dm644 comm/mail/branding/thunderbird/default${i}.png \
      "$PKG/usr/share/icons/hicolor/${i}x${i}/apps/$name.png"
  done

  install -Dm644 comm/mail/branding/thunderbird/TB-symbolic.svg \
    "$PKG/usr/share/icons/hicolor/symbolic/apps/thunderbird-symbolic.svg"

  mkdir -pv $PKG/usr/share/applications
  install -Dm644 $SRC/thunderbird.desktop \
    $PKG/usr/share/applications/thunderbird.desktop

  mkdir -pv $PKG/usr/lib/thunderbird/defaults/pref

  cat > $PKG/usr/lib/thunderbird/defaults/pref/vendor.js << "EOF"
// Use LANG environment variable to choose locale
pref("intl.locale.requested", "");
EOF

  for lang in da de en-US es-ES fi fr it nb-NO nl pt-PT sv-SE
  do
    install -Dm644 $SRC/$lang.xpi \
      $PKG/usr/lib/thunderbird/extensions/langpack-$lang@thunderbird.mozilla.org.xpi
  done

  #mountpoint /dev/shm >/dev/null && umount /dev/shm
}
