# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>

_realname=libosmpbf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.1
pkgrel=4
pkgdesc="A library to support OpenStreetMap's protocolbuffer binary .pbf format (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/openstreetmap/OSM-binary"
license=('spdx:LGPL-3.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/openstreetmap/OSM-binary/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-fix-linking.patch)
sha256sums=('183ad76c5905c7abd35d938824320ffb82d9ca8987796018f2da8380b51cdac2'
            'c59f3acd279201ae1a5c4fe50099a391d7c5fd072e8c03fdbef2f9d00a1d892e')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  tar -xf "${srcdir}"/${_realname}-${pkgver}.tar.gz -C "${srcdir}" || true

  cd "${srcdir}"/OSM-binary-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-linking.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

  CXXFLAGS+=" -DPROTOBUF_USE_DLLS" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    ../OSM-binary-${pkgver}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/OSM-binary-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
