# Maintainer: Yusuke Sasaki <yusuke.sasaki.nuem@gmail.com>

_realname=cppcheck
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.3
pkgrel=1
pkgdesc="static analysis of C/C++ code (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://cppcheck.sourceforge.io/"
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-pcre")
makedepends=("${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5: cppcheck-gui"
            "${MINGW_PACKAGE_PREFIX}-python-pygments: cppcheck-htmlreport")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/danmar/cppcheck/archive/${pkgver}.tar.gz"
        '001-cppcheck-2.2-change-language-files-path.patch'
        '002-cppcheck-2.2-change-cfg-path.patch')
sha256sums=('3c622628ebf46f68909c7a96743b07b6207894a0772fbe802603732152ab1035'
            'e741bfafbeb3ade454a42563a1c4617ae3dc87fd6a6f409e1ae4b735b68997d3'
            '6f53ad14361717b3f994033483c14ad35c5790f703e03fe8aebaca83936ea61b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/001-cppcheck-2.2-change-language-files-path.patch"
  patch -p1 -i "${srcdir}/002-cppcheck-2.2-change-cfg-path.patch"
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  
  LANG='en_US.UTF-8' make \
    PREFIX=${MINGW_PREFIX} \
    CXX=${MINGW_PREFIX}/bin/g++ \
    LD=${MINGW_PREFIX}/bin/ld \
    MATCHCOMPILER=yes \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck \
    HAVE_RULES=yes \
    LDFLAGS=-lshlwapi RDYNAMIC=

  make man \
    DB2MAN=${MINGW_PREFIX}/share/xml/docbook/xsl-stylesheets-1.79.2/manpages/docbook.xsl \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck

  cd gui
  lrelease gui.pro
  ${MINGW_PREFIX}/bin/qmake HAVE_RULES=yes HAVE_QCHART=yes CONFIG+=release
  make \
    CXX=${MINGW_PREFIX}/bin/g++ \
    LD=${MINGW_PREFIX}/bin/ld \
    PREFIX=${MINGW_PREFIX} \
    MATCHCOMPILER=yes \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck \
    HAVE_RULES=yes \
    HAVE_QCHART=yes
}

check() {
  cd "${srcdir}"/build-${CARCH}
   LANG='en_US.UTF-8' make test \
     MATCHCOMPILER=yes \
     CXX=${MINGW_PREFIX}/bin/g++ \
     LD=${MINGW_PREFIX}/bin/ld \
     PREFIX="${MINGW_PREFIX}" \
     FILESDIR=${MINGW_PREFIX}/share/cppcheck \
     HAVE_RULES=yes \
     HAVE_QCHART=yes \
     LDFLAGS=-lshlwapi RDYNAMIC=
}

package() {
  cd "${srcdir}"/build-${CARCH}

  LANG='en_US.UTF-8' make install \
    DESTDIR="${pkgdir}" \
    CXX=${MINGW_PREFIX}/bin/g++ \
    LD=${MINGW_PREFIX}/bin/ld \
    PREFIX="${MINGW_PREFIX}" \
    MATCHCOMPILER=yes \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck \
    HAVE_QCHART=yes \
    HAVE_RULES=yes

  # man and COPYING
  install -Dpm644 cppcheck.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/cppcheck.1"
  install -Dpm644 COPYING    "${pkgdir}${MINGW_PREFIX}/share/licenses/cppcheck/COPYING"

  # GUI
  install -m755 build/gui/cppcheck-gui.exe "${pkgdir}${MINGW_PREFIX}/bin"
  install -d "${pkgdir}${MINGW_PREFIX}/share/cppcheck/cfg"
  install -d "${pkgdir}${MINGW_PREFIX}/share/cppcheck/help"
  install -d "${pkgdir}${MINGW_PREFIX}/share/cppcheck/lang"
  install -D cfg/* -t "${pkgdir}${MINGW_PREFIX}/share/cppcheck/cfg"
  install -D gui/help/online-help.qch -t "${pkgdir}${MINGW_PREFIX}/share/cppcheck/help"
  install -D gui/help/online-help.qhc -t "${pkgdir}${MINGW_PREFIX}/share/cppcheck/help"
  install -D gui/*.qm -t "${pkgdir}${MINGW_PREFIX}/share/cppcheck/lang"
}
