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

_realname=graphviz
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=14.1.1
pkgrel=3
pkgdesc="Graph Visualization Software (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.graphviz.org/'
msys2_repository_url="https://gitlab.com/graphviz/graphviz"
msys2_references=(
  'archlinux: graphviz'
  'cpe: cpe:/a:graphviz:graphviz'
)
license=('spdx:EPL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-devil"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-gts")
         $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-libgd")
         "${MINGW_PACKAGE_PREFIX}-libltdl"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             #"${MINGW_PACKAGE_PREFIX}-lua" problem with distinguishing between the two versions we have and links to the wrong version sigh
             $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-base")
             "${MINGW_PACKAGE_PREFIX}-swig"
             #"${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
             "groff")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-base: gvedit"
            "${MINGW_PACKAGE_PREFIX}-python: for python modules")
install=${_realname}-${MSYSTEM}.install
source=(https://gitlab.com/graphviz/graphviz/-/archive/${pkgver}/graphviz-${pkgver}.tar.bz2
        0001-configure-check-freeglut.patch
        0002-fix-find-qt6.patch
        0003-gdiplus-fix-library.patch
        0005-makefile-no-undefined.patch
        0006-tclpkg-fix-dll-extension.patch)
noextract=("graphviz-${pkgver}.tar.bz2")
sha256sums=('975f4b9a7a7a7b614900ecd12919ccbd36a680818cc6fc527389b93e919410f0'
            '327e75c8c3a8fef2803ce3301e1a3132c6364d297b50f19bf791dd6f0452a81a'
            'f8835eb89d59b3a83b8c94aba28272b03693d08505889f3cb36941b266f26830'
            '82162202b4180cf49c0e40848794cc731a416b077e4955b6c4d02b0180deb7c0'
            '303fbd53d7502582f36acd384f6b306607ffcd35aab4a94b057307734ece51ae'
            '7428d8454198f4748e93ca328c53761d4754f9db3def444304caa315f661e390')

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

prepare() {
  echo "Extracting graphviz-${pkgver}.tar.bz2 ..."
  tar -xjf graphviz-${pkgver}.tar.bz2 || true

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

  _apply_patch_with_msg \
    0001-configure-check-freeglut.patch \
    0002-fix-find-qt6.patch \
    0003-gdiplus-fix-library.patch \
    0005-makefile-no-undefined.patch \
    0006-tclpkg-fix-dll-extension.patch

  ./autogen.sh
}

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

  declare -a _extra_config
  if [ "${CARCH}" != "i686" ]; then
    _extra_config+=("--with-qt=yes" "--with-gts=yes" "--with-libgd=yes")
  fi

  # 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} \
    --enable-python3=yes \
    --enable-swig=yes \
    --with-expat=yes \
    --with-freetype2=yes \
    --with-gdiplus=yes \
    --with-ghostscript=yes \
    --with-glut=yes \
    --with-gdincludedir=${gd_incdir} \
    --with-pangocairo=yes \
    --with-poppler=yes \
    --with-rsvg=yes \
    --with-webp=yes \
    --enable-guile=no \
    --enable-go=no \
    --enable-perl=no \
    --enable-php=no \
    --enable-tcl=no \
    --enable-ruby=no \
    --enable-python=no \
    --enable-lua=no \
    --enable-r=no \
    --enable-sharp=no \
    --enable-java=no \
    --with-smyrna=no \
    --with-gtk=no \
    --with-gtkglext=no \
    --with-gdk=no \
    --with-platformsdkincludedir=${MINGW_PREFIX}/include \
    --with-platformsdklibdir=${MINGW_PREFIX}/lib \
    --enable-shared \
    --disable-static \
    "${_extra_config[@]}"

  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;
