# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Naveen M K <naveen521kk@gmail.com>

_realname=inkscape
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4.2
pkgrel=12
pkgdesc="Vector graphics editor using the SVG file format (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://inkscape.org/"
msys2_repository_url="https://gitlab.com/inkscape/inkscape"
msys2_references=(
  "cpe: cpe:/a:inkscape:inkscape"
)
license=("spdx:GPL-2.0-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-aspell"
         "${MINGW_PACKAGE_PREFIX}-atkmm"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-cairomm"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gc"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-gspell"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-gtkmm3"
         "${MINGW_PACKAGE_PREFIX}-gtksourceview4"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lib2geom"
         "${MINGW_PACKAGE_PREFIX}-libcdr"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-librevenge"
         "${MINGW_PACKAGE_PREFIX}-libsigc++"
         "${MINGW_PACKAGE_PREFIX}-libvisio"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwpg"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-pangomm"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-scour"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools")
optdepends=(#"${MINGW_PACKAGE_PREFIX}-pstoedit: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-texlive-scheme-medium: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: some extensions"
            "${MINGW_PACKAGE_PREFIX}-python-lxml: some extensions and filters"
            #"${MINGW_PACKAGE_PREFIX}-scour: optimized SVG output"
            #"${MINGW_PACKAGE_PREFIX}-uniconvertor: reading/writing to some proprietary formats"
            )
source=("https://media.inkscape.org/dl/resources/file/${_realname}-${pkgver}.tar.xz"
        inkscape-1.0.1-unbundle.patch
        inkscape-1.0.1-install-layout.patch
        007-no-console-clang.patch
        008-fix-glib-2-84-build.patch
        https://gitlab.com/inkscape/inkscape/-/commit/40f5b15b7e29908b79c54e81db6f340936102e08.patch
        https://gitlab.com/inkscape/inkscape/-/commit/ce52c5f96106ae5747171663a46831f21aa52d95.patch
        https://gitlab.com/inkscape/inkscape/-/commit/b60d81745016b5f20d4c6aec6d073b8a6f3e499c.patch
        009-popper-15-10.patch)
sha256sums=('2000530c7917e5260c9e8575a7154ff6926643d2006487d714e304a963f0c782'
            '92387251c1740f1a57cde9e587cc673ef1600813617bd2c9db65598c324a24d4'
            '131b2e1190637df0554ef1ee8cf46440689584375c117d057ab47d5871c58128'
            'c49d38b7c1a66e4b40e1b75fb055b730eb863a67a74d899da6d4d6db6baedcec'
            '776c6bae0eb646313f30e1945438f678e19edc2ea5dcd1851ca89cd4d01d2fd9'
            'a789592ad19ee26c0085145095adf06a5e3243fc2ee3f3de4a3e0cb2e3933609'
            '949cf3a3d5dfb007101900902e07859e99e593be64027597a4d2f3b52b3b6c91'
            'cda1f99901c9bdaad736c6bbf171012f39d00f4134efd0b6c49b499f8fd6c102'
            '30e9c60bf519a7e35e17fb884f8d9e5ab49409beffad7a3ddeefd1e1666452ba')
noextract=("${_realname}-${pkgver}.tar.xz")

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

prepare() {
  # Symlinks only in packaging/macos and src/3rdparty/cairo folders
  tar -xf ${_realname}-${pkgver}.tar.xz || true

  mv "${srcdir}"/${_realname}-${pkgver}_* "${srcdir}"/${_realname}-${pkgver}
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    inkscape-1.0.1-unbundle.patch \
    inkscape-1.0.1-install-layout.patch \
    007-no-console-clang.patch \
  
  # https://gitlab.com/inkscape/inkscape/-/issues/5615
  apply_patch_with_msg \
    008-fix-glib-2-84-build.patch

  # https://gitlab.com/inkscape/inkscape/-/issues/5954
  apply_patch_with_msg 009-popper-15-10.patch

  apply_patch_with_msg \
    40f5b15b7e29908b79c54e81db6f340936102e08.patch \
    ce52c5f96106ae5747171663a46831f21aa52d95.patch \
    b60d81745016b5f20d4c6aec6d073b8a6f3e499c.patch
}

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

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  CFLAGS+=" -Wno-implicit-function-declaration" \
  MSYS=winsymlinks:native \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_TESTING=OFF \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -DCMAKE_AR=${MINGW_PREFIX}/bin/ar \
    -DWITH_IMAGE_MAGICK=OFF \
    -DWITH_INTERNAL_2GEOM=OFF \
    ../${_realname}-${pkgver}

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

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

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
