# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=gimp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_pkgver=3.0.6
pkgver=${_pkgver}
pkgrel=2
pkgdesc="GNU Image Manipulation Program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gimp.org/downloads/devel/"
msys2_repository_url="https://gitlab.gnome.org/GNOME/gimp"
msys2_references=(
  'archlinux: gimp'
  'aur: gimp-devel'
  "cpe: cpe:/a:gimp:gimp"
)
msys2_ignore_vulnerabilities=(
  'CVE-2025-8672' # macOS-specific
)
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
provides=("${MINGW_PACKAGE_PREFIX}-gimp3")
conflicts=(
  "${MINGW_PACKAGE_PREFIX}-gimp3"
  "${MINGW_PACKAGE_PREFIX}-gimp2"
)
replaces=("${MINGW_PACKAGE_PREFIX}-gimp3")
depends=("${MINGW_PACKAGE_PREFIX}-appstream"
         #"${MINGW_PACKAGE_PREFIX}-aalib"
         "${MINGW_PACKAGE_PREFIX}-atk"
         "${MINGW_PACKAGE_PREFIX}-babl"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gegl"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-gexiv2"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-glib-networking"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libiff"
         "${MINGW_PACKAGE_PREFIX}-libilbm"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libjxl"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libmypaint"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         #"${MINGW_PACKAGE_PREFIX}-luajit"
         #"${MINGW_PACKAGE_PREFIX}-lua51-lgi"
         "${MINGW_PACKAGE_PREFIX}-mypaint-brushes"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-gobject"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-qoi"
             "${MINGW_PACKAGE_PREFIX}-vala")
source=("https://download.gimp.org/pub/gimp/v3.0/gimp-${_pkgver}.tar.xz")
sha256sums=('246c225383c72ef9f0dc7703b7d707084bbf177bd2900e94ce466a62862e296b')

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

prepare() {
  cd "gimp-${_pkgver}"
}

build() {
  CFLAGS+=" -Wno-incompatible-pointer-types" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --buildtype=plain \
    --auto-features=enabled \
    --prefix=${MINGW_PREFIX} \
    -Daa=disabled \
    -Dalsa=disabled \
    -Dappdata-test=disabled \
    -Dbug-report-url="https://github.com/msys2/MINGW-packages/issues" \
    -Dcheck-update=no \
    -Dgi-docgen=disabled \
    -Dgudev=disabled \
    -Dheadless-tests=disabled \
    -Djavascript=disabled \
    -Dlibbacktrace=false \
    -Dlibunwind=false \
    -Dlinux-input=disabled \
    -Dxcursor=disabled \
    -Dwin32-debug-console=disabled \
    "build-${MSYSTEM}" \
    "gimp-${_pkgver}"

  meson compile -C "build-${MSYSTEM}"
}

package() {
  meson install -C "build-${MSYSTEM}" --destdir="${pkgdir}"

  # Adjust the python interpreter path
  sed -e "s|\=.*|\=python3w.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/gimp/3.0/interpreters/pygimp.interp
  sed -e "s|::python3|::python3w.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/gimp/3.0/interpreters/pygimp.interp

  rm -f "${pkgdir}${MINGW_PREFIX}/lib/gimp/3.0/modules/*.dll.a"

  install -Dm644 "gimp-${_pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "gimp-${_pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
