# Maintainer: Andreas Grapentin <andreas@grapentin.org>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: André Silva <emulatorman@hyperbola.info>

_pkgbase=iceape
pkgbase=${_pkgbase}-l10n
epoch=1
_pkgver_armv7h=2.49.4
_pkgver_i686=2.49.4
_pkgver_x86_64=2.49.4
_pkgrel_armv7h=1
_pkgrel_i686=1
_pkgrel_x86_64=1
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/$_pkgbase"
license=(MPL GPL)
depends=("$_pkgbase=$epoch:$pkgver")
makedepends=('unzip' 'zip')

pkgname=()
source=(brand.dtd
        brand.properties
        communicator-region.properties
        editor-region.properties
        messenger-region.properties
        navigator-region.properties
        languages.${CARCH}
        sha512sums.${CARCH})

# load languages list and .xpi checksums
source languages.${CARCH}  &> /dev/null # sets $_languages
source sha512sums.${CARCH} &> /dev/null # sets $_sha512sums

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

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

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

# compile checksums
_languages_x86_64_shasum='1cc581a43b69834c94836b4f891cb9e35538d3df0538d449fa3efe8f26b79b96f05c7f210e48fefe5ba96c95cd40a8a3180f8611f31367c4f16930327f9e4585'
_languages_i686_shasum='1cc581a43b69834c94836b4f891cb9e35538d3df0538d449fa3efe8f26b79b96f05c7f210e48fefe5ba96c95cd40a8a3180f8611f31367c4f16930327f9e4585'
_languages_armv7h_shasum='1cc581a43b69834c94836b4f891cb9e35538d3df0538d449fa3efe8f26b79b96f05c7f210e48fefe5ba96c95cd40a8a3180f8611f31367c4f16930327f9e4585'
_sha512sums_x86_64_shasum='947f493bd305ea4993367122cd78df76db9b54f3e7aecb87dcfcb94010cee84585bb18bec7cd63a6f15b8c40dd29f131cfeba92e8945ff57bbcde0d11e3a1629'
_sha512sums_i686_shasum='947f493bd305ea4993367122cd78df76db9b54f3e7aecb87dcfcb94010cee84585bb18bec7cd63a6f15b8c40dd29f131cfeba92e8945ff57bbcde0d11e3a1629'
_sha512sums_armv7h_shasum='947f493bd305ea4993367122cd78df76db9b54f3e7aecb87dcfcb94010cee84585bb18bec7cd63a6f15b8c40dd29f131cfeba92e8945ff57bbcde0d11e3a1629'
sha256sums=('08b88f2ef2b9d780945389628d7fe4940d8270554e76d025449f6908e2c1affad1faa8cfe1e95889c59b7fa4dfbee60f67d68b8ac031b568256bb2f8a9106910' # brand.dtd
            '69460ad8100fba9dfaf6993a8f061717cb3cb39fa13b1d8d040d7f91d1c482c62548ae184c698e23f28abc0ecfec7e7b11f22b55083e65a9b41d3fc7715a6633' # brand.properties
            'fca487a975a58ade57edc35674ce4ebdffabcf5e241ab1ac0eb08dc6d64e26a4990fbc6229d972fa3172e747368b35b5396a4f7d9c2ed1a55fc25c72a75f2842' # communicator-region.properties
            'f6fcb5294acab6ad26942da9466da7ee653c54ead4d8b5835894e35acf87d874e95a2357569574ec645c6ab67aa333210666e9a8462e7a36a7463100e78386a6' # editor-region.properties
            '3b40d16c9871502005b81ed7e35d4d5a6ca1ba17d75d53b0e623274fd6be49071cb16e2a36e35e06485b7d5eebbdfc66e511b975c2fa029f97d7e437fdf57fa9' # messenger-region.properties
            '7542559b4da72739cf5a1f96c2ecbf771a3c6e559c7ac9a50eb67e4dbe5c34ac47ce1684286ae88a80f52b17131ac881fb30be83be001b385184c5e2e2c8683f' # navigator-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 ${_pkgbase^}."

  unzip iceape-l10n-$pkgver-$1.xpi -d $1
  rm -v iceape-l10n-$pkgver-$1.xpi
  install -vDm644 $srcdir/brand.dtd $1/chrome/$1/locale/$1/branding
  install -vDm644 $srcdir/brand.properties $1/chrome/$1/locale/$1/branding
  install -vDm644 $srcdir/navigator-region.properties $1/chrome/$1/locale/$1/navigator-region/region.properties
  install -vDm644 $srcdir/messenger-region.properties $1/chrome/$1/locale/$1/messenger-region/region.properties
  install -vDm644 $srcdir/communicator-region.properties $1/chrome/$1/locale/$1/communicator-region/region.properties
  install -vDm644 $srcdir/editor-region.properties $1/chrome/$1/locale/$1/editor-region/region.properties
  sed -i -e 's/seamonkey/iceape/' $1/install.rdf
  rm -rv $1/chrome/$1/locale/$1/global-platform/{mac,win}

  cd $1
  zip -r langpack-$1@iceape.mozilla.org.xpi .
  mv -v langpack-$1@iceape.mozilla.org.xpi $srcdir
  cd ..
  rm -rv $1

  install -vDm644 langpack-$1@iceape.mozilla.org.xpi \
    "$pkgdir/usr/lib/iceape-$pkgver/extensions/langpack-$1@iceape.mozilla.org.xpi"
}
