# Contributor: Melven Roehrig-Zoellner <Melven.Roehrig-Zoellner@DLR.de>

_realname=tixi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.4
pkgrel=1
pkgdesc="TiXI is a fast and simple XML interface library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/DLR-SC/tixi'
license=('Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/DLR-SC/tixi/archive/v${pkgver}.tar.gz"
        0001-Dont-build-and-install-the-examples.patch
        0002-Fix-ctypes-DLL-loading-in-Python-3.8-new-policy.patch)
sha256sums=('9080d2a617b7c411b9b4086de23998ce86e261b88075f38c73d3ce25da94b21c'
            '81bc8d93195bf3c028beb93b5b2d02f38afaf91242c6a7bd59c1f7c4dd3d484e'
            'f8f1f685ec634c7362b4aff43f6db34c4cde3168ca28fa194ff990150463eeec')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-Dont-build-and-install-the-examples.patch"
  patch -Np1 -i "${srcdir}/0002-Fix-ctypes-DLL-loading-in-Python-3.8-new-policy.patch"
}


build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  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 \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

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


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

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

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