# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=jsonschema
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.6.0
pkgrel=5
pkgdesc="An implementation of JSON Schema validation for Python (mingw-w64)"
arch=('any')
url="https://pypi.python.org/pypi/jsonschema"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-functools32")
_dtoken="58/b9/171dbb07e18c6346090a37f03c7e74410a1a56123f847efed59af260a298"
source=("https://pypi.python.org/packages/${_dtoken}/${_realname}-${pkgver}.tar.gz")
sha256sums=('6ff5f3180870836cae40f06fa10419f557208175f13ad7bc26caa77beb1f6e02')

all_build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/${1} setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete
}

package_python3-jsonschema() {
  local interpreter=python3
  depends=("${MINGW_PACKAGE_PREFIX}-python3")
  install=${_realname}3-${CARCH}.install

  all_build ${interpreter}

  install -D -m644 json/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE
  
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}

package_python2-jsonschema() {
  local interpreter=python2
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-functools32")
  install=${_realname}2-${CARCH}.install

  all_build ${interpreter}

  install -D -m644 json/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE

# This entire section should be removed if the package does NOT install
# anything in the /mingw*/bin directory. 
### begin section ###
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

# for Python2 packages, you want to rename some stuff from the bin directory 
# with the 2 suffix like in the mingw-w64-python-pygments package to avoid
# conflicts when installing both the Python2 and Python3 packages
  for f in jsonschema; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done
#### end section ####

}

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

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

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

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