# Maintainer: Martell Malone <martellmalone@gmail.com>
# Contributor: Alethea Rose <alethea@alethearose.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=http-parser
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.9.4
pkgrel=2
pkgdesc="Parser for HTTP Request/Response written in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/nodejs/http-parser'
license=('MIT')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/nodejs/http-parser/archive/v${pkgver}.tar.gz")
options=('strip' 'staticlibs')
sha256sums=('467b9e30fd0979ee301065e70f637d525c28193449e1b13fbcb1b1fab3ad224f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  sed -s "s|-Werror||g" -i Makefile
  sed -s "s|-fPIC||g" -i Makefile
  sed -s "s|-Wl,-soname=\$(SONAME)||g" -i Makefile
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  cp -R ${srcdir}/${_realname}-${pkgver}/. "${srcdir}"/build-${CARCH}

  CC=${MINGW_PREFIX}/bin/gcc \
  AR=${MINGW_PREFIX}/bin/ar \
  SONAME=libhttp_parser-2.dll \
  LIBNAME=libhttp_parser-2.dll \
  LDFLAGS+=' -Wl,--out-implib=libhttp_parser.dll.a' \
  make -j1 \
    library package
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make test || true
}

package() {
  cd "${srcdir}"/build-${CARCH}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,include,lib}
  install -Dm644 http_parser.h "${pkgdir}${MINGW_PREFIX}/include/http_parser.h"
  cp *.a ${pkgdir}${MINGW_PREFIX}/lib/
  cp *.dll ${pkgdir}${MINGW_PREFIX}/bin/
}
