# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=pupnp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.14.24
pkgrel=1
pkgdesc="Portable SDK for UPnP* Devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/pupnp/pupnp"
msys2_references=(
  'archlinux: libupnp'
  "cpe: cpe:/a:libupnp_project:libupnp"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/pupnp/pupnp/archive/release-${pkgver}/libupnp-${pkgver}.tar.gz
        001-fix-dll-export.patch
        002-libupnp-win32.patch)
sha256sums=('2ca172d70dee4000f028fe3ec1df53b43c39e6e0289aff6e9fe25349d516c813'
            '262a8496205f78cffb6662acafdc9ba35957ff2971d2fd6ef94ff8acaab2ca50'
            '99a0334ff2dd01fdcbe37d7cfbbbc86dacbf2cb7cccd533be3c8833e4367af1c')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/pupnp-release-${pkgver}"
  apply_patch_with_msg \
    001-fix-dll-export.patch \
    002-libupnp-win32.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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  CFLAGS+=" -Wno-incompatible-pointer-types" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DSTATIC_POSTFIX="-static" \
    -DBUILD_TESTING=OFF \
    -Dclient=ON \
    -Ddevice=ON \
    -Dwebserver=ON \
    -Dssdp=ON \
    -Doptssdp=ON \
    -Dsoap=ON \
    -Dgena=ON \
    -Dtools=ON \
    -Dipv6=ON \
    -Dunspecified_server=OFF \
    -Dopen_ssl=OFF \
    -Dblocking_tcp_connections=OFF \
    -Dscriptsupport=ON \
    -Dreuseaddr=ON \
    -Dsamples=ON \
    ../pupnp-release-${pkgver}

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest . || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # License
  install -Dm644 "${srcdir}"/pupnp-release-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
