# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=yelp-tools
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=42.1
pkgrel=1
pkgdesc="Tools for creating Yelp documentation (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('GPL')
url="https://www.gnome.org"
depends=("intltool"
         "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-itstool"
         "${MINGW_PACKAGE_PREFIX}-python-lxml"
         "${MINGW_PACKAGE_PREFIX}-python-mallard-ducktype"
         "${MINGW_PACKAGE_PREFIX}-yelp-xsl")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' '!debug' 'staticlibs')
source=(https://download.gnome.org/sources/${_realname}/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        "yelp-no-symlinks.patch")
sha256sums=('3e496a4020d4145b99fd508a25fa09336a503a4e8900028421e72c6a4b11f905'
            '276286ade313f9c31a990e77742b20a34c59d50f84a21e5d05c38e5a8ee654be')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/yelp-no-symlinks.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    "${extra_config[@]}" \
    -Dhelp=true \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson.exe compile
}

check () {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe test || true
}

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

  local WIN_PREFIX="$(cygpath -m ${MINGW_PREFIX})"

  for ff in yelp-build yelp-check yelp-new; do
    sed -e "s| xsltproc| ${MINGW_PREFIX}/bin/xsltproc|g" \
     -e "s|${WIN_PREFIX}|${MINGW_PREFIX}|g" \
     -i ${pkgdir}/${MINGW_PREFIX}/bin/$ff
  done

  for ff in mal-ncx mal-opf; do
    sed -e "s|${WIN_PREFIX}|${MINGW_PREFIX}|g" \
     -i ${pkgdir}${MINGW_PREFIX}/share/yelp-tools/xslt/$ff.xsl
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.GPL" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.GPL"
}
