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

_realname=gimp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_basever=2.10.24
_prever=
pkgver=${_basever}${_prever//-/}
pkgrel=2
pkgdesc="GNU Image Manipulation Program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.gimp.org/"
license=('GPL' 'LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-babl"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-gegl"
         "${MINGW_PACKAGE_PREFIX}-gexiv2"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-glib-networking"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-iso-codes"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libmypaint"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-mypaint-brushes"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python2-pygtk"
         "${MINGW_PACKAGE_PREFIX}-python2-gobject2"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pygobject2-devel"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "intltool")
options=('strip' 'makeflags')
source=(https://download.gimp.org/pub/gimp/v${_basever%.*}/${_realname}-${_basever}${_prever}.tar.bz2
        0001-find-directx-fix.patch
        0002-python-m4.patch
        0003-mingw-add-ModuleFileName-dir-to-PATH.patch
        0004-replace-pygtk-codegen.patch
        0005-fix-link-python-plugins.patch
        0006-ucrt-fixes.patch)
sha256sums=('bd1bb762368c0dd3175cf05006812dd676949c3707e21f4e6857435cb435989e'
            'f7f9badc547f1a0317b95e012530db8df63b7c7038c9d9ff8d0b510c86f43439'
            'ae9b6e8fa40fd471cb58f17454219abf531565b5fbaf616e5fc81b3869bbc4b5'
            '682656d1f7ec70bfd3dd10eba3f8db8abb75c80e44933f8c09c730cea5aeb549'
            '98b6212e669eb3a93877514ee6d3284cb3a44ae6d57b6ecb29de3a076db3dbd2'
            '01651bb582b2120aae67752f6a28c0750ea690d46259d855e28f8b18da567bf5'
            '8efdbe7a3c36076d0be870c74c2ff9f5d14193b081f216365a6612cc8221c2c8')

prepare() {
  cd "${srcdir}/${_realname}-${_basever}${_prever}"
  patch -p1 -i ${srcdir}/0001-find-directx-fix.patch
  patch -p1 -i ${srcdir}/0002-python-m4.patch
  patch -p1 -i ${srcdir}/0003-mingw-add-ModuleFileName-dir-to-PATH.patch
  patch -p1 -i ${srcdir}/0004-replace-pygtk-codegen.patch
  patch -p1 -i ${srcdir}/0005-fix-link-python-plugins.patch
  patch -p1 -i ${srcdir}/0006-ucrt-fixes.patch

  rm -fr autom4te.cache
  libtoolize -c
  aclocal -I m4macros
  automake -a -c
  autoheader
  autoconf

  #autoreconf -fi -I m4macros
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  cp -rf ${_realname}-${_basever}${_prever} build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  PYTHON=${MINGW_PREFIX}/bin/python2 \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --enable-mp \
    --enable-gimp-console \
    --enable-python \
    --without-aa \
    --with-pdbgen \
    --with-directx-sdk=${MINGW_PREFIX}/${MINGW_CHOST} \
    --without-x \
    --without-webkit

  # V=1 because of a race condition (a generated 'bin2c' type
  # file ending up truncated once). This might avoid it.
  make V=1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  ln -s gimptool-2.0 "${pkgdir}${MINGW_PREFIX}/bin/gimptool.exe"
  sed -e "s|${MINGW_PREFIX}/bin/python2|python2w.exe|g" -i ${pkgdir}${MINGW_PREFIX}/lib/gimp/2.0/interpreters/pygimp.interp
  rm -f "${pkgdir}${MINGW_PREFIX}/lib/*.def"
  rm -f "${pkgdir}${MINGW_PREFIX}/lib/gimp/2.0/modules/*.dll.a"

  cp ${pkgdir}${MINGW_PREFIX}/bin/gimp-${pkgver%.*}.exe ${pkgdir}${MINGW_PREFIX}/bin/gimp.exe
}
