# Maintainer: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>

_realname='pidgin'
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.11.0
pkgrel=2
pkgdesc='Multi-protocol instant messaging client (mingw-w64)'
url='https://pidgin.im'
license=(GPL2 partial:'GPL2+') # GPL2+, but Novell and SILC are GPL2-only
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')

options=(!libtool strip staticlibs)
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.bz2"
        001-autotools-and-fhs.patch
        002-build-fixes.patch
        003-build-fixes.patch)
sha256sums=('f72613440586da3bdba6d58e718dce1b2c310adf8946de66d8077823e57b3333'
            'a5341d2afa5ba30f5ba1ec0092a67671ed18055d01ba9febef5983a246b58841'
            '2e6cdf921bbf09e0952d26cca2e1f1d502d72714419c723ca1ebacfbbbf6fa24'
            'aaf43a3b068632752aec62f67f1b285f66166a48e9195931ca4fa10aac61b748')
provides=(${MINGW_PACKAGE_PREFIX}-libpurple)
depends=(${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme
         ${MINGW_PACKAGE_PREFIX}-ca-certificates
         #${MINGW_PACKAGE_PREFIX}-cyrus-sasl
         #${MINGW_PACKAGE_PREFIX}-dbus
         ${MINGW_PACKAGE_PREFIX}-farstream
         ${MINGW_PACKAGE_PREFIX}-freetype
         ${MINGW_PACKAGE_PREFIX}-fontconfig
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-gnutls
         ${MINGW_PACKAGE_PREFIX}-gsasl
         ${MINGW_PACKAGE_PREFIX}-gst-plugins-base
         ${MINGW_PACKAGE_PREFIX}-gst-plugins-good
         ${MINGW_PACKAGE_PREFIX}-gtk2
         ${MINGW_PACKAGE_PREFIX}-gtkspell
         ${MINGW_PACKAGE_PREFIX}-libgadu
         ${MINGW_PACKAGE_PREFIX}-libidn
         #${MINGW_PACKAGE_PREFIX}-libxml2
         ${MINGW_PACKAGE_PREFIX}-meanwhile
         #${MINGW_PACKAGE_PREFIX}-nspr
         ${MINGW_PACKAGE_PREFIX}-nss
         ${MINGW_PACKAGE_PREFIX}-ncurses
         ${MINGW_PACKAGE_PREFIX}-silc-toolkit
         ${MINGW_PACKAGE_PREFIX}-zlib)
makedepends=(gtk-doc rsync
             #${MINGW_PACKAGE_PREFIX}-dbus-glib
             ${MINGW_PACKAGE_PREFIX}-doxygen
             ${MINGW_PACKAGE_PREFIX}-gcc
             ${MINGW_PACKAGE_PREFIX}-pkg-config
             ${MINGW_PACKAGE_PREFIX}-python2)

prepare() {
    cd "${srcdir}/${_realname}-${pkgver}"

    # From https://build.opensuse.org/package/view_file/windows:mingw:win32/mingw32-pidgin/pidgin-2.10.11-autotools.patch
    rm -f pidgin/plugins/win32/Makefile.am
    rm -f pidgin/plugins/win32/transparency/Makefile.am
    rm -f pidgin/plugins/win32/winprefs/Makefile.am
    patch -p1 < "${srcdir}/001-autotools-and-fhs.patch"
    patch -p1 < "${srcdir}/002-build-fixes.patch"
    patch -p1 < "${srcdir}/003-build-fixes.patch"
    libtoolize --force --copy --install
    automake --add-missing
    autoreconf -f -i
}

build() {
    msg2 'Synchronizing build directory'
    rsync --recursive --times "${srcdir}/${_realname}-${pkgver}"/* "${srcdir}/${_realname}-${pkgver}.build.${CARCH}"
    cd "${srcdir}/${_realname}-${pkgver}.build.${CARCH}"

    sed -i "s#env python#env python2#" */plugins/*.py
    sed -i "s#env python#env python2#" libpurple/purple-{remote,notifications-example,url-handler}
    INTLTOOL_PERL=/usr/bin/perl ./configure \
        --prefix=${MINGW_PREFIX} \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --sysconfdir=${MINGW_PREFIX}/etc \
        --with-python=${MINGW_PREFIX}/bin/python2 \
        --with-system-ssl-certs=${MINGW_PREFIX}/ssl/certs \
        --without-x \
        --disable-avahi \
        --disable-schemas-install \
        --disable-screensaver \
        --disable-perl \
        --disable-tcl \
        --disable-tk \
        --disable-sm \
        --disable-nm \
        --disable-dbus \
        #--enable-cyrus-sasl

    make
}

package() {
    cd "${srcdir}/${_realname}-${pkgver}.build.${CARCH}"
    make DESTDIR="${pkgdir}" install
    install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
