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

_realname=graphviz
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.44.1
pkgrel=5
pkgdesc="Graph Visualization Software (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://www.graphviz.org/'
license=('EPL')
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}-gtk2"
         "${MINGW_PACKAGE_PREFIX}-gtkglext"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-libglade"
         "${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}-libtool")
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"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             #"${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}-tcl" needs a path to tclsh.sh
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "git")
options=('strip')
install=${_realname}-${MSYSTEM}.install
source=(#${_realname}-${pkgver}::git+https://gitlab.com/graphviz/graphviz.git#tag=stable_release_${pkgver}
        https://www2.graphviz.org/Packages/stable/portable_source/graphviz-${pkgver}.tar.gz
        001-msvc-pragma.patch.patch
        002-no-undefined-and-def-files.patch
        003-fix-dependency-detect.patch
        004-exports.patch
        005-plugin-exports.patch
        006-missing-libs.patch
        007-broken-freeglut.patch
        008-fix-dll-names-in-def.patch
        009-fix-warnings.patch
        010-no-extern-time.patch
        011-strcasecmp-mingw.patch
        012-cast-error.patch
        013-further-fixes.patch)
sha256sums=('8e1b34763254935243ccdb83c6ce108f531876d7a5dfd443f255e6418b8ea313'
            'a64e23a55481ff36641466ef80607bcdc1bbf20f992804620f4a58b5b8c04f8d'
            'd1e7e4dc1b55463123e3ed8f999745bf06078766ee24071be8866ac75fe1309e'
            'bd1d89894af4b89d14cfa028238aa87f0a6044c43f988f39a0c8512ab07285a2'
            'b5ce65be7a0f8012aecdf15229e3db8012ddc78152fa849a54ffe87ca6bfe119'
            '35ee3d44abcb6d30a10ea5a797b214b19e929a0555297a8dce35e4842c059b15'
            '8fe551e8fc8c7f048c8c497f983e3bec60ac9489dec307508160199a5162ab3c'
            '6f066e2d08d296355da0e735429ade1a459940225288a70939f547c36562f982'
            '26a80a0071a0821de8019463788e966047551a93451386f5cf209c77cefe65e9'
            '0ac5c4b11297ed751f32ab6e165d75f27d7ec30efc3c26c23b73fbce2b097179'
            '01387eb1c9544b9e8aeb66a6ec23777c5bb3e2770b1672d2596b2b3d550319fd'
            'f9a4aa81bf2f918cc608dc49852c57782b610c2e56fea7e43a53a7c5bd655e90'
            'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
            '2cc48cd408a2949391e5e5232b107300978a82c31eaf54a35a4d87d3a503c643')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-msvc-pragma.patch.patch
  patch -p1 -i ${srcdir}/002-no-undefined-and-def-files.patch
  patch -p1 -i ${srcdir}/003-fix-dependency-detect.patch
  patch -p1 -i ${srcdir}/004-exports.patch
  patch -p1 -i ${srcdir}/005-plugin-exports.patch
  patch -p1 -i ${srcdir}/006-missing-libs.patch
  patch -p1 -i ${srcdir}/007-broken-freeglut.patch
  patch -p1 -i ${srcdir}/008-fix-dll-names-in-def.patch
  patch -p1 -i ${srcdir}/009-fix-warnings.patch
  patch -p1 -i ${srcdir}/010-no-extern-time.patch
  patch -p1 -i ${srcdir}/011-strcasecmp-mingw.patch
  patch -p1 -i ${srcdir}/012-cast-error.patch
  patch -Np1 -i ${srcdir}/013-further-fixes.patch

  ./autogen.sh NOCONFIG
}

build() {
  local gd_incdir=$(pkg-config --variable=includedir gdlib)
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}
  # 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-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-qt \
    --with-platformsdkincludedir=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    --with-platformsdklibdir=${MINGW_PREFIX}/${MINGW_CHOST}/lib \
    --with-gdincludedir=${gd_incdir} \
    --enable-shared \
    --disable-static

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install
}
