# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=graphviz
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=9.0.0
pkgrel=1
pkgdesc="Graph Visualization Software (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.graphviz.org/'
license=('spdx:EPL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-devil"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libgd"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-gts"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libltdl")
makedepends=(#"${MINGW_PACKAGE_PREFIX}-ocaml" not tested yet need to build ocaml first
             #"${MINGW_PACKAGE_PREFIX}-lua" problem with distinguishing between the two versions we have and links to the wrong version sigh
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python"
             #"${MINGW_PACKAGE_PREFIX}-ruby" unable to find ruby's pkgconfig file looks for ruby.pc no ruby$(VERSION).pc as it is named now 
             "${MINGW_PACKAGE_PREFIX}-swig"
             #"${MINGW_PACKAGE_PREFIX}-tcl" needs a path to tclsh.sh
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "groff")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-base: gvedit"
            "${MINGW_PACKAGE_PREFIX}-python: for python modules")
install=${_realname}-${MSYSTEM}.install
source=(https://gitlab.com/graphviz/graphviz/-/archive/${pkgver}/graphviz-${pkgver}.tar.gz
        0001-configure-check-freeglut.patch
        0003-gdiplus-fix-library.patch
        0004-gvpr-remove-buiddir.patch
        0005-makefile-no-undefined.patch
        0006-tclpkg-fix-dll-extension.patch)
noextract=("graphviz-${pkgver}.tar.gz")
sha256sums=('504d19b5d0e5398a57e9d9de42393f90b9e79aff0969b4ebc3b891ccb39602ed'
            '327e75c8c3a8fef2803ce3301e1a3132c6364d297b50f19bf791dd6f0452a81a'
            '21cc8c686af53355ae345236a92ae16181eb85e999ff961c612aa4e3c1ccbd79'
            'cee214ebd9b3d905fb502a8a5009015198774a1c18afd49a870ce57e14ddd00d'
            '303fbd53d7502582f36acd384f6b306607ffcd35aab4a94b057307734ece51ae'
            '2391b6d4b9fc3c9c5da9ef61187af0dc5f17d2277ab1e0d3e9a7d4a0dbf6338c')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar -xf graphviz-${pkgver}.tar.gz || true

  cd "${srcdir}"/${_realname}-${pkgver}

  _apply_patch_with_msg \
    0001-configure-check-freeglut.patch \
    0003-gdiplus-fix-library.patch \
    0004-gvpr-remove-buiddir.patch \
    0005-makefile-no-undefined.patch \
    0006-tclpkg-fix-dll-extension.patch

  ./autogen.sh
}

build() {
  local gd_incdir=$(pkg-config --variable=includedir gdlib)

  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # else the libgdi plugin refuses to link.
  export lt_cv_deplibs_check_method=${lt_cv_deplibs_check_method='pass_all'}
  # the -enable-stuff below is for old type linking to graphviz 
  # and yields some nasty surprises when swig is enabled. 
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-guile=no \
    --enable-go=no \
    --enable-perl=no \
    --enable-php=no \
    --enable-ocaml=no \
    --enable-tcl=no \
    --enable-ruby=no \
    --enable-python=no \
    --enable-lua=no \
    --enable-r=no \
    --enable-sharp=no \
    --enable-swig=yes \
    --enable-java=no \
    --with-webp=yes \
    --with-gdiplus=yes \
    --with-smyrna=no \
    --with-gtk=no \
    --with-gtkglext=no \
    --with-gdk=no \
    --with-qt \
    --with-platformsdkincludedir=${MINGW_PREFIX}/include \
    --with-platformsdklibdir=${MINGW_PREFIX}/lib \
    --with-gdincludedir=${gd_incdir} \
    --enable-shared \
    --disable-static

  make
}

package_graphviz() {
  cd ${srcdir}/build-${MSYSTEM}

  local _pythonpath
  _pythonpath=$(python -c "import sysconfig; print(sysconfig.get_path('platlib'))")
  make DESTDIR="${pkgdir}" PYTHON3_INSTALL_DIR="$(cygpath ${_pythonpath})" install

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_graphviz-docs() {
  pkgdesc+=" (documentation)"
  depends=()
  optdepends=()

  cd "${srcdir}"/build-${MSYSTEM}
  mv dest/* "${pkgdir}"
}

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