# Maintainer: jc_gargma <jc_gargma@iserlohn-fortress.net>
# Maintainer (Arch): Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Aqua-sama <aqua@iserlohn-fortress.net>
# Contributor (Arch): Ionut Biru <ibiru@archlinux.org>
# Contributor (Arch): Jakub Schmidtke <sjakub@gmail.com>

# # I maintain this because:
# Arch version uses eme
# Arch version uses git
# Arch version uses python2
# Arch version uses clang
# Arch version requires signed addons
# Arch version doesn't remove system addons
# Arch version enables debug symbols
# Arch version lacks privacy and security by default

_modclang=0
#_modclang=1 # FIXME

_pkgname=firefox
pkgname=${_pkgname}-esr
pkgver=78.6.0
_pkgver=${pkgver}esr
pkgrel=1.2
pkgdesc="Standalone web browser from mozilla.org"
arch=(x86_64 aarch64)
license=(MPL GPL LGPL)
url="https://www.mozilla.org/firefox/"
depends=(gtk3 libxt mime-types dbus-glib ffmpeg ttf-font libpulse alsa-lib
         bzip2 libffi icu libjpeg-turbo libevent libvpx nspr nss pixman libpng sqlite libwebp zlib)
makedepends=(unzip zip diffutils yasm mesa imake inetutils xorg-server-xvfb
             autoconf2.13 rust clang llvm jack gtk2 nodejs cbindgen nasm
             python-setuptools python-psutil python-zstandard lld)
optdepends=('networkmanager: Location detection via available WiFi networks'
            'libnotify: Notification integration'
            'alsa-utils: ALSA support'
            'jack: Jack support'
            'pulseaudio: PulseAudio support'
            'speech-dispatcher: Text-to-Speech'
            'hunspell-en_CA: Spell checking, Canadian English'
            'firefox-preferences: Custom vendor.js with security by default'
            'firefox-preferences-basic: Custom vendor.js with less breaking changes')
options=(!emptydirs !makeflags)
conflicts=(firefox)
provides=(firefox)
source=(
        "https://archive.mozilla.org/pub/$_pkgname/releases/$_pkgver/source/$_pkgname-$_pkgver.source.tar.xz"{,.asc}
        0001-Use-remoting-name-for-GDK-application-names.patch
        rust_1.48.patch.gz
        $pkgname.desktop
        distribution.ini
        mozconfig.in
        mozconfig.in.clang
        mozconfig.in.clang.pgo
        mozconfig.common.override
        arc4_enable-pgo-for-gcc.patch
        arc4_remove-health-reporting.patch
        arc4_remove-system-addons.patch
        )
