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

_realname=graphviz
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}-docs"))
pkgver=12.2.1
pkgrel=4
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}-libltdl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         $([[ ${CARCH} == i686 ]] || echo \
           "${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" \
           "${MINGW_PACKAGE_PREFIX}-gts" \
           "${MINGW_PACKAGE_PREFIX}-libgd" \
           "${MINGW_PACKAGE_PREFIX}-librsvg" \
           "${MINGW_PACKAGE_PREFIX}-libwebp" \
           "${MINGW_PACKAGE_PREFIX}-pango" \
           "${MINGW_PACKAGE_PREFIX}-poppler")
          )
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")
if [[ ${CARCH} != i686 ]]; then
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-base: gvedit"
              "${MINGW_PACKAGE_PREFIX}-python: for python modules")
fi
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
        0004-gvpr-remove-buiddir.patch
        0005-makefile-no-undefined.patch
        0006-tclpkg-fix-dll-extension.patch)
noextract=("graphviz-${pkgver}.tar.bz2")
sha256sums=('a990b38c3ea807a06597ce8d46d87878e59bb3fb12609fd98c02a861a4ca81b8'
            '327e75c8c3a8fef2803ce3301e1a3132c6364d297b50f19bf791dd6f0452a81a'
            'f8835eb89d59b3a83b8c94aba28272b03693d08505889f3cb36941b266f26830'
            '21cc8c686af53355ae345236a92ae16181eb85e999ff961c612aa4e3c1ccbd79'
            'cee214ebd9b3d905fb502a8a5009015198774a1c18afd49a870ce57e14ddd00d'
            '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 \
    0004-gvpr-remove-buiddir.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
    local gd_incdir=$(pkg-config --variable=includedir gdlib)
    _extra_config+=(
      "--enable-python3=yes"
      "--enable-swig=yes"
      "--with-expat=yes"
      "--with-glut=yes"
      "--with-ghostscript=yes"
      "--with-gts=yes"
      "--with-freetype2=yes"
      "--with-libgd=yes"
      "--with-gdincludedir=${gd_incdir}"
      "--with-pangocairo=yes"
      "--with-poppler=yes"
      "--with-qt=yes"
      "--with-rsvg=yes"
      "--with-webp=yes")
  else
    _extra_config+=(
      "--enable-python3=no"
      "--enable-swig=no"
      "--with-expat=no"
      "--with-glut=no"
      "--with-ghostscript=no"
      "--with-gts=no"
      "--with-freetype2=no"
      "--with-libgd=no"
      "--with-pangocairo=no"
      "--with-poppler=no"
      "--with-qt=no"
      "--with-rsvg=no"
      "--with-webp=no")
  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-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-gdiplus=yes \
    --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;
