# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# 1.11.13 - This version supports both Python 2 and 3, but we want only the
# the package for Python 2 because I am providing a separate package for
# django 2.0.7 that does not support Python 2.

_realname=django
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=1.11.13
pkgrel=2
pkgdesc="A high-level Python Web framework that encourages rapid development and clean design (mingw-w64)"
arch=('any')
url='http://www.djangoproject.com/'
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-pytz"
             "${MINGW_PACKAGE_PREFIX}-python2-bcrypt"
             "${MINGW_PACKAGE_PREFIX}-python2-argon2_cffi"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
options=('staticlibs' 'strip' '!debug')
source=("Django-$pkgver.tar.gz"::"https://www.djangoproject.com/download/$pkgver/tarball/")
sha512sums=('9552b76f693d2e47681bc24cc8ee19c9c42e194a21e578f28d1705641b2490a1d9289d9870105fb480ebd3f7ad2746b1bee8382857c55ba632192d72426ada06')

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

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

prepare() {
  cd "${srcdir}"
  rm -rf python2-build-${CARCH}| true
  cp -r "${_realname}-${pkgver}" "python2-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {
    msg "Python 2 build for ${CARCH}"  
    cd "${srcdir}/python2-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python2 setup.py build
}

check() {
    msg "Python 2 test for ${CARCH}"
    cd "${srcdir}/python2-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python2 setup.py check
}

package() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-pytz")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-mysql: for MySQL backend"
              "${MINGW_PACKAGE_PREFIX}-python2-psycopg2: for PostgreSQL backend"
              "${MINGW_PACKAGE_PREFIX}-python2-bcrypt: for bcrypt support"
              "${MINGW_PACKAGE_PREFIX}-python2-argon2_cffi: for Argon2 support")
  install=${_realname}2-${CARCH}.install
  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=1 --skip-build

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

# 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|${PREFIX_WIN}/bin/|${MINGW_PREFIX}/bin/|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 django-admin; 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}.py
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done
#### end section ####
}

