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

_realname=pylint
pkgbase=mingw-w64-python3-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.2.2
pkgrel=1
pkgdesc="Analyzes Python code looking for bugs and signs of poor quality (mingw-w64)"
arch=('any')
url='http://pylint.org/'
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-python3-astroid"
         "${MINGW_PACKAGE_PREFIX}-python3-colorama"
         "${MINGW_PACKAGE_PREFIX}-python3-mccabe" 
         "${MINGW_PACKAGE_PREFIX}-python3-isort")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-astroid"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-six")
checkdepends=("${MINGW_PACKAGE_PREFIX}-mpdecimal"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest-runner")
optdepends=("${MINGW_PACKAGE_PREFIX}-tk: Pylint GUI"
            "${MINGW_PACKAGE_PREFIX}-graphviz: To have other output formats than dot or vcg")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-$pkgver.tar.gz"::"https://github.com/PyCQA/pylint/archive/pylint-$pkgver.tar.gz")
sha512sums=('5c581c6719e7d8c5d7ffe8c252144b327fc1f4875bc43ba8f2aa14f6176f50b58ac0afa511fb34ff1db2d56764f004809b04dbbc012b5ad7b55fee89dd9b9bc5')

# 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}"
 
  sed -i 's/pytest<3.3/pytest/' pylint-pylint-$pkgver/setup.py
  pushd "${_realname}-${_realname}-${pkgver}/pylint"
    sed -e "s|/usr/bin/env python|/usr/bin/env python3|" -e "s|/usr/bin/python$|/usr/bin/python3|" -i epylint.py __main__.py test/input/noext test/data/ascript
  popd
  rm -rf python3-build-${CARCH} | true
  cp -r "${_realname}-${_realname}-${pkgver}" "python3-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 3 build for ${CARCH}"  
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py build
}

check() {
  msg "Python 3 test for ${CARCH}"
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py pytest || warning "tests failed"
}


package() {
  #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 -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_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|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
