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

_realname=gimp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_basever=2.10.8
_prever=
pkgver=${_basever}${_prever//-/}
pkgrel=3
pkgdesc="GNU Image Manipulation Program (mingw-w64)"
arch=('any')
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}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${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}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python2-pygtk"
         "${MINGW_PACKAGE_PREFIX}-python2-gobject"
         #"${MINGW_PACKAGE_PREFIX}-webkitgtk2"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-iso-codes"
             "intltool")
options=('strip' 'makeflags')
install=gimp-${CARCH}.install
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)
sha256sums=('d849c1cf35244938ae82e521b92b720ab48b8e9ed092d5de92c2464ef5244b9b'
            'f7f9badc547f1a0317b95e012530db8df63b7c7038c9d9ff8d0b510c86f43439'
            '6dae330b6cf6e7fd7ca7fc5fa95db1777323ce7cee3650eee27814ac70ecea4d'
            '682656d1f7ec70bfd3dd10eba3f8db8abb75c80e44933f8c09c730cea5aeb549'
            'e7cc5603c51517add367d8062eb3a28d5b6c51eded6cb51f749c4570f27b91a7'
            '01651bb582b2120aae67752f6a28c0750ea690d46259d855e28f8b18da567bf5')

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

  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
}
