# Maintainer (arch): Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>
# Contributor: Thomas Baechler <thomas@archlinux.org>
# Contributor: Jaroslaw Swierczynski <swiergot@juvepoland.com>
# Contributor: Michal Hybner <dta081@gmail.com>
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Maintainer: Andreas Grapentin <andreas@grapentin.org>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Luke Shumaker <lukeshu@parabola.nu>
# Contributor: fauno <fauno@kiwwwi.com.ar>
# Contributor: Figue <ffigue@gmail.com>
# Contributor: bill-auger <bill-auger@programmer.net>


# parabola changes and rationale:
#  - rebranded to iceweasel
#  - added replaces, conflicts: firefox-i18n-*


_pkgbase=iceweasel
pkgbase=${_pkgbase}-l10n
epoch=1
_pkgver_armv7h=75.0
_pkgver_i686=87.0
_pkgver_x86_64=87.0
_pkgrel_armv7h=1.parabola1
_pkgrel_i686=1.parabola1
_pkgrel_x86_64=1.parabola1
eval "pkgver=\$_pkgver_${CARCH}" # this is actually an 'any' package
eval "pkgrel=\$_pkgrel_${CARCH}" # but our different arches do not always roll at the same speed
pkgdesc="Language pack for ${_pkgbase^}"
arch=('armv7h' 'i686' 'x86_64')
url=https://wiki.parabola.nu/iceweasel
license=(MPL GPL LGPL)
depends=(iceweasel=${epoch}:${pkgver})
makedepends=('unzip' 'zip')

pkgname=()
source=(brand.dtd
        brand.properties
        region.properties
        languages.${CARCH}
        sha512sums.${CARCH})

# load languages list and .xpi checksums
source languages.${CARCH}  # sets $_languages
source sha512sums.${CARCH} # sets $_sha512sums

# compile package names and .xpi sources
_src_url=https://ftp.mozilla.org/pub/firefox/releases/${pkgver}/linux-x86_64/xpi
for _lang in "${_languages[@]}"; do
  _locale=${_lang%% *}
  _pkgname=${pkgbase}-${_locale,,}

  pkgname+=($_pkgname)
  source+=(${pkgbase}-${pkgver}-${_locale}.xpi::${_src_url}/${_locale}.xpi)

  eval "package_${_pkgname}() {
    _package ${_lang}
  }"
done

# compile checksums
_languages_armv7h_shasum='c259b9886024281a92fa8c834c673cf0dd34480a1ba227209872c5e923841d7f343d45885e245fe33b8ca3d24c7f2b527e6c49d6c665133781825076ee4f2cf9'
_languages_i686_shasum="6541960f06b734e954c65b5c5c3b0c22aa263f5912a49f798450594cc7531a6769d3655bcda4d9a49e483c01e40517e62fe6ff021db60c3a139b372c71c7d1e5"
_languages_x86_64_shasum="6541960f06b734e954c65b5c5c3b0c22aa263f5912a49f798450594cc7531a6769d3655bcda4d9a49e483c01e40517e62fe6ff021db60c3a139b372c71c7d1e5"
_sha512sums_armv7h_shasum='ba69d1ac848440397617011bf94fdffd83bdda39c21bcb6ddfcf621e76db380bf5ec0e2afe535e226efd450506dd90594f47ab12e84705efc09c3bf7cca31359'
_sha512sums_i686_shasum="304a53ba47e2456a166cbc5d946533d5fced874bb4739c69e875c867e0be6214dfb5fd3e938b402cee2162d4d15feb9891441af71a017e8d1b1f4ca2fd0bd6c2"
_sha512sums_x86_64_shasum="304a53ba47e2456a166cbc5d946533d5fced874bb4739c69e875c867e0be6214dfb5fd3e938b402cee2162d4d15feb9891441af71a017e8d1b1f4ca2fd0bd6c2"
sha512sums=('bfe82e11a3a1b8006b58a54897f297bd17191ca2e7b08f95d66ec6bfe356caf9eb4d96b3dde5f36db0331d012bde2f03e7087aad1e087800b404f82fa9d786ad' # brand.dtd
            'a1a7e44f3358b131157d2acec243d35ef234e42a6522edfa5b2be9b497a65b2227c64b7fb989c0780c55333c0075f8f2198d99cde8f602bb3f9b8619dc57ab2c' # brand.properties
            '31d33e90be1b5c0e76a0c374684f9a7b4076f7bcca8b76309048b2e3fc7f27c0bc8a2a9f9e66bd8c51fe8780a52a1011c540b071dc82f78d72a1d5ad29fee4f9' # region.properties
            $(eval "echo \$_languages_${CARCH}_shasum")                                                                                        # languages.${CARCH}
            $(eval "echo \$_sha512sums_${CARCH}_shasum")                                                                                       # sha512sums.${CARCH}
            "${_sha512sums[@]}")

# Don't extract anything
noextract=(${source[@]%%::*})

_package() {
  pkgdesc="$2 for Iceweasel"
  replaces=(firefox-i18n-${1,,})
  conflicts=(firefox-i18n-${1,,})

  # explode for branding
  unzip $pkgbase-$pkgver-$1.xpi -d $1
  rm -v $pkgbase-$pkgver-$1.xpi

  # apply parabola branding
  sed -i 's|Mozilla Firefox|Parabola GNU/Linux-libre Iceweasel|g' $(grep -rlI 'Mozilla Firefox' $1)
  sed -i 's|Firefox|Iceweasel|g' $(grep -rlI 'Firefox' $1)
  install -vDm644 "$srcdir"/brand.dtd $1/browser/chrome/$1/locale/branding
  install -vDm644 "$srcdir"/brand.properties $1/browser/chrome/$1/locale/branding
  install -vDm644 "$srcdir"/region.properties $1/browser/chrome/$1/locale/browser-region
  if [ -f $1/chrome/$1/locale/$1/global/aboutRights.dtd ]; then
    sed -i 's|Parabola GNU/Linux-libre Iceweasel|Mozilla Firefox|' $1/chrome/$1/locale/$1/global/aboutRights.dtd
    sed -i 's|Iceweasel|Firefox|' $1/chrome/$1/locale/$1/global/aboutRights.dtd
  fi
  rm -rv $1/chrome/$1/locale/$1/global-platform/{mac,win}

  # re-pack
  cd $1
  zip -r "$srcdir"/$pkgbase-$pkgver-$1.xpi .
  cd ..
  rm -rv $1

  install -Dm644 $pkgbase-$pkgver-$1.xpi \
    "$pkgdir/usr/lib/iceweasel/browser/extensions/langpack-$1@firefox.mozilla.org.xpi"
}
