# Maintainer (Arch): Eli Schwartz <eschwartz@archlinux.org>
# Contributor (Arch): Hyacinthe Cartiaux <hyacinthe.cartiaux@free.fr>
# Maintainer: Freemor <freemor@freemor.ca>
# Contributor: Theo von Arx <theova@member.fsf.org>

_pkgname=https-everywhere
pkgname=iceweasel-${_pkgname}
pkgver=2020.5.20
pkgrel=1
pkgdesc="Plugin for iceweasel which ensures you are using https whenever it's possible."
arch=('any')
url="https://www.eff.org/https-everywhere"
license=('GPL3')
groups=('iceweasel-addons')
makedepends=("unzip")
replaces=(firefox-extension-${_pkgname})
source=("${_pkgname}-${pkgver}.xpi::https://www.eff.org/files/https-everywhere-${pkgver}-eff.xpi"
        "${_pkgname}-${pkgver}.xpi.sig::https://www.eff.org/files/https-everywhere-${pkgver}-eff.xpi.sig")
noextract=("${_pkgname}-${pkgver}.xpi")
sha256sums=('86b33a1e76ce369656f7a19ec930a0fb8c6d8828721cd8798c4d4f07931fb4a6'
            'SKIP')
b2sums=('f1ea90cc90e4ea148b396db81968ab4fc5ae8e13f1ba56a0da5ef7ae4e067d65f732db3886a847f0831c3588d7b9ec6b80368f56eb268c1ba44360ee3485e25f'
        'SKIP')
validpgpkeys=('1073E74EB38BD6D19476CBF8EA9DBF9FB761A677'  # William Budington <bill@eff.org>
              'CE340E9D077F1DC0F4FA7B030D16CFA2BA1F7420') # Alexis <alexis@eff.org>

prepare() {
  cd "$srcdir"

  unzip -qqo "${_pkgname}-${pkgver}.xpi" -d "${_pkgname}-${pkgver}"
}

package() {
  depends=("iceweasel")
  cd "${srcdir}"

  if [[ -f ${_pkgname}-${pkgver}/install.rdf ]]; then
    _extension_id="$(sed -n '/.*<em:id>\(.*\)<\/em:id>.*/{s//\1/p;q}' ${_pkgname}-${pkgver}/install.rdf)"
  else
    _extension_id="$(sed -n 's/.*"id": "\(.*\)".*/\1/p' ${_pkgname}-${pkgver}/manifest.json)"
  fi
  _extension_dest="${pkgdir}/usr/lib/iceweasel/browser/extensions/${_extension_id}"
  # Should this extension be unpacked or not?
  if grep -q '<em:unpack>true</em:unpack>' ${_pkgname}-${pkgver}/install.rdf 2>/dev/null; then
    install -dm755 "${_extension_dest}"
    cp -R ${_pkgname}-${pkgver}/* "${_extension_dest}"
    chmod -R ugo+rX "${_extension_dest}"
  else
    install -Dm644 ${_pkgname}-${pkgver}.xpi "${_extension_dest}.xpi"
  fi
}
