# Maintainer: Steve Jemens <sjemens@hotmail.com>

_realname=xlnt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.0
pkgrel=3
pkgdesc="User-friendly xlsx library for C++14 (mingw-w64)"
url="https://github.com/tfussell/xlnt"
license=('MIT')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
              "unzip")
source=("${_realname}-${pkgver}.zip"::"https://github.com/tfussell/xlnt/archive/v${pkgver}.zip"
        001-include-limits.patch)
noextract=("${_realname}-${pkgver}.zip") # bsdtar fails extracting the symlinks
sha256sums=('27db56b53745bf40e94edc531eb54ef67a75a40c9acc47609f0d8c8950b46d4c'
            '2b1029f172bb5f80db7f408be6f68a3b63d11188fccd1f6dd19cad0060f0bd62')

prepare() {
  unzip -oq "${_realname}-${pkgver}.zip" -d "${srcdir}"

  # Causes error in clang, not so important
  cd "${srcdir}/${_realname}-${pkgver}"
  rm "third-party/libstudxml/version"

  patch -p1 -i "${srcdir}"/001-include-limits.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  echo "Building static library"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DSTATIC=ON \
    -DTESTS=OFF \
    ../${_realname}-${pkgver}

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DSTATIC=OFF \
    -DTESTS=OFF \
    ../${_realname}-${pkgver}

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

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

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

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