# Maintainer (arch): Evangelos Foutras <evangelos@foutrelis.com>
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: Alexander Fehr <pizzapunk gmail com>
# Contributor: Lucien Immink <l.immink@student.fnt.hvu.nl>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: bill-auger <bill-auger@programmer.net>


# parabola changes and rationale:
# - remove modules for and artwork non-privacy-respecting services
# - generate and install doxygen API docs (upstream no longer publishes them)


pkgname=('pidgin' 'libpurple' 'finch')
pkgver=2.14.4
# _libnsl_ver=3 # WIP - pin to coupled dependency versions
pkgrel=1
pkgrel+=.nonprism3
arch=('x86_64')
arch+=('armv7h' 'i686')
url="https://pidgin.im/"
license=('GPL')
makedepends=('startup-notification' 'gtkspell' 'libxss' 'nss' 'libsasl' 'libsm'
             'libidn' 'libgadu' 'python' 'hicolor-icon-theme' 'farstream' 'tk'
             'libnsl' 'avahi' 'ca-certificates' 'intltool' 'libnm' 'dbus-glib'
             'libgnt' 'libxcrypt')
makedepends=(${makedepends[*]/libgadu})
makedepends+=(doxygen)
# WIP - pin to coupled dependency versions
# depends=( ${depends[*]/libnsl/libnsl>=${_libnsl_ver}} ) # pin to coupled dependency versions
# depends+=( ${depends[*]/libnsl/libnsl.so} ) # pin to coupled dependency versions
depends+=( libnsl.so ) # WIP - pin to coupled dependency versions
options=('!emptydirs')
source=(https://downloads.sourceforge.net/project/pidgin/Pidgin/$pkgver/$pkgname-$pkgver.tar.bz2{,.asc})
source+=(remove-services.patch)
sha256sums=('67c1a0104b0624a50100c6d73cd5504d53c9d221922b1b28fa86020531a9904e'
            'SKIP')
sha256sums+=('49b54d48ada9008b88c16af0cb059f307a2e90ad81a2d0f9f727cf573784962e')
validpgpkeys=('40DE1DC7288FE3F50AB938C548F66AFFD9BDB729') # Gary Kramlich <grim@reaperworld.com>


prepare() {
  cd $pkgbase-$pkgver

  # FIXME: option 'subdir-objects' is disabled
  autoreconf -fiv

  echo "applying remove-services.patch"
  patch -Np1 -i "${srcdir}"/remove-services.patch

  # replace 'Linux' with 'GNU/Linux' in doc strings
  sed -i 's|on Linux|on GNU/Linux|' doc/pidgin.1.in
  sed -i 's|on Linux|on GNU/Linux|' libpurple/valgrind.h
  sed -i 's|On Linux|On GNU/Linux|' libpurple/connection.h

  # delete icons for removed services
  # ASSERT: these correspond to the remove-services.patch changes to pidgin/pixmaps/Makefile.am
  #         and mutially exclusive to those in 'available_protocols' in build()
  local remove_imgs_rx='aim\.|facebook\.|gadu-gadu\.|google-talk\.|icq\.|meanwhile\.|novell\.'
  local remove_imgs=$(find pidgin/pixmaps/protocols/ -name '*.png' -or -name '*.svg' | \
                      grep -E "${remove_imgs_rx}"                                      )
  rm ${remove_imgs}
  rm pidgin/pixmaps/Makefile.in ; automake ;

  # delete references to removed services
  local desktop_file=pidgin/data/pidgin.desktop.in
  rm ${desktop_file}
  sed -i 's|^\(_Comment=\).*|\1Chat over IRC, Jabber/XMPP, and more|' ${desktop_file}.in
}

build() {
  cd $pkgbase-$pkgver

  local available_protocols='bonjour,irc,jabber,silc,simple,zephyr'
  ./configure \
    --with-dynamic-prpls=${available_protocols} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --disable-schemas-install \
    --disable-gevolution \
    --disable-meanwhile \
    --disable-gnutls \
    --enable-cyrus-sasl \
    --enable-nm \
    --with-system-ssl-certs=/etc/ssl/certs
  make
}

package_pidgin(){
  pkgdesc="Multi-protocol instant messaging client"
  pkgdesc+=", without support for non-privacy-respecting services"
  depends=('libpurple' 'startup-notification' 'gtkspell' 'libxss' 'libsm'
           'gst-plugins-base' 'gst-plugins-good' 'hicolor-icon-theme')
  optdepends=('aspell: for spelling correction')

  cd $pkgbase-$pkgver

  # For linking
  make -C libpurple DESTDIR="$pkgdir" install-libLTLIBRARIES

  make -C pidgin DESTDIR="$pkgdir" install
  make -C doc DESTDIR="$pkgdir" install

  # Remove files that are packaged in libpurle
  make -C libpurple DESTDIR="$pkgdir" uninstall-libLTLIBRARIES

  # Remove files that are packaged in finch
  rm "$pkgdir/usr/share/man/man1/finch.1"

  # generate doxygen API docs
  make docs

  # https://bugs.archlinux.org/task/53770
  # https://bugs.archlinux.org/task/69026
  find "$pkgdir/usr/lib/perl5" -name perllocal.pod -delete
}

package_libpurple(){
  pkgdesc="IM library extracted from Pidgin"
  pkgdesc+=", without support for non-privacy-respecting services"
  depends=('farstream' 'libsasl' 'libidn' 'libnsl' 'libgadu' 'dbus-glib' 'nss'
           'libnm' 'libxcrypt')
  depends=(${depends[*]/libgadu})
  optdepends=('avahi: Bonjour protocol support'
              'ca-certificates: SSL CA certificates'
              'python-dbus: for purple-remote and purple-url-handler'
              'tk: Tcl/Tk scripting support')

  cd $pkgbase-$pkgver

  for _dir in libpurple share/sounds share/ca-certs m4macros po; do
    make -C "$_dir" DESTDIR="$pkgdir" install
  done

  # install doxygen API docs
  install -Dm644 doc/html/search/* -t "$pkgdir"/usr/share/gtk-doc/html/pidgin/search/
  rm -rf doc/html/search/
  install -Dm644 doc/html/* -t "$pkgdir"/usr/share/gtk-doc/html/pidgin/

  # https://bugs.archlinux.org/task/53770
  # https://bugs.archlinux.org/task/69026
  find "$pkgdir/usr/lib/perl5" -name perllocal.pod -delete

  # FIXME: redmine #3465 - error while loading shared libraries: libnsl.so.2:
  ln -s libnsl.so "$pkgdir/usr/lib/libnsl.so.2"
}

package_finch(){
  pkgdesc="A ncurses-based messaging client"
  pkgdesc+=", without support for non-privacy-respecting services"
  depends=('libpurple' 'libgnt' 'libx11')

  cd $pkgbase-$pkgver

  # For linking
  make -C libpurple DESTDIR="$pkgdir" install-libLTLIBRARIES

  make -C finch DESTDIR="$pkgdir" install
  make -C doc DESTDIR="$pkgdir" install

  # Remove files that are packaged in libpurle
  make -C libpurple DESTDIR="$pkgdir" uninstall-libLTLIBRARIES

  rm "$pkgdir"/usr/share/man/man1/pidgin.1
}
