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

_realname=tmxlite
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=1.4.4
pkgrel=1
pkgdesc="A lightweight C++14 parser for Tiled tmx files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/fallahn/tmxlite'
license=('spdx:Zlib')
depends=("${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('ec8893efc8396308f291c284cb09f007441a15aabbb0e5722096cf79c65c9e58')

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DUSE_EXTLIBS=ON \
    -DTMXLITE_STATIC_LIB=ON \
    ../${_realname}-${pkgver}/${_realname}
  cmake --build .

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DUSE_EXTLIBS=ON \
    -DTMXLITE_STATIC_LIB=OFF \
    ../${_realname}-${pkgver}/${_realname}
  cmake --build .

  cd "${srcdir}/${_realname}-${pkgver}/${_realname}/documentation"

  ${MINGW_PREFIX}/bin/doxygen Doxyfile
}

package_tmxlite() {
  DESTDIR="${pkgdir}" cmake --install "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install "${srcdir}/build-${MSYSTEM}-shared"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/cmake/Modules/"
  cp "${srcdir}/${_realname}-${pkgver}/${_realname}/cmake/modules/FindTMXLITE.cmake" \
    "${pkgdir}${MINGW_PREFIX}/share/cmake/Modules/"

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

package_tmxlite-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "${srcdir}/${_realname}-${pkgver}/${_realname}/documentation/html" \
    "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
}

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