# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=serf
pkgname=("lib${pkgbase}" "lib${pkgbase}-devel")
pkgver=1.3.10
pkgrel=2
pkgdesc="High-performance asynchronous HTTP client library"
arch=('i686' 'x86_64')
url="https://serf.apache.org/"
msys2_references=(
  "anitya: 1720"
  "cpe: cpe:/a:serf_project:serf"
)
license=('Apache')
makedepends=('scons' 'apr-util-devel' 'heimdal-devel' 'zlib-devel' 'openssl-devel' 'libiconv-devel' 'gcc')
source=(https://archive.apache.org/dist/serf/${pkgbase}-${pkgver}.tar.bz2
        03-destdir.patch
        05-disable-SHLIBVERSION.patch
        06-strcasecmp.patch
        07-sspi-auth.patch)
sha256sums=('be81ef08baa2516ecda76a77adf7def7bc3227eeb578b9a33b45f7b41dc064e6'
            '87767cd9b5e568a10f234df46f413a408613965e9d1b8cde1c0145f7eb20f0f1'
            '8057f20b42f5ca49c886aecc33b0686f3505f0d75918a8ec552424c10fd69e35'
            '313b272609729d80dedf608f9f1939526e687fdf4a1b50e1a66505c93c2ee56f'
            '84a43b3b6d3024195fdc69d215898ac426286329025bfa239ebe27a5c7507bc0')

prepare() {
  cd ${pkgbase}-${pkgver}

  patch -p1 -i ${srcdir}/03-destdir.patch
  patch -p1 -i ${srcdir}/05-disable-SHLIBVERSION.patch
  patch -p1 -i ${srcdir}/06-strcasecmp.patch
  patch -p1 -i ${srcdir}/07-sspi-auth.patch
}

build() {
  cd ${pkgbase}-${pkgver}
  scons PREFIX=/usr \
        GSSAPI=/usr/bin/krb5-config \
        CFLAGS="${CFLAGS} -D_XOPEN_SOURCE=700"

  install -d "${srcdir}/dest/usr"
  scons DESTDIR="${srcdir}/dest" install

  versions=($(echo ${pkgver} | tr -t '.' '\n'))
  major=${versions[0]}
  minor=${versions[1]}

  # Rename the libraries
  mv ${srcdir}/dest/usr/lib/libserf-${major}-0.dll.a ${srcdir}/dest/usr/lib/libserf-${major}.dll.a
  mv ${srcdir}/dest/usr/lib/libserf-${major}-0.a ${srcdir}/dest/usr/lib/libserf-${major}.a

  # Install the DLL
  rm ${srcdir}/dest/usr/lib/msys-serf-${major}-0.dll
  install -Dm755 msys-serf-${major}-0.dll ${srcdir}/dest/usr/bin/msys-serf-${major}-0.dll
}

#check() {
#  cd ${pkgbase}-${pkgver}
#  scons check
#}

package_libserf() {
  depends=('apr-util' 'libopenssl' 'zlib')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libserf-devel() {
  pkgdesc="Libserf headers and libraries"
  groups=('development')
  depends=("libserf=${pkgver}" 'apr-util-devel' 'openssl-devel' 'zlib-devel')
  options=('staticlibs')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
