# Maintainer : André Silva <emulatorman@parabola.nu>
# Contributor : Márcio Silva <coadde@parabola.nu>

# We're getting this from Debian Sid
_debname=icedove
_debver=31.0
_debrel=deb3
_debrepo=http://ftp.debian.org/debian/pool/main/
debfile() { echo $@|sed -r 's@(.).*@\1/&/&@'; }

pkgname=${_debname}
epoch=1
pkgver=${_debver}.${_debrel}
pkgrel=2.nonprism1
pkgdesc="A libre version of Debian Icedove, the standalone mail/news reader based on Mozilla Thunderbird, without support for unsafe and dangerous for privacy protocols"
arch=('i686' 'x86_64' 'mips64el')
license=('MPL' 'GPL' 'LGPL')
url="http://packages.debian.org/sid/${pkgname}"
depends=('alsa-lib' 'dbus-glib' 'desktop-file-utils' 'gtk2' 'hicolor-icon-theme' 'hunspell' 'libevent' 'libvpx' 'libxt' 'mime-types' 'mozilla-common' 'mozilla-searchplugins' 'nss' 'sqlite' 'startup-notification')
makedepends=('unzip' 'zip' 'pkg-config' 'python2' 'wireless_tools' 'yasm' 'mesa' 'libpulse' 'autoconf2.13' 'quilt' 'jquery-ui')
optdepends=('libcanberra: for sound support')
replaces=('thunderbird' "${pkgname}-libre" "${pkgname}-libre-nonprism")
conflicts=('thunderbird' "${pkgname}-libre" "${pkgname}-libre-nonprism")
provides=('thunderbird')
install=${pkgname}.install
source=("${_debrepo}/`debfile ${_debname}`_${_debver}.orig.tar.xz"
        "${_debrepo}/`debfile ${_debname}`_${_debver}-${_debrel#deb}.debian.tar.xz"
        mozconfig
        vendor.js
        ${pkgname}.desktop)
options=(!emptydirs)
md5sums=('1ea1f4340b1ab29a29aed74f4d6197bd'
         'f97baf462902b155b370f010c112ad82'
         '5f2297712c3dab7b15d7d5c8a6fa22d2'
         'b960d6e999cf49e8875743e35a00ed41'
         'e785e0c267f4435ae1a9aa0b03bcacfb')

