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

_realname=miniupnpc
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
_base_ver=2.1
_snap_date=20201016
pkgver=${_base_ver}.${_snap_date}
pkgrel=2
pkgdesc="A small UPnP client library/tool to access Internet Gateway Devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://miniupnp.free.fr"
license=('BSD')
options=('staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(https://miniupnp.tuxfamily.org/files/${_realname}-${pkgver}.tar.gz{,.sig}
        001-fix-static-library-name.patch
        002-python-module-build.patch)
sha256sums=('69f72fe355f911b807915f78dcfc0af772c0f22bc7ab1157e70f334e37db7d39'
            'SKIP'
            '06faa3e848bf575da7855cff11215e1622ea039e3e1f3ad3e1390f994809ab78'
            'b0bf9c70b1ffe4b765455274ef132f0639c8a0ef18fbf9776b9169eebbf82c86')
validpgpkeys=('751E9FF6944A3B36A5432216DB511043A31ACAAF') # miniupnp <miniupnp@free.fr>

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-static-library-name.patch
  patch -p1 -i ${srcdir}/002-python-module-build.patch
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}
  #mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUPNPC_BUILD_SHARED=ON \
    -DUPNPC_BUILD_STATIC=ON \
    .

  make

  msg "Build Python extension..."
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setupmingw32.py build
}

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

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/man3/miniupnpc.3 \
    "${pkgdir}${MINGW_PREFIX}/share/man/man3/miniupnpc.3"
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  msg "Install Python${pyt} extension..."
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setupmingw32.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"
}
