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

_realname=pylint
pkgbase=mingw-w64-python3-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.5.0
pkgrel=1
pkgdesc="Analyzes Python code looking for bugs and signs of poor quality (mingw-w64)"
arch=('any')
url='https://pylint.org/'
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-python-astroid"
         "${MINGW_PACKAGE_PREFIX}-python-colorama"
         "${MINGW_PACKAGE_PREFIX}-python-mccabe"
         "${MINGW_PACKAGE_PREFIX}-python-isort")
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-astroid"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-six")
checkdepends=("${MINGW_PACKAGE_PREFIX}-mpdecimal"
              "${MINGW_PACKAGE_PREFIX}-python-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')
install=${_realname}3-${CARCH}.install
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/PyCQA/pylint/archive/pylint-${pkgver}.tar.gz")
sha512sums=('9c010dce76ec1f07d0942965f65c8a22a79cc647f93448c19e5729837d3ac7910bc4e78b86a65f193e66750a6da924e9a5a46d977dcf168022609d7564748f14')

# 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}"
    sed -e "s|/usr/bin/python$|${MINGW_PREFIX}/bin/python|" -i pylint/epylint.py pylint/__main__.py tests/input/noext tests/data/ascript
  popd
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${_realname}-${pkgver}" "python-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

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

package() {
  cd "${srcdir}/python-build-${CARCH}"
  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 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"

  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
}
