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

_realname=pylint
pkgbase=mingw-w64-python2-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=1.9.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}-python2-astroid"
         "${MINGW_PACKAGE_PREFIX}-python2-backports.functools_lru_cache"
         "${MINGW_PACKAGE_PREFIX}-python2-colorama"
         "${MINGW_PACKAGE_PREFIX}-python2-configparser"
         "${MINGW_PACKAGE_PREFIX}-python2-isort"
         "${MINGW_PACKAGE_PREFIX}-python2-mccabe" 
         "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python2-singledispatch"
         "${MINGW_PACKAGE_PREFIX}-python2-six")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-astroid"
             "${MINGW_PACKAGE_PREFIX}-python2-backports.functools_lru_cache"
             "${MINGW_PACKAGE_PREFIX}-python2-colorama"
             "${MINGW_PACKAGE_PREFIX}-python2-configparser"
             "${MINGW_PACKAGE_PREFIX}-python2-singledispatch"
             "${MINGW_PACKAGE_PREFIX}-python2-six")
checkdepends=("${MINGW_PACKAGE_PREFIX}-mpdecimal"
              "${MINGW_PACKAGE_PREFIX}-python2-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=('5b2f7f00c7ff2ca5e53a9bb616f314a8f1460ec1acd8123407bc73c23a26502d1adb6cffad8020f0c3d9964adf316929fd159c5720f019c5ce6aa8cdc791955d')

# 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}-${_realname}-${pkgver}" "python2-build-${CARCH}"

  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 python2|" -e "s|/usr/bin/python$|${MINGW_PREFIX}/bin/python2|" -i epylint.py __main__.py test/input/noext test/data/ascript
  popd

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

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

check() {
  msg "Python 2 test for ${CARCH}"
  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 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}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 COPYING "${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|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
     if [[ ${_f} != *"."* ]];then
      sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
     fi
  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 epylint pylint pyreverse symilar; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
    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
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}

  done
#### end section ####
}