prepare() {
  export DEBIAN_BUILD="comm-esr31"

  export QUILT_PATCHES=debian/patches
  export QUILT_REFRESH_ARGS='-p ab --no-timestamps --no-index'
  export QUILT_DIFF_ARGS='--no-timestamps'

  mv debian "${srcdir}/${DEBIAN_BUILD}"
  cd "${srcdir}/${DEBIAN_BUILD}"

  mv debian/${pkgname}-branding "${srcdir}/${DEBIAN_BUILD}/mail/branding/${pkgname}"

  cp -a debian/app-icons/${pkgname}big.svg debian/app-icons/${pkgname}_icon.svg
  for i in 16x16 22x22 24x24 32x32 48x48 256x256; do
      install -Dm644 "debian/app-icons/${pkgname}${i/x*/}.png" "${srcdir}/${DEBIAN_BUILD}/mail/branding/${pkgname}/mailicon${i/x*/}.png"
  done
  for i in 48x48 64x64; do
      install -Dm644 "debian/app-icons/${pkgname}${i/x*/}.png" "${srcdir}/${DEBIAN_BUILD}/mail/branding/${pkgname}/content/icon${i/x*/}.png"
  done

  # Fix orthographic issue ("Icdove" to "Icedove") in MOZ_APP_BASENAME line for confvars.sh.
  sed -i 's|Icdove|Icedove|' debian/patches/debian-hacks/Icedove-branding.patch

  quilt push -av

  # Fix paths on makefile
  sed -i 's|topsrcdir = [.][.]/[.][.]/[.][.]/|topsrcdir = @top_srcdir@|;
          s|include $(topsrcdir)/config/autoconf.mk|include $(DEPTH)/config/autoconf.mk|;
          s|include $(DEPTH)/config/rules.mk|include $(topsrcdir)/config/rules.mk|;
         ' mail/branding/icedove/Makefile.in

  sed -i 's|topsrcdir      = [.][.]/[.][.]/[.][.]/[.][.]|topsrcdir      = @top_srcdir@|;
          s|include $(topsrcdir)/config/autoconf.mk|include $(DEPTH)/config/autoconf.mk|;
          s|include $(DEPTH)/config/rules.mk|include $(topsrcdir)/config/rules.mk|;
         ' mail/branding/icedove/locales/Makefile.in

  # Fix package-manifest.in
  sed -i '\|; Phishing Protection| s|$|\n#ifdef MOZ_SAFE_BROWSING|;
          \|@BINPATH@/components/url-classifier[.]xpt| s|$|\n#endif|
         ' mail/installer/package-manifest.in

  # Fix branding
  sed -i 's|Icedove Mail/News|Icedove|' mail/branding/icedove/locales/en-US/brand.{dtd,properties}

  # Replace common URLs
  sed -i '\|extensions[.]getAddons[.]get[.]url| s|https://services[.]addons[.]mozilla[.]org.\+["][)][;]|http://directory.fsf.org/wiki/Icedove");|g;
          \|extensions[.]getAddons[.]search[.]browseURL| s|https://addons[.]mozilla[.]org.\+["][)][;]|http://directory.fsf.org/wiki/Icedove");|g;
          \|extensions[.]getAddons[.]search[.]url| s|https://services[.]addons[.]mozilla[.]org.\+["][)][;]|http://directory.fsf.org/wiki/Icedove");|g;
          \|extensions[.]webservice[.]discoverURL| s|https://services[.]addons[.]mozilla[.]org.\+["][)][;]|http://directory.fsf.org/wiki/Icedove");|g;
         ' mail/app/profile/all-thunderbird.js

  # Remove support for unsafe and dangerous for privacy protocols
  sed -i '\|facebook|d
          \|gtalk|d
          \|twitter|d
         ' chat/moz.build
  sed -i '\|facebook[.]js|d
          \|facebook[.]manifest|d
          \|gtalk[.]js|d
          \|gtalk[.]manifest|d
          \|twitter[.]js|d
          \|twitter[.]manifest|d
         ' mail/installer/package-manifest.in
  rm -r chat/protocols/{facebook,gtalk,twitter}

  cp "${srcdir}/mozconfig" .mozconfig

  # configure script misdetects the preprocessor without an optimization level
  # https://bugs.archlinux.org/task/34644
  sed -i '/ac_cpp=/s/$CPPFLAGS/& -O2/' mozilla/configure

  # Add symlinks to use jquery files built for us
  ln -s /usr/share/javascript/jquery/jquery.min.js mail/jquery
  ln -s /usr/share/javascript/jquery-ui/jquery-ui.min.js mail/jquery
}

build() {
  export DEBIAN_BUILD="comm-esr31"

  cd "${srcdir}/${DEBIAN_BUILD}"

  export LDFLAGS="${LDFLAGS} -Wl,-rpath,/usr/lib/${pkgname}"
  export PYTHON="/usr/bin/python2"

  make -j1 -f client.mk build MOZ_MAKE_FLAGS="${MAKEFLAGS}"
}

package() {
  export DEBIAN_BUILD="comm-esr31"

  cd "${srcdir}/${DEBIAN_BUILD}"

  make -j1 -f client.mk DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/vendor.js" "${pkgdir}/usr/lib/${pkgname}/defaults/preferences/vendor.js"

  for i in 16x16 22x22 24x24 32x32 48x48 64x64 128x128 256x256; do
      install -Dm644 "debian/app-icons/${pkgname}${i/x*/}.png" "${pkgdir}/usr/share/icons/hicolor/$i/apps/${pkgname}.png"
  done
  install -Dm644 "debian/app-icons/${pkgname}_icon.svg" "${pkgdir}/usr/share/icons/hicolor/scalable/apps/${pkgname}.svg"
  
  install -Dm644 "${srcdir}/${pkgname}.desktop" \
      "${pkgdir}/usr/share/applications/${pkgname}.desktop"
  
  rm -rf "${pkgdir}"/usr/lib/${pkgname}/{dictionaries,hyphenation,searchplugins}
  ln -sf /usr/share/hunspell "${pkgdir}/usr/lib/${pkgname}/dictionaries"
  ln -sf /usr/share/hyphen "${pkgdir}/usr/lib/${pkgname}/hyphenation"
  ln -sf /usr/lib/mozilla/searchplugins "${pkgdir}/usr/lib/${pkgname}/searchplugins"

  # We don't want the development stuff
  rm -r "${pkgdir}"/usr/{include,lib/${pkgname}-devel,share/idl}
}