# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=jsonnet
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.20.0
pkgrel=5
pkgdesc="The data templating language (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/google/jsonnet"
msys2_references=(
  'purl: pkg:pypi/jsonnet'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('!strip')
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        '001-jsonnet-0.20.0-cxx17.patch'
        '002-jsonnet-0.20.0-include-nlohmann-json.patch'
        '003-jsonnet-0.20.0-link-libraries.patch'
        '004-jsonnet-0.20.0-core-runtime.patch'
        '005-jsonnet-0.20.0-cpp-runtime.patch'
        '006-jsonnet-0.20.0-c4core-nopack.patch'
        '007-jsonnet-0.20.0-rapidyaml-aarch64.patch')
sha256sums=('77bd269073807731f6b11ff8d7c03e9065aafb8e4d038935deb388325e52511b'
            '83dc8dabde1caa87a5d59f945952f61fb1b7d77079d3c849772464674eef6c6d'
            '606fa576a6ad4c71ce5c3bfd5ab6dc9461ccfc36e959830e133123a0cb652c93'
            'e2ea63010cbe37296715139878393d26c5b3c04ca1f3cd8cb60bc6a2ec8e9402'
            'dfe3393b4eb6a3f4d0b18b683970bb50d8267d8041221beeb4a8e9386f7f4ce2'
            'f75f4330f6b18219a01660a4f4d4b3cb9928441def0a0e739c2503870363ca40'
            '38dfe387e68d67ac85c21a19ae8ec5e6af18fbd3649b44f3ddde1866bea3b974'
            '9be36ff6405ee9dbe868654f1cdd2e38527ca8cde7bc357711109b47b3c05de5')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

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

  apply_patch_with_msg \
    001-jsonnet-0.20.0-cxx17.patch \
    002-jsonnet-0.20.0-include-nlohmann-json.patch \
    003-jsonnet-0.20.0-link-libraries.patch \
    004-jsonnet-0.20.0-core-runtime.patch \
    005-jsonnet-0.20.0-cpp-runtime.patch \
    006-jsonnet-0.20.0-c4core-nopack.patch \
    007-jsonnet-0.20.0-rapidyaml-aarch64.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake \
    -G"Ninja" \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DUSE_SYSTEM_JSON=ON \
    -DBUILD_TESTS=OFF \
    ../${_realname}-${pkgver}

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

  msg "Python build"
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/python-build-${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"

  export CFLAGS="$CFLAGS -fPIC"
  export CXXFLAGS="$CXXFLAGS -fPIC"
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

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

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

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

package_python-jsonnet() {
  pkgdesc="Python bindings for Google's Jsonnet - The data templating language (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-python")

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

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

# 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;
