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

_realname=Pillow
pkgbase=mingw-w64-python-pillow
pkgname=("${MINGW_PACKAGE_PREFIX}-python-pillow")
pkgver=7.2.0
pkgrel=1
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-python3-pillow=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python3-pillow")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-python3-pillow")
arch=('any')
license=('custom')
url="https://github.com/python-pillow/Pillow/"
pkgdesc="Python Imaging Library (PIL) fork Python3 version (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libimagequant"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-olefile"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-libraqm"
             "${MINGW_PACKAGE_PREFIX}-tk")
optdepends=("${MINGW_PACKAGE_PREFIX}-tk: for the ImageTK module"
            "${MINGW_PACKAGE_PREFIX}-libraqm: complex text layout support")
options=('staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/python-pillow/Pillow/archive/${pkgver}.tar.gz
        001-libtiff-fileio.patch
        002-fix-photoimage-crash.patch
        003-remove-hardcoded-include-path.patch)
sha256sums=('0fd19382a1d76d3296f333223ed9429d2110cc0b44ad20b10c5f6255ce9a3bf4'
            'daed469b7ba87ee03830331d79492364966981effe6cf4c71c731a868628adc2'
            '1ba2504ab80b0078fbb92b63775ff9e7ef2e637253d6999ace4c023c112c796c'
            'b5067befed4fc347106084f60e5b553c299739dc82fa13915e4480fc1ae19739')

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

  patch -Np1 -i "${srcdir}"/001-libtiff-fileio.patch
  patch -Np1 -i "${srcdir}"/002-fix-photoimage-crash.patch
  patch -Np1 -i "${srcdir}"/003-remove-hardcoded-include-path.patch

  cd ${srcdir}
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}"
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python selftest.py || true

  #setup.py test
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  local _py3basever=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install \
   --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}/" \
   --optimize=0 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -dm755 "${pkgdir}${MINGW_PREFIX}/include/python${_py3basever}m/"
  install -m644 -t "${pkgdir}${MINGW_PREFIX}/include/python${_py3basever}m/" src/libImaging/*.h

  # clean up bins
  #  cd "${pkgdir}${MINGW_PREFIX}/bin"
  #  for f in *.py; do
  #    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${f}
  #    mv "$f" "${f%.py}"
  #  done
}