b2sums=('9d082b6104bffbd0d918dc1adf2a01ac32613d8048faf399177834c4949714a00c1f4383b778a21d6b6684296d8f9bd44fa547ee2507a8631d206e3764d2ceeb'
        'SKIP'
        'e4cf5d0ddd2c7271f1723b5c313f0aa8a7bf2242fb301ceba1ab5a4dca655ca7b9461d0aaebe5eaccf84b2f7a4c4199cbb0c18eab2bdaff273fcb730ec27219e'
        '3eba6b3d45c52cbc708e4abdcc581aad3a6a75ed335a29e19092cb225b88f1b86457ed03853d841615f2fdb2678b49e9b7d2b3a65dff387cd81959e7e5cc4d88'
        '1c3af4cc5199cf7b9f3aad9ceab98b3e8a3dc0d38f50896ed2f32ac648f1e904a1cdda5da559964885989edbfab4c4bf65448e9a0f71d946c77ec0ab5a6aff4c'
        '877eda442eb4730600dccf209a6e961ad3122ab456bbfda4ff041e8493ebaaf96224ac218360931259ff4214482f6bf65b3a3052f977ade1a274f38ffd859535'
        '44fdd3e403376f9602341781e2bc632c03be2d5c44d91b66b674ff1056cc80e78dccbfb5895282e60d508025bea6871d42bfc5a0a80ba2360df165bce91ed50d'
        '471d01a9d0914d8c1b62cd5ebe5c6ee319a28f3bc629b415bb39edaa280383919e09967ecaa3fdeab7bbce12ccba4e0440bf18474f538addc528672cee618e3a'
        'bd967166ec95f134114d26de85559c8e4a0d18a40ea63d1fc045897198274ed6e523bb320632395e9b5277eb122c49e41d989413a3a019f8a868f7ab7743dc0d'
        '1c7ac58af8b817067c7d79ffef8d9100f513d77f4fc1a2bce2684ff8d8d3cbc3d37b31865c7e5d873fd71d4649374bbd25ca08166d8afb25ffbd597759655063'
        '087ea758132905cab848e3a13a13537b42f70ba3a8bfd1ef4e2933d3e874e94038b50ee583b9e7e0c7b7cc7c2cf570f7f613b661f47121b7bf207f433b939f3d'
        '020695637162ce981929f54ea556100bf17e92931179a5849ca8265dfc0016f1b6599d9f77e9f71f192a1a4bb3b2d1eadbf5d5554e6c33bfa3f9388b0a3c7c6e'
        '635a84c64ea9dccf41fbde33b58a72a8123ecd841995ad045bd329e23855f56ffce05670fe2d69a12c4cef7a7399bc6becfc5f40bfd8cea9a4006074672a634d')
validpgpkeys=('14F26682D0916CDD81E37B6D61B7B526D98F0353')  # Mozilla Software Releases

prepare() {
#   mkdir mozbuild
  cd ${_pkgname}-${pkgver}

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1530052
  patch -Np1 -i ../0001-Use-remoting-name-for-GDK-application-names.patch

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1663715
  patch -Np1 -i ../rust_1.48.patch

  if [ "${_modclang}" != "1" ]; then
    # Apply gcc pgo patches
    patch -p0 $srcdir/${_pkgname}-${pkgver}/security/sandbox/linux/moz.build $srcdir/arc4_enable-pgo-for-gcc.patch
  fi

  if [ "${_modclang}" == "1" ]; then
    # Add the custom mozconfig for pgo
    cp $srcdir/mozconfig.in.clang.pgo $srcdir/${_pkgname}-${pkgver}/.mozconfig
  else
    # Add the custom mozconfig
    cp $srcdir/mozconfig.in $srcdir/${_pkgname}-${pkgver}/.mozconfig
  fi

  # MOZ_TELEMETRY_REPORTING only applied correctly when set in this override file
  cp $srcdir/mozconfig.common.override $srcdir/${_pkgname}-${pkgver}/build/mozconfig.common.override


  # Disable various components at the source level
  #sed -i 's|[;]1|;0|' $srcdir/${_pkgname}-${pkgver}/toolkit/components/telemetry/TelemetryStartup.manifest
  sed -i 's/;1/;0/' $srcdir/${_pkgname}-${pkgver}/toolkit/components/telemetry/components.conf

  # Patch out health reporting so that MOZ_DATA_REPORTING=0 can be applied.
  patch -p0 $srcdir/${_pkgname}-${pkgver}/browser/moz.configure $srcdir/arc4_remove-health-reporting.patch

  # Remove @RESPATH@/browser/features/* to prevent system addons from being part of the output package.
  patch -p0 $srcdir/${_pkgname}-${pkgver}/browser/installer/package-manifest.in $srcdir/arc4_remove-system-addons.patch


  # Hide mobile promo
#  echo ".fxaMobilePromo {  display: none !important; }" >> browser/themes/shared/incontentprefs/preferences.inc.css
  # Hide plugindeprecation-notice
  echo "#plugindeprecation-notice {  display: none !important; }" >> toolkit/mozapps/extensions/content/extensions.css
  # Hide warnings on unsigned extensions
  echo ".warning{display: none!important; } .addon[notification=warning]{background-image: none!important;}" >> toolkit/mozapps/extensions/content/extensions.css
}

