makedepends=(startup-notification node cbindgen icu libvpx libwebp nspr nss libevent rustc dbus-glib autoconf2-13 pulseaudio libnotify yasm sqlite alsa-lib gtk3 zip unzip gstreamer gstreamer-plugins-base gstreamer-plugins-good gstreamer-plugins-libav shared-mime-info cmake xorg-libxcomposite xorg-libxdamage)
run=(shared-mime-info icu libevent pulseaudio sqlite libvpx nss alsa-lib gtk3 zip unzip gstreamer gstreamer-plugins-base gstreamer-plugins-good gstreamer-plugins-libav)

description="Web Browser from Mozilla - ESR (extended support release)"
url="http://www.mozilla.com/firefox/"

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

name=firefox
version=153.1.0
release=2

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

build() {

  ARCH=`uname -m`

  for lang in ar da de es-ES fi fr it nb-NO nl pt-PT ru sv-SE tr ja zh-CN hi-IN
  do 
    wget --prefer-family=IPv4 --quiet http://download.cdn.mozilla.net/pub/mozilla.org/firefox/releases/${version}esr/linux-$ARCH/xpi/$lang.xpi
  done

  cd $name-$version

  sed -i '/so.61/i \  "libavcodec.so.62",' dom/media/platforms/ffmpeg/FFmpegRuntimeLinker.cpp

  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:
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=browser
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@/firefox-build-dir
EOF

  sed -i '/VIRAMA = 47/a CLASS_CHARACTER,' intl/lwbrk/LineBreaker.cpp
  sed -i 's/icu-i18n/icu-uc &/' js/moz.configure

  ##avoid undefined references to `wl_proxy_marshal_flags' errors
  #export LDFLAGS="${LDFLAGS} -lwayland-client"

  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
  #export MACH_USE_SYSTEM_PYTHON=1

  SHELL=/bin/sh ./mach build
  SHELL=/bin/sh ./mach buildsymbols

  mkdir -pv $PKG/usr/{bin,lib{,/mozilla/plugins}}

  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

  ln -svf ../mozilla/plugins $PKG/usr/lib/firefox/browser/

  # Launcher 
  mkdir -pv $PKG/usr/share/applications
  install -Dm644 $SRC/firefox.desktop $PKG/usr/share/applications/firefox.desktop

  mkdir -pv $PKG/usr/share/pixmaps
  ln -sf /usr/lib/firefox-$version/browser/icons/mozicon128.png $PKG/usr/share/pixmaps/firefox.png

  for i in 16 22 24 32 48 256
  do
    mkdir -p $PKG/usr/share/icons/hicolor/${i}x${i}/apps
    cp browser/branding/official/default${i}.png \
      $PKG/usr/share/icons/hicolor/${i}x${i}/apps/firefox.png
  done

  for lang in ar da de tr es-ES fi fr it nb-NO nl pt-PT ru sv-SE tr ja zh-CN hi-IN
  do 
    install -Dm644 $SRC/$lang.xpi \
      $PKG/usr/lib/firefox/browser/extensions/langpack-$lang@firefox.mozilla.org.xpi
  done

  install -Dm644 /dev/stdin $PKG/usr/lib/firefox/browser/defaults/preferences/vendor.js <<END
// Use LANG environment variable to choose locale
pref("intl.locale.requested", "");

// Disable default browser checking.
pref("browser.shell.checkDefaultBrowser", false);

// Don't disable our bundled extensions in the application directory
pref("extensions.autoDisableScopes", 11);
pref("extensions.shownSelectionUI", true);
END

}
