# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>.

_realname=netaddr
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.7.19
pkgrel=1
pkgdesc="Pure Python network address representation and manipulation library (mingw-w64)"
arch=('any')
url='https://github.com/drkjam/netaddr'
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python2-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-pytest"
              "${MINGW_PACKAGE_PREFIX}-python2-pytest")
options=('staticlibs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/drkjam/${_realname}/archive/${_realname}-${pkgver}.tar.gz
        fix-${pkgver}-python3.patch
        pep479-return-instead-of-raise-StopIteration.patch)
sha512sums=('f07fb4de08aef4925dbc429b4ee93b25855273f083405d4e763c2fbc996ee35b09b87b10117040b073d45ac9f51512083116883ff48216e68aa1e429f83d5278'
            '8db4e6cbaf03b95092561dfcf881616920da2ab45f205d8bd29f1f6e9e7681a7d591c0a52dbdb772a41cac0b42d643c2dafcad22a659524a35cb6b06dd98d38d'
            'ff4b46184cc98c360ba377a72ead48a64fd78477080c7061652f79dc44d7b6803111cee885502b7d98e08d66d440e7b9afd65faa97674b2c487a2ba78e684e9d')


# 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}"
  pushd "${_realname}-${_realname}-${pkgver}"
    sed -r 's|#!/usr/bin/env python||g' -i netaddr/eui/ieee.py netaddr/ip/iana.py
    apply_patch_with_msg  "fix-${pkgver}-python3.patch" \
      "pep479-return-instead-of-raise-StopIteration.patch"
  popd 
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${_realname}-${pkgver}" "${builddir}"
  done
  # 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() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
    cd docs
    make  text
#   make man
  done  
}

check() {
    msg "Python 2 test for ${CARCH}"
    cd "${srcdir}/python2-build-${CARCH}"
    LC_ALL='en_US.UTF-8' ${MINGW_PREFIX}/bin/py.test2
    msg "Python 3 test for ${CARCH}"
    cd "${srcdir}/python3-build-${CARCH}"
    LC_ALL='en_US.UTF-8' ${MINGW_PREFIX}/bin/py.test
}

package_python3-netaddr() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python3-ipython: netaddr interractive command support")

  #This package install is needed for .fixups with .EXE's 
  #in the bit directory.  The install files "python-exe-installs"
  #and should be renamed to your _realname .
  install=${_realname}3-${CARCH}.install

  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=1 --skip-build

  install -Dm 644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
  install -Dm 644 README.md "${pkgdir}${MINGW_PREFIX}/share/doc/python3-${_realname}/README.md"
  install -Dm 644 docs/build/text/* -t "${pkgdir}${MINGW_PREFIX}/share/doc/python3-${_realname}/"
#  install -Dm 644 docs/build/man/netaddr.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/netaddr.1"

# 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/*; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}/bin/|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
#### end section ####
}

package_python2-netaddr() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python3-ipython: netaddr interractive command supprt")
  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 -Dm 644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
  install -Dm 644 README.md "${pkgdir}${MINGW_PREFIX}/share/doc/python2-${_realname}/README.md"
  install -Dm 644 docs/build/text/* -t "${pkgdir}${MINGW_PREFIX}/share/doc/python2-${_realname}/"
#  install -Dm 644 docs/build/man/netaddr.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/netaddr.1"

# 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/*; do
    sed -e "s|python[^3]|python|" \
        -e "s|/usr/bin/env |${MINGW_PREFIX}/bin/|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}/|g" -i ${_f}
  done

  for f in netaddr; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}
  done
#### end section ####
}

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

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

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

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