build() {
  cd ${_pkgname}-${pkgver}

  export MOZ_NOSPAM=1
  export MOZBUILD_STATE_PATH="$srcdir/mozbuild"
  export MACH_USE_SYSTEM_PYTHON=1

  export CFLAGS="$CFLAGS -O3 -fstack-protector-all -fPIC"
  export CXXFLAGS="$CXXFLAGS -O3 -fstack-protector-all -fPIC"

  # LTO needs more open files
  ulimit -n 4096

  if [ "${_modclang}" == "1" ]; then
  # clang does not support -fstack-clash-protection, so remove it
  #export CFLAGS="$(echo $CFLAGS | sed 's/ -fstack-clash-protection//' - )"
  #export CXXFLAGS="$(echo $CXXFLAGS | sed 's/ -fstack-clash-protection//' - )"

  # Do 3-tier PGO
  echo "Building instrumented browser..."
  ./mach build

  echo "Profiling instrumented browser..."
  ./mach package
  LLVM_PROFDATA=llvm-profdata \
    JARLOG_FILE="$PWD/jarlog" \
    xvfb-run -s "-screen 0 1920x1080x24 -nolisten local" \
    ./mach python build/pgo/profileserver.py

  stat -c "Profile data found (%s bytes)" merged.profdata
  test -s merged.profdata

  stat -c "Jar log found (%s bytes)" jarlog
  test -s jarlog

  echo "Removing instrumented browser..."
  ./mach clobber

  echo "Building optimized browser..."
  # Add the custom mozconfig
  cp $srcdir/mozconfig.in.clang $srcdir/${_pkgname}-${pkgver}/.mozconfig
  cat >.mozconfig - <<END
ac_add_options --with-pgo-profile-path=${PWD@Q}/merged.profdata
ac_add_options --with-pgo-jarlog=${PWD@Q}/jarlog
mk_add_options MOZ_OBJDIR=${PWD@Q}/obj
END
  ./mach build
  else
    echo "Building optimized browser..."
    xvfb-run -s "-screen 0 1920x1080x24 -nolisten local" ./mach build
  fi
}

package() {
  cd ${_pkgname}-${pkgver}
  DESTDIR="$pkgdir" ./mach install


  install -Dm644 $srcdir/distribution.ini \
  "$pkgdir/usr/lib/$_pkgname/distribution/distribution.ini"

  # don't duplicate the binary, use a symlink instead
  rm "$pkgdir/usr/bin/firefox"
  ln -s "/usr/lib/$_pkgname/firefox" "$pkgdir/usr/bin/firefox"


  # icons
  local i theme=official
  for i in 16 22 24 32 48 64 128 256; do
    install -Dvm644 browser/branding/$theme/default$i.png \
      "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/$_pkgname.png"
  done
  install -Dvm644 browser/branding/$theme/content/about-logo.png \
    "$pkgdir/usr/share/icons/hicolor/192x192/apps/$_pkgname.png"
  install -Dvm644 browser/branding/$theme/content/about-logo@2x.png \
    "$pkgdir/usr/share/icons/hicolor/384x384/apps/$_pkgname.png"
  install -Dvm644 browser/branding/$theme/content/identity-icons-brand.svg \
    "$pkgdir/usr/share/icons/hicolor/symbolic/apps/$_pkgname-symbolic.svg"

  # install desktop file
  install -Dvm644 ../$pkgname.desktop \
    "$pkgdir/usr/share/applications/$pkgname.desktop"

   # Use system certificates
  local nssckbi="$pkgdir/usr/lib/$pkgname/libnssckbi.so"
  if [[ -e $nssckbi ]]; then
    ln -srfv "$pkgdir/usr/lib/libnssckbi.so" "$nssckbi"
  fi
}