# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=libxml2
pkgbase=mingw-w64-jellyfin-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-jellyfin-${_realname}")
pkgver=2.15.3
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="XML parsing library, version 2 (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-jellyfin-libiconv"
         "${MINGW_PACKAGE_PREFIX}-jellyfin-xz"
         "${MINGW_PACKAGE_PREFIX}-jellyfin-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings")
license=('spdx:MIT')
url="https://gitlab.gnome.org/GNOME/libxml2/-/wikis/home"
msys2_repository_url="https://gitlab.gnome.org/GNOME/libxml2"
msys2_references=(
  "cpe: cpe:/a:xmlsoft:libxml2"
)
install=${_realname}-${MSYSTEM}.install
source=("https://download.gnome.org/sources/libxml2/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        https://www.w3.org/XML/Test/xmlts20130923.tar.gz
        0001-do-not-export-DllMain.patch
        0029-xml2-config-win-paths.patch
        0031-apply-msvc-relocation.patch
        0032-cmake-relocation.patch)
sha256sums=('78262a6e7ac170d6528ebfe2efccdf220191a5af6a6cd61ea4a9a9a5042c7a07'
            '9b61db9f5dbffa545f4b8d78422167083a8568c59bd1129f94138f936cf6fc1f'
            'baad28b724738464cebf1a4e3320111b848fd7b14619e5c0134784ef7ad2aa3a'
            '278b4531da3d2aabda080c412c5122b471202dd6df67768b38bb0c31c7a0e508'
            'bcb2565878e524df3e3bcd1631dbcefc205e9b435a2e537c1810996b97c4dd4a'
            'd2bd68da35aa68197f1b0ebed2c01685a7f4c780430840784a861a540d05b130')

export FF_MINGW_PREFIX="${MINGW_PREFIX}/ffbuild"

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}
# =========================================== #

prepare() {
  cd ${_realname}-${pkgver}

  # https://github.com/msys2/MINGW-packages/issues/17820
  # https://github.com/msys2/MINGW-packages/issues/25716
  apply_patch_with_msg \
    0001-do-not-export-DllMain.patch

  apply_patch_with_msg \
    0031-apply-msvc-relocation.patch \
    0032-cmake-relocation.patch

  # https://github.com/msys2/MINGW-packages/issues/10577
  apply_patch_with_msg \
    0029-xml2-config-win-paths.patch

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  export PKG_CONFIG_PATH="${FF_MINGW_PREFIX}/lib/pkgconfig:${PKG_CONFIG_PATH}"
  # Static build
  msg2 "Building static for ${MSYSTEM}"
  [[ -d "${srcdir}"/build-static-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-static-${MSYSTEM}
  mkdir -p ${srcdir}/build-static-${MSYSTEM} && cd ${srcdir}/build-static-${MSYSTEM}
  ../${_realname}-${pkgver}/configure \
    --prefix=${FF_MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build="${MINGW_CHOST}" \
    --without-python \
    --with-modules \
    --with-http \
    --enable-static \
    --disable-shared \
    --with-threads=win32 \
    CFLAGS="${CFLAGS} -DLIBXML_STATIC_FOR_DLL -DNOLIBTOOL"
  make
}

check() {
  :
}

package_libxml2() {
  # First install shared
  make -C ${srcdir}/build-static-${MSYSTEM} install DESTDIR="${pkgdir}"

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/Copyright" "${pkgdir}${FF_MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  # Remove hard coded library path from xlm2-config and libxml-2.0.pc files
  local PREFIX_DEPS=$(cygpath -am ${FF_MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i "${pkgdir}"${FF_MINGW_PREFIX}/bin/xml2-config
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i "${pkgdir}"${FF_MINGW_PREFIX}/lib/pkgconfig/libxml-2.0.pc
}

# 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}-jellyfin-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
