# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=imagecodecs
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2021.6.8
pkgrel=1
pkgdesc="Image transformation, compression, and decompression codecs (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://github.com/cgohlke/imagecodecs'
license=('BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-blosc"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-charls"
         "${MINGW_PACKAGE_PREFIX}-jxrlib"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-libavif"
         "${MINGW_PACKAGE_PREFIX}-libdeflate"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zopfli")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/cgohlke/${_realname}/archive/v${pkgver}.tar.gz")
sha256sums=('f9da8c53f8b7400a46298093ec71a44758e860fe9dcba9f48434ebbacfdb7817')

prepare() {
  cd "${srcdir}"

  rm -rf python-build-${MINGW_ARCH} || true
  cp -r "${_realname}-${pkgver}" "python-build-${MINGW_ARCH}"
}

build() {
  msg "Python build for ${MINGW_ARCH}"  
  cd "${srcdir}/python-build-${MINGW_ARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${MINGW_ARCH}"
  cd "${srcdir}/python-build-${MINGW_ARCH}"
  ${MINGW_PREFIX}/bin/python setup.py check --strict
}

package() {
  cd "${srcdir}/python-build-${MINGW_ARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -Dm644 3rdparty/bitshuffle/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-bitshuffle"
  install -Dm644 3rdparty/cfitsio/License.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-cfitsio"
  install -Dm644 3rdparty/liblj92/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-liblj92"
  install -Dm644 3rdparty/liblzf/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-liblzf"
  install -Dm644 3rdparty/postgresql/COPYRIGHT "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-postgresql"

  # remove shebang line
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done
}
