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

_realname=cppcheck
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.18.3
pkgrel=1
pkgdesc="static analysis of C/C++ code (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://cppcheck.sourceforge.io/"
msys2_repository_url="https://github.com/danmar/cppcheck"
msys2_references=(
  'archlinux: cppcheck'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-pcre")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-charts"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-tinyxml2")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-pygments: cppcheck-htmlreport"
            "${MINGW_PACKAGE_PREFIX}-qt6-charts: cppcheck-gui"
            "${MINGW_PACKAGE_PREFIX}-qt6-tools: cppcheck-gui")
source=("https://github.com/danmar/cppcheck/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        '001-cppcheck-2.2-change-language-files-path.patch'
        '002-cppcheck-2.2-change-cfg-path.patch')
sha256sums=('e37c94e190cdddc65682649b02b72939761585bddd8ada595f922e190a26a2be'
            'e741bfafbeb3ade454a42563a1c4617ae3dc87fd6a6f409e1ae4b735b68997d3'
            'f528fd133a2a7ffd159ea130074195d64a50035cdf7c025b08bd74b55c8bb169')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

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

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "y"; then
    _extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    _extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DFILESDIR=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUSE_MATCHCOMPILER=ON \
    -DFILESDIR=${MINGW_PREFIX}/share/cppcheck \
    -DHAVE_RULES=ON \
    -DBUILD_GUI=ON \
    -DWITH_QCHART=ON \
    -DUSE_BUNDLED_TINYXML2=OFF \
    -DUSE_QT6="ON" \
    "${_extra_config[@]}" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  # man
  cd "${srcdir}"/${_realname}-${pkgver}
  make man \
    DB2MAN=${MINGW_PREFIX}/share/xml/docbook/xsl-stylesheets-1.79.2/manpages/docbook.xsl \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck
  # help
  ${MINGW_PREFIX}/share/qt6/bin/qhelpgenerator gui/help/online-help.qhcp -o gui/help/online-help.qhc
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cmake -DBUILD_TESTS=ON ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  cd bin && ./testrunner -g -q
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  # Fix location of language files
  install -d "${pkgdir}"${MINGW_PREFIX}/share/${_realname}/lang
  mv "${pkgdir}"${MINGW_PREFIX}/bin/cppcheck_*.qm "${pkgdir}"${MINGW_PREFIX}/share/${_realname}/lang/
  # Remove freedesktop (Linux) files
  rm -r "${pkgdir}"${MINGW_PREFIX}/share/applications
  rm -r "${pkgdir}"${MINGW_PREFIX}/share/icons

  cd "${srcdir}"/${_realname}-${pkgver}
  # 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"

  # Install extra files
  install -Dpm644 htmlreport/cppcheck-htmlreport "${pkgdir}${MINGW_PREFIX}/bin/cppcheck-htmlreport"
  install -d "${pkgdir}${MINGW_PREFIX}/share/cppcheck/help"
  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"
}
