# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>
# Contributor: Tim S. <stahta01@gmail.com>

# Note: Added "ENABLE_itk=OFF" because of random build errors;
# It appeared to think it found the itk library; but, it then errored out.
# Set "ENABLE_qt=OFF" and "DEFAULT_NO_CAIRO_DEVICES=ON" to reduce warnings.
# Had to define PLD_png and etc to ON to get libGD to be searched.

_wx_basever=3.2

_realname=plplot
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.15.0
pkgrel=26
pkgdesc="Scientific plotting software (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://plplot.sourceforge.io/"
msys2_references=(
  'gentoo: '
)
license=('spdx:LGPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libgd"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-pango"
         #"${MINGW_PACKAGE_PREFIX}-python"
         #"${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-shapelib"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs"
         "${MINGW_PACKAGE_PREFIX}-qhull"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-libgd"
             "${MINGW_PACKAGE_PREFIX}-fc"
             #"${MINGW_PACKAGE_PREFIX}-gcc-ada"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw")
optdepends=("${MINGW_PACKAGE_PREFIX}-swig: connects Plplot C library to Python and Lua")
source=(https://downloads.sourceforge.net/sourceforge/plplot/${_realname}-${pkgver}.tar.gz
        find-gd.patch
        Include-wx-msw-wrapwin.h-instead-of-Windows.h.patch
        pdf-linking-libharu.patch
        fix-pkgconfig.patch
        fix-install-dir.patch
        fix-cmake-path-relocation.patch
        fix-wingdi.patch
        include-qpainterpath.patch)
sha256sums=('b92de4d8f626a9b20c84fc94f4f6a9976edd76e33fb1eae44f6804bdcc628c7b'
            '57ad3a43d5ad467b89889a42d0b033e35cf322b9b83f2f41a62fe5d74a5d01b8'
            'fc27681c6ca521308ac0d873b3ed78c335e676ca6d367fd8d000a41580565ed8'
            'abf5ee33dad2108220d0973de4279ce04007b9fb429184d194a6cf951005c8be'
            '4bbd508fbd83fcdbce779216a224b15fac55d92c9bb66d66d1d95c4eac1d1322'
            'c11a37adea8134f9497fd45f24cd153cce18855ddff66ea5ba6ef8875da022fc'
            'a79ce95cc2dc0a4eade6f5f68b3f7d6cd335f0fc8fcb18bb69468ea4ef6b6ab2'
            '675b7bdf421a81ba390b023dc94a0d43604180fa60a88364c6883fb02a950acc'
            '8d5d647042b5ae51043bd48589622a85c807785287a1024b75d4fcb7ab7da22e')

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

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

  apply_patch_with_msg \
    find-gd.patch \
    Include-wx-msw-wrapwin.h-instead-of-Windows.h.patch \
    pdf-linking-libharu.patch \
    fix-pkgconfig.patch \
    fix-install-dir.patch \
    fix-cmake-path-relocation.patch \
    fix-wingdi.patch \
    include-qpainterpath.patch
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # cmake may be confused if there is a Windows installation
  # of the official Python MSI. Please do the Python-enabled
  # build only, if no official Python is installed.
  local pyexe="${MINGW_PREFIX}/bin/python3.exe"
  local pyver=$(${pyexe} -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  local pylib="${MINGW_PREFIX}/lib/libpython${pyver}.dll.a"
  if [[ ${pyver:0:1} != "3" ]] ; then
     pylib="${MINGW_PREFIX}/lib/libpython${pyver}m.dll.a"
  fi

  CFLAGS+=" -Wno-incompatible-pointer-types -Wno-implicit-function-declaration -Wno-int-conversion" \
  CXXFLAGS+=" -Wno-deprecated-declarations -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TEST=OFF \
    -DPYTHON_VERSION=${pyver} \
    -DPYTHON_EXECUTABLE=${pyexe} \
    -DPYTHON_LIBRARY=${pylib} \
    -DPYTHON_INCLUDE_DIRS=${MINGW_PREFIX}/include/python${pyver} \
    -DQHULL_INCLUDE_DIRS=${MINGW_PREFIX}/include \
    -DQHULL_LIBRARY_DIRS=${MINGW_PREFIX}/lib \
    -DQHULL_LIBRARIES=qhullstatic \
    -DGD_INCLUDE_DIR=${MINGW_PREFIX}/include \
    -DwxWidgets_CONFIG_EXECUTABLE=${MINGW_PREFIX}/bin/wx-config-${_wx_basever} \
    -DDEFAULT_NO_BINDINGS=ON \
    -DENABLE_ada=OFF \
    -DENABLE_cxx=ON \
    -DENABLE_fortran=ON \
    -DENABLE_lua=ON \
    -DENABLE_python=OFF \
    -DENABLE_tcl=ON \
    -DENABLE_tk=ON \
    -DENABLE_DYNDRIVERS=OFF \
    -DENABLE_wxwidgets=ON \
    -DENABLE_itk=OFF \
    -DENABLE_qt=ON \
    -DPLD_png=ON \
    -DPLD_jpeg=ON \
    -DPLD_gif=ON \
    -DPLD_wingcc=ON \
    -DPLD_wingdi=ON \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  local MINGW_PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  local MSYS2_USR_WIN=$(cygpath -am /usr)

  # Fix paths in pkgconfig files (#17541)
  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -e "s|${MINGW_PREFIX}/include|\${prefix}/include|g" \
    -e "s|${MINGW_PREFIX}/lib|\${prefix}/lib|g" {} \;

  # Fix paths in cmake files
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
    sed -e "s|${MINGW_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Fix paths in example files
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name '*.sh' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name 'Makefile' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name '*.sh' \
    -exec sed -i -e "s|${MSYS2_USR_WIN}|/usr|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/share/plplot${pkgver}/examples" -type f -name 'Makefile' \
    -exec sed -i -e "s|${MSYS2_USR_WIN}|/usr|g" {} \;

  # Fix paths in the remaing odd files
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tcl/tclsh_standard_examples
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_runAllDemos
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_standard_examples
  sed -s "s|${MINGW_PREFIX_WIN}|\${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/cmake/modules/plplot_configure.cmake
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/cmake/modules/plplot_configure.cmake
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tcl/pltcl_standard_examples
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tcl/tclsh_standard_examples
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/plserver_runAllDemos
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/plserver_standard_examples
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_runAllDemos
  sed -s "s|${MSYS2_USR_WIN}|/usr|g" -i "${pkgdir}"${MINGW_PREFIX}/share/plplot${pkgver}/examples/tk/wish_standard_examples

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/$_realname
  cp -pf "${srcdir}"/${_realname}-${pkgver}/Copyright \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
  cp -pf "${srcdir}"/${_realname}-${pkgver}/COPYING* \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
}
