# Maintainer (Arch:firefox): Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>
# Contributor (Arch:firefox): Ionut Biru <ibiru@archlinux.org>
# Contributor (Arch:firefox): Jakub Schmidtke <sjakub@gmail.com>

# Contributor (ConnochaetOS:iceweasel): Henry Jensen <hjensen@connochaetos.org>

# Maintainer: Andreas Grapentin <andreas@grapentin.org>
# Maintainer: Luke Shumaker <lukeshu@parabola.nu>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: fauno <fauno@kiwwwi.com.ar>
# Contributor: vando <facundo@esdebian.org>
# Contributor: Figue <ffigue at gmail>
# Contributor: evr <evanroman at gmail>
# Contributor: Muhammad 'MJ' Jassim <UnbreakableMJ@gmail.com>

pkgname=icecat
_pkgver=60.3.0-gnu1
pkgver=${_pkgver//-/_}
pkgrel=1.testing1
pkgdesc="GNU IceCat - a libre standalone web browser based on Mozilla Firefox."
arch=(x86_64 i686 armv7h)
license=(MPL GPL LGPL)
url="http://www.gnu.org/software/gnuzilla/"
depends=(gtk3 gtk2 mozilla-common libxt startup-notification mime-types dbus-glib ffmpeg
         nss hunspell-en_US sqlite ttf-font libpulse libvpx icu)
makedepends=(unzip zip diffutils python2 yasm mesa imake gconf inetutils xorg-server-xvfb
             autoconf2.13 rust clang llvm jack mozilla-searchplugins)
optdepends=('networkmanager: Location detection via available WiFi networks'
            'libnotify: Notification integration'
            'pulseaudio: Audio support'
            'speech-dispatcher: Text-to-Speech')
options=(!emptydirs !makeflags !strip)
source=(http://ftp.gnu.org/gnu/gnuzilla/${_pkgver%-*}/$pkgname-$_pkgver.tar.bz2{,.sig}
        $pkgname.desktop
        gnu_headshadow.png
        libre.patch)
sha256sums=('6145327092b4b195a4f63d0e86f4857eeba5607ffeb69b6f3bceb36e89a19645'
            'SKIP'
            '2f9d117c1524312fb45f3e953cb0de02c525fd385a9b2b6d47308c8aaab0aedd'
            '93e3001ce152e1d142619e215a9ef07dd429943b99d21726c25da9ceb31e31cd'
            'd32f97e812c536efd9bdc7043efd6f9472a1edd9f814b453805c7c10ffea25a6')
validpgpkeys=('A57369A8BABC2542B5A0368C3C76EED7D7E04784') # Ruben Rodriguez

prepare() {
  cd "$srcdir/$pkgname-${pkgver%_*}"

  # Patch to move files directly to /usr/lib/icecat. No more symlinks.
  sed -e 's;$(libdir)/$(MOZ_APP_NAME)-$(MOZ_APP_VERSION);$(libdir)/$(MOZ_APP_NAME);g' -i config/baseconfig.mk
  sed -e 's;$(libdir)/$(MOZ_APP_NAME)-devel-$(MOZ_APP_VERSION);$(libdir)/$(MOZ_APP_NAME)-devel;g' -i config/baseconfig.mk

  cat >.mozconfig <<END
ac_add_options --enable-application=browser

ac_add_options --prefix=/usr
ac_add_options --enable-release
ac_add_options --enable-linker=gold
ac_add_options --enable-hardening
ac_add_options --enable-optimize
ac_add_options --enable-rust-simd

# Branding
ac_add_options --enable-official-branding
ac_add_options --with-distribution-id=org.gnu

# System libraries
ac_add_options --with-system-zlib
ac_add_options --with-system-bz2
ac_add_options --with-system-icu
ac_add_options --with-system-jpeg
ac_add_options --with-system-libvpx
ac_add_options --with-system-nspr
ac_add_options --with-system-nss
ac_add_options --enable-system-sqlite
ac_add_options --enable-system-ffi

# Features
ac_add_options --enable-alsa
ac_add_options --enable-startup-notification
ac_add_options --disable-crashreporter
ac_add_options --disable-updater
ac_add_options --disable-debug-symbols
ac_add_options --disable-tests
ac_add_options --disable-eme
ac_add_options --disable-gconf

ac_add_options --with-app-basename=icecat
ac_add_options --with-app-name=icecat
ac_add_options --enable-update-channel=release
END

  if [[ "$CARCH" == arm* ]]; then
    sed -i \
        -e '/enable-gold/s/^/#/' \
        -e '/enable-pie/s/^/#/' \
        -e '/enable-optimize/d' \
      .mozconfig

    cat >> .mozconfig << END
ac_add_options --disable-webrtc
ac_add_options --disable-elf-hack
ac_add_options --enable-optimize="-g -O2 -fno-schedule-insns"
END
  fi

  # Put gnu_headshadow.png in the source code
  # install -m644 "$srcdir/gnu_headshadow.png" \
  #   browser/base/content/abouthome

  # Patch and remove anything that's left
  # patch -Np1 -i "$srcdir/libre.patch"
  # rm -v browser/base/content/abouthome/snippet*.png || true
  # sed -i '\|abouthome/snippet|d
  #        ' browser/base/jar.mn

  # Load our searchplugins
  # rm -rf -- browser/locales/searchplugins
  # cp -aT -- /usr/lib/mozilla/searchplugins browser/locales/searchplugins
}

build() {
  cd "$srcdir/$pkgname-${pkgver%_*}"

  ./mach build
  ./mach buildsymbols
}

package() {
  local _icu_ver
  _icu_ver=$(pacman -S --print-format='%v' icu)
  depends+=("icu>=${_icu_ver}" "icu<$((${_icu_ver%%.*} + 1))")

  cd "$srcdir/$pkgname-${pkgver%_*}"
  DESTDIR="$pkgdir" ./mach install

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

// Use system-provided dictionaries
pref("spellchecker.dictionary_path", "/usr/share/hunspell");

// 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);

// Disable "alt" as a shortcut key to open full menu bar. Conflicts with "alt" as a modifier
pref("ui.key.menuAccessKeyFocuses", false);

// Make sure that Firefox Social stuff are empty
pref("social.whitelist", "");
pref("social.directories", "");

// Disable the GeoLocation API for content
pref("geo.enabled", false);

// Make sure that the request URL of the GeoLocation backend is empty
pref("geo.wifi.uri", "");

// Disable the least secure encryption protocols
pref("security.ssl3.ecdhe_ecdsa_rc4_128_sha", false);
pref("security.ssl3.ecdhe_rsa_rc4_128_sha", false);
pref("security.ssl3.rsa_rc4_128_md5", false);
pref("security.ssl3.rsa_rc4_128_sha", false);
END

  _distini="$pkgdir/usr/lib/$pkgname/distribution/distribution.ini"
  install -Dm644 /dev/stdin "$_distini" <<END
[Global]
id=parabola
version=1.0
about=Icecat for Parabola GNU/Linux-libre

[Preferences]
app.distributor=parabola
app.distributor.channel=$pkgname
app.partner.parabola=parabola
END

  install -m755 -d ${pkgdir}/usr/share/applications
  install -m755 -d ${pkgdir}/usr/share/pixmaps

  for i in 16 22 24 32 48 256; do
    install -Dm644 "browser/branding/official/default$i.png" \
      "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/$pkgname.png"
  done
  install -Dm644 "browser/branding/official/content/icon64.png" \
    "$pkgdir/usr/share/icons/hicolor/64x64/apps/$pkgname.png"
  install -Dm644 "browser/branding/official/mozicon128.png" \
    "$pkgdir/usr/share/icons/hicolor/128x128/apps/$pkgname.png"
  install -Dm644 "browser/branding/official/content/about-logo.png" \
    "$pkgdir/usr/share/icons/hicolor/192x192/apps/$pkgname.png"
  install -Dm644 "browser/branding/official/content/about-logo@2x.png" \
    "$pkgdir/usr/share/icons/hicolor/384x384/apps/$pkgname.png"

  install -Dm644 ../$pkgname.desktop \
    "$pkgdir/usr/share/applications/$pkgname.desktop"

  # Install a wrapper to avoid confusion about binary path
  install -Dm755 /dev/stdin "$pkgdir/usr/bin/$pkgname" <<END
#!/bin/sh
exec /usr/lib/$pkgname/$pkgname "\$@"
END

  # Replace duplicate binary with wrapper
  # https://bugzilla.mozilla.org/show_bug.cgi?id=658850
  ln -srf "$pkgdir/usr/bin/$pkgname" \
    "$pkgdir/usr/lib/$pkgname/$pkgname-bin"
}
