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

_realname=gtk-doc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.33.2
pkgrel=2
pkgdesc="Documentation tool for public library API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gtk.org/gtk-doc/"
license=("GPL" "FDL")
options=(staticlibs strip)
depends=("${MINGW_PACKAGE_PREFIX}-docbook-xsl"
         "${MINGW_PACKAGE_PREFIX}-docbook-xml"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-pygments")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-itstool"
             "${MINGW_PACKAGE_PREFIX}-meson")
url="https://www.gtk.org/gtk-doc/"
source=(https://download.gnome.org/sources/${_realname}/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        0001-exeext.patch
        pyscript2exe.py)
sha256sums=('cc1b709a20eb030a278a1f9842a362e00402b7f834ae1df4c1998a723152bf43'
            'e30928ae5f89b9a8f5a8757ea772749f25c3a445b3d7e441faa318540b6526c9'
            'e57174517b08cf8f9fb4f43a381d342d23d2db3cad661107f35ca21c39b5734d')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}"/0001-exeext.patch
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    --default-library shared \
    --buildtype plain \
    -Dyelp_manual=false \
    -Dtests=false \
    ../${_realname}-${pkgver}

  meson compile
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" meson install

  # Make relocatable (hacky... :/)
  for name in check fixxref mkdb mkhtml mkman mkpdf rebase scan scangobj depscan mkhtml2; do
    sed -s "s|'${MINGW_PREFIX}|sys.prefix + '|g" -i "${pkgdir}${MINGW_PREFIX}/bin/gtkdoc-${name}"
  done

  for name in check fixxref mkdb mkhtml mkman mkpdf rebase scan scangobj depscan mkhtml2; do
    ${MINGW_PREFIX}/bin/python \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/gtkdoc-${name}"
  done

  local _config_data="${pkgdir}${MINGW_PREFIX}/share/gtk-doc/python/gtkdoc/config_data.py"
  echo -e "import sys\n$(cat "${_config_data}")" > "${_config_data}"
  sed -s "s|'$(cygpath -m ${MINGW_PREFIX})|sys.prefix + '|g" -i "${_config_data}"
  sed -s "s|'${MINGW_PREFIX}|sys.prefix + '|g" -i "${_config_data}"
}
