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

_realname=libevent
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.12
pkgrel=13
pkgdesc="An event notification library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
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')
depends=("${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
optdepends=("${MINGW_PACKAGE_PREFIX}-python")
source=(https://github.com/${_realname}/${_realname}/releases/download/release-${pkgver}-stable/${_realname}-${pkgver}-stable.tar.gz
        '001-event2-02-win32.patch'
        '002-http-server-win32.patch'
        '003-cmake-update.patch'
        '004-time_t-compute.patch'
        '005-limit-MSVC-to-clang-cl.patch::https://github.com/libevent/libevent/commit/236762a3.patch'
        '006-fix-build-on-aarch64.patch'
        '007-fix-dll-version.patch'
        '008-automatic-choose-linkage-type.patch')
sha256sums=('92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb'
            'e572ed628daba12915aaa5867b35ef1b0ca3ed3eb6e9759f97136dd8eab32c08'
            'a587d4698b6fa2caba9e08388aba5f4ca6f4584a780c228df5b32e1260cf20c2'
            'be138bacbe5e8e95b5c6d593cfc69c6f663046561dd67e334b429ccf9dd29bf1'
            'e4c72c765ab33e3a606ba471496af41264909653863e0bef5017552db8c867fc'
            '30d91d6370789720ae9dc5be6e9164a7bae46313c10c66b8f37dbd1ec1a258f1'
            '249d6255e3f9a89fae149ed67e33e23e3fa10a9e2604a606c3855f5ea0e2dc20'
            '111dab201d50a6929af68222f8545e9b34973c3eb4cc36f7065fc7f5f7ccb3df'
            '4acb421606559ba443266cee4689a9aee9f75326a7529c912d033719aebfbfd9')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

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

  # to make transmission work, taken from:
  # https://github.com/transmission/libevent/commit/72e50166aaa2c3b3c35e336039df7101bd418264
  _apply_patch_with_msg \
    001-event2-02-win32.patch
  # https://github.com/libevent/libevent/pull/1575
  _apply_patch_with_msg \
    002-http-server-win32.patch
  
  _apply_patch_with_msg \
    003-cmake-update.patch \
    004-time_t-compute.patch \
    005-limit-MSVC-to-clang-cl.patch \
    006-fix-build-on-aarch64.patch \
    007-fix-dll-version.patch \
    008-automatic-choose-linkage-type.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [ "${CARCH}" = "i686" ]; then
    # Need for 32-bit toolchain to have 64bit time
    CFLAGS+=" -D__MINGW_USE_VC2005_COMPAT"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DEVENT_LIBRARY_TYPE=BOTH \
      -DEVENT__DISABLE_BENCHMARK=ON \
      -DEVENT__DISABLE_TESTS=ON \
      -DEVENT__DISABLE_REGRESS=ON \
      -DEVENT__DISABLE_SAMPLES=ON \
      -DEVENT__DISABLE_MBEDTLS=ON \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}-stable

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}-stable/LICENSE* -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  sed -s "s|$(cygpath -m ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}_openssl.pc"
}
