# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=xalan-c
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.12
pkgrel=5
pkgdesc="An XSLT processing library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://xalan.apache.org/xalan-c"
license=('APACHE')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-tools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-xerces-c")
options=('!libtool' 'strip')
source=(https://archive.apache.org/dist/xalan/xalan-c/sources/xalan_c-${pkgver}.tar.gz
        01-mingw-icu.patch
        02-mingw-as-msvc.patch
        03-install-layout.patch
        04-no-std-file.patch)
sha256sums=('ee7d4b0b08c5676f5e586c7154d94a5b32b299ac3cbb946e24c4375a25552da7'
            '64ca2fb25114fd9c35c0a0815c7308ab3222eb421965892a402c4f30d23f180b'
            '8ccef1339fcfb6472861545295a9cb72a6b8da3a81407d082a5df51ac5f5c8e0'
            '35f2cf6b4aefb29c1c88f94b808c07a2b2845c025f236b38f4e31b5e378ba874'
            '0b2bcce90c90261e309a7d3914631f91deaca00930acda487c9b828c581ea968')

prepare(){
  cd "${srcdir}"/${_realname//-/_}-${pkgver}
  patch -p1 -i ${srcdir}/01-mingw-icu.patch
  patch -p1 -i ${srcdir}/02-mingw-as-msvc.patch
  patch -p1 -i ${srcdir}/03-install-layout.patch
  patch -p1 -i ${srcdir}/04-no-std-file.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

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname//-/_}-${pkgver}

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

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

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