# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Nicolas Bats <sl1200mk2@gmail.com>
# Contributor: Karlson2k (Evgeny Grin) <k2k@narod.ru>
# Contributor: Marcin Sielski <marcin.sielski@motorolasolutions.com>

_realname=libmicrohttpd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.72
pkgrel=2
pkgdesc="GNU libmicrohttpd is a small C library that is supposed to make it easy to run an HTTP server as part of another application (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.gnu.org/software/libmicrohttpd"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libtool"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
checkdepends=("${MINGW_PACKAGE_PREFIX}-curl")
depends=("${MINGW_PACKAGE_PREFIX}-gnutls")
source=("https://ftp.gnu.org/gnu/libmicrohttpd/${_realname}-${pkgver}.tar.gz"{,.sig})
validpgpkeys=('D8423BCB326C7907033929C7939E6BE1E29FC3CC'  # Christian Grothoff <christian@grothoff.org>
              'EA812DBEFA5A7EF17DA8F2C1460A317C3326D2AE') # Karlson2k (Evgeny Grin) <k2k@narod.ru>
sha256sums=('0ae825f8e0d7f41201fd44a0df1cf454c1cb0bc50fe9d59c26552260264c2ff8'
            'SKIP')

prepare() {
  cd ${_realname}-${pkgver}
  # autoreconf to get updated libtool files with clang support
  autoreconf -fiv
}

build() {
  declare -a extra_config
  if check_option "debug" "y"; then
    extra_config+=("--enable-asserts")
  else
    extra_config+=("--disable-asserts")
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
    CPPFLAGS="${CPPFLAGS} -D_WIN32_WINNT=0x0601" \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    "${extra_config[@]}" \
    --disable-examples \
    --enable-https \
    --enable-shared \
    --enable-static

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  cd src/microhttpd/ # Run only basic tests until curl fix crash in curl_global_cleanup ()
  make -j1 check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
