# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

# Remember to update mingw-w64-kicad-doc and mingw-w64-kicad-library when
# updating this build rule.

_realname=kicad
_wx_basever=3.2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.0.8
pkgrel=1
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64') # 'mingw32' 'clang32')
url="https://www.kicad.org/"
license=("spdx:GPL-3.0-or-later")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
options=('strip' '!debug')
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
depends=(
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-glew"
  "${MINGW_PACKAGE_PREFIX}-glm"
  "${MINGW_PACKAGE_PREFIX}-ngspice"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-wxPython"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-freeglut"
  "${MINGW_PACKAGE_PREFIX}-zlib"
  $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-opencascade")
  "${MINGW_PACKAGE_PREFIX}-cairo"
  "${MINGW_PACKAGE_PREFIX}-kicad-footprints"
  "${MINGW_PACKAGE_PREFIX}-kicad-symbols"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-doxygen"
  "${MINGW_PACKAGE_PREFIX}-swig"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw"
  "git"
)
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-kicad-templates: for template libraries"
  "${MINGW_PACKAGE_PREFIX}-kicad-packages3D: for 3D model libraries"
)
_doc=("ca" "de" "en" "es" "fr" "id" "it" "ja" "nl" "pl" "ru" "zh")
for _doclang in ${_doc[@]}; do
  optdepends+=("${MINGW_PACKAGE_PREFIX}-${_realname}-doc-${_doclang}: for documentation (${_doclang})");
done
source=(
  "https://gitlab.com/kicad/code/kicad/-/archive/${pkgver}/kicad-${pkgver}.tar.bz2"
  '002-ki-6.0-cmake-fixes-for-MINGW-CLANG.patch'
  '003-ki-6.0-code-fixes-for-GNUC-CLANG.patch'
  '006-ki-6.0-rewrite-kiwin32_rc_for_clang.patch'
  # This is not a patch to use with standard production builds
  '007-ki-6.0-manifest-remove-win10-11-manifest-support.patch'
)
sha256sums=('a97b3a831e7d0a2f3b54e2dd4258fe42bb0657e1ddb8c9aa2cc3f8e3b85a002d'
            '2924a86849c02aecd21cded0bd2069353fca33c3364f9b41f9bfdd80e19085cf'
            'd8d5f4bdd0aa6d8a907710c523f6f95840636cb2ef69e5275c6ed4966f134353'
            'e03dbb58409145c8fb54991d8259f14bc0ba6b21abca24f9b914ec354c9df89c'
            '550397b8d2ba66b924933020e409e868d3e4b2352a235f329798bc631b9fd28f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    002-ki-6.0-cmake-fixes-for-MINGW-CLANG.patch \
    003-ki-6.0-code-fixes-for-GNUC-CLANG.patch \
    006-ki-6.0-rewrite-kiwin32_rc_for_clang.patch
  # This patch is needed for old Intel GPU drivers and should not be
  # used for the production version of the package
  #apply_patch_with_msg \
    #007-ki-6.0-manifest-remove-win10-11-manifest-support.patch
}

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DCMAKE_PREFIX_PATH=;DEFAULT_INSTALL_PATH=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_PREFIX_PATH="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DDEFAULT_INSTALL_PATH="${MINGW_PREFIX}" \
      -DwxWidgets_CONFIG_EXECUTABLE="${MINGW_PREFIX}"/bin/wx-config-${_wx_basever} \
      -DOCC_INCLUDE_DIR="${MINGW_PREFIX}"/include/opencascade \
      -DKICAD_SCRIPTING_WXPYTHON=ON \
      -DKICAD_BUILD_I18N=ON \
      -DKICAD_BUILD_QA_TESTS=OFF \
      -DPYTHON_EXECUTABLE="${MINGW_PREFIX}"/bin/python.exe \
      -DPYTHON_INCLUDE_DIR=$("${MINGW_PREFIX}"/bin/python.exe -c "from sysconfig import get_paths as gp; print(gp()[\"include\"])") \
      -DPYTHON_ROOT_DIR="${MINGW_PREFIX}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build . # --parallel 1 --verbose
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install . # --parallel 1 --verbose

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.README" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.README"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.GPLv3" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.GPLv3"
}
