# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=flatbuffers
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=25.9.23
pkgrel=1
pkgdesc='Memory Efficient Serialization Library (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://google.github.io/flatbuffers/'
msys2_repository_url="https://github.com/google/flatbuffers"
msys2_references=(
  'archlinux: flatbuffers'
  'gentoo: dev-libs/flatbuffers'
  'purl: pkg:pypi/flatbuffers'
  "cpe: cpe:2.3:a:google:flatbuffers"
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
depends=("${MINGW_PACKAGE_PREFIX}-libsystre")
source=("https://github.com/google/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-cmake-fix-install.patch)
sha256sums=('9102253214dea6ae10c2ac966ea1ed2155d22202390b532d1dea64935c518ada'
            '85564772505b69ef4a3dbbad5c85d0fadf6db10628e98346c546510fe7370d3e')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  plain "Extracting ${_realname}-${pkgver}.tar.gz"
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-cmake-fix-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 \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DFLATBUFFERS_BUILD_SHAREDLIB=ON \
    -DFLATBUFFERS_BUILD_TESTS=OFF \
    ../${_realname}-${pkgver}

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

  # Python bindings
  cp -r "${srcdir}"/${_realname}-${pkgver}/python "${srcdir}"/python-build-${MSYSTEM}
  cd "${srcdir}"/python-build-${MSYSTEM}
  VERSION="${pkgver}" \
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

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

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

package_python-flatbuffers() {
  pkgdesc='An efficient cross platform serialization library for Python (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-python")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: support for Numpy arrays")
  options=(!strip)

  cd "python-build-${MSYSTEM}"
  VERSION="${pkgver}" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
