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

_realname=Pillow
pkgbase=mingw-w64-python-pillow
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-pillow" "${MINGW_PACKAGE_PREFIX}-python3-pillow")
pkgver=5.3.0
pkgrel=1
arch=('any')
license=('custom')
url="https://github.com/python-pillow/Pillow/"
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-libraqm"
             "${MINGW_PACKAGE_PREFIX}-tk")
optdepends=("${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-freeze-support.patch
        002-shared-openjpeg.patch
        dlopen-fix.patch)
sha256sums=('9de4853d8b640c0185665735a1c3266b6d51636b3a3d8e3ea7876c485194b6e1'
            '6865da61534a45645fff71e139226bbce00c636a5f82ef824d13ad5a22efaac5'
            '581387337db4c6c0a7dae05d6462da92f47e822bd6b482a7a4e7c8637ca7e9f2'
            '46d7a5c146ec4cc7d0f359f0875570132ce70fec4836281976da586478a09258')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f ${_fname} ] || [ -d ${_fname} ]; then
      rm -rf ${_fname}
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  apply_patch_with_msg 001-freeze-support.patch \
    002-shared-openjpeg.patch \
    dlopen-fix.patch
  cd ${srcdir}
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH}| true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

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

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

#setup.py test
  done  
}

package_python3-pillow() {
  pkgdesc="Python Imaging Library (PIL) fork Python3 version (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-python3"
            "${MINGW_PACKAGE_PREFIX}-python3-olefile")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  optdepends=("${MINGW_PACKAGE_PREFIX}-tk: for the ImageTK module")

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

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_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
}

package_python2-pillow() {
  pkgdesc="Python Imaging Library (PIL) fork. Python2 version (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-python2"
            "${MINGW_PACKAGE_PREFIX}-python2-olefile")
  provides=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
  optdepends=("${MINGW_PACKAGE_PREFIX}-tk: for the ImageTK module")
  
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  local _py2basever=$(${MINGW_PREFIX}/bin/python2 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install \
    --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}/" \
    --optimize=0 --skip-build

  sed -i 's|/usr/local/bin/python$|/usr/bin/env python2|' \
    "${pkgdir}${MINGW_PREFIX}/lib/python${_py2basever}/site-packages/PIL/OleFileIO.py"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  install -dm755 "${pkgdir}${MINGW_PREFIX}/include/python${_py2basever}/"
  install -m644 -t "${pkgdir}${MINGW_PREFIX}/include/python${_py2basever}/" 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}2"
#  done
}

package_mingw-w64-i686-python2-pillow() {
  package_python2-pillow
}

package_mingw-w64-i686-python3-pillow() {
  package_python3-pillow
}

package_mingw-w64-x86_64-python2-pillow() {
  package_python2-pillow
}

package_mingw-w64-x86_64-python3-pillow() {
  package_python3-pillow
}
