# Maintainer: fauxpark <fauxpark@gmail.com>

_realname=libnatpmp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=r84.07004b9
pkgrel=2
pkgdesc="A portable and fully compliant implementation of the NAT-PMP protocol (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://miniupnp.free.fr/libnatpmp.html'
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             'git')
_commit='07004b97cf691774efebe70404cf22201e4d330d'
source=("${_realname}::git+https://github.com/miniupnp/libnatpmp#commit=$_commit"
        '0001-shared-lib-and-install.patch')
sha256sums=('SKIP'
            'b635e9fb52f21397e097034cc5833356a1a4cab9690ac1a9493b7a49b7d70a46')

pkgver() {
  cd "${srcdir}/${_realname}"

  printf "r%s.%s" "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd "${srcdir}/${_realname}"

  patch -Np1 -i "${srcdir}/0001-shared-lib-and-install.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=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      ../${_realname}

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

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

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

  install -Dm644 "${srcdir}/${_realname}/natpmpc.1" -t "${pkgdir}${MINGW_PREFIX}/share/man/man1"

  install -Dm644 "${srcdir}/${_realname}/LICENSE" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/libnatpmp"
}
