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

pkgname=('libevent' 'libevent-devel')
pkgver=2.1.12
pkgrel=4
pkgdesc="An event notification library"
arch=('i686' 'x86_64')
url="https://libevent.org/"
msys2_repository_url="https://github.com/libevent/libevent"
msys2_references=(
  "cpe: cpe:/a:libevent_project:libevent"
  "cpe: cpe:/a:niels_provos:libevent"
)
license=('spdx:BSD-3-Clause')
makedepends=('openssl-devel' 'autotools' 'gcc')
source=("https://github.com/${pkgname}/${pkgname}/releases/download/release-${pkgver}-stable/${pkgname}-${pkgver}-stable.tar.gz"{,.asc}
        "0001-Fix-DNS-lookups-in-MSYS2.patch"
        "libevent-2.1.12-openssl-compat.patch")
sha256sums=('92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb'
            'SKIP'
            '38246613788aa843abcaf1d895824af40db5a927ff32814b984552d6f8f8bb96'
            'e41527c55f6dacc6453b13eeeea2664798496896b796abcd2a5c8304c9eb2bae')
validpgpkeys=('9E3AC83A27974B84D1B3401DB86086848EF8686D') # Azat Khuzhin <azat@libevent.org>

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}-stable
  patch -p1 -i ${srcdir}/0001-Fix-DNS-lookups-in-MSYS2.patch

  # Taken from https://github.com/archlinux/svntogit-packages/blob/95e06cdb083da6da642a0e01caf6446758a2fd08/trunk/libevent-2.1.12-openssl-compat.patch
  # which is based on https://github.com/libevent/libevent/pull/1288
  patch -p1 -i ${srcdir}/libevent-2.1.12-openssl-compat.patch

  ./autogen.sh
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}-stable
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --disable-libevent-regress
  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}-stable
  make -k check
}

package_libevent() {
  depends=('openssl')
  optdepends=('python: to use event_rpcgen.py')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin $pkgdir/usr/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}-stable/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_libevent-devel() {
  pkgdesc="Libevent headers and libraries"
  groups=('development')
  depends=("libevent=${pkgver}" "openssl-devel")

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