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

_realname=tinyxml
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.6.2
pkgrel=5
pkgdesc="Simple, small, C++ XML parser that can be easily integrated into other programs (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://www.grinninglizard.com/tinyxml"
msys2_references=(
  "cpe: cpe:/a:tinyxml_project:tinyxml"
)
license=('spdx:Zlib')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
source=("https://downloads.sourceforge.net/tinyxml/tinyxml_${pkgver//./_}.tar.gz"
        "entity.patch"
        "tinyxml-2.5.3-stl.patch"
        "tinyxml.pc")
sha256sums=('15bdfdcec58a7da30adc87ac2b078e4417dbe5392f3afb719f9ba6d062645593'
            'ef493209b0a51160171fd834a7ecdddd02679463b85fb89a2ea254213e47f99b'
            '3baf2c4dbc2c8f54a151dac8860113d2f549174f83ed85d552b094dfaebb52af'
            '60bcff16486698f4d2f076f5a3c6e5566e098e3d0d30f1bdbbe7012d3d36c095')

prepare() {
  cd "${srcdir}/tinyxml"
  patch -p0 -i "${srcdir}"/entity.patch
  patch -p1 -i "${srcdir}"/tinyxml-2.5.3-stl.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}"
  cp -r "${srcdir}/tinyxml/"* "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"
  for file in $(ls *.cpp | grep -v xmltest); do
    ${CXX} -c -Wall -Wno-unknown-pragmas -Wno-format -O3 ${file}
  done
  ar rcs libtinyxml.a $(ls *.o)
  ${CXX} -shared -o libtinyxml-0.dll -Xlinker --out-implib -Xlinker libtinyxml.dll.a $(ls *.o)
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  install -Dm644 libtinyxml.a "${pkgdir}${MINGW_PREFIX}/lib/libtinyxml.a"
  install -Dm644 libtinyxml-0.dll "${pkgdir}${MINGW_PREFIX}/bin/libtinyxml-0.dll"
  install -m644 libtinyxml.dll.a "${pkgdir}${MINGW_PREFIX}/lib/"
  install -Dm644 "${srcdir}/tinyxml.pc" "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/tinyxml.pc"
  install -Dm644 tinyxml.h "${pkgdir}${MINGW_PREFIX}/include/tinyxml.h"
  install -m644 tinystr.h "${pkgdir}${MINGW_PREFIX}/include/"
  sed -i "s,@PREFIX@,${MINGW_PREFIX}," "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/tinyxml.pc"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}
