# 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.

_wx_basever=3.2

_realname=kicad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-meta")
pkgver=9.0.2
pkgrel=2
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=(any)
mingw_arch=('ucrt64' 'clang64')
url="https://www.kicad.org/"
msys2_repository_url="https://gitlab.com/kicad/code/kicad"
msys2_references=(
  'archlinux: kicad'
  "cpe: cpe:/a:kicad:eda"
  "cpe: cpe:/a:kicad:kicad_eda"
)
license=("spdx:GPL-3.0-or-later")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-abseil-cpp"
  "${MINGW_PACKAGE_PREFIX}-cairo"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-fmt"
  "${MINGW_PACKAGE_PREFIX}-freeglut"
  "${MINGW_PACKAGE_PREFIX}-glew"
  "${MINGW_PACKAGE_PREFIX}-glm"
  "${MINGW_PACKAGE_PREFIX}-nng"
  "${MINGW_PACKAGE_PREFIX}-protobuf"
  "${MINGW_PACKAGE_PREFIX}-kicad-footprints"
  "${MINGW_PACKAGE_PREFIX}-kicad-symbols"
  "${MINGW_PACKAGE_PREFIX}-libgit2"
  "${MINGW_PACKAGE_PREFIX}-ngspice"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-opencascade"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-wxPython"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets${_wx_basever}-msw-libs"
  "${MINGW_PACKAGE_PREFIX}-zlib"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${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'
  '004-fix-loading-ngspice-dll.patch'
  '006-ki-6.0-rewrite-kiwin32_rc_for_clang.patch'
  '007-llvm-libcxx-19.patch'
  '009-clang-needs-explicit-json-implementations.patch'
)
sha256sums=('a184642b085f446e1709e373ea2b12872295ad5b3cc2ef894af155928d7f8e0d'
            '2924a86849c02aecd21cded0bd2069353fca33c3364f9b41f9bfdd80e19085cf'
            'd8d5f4bdd0aa6d8a907710c523f6f95840636cb2ef69e5275c6ed4966f134353'
            'f35a96c2393c21c266dbcd42616df64f9ee13b2423478bf6de029a3ad4e0ee8a'
            '3155b9515ec7c094221441ce337c566c346bf76bb7aa42e86660cfdfb599e307'
            '5531acad48988772e4453f42ac749f4001170e4be892d3f224b37e0e606ab5ab'
            '7523f89100db3dd594c10ab76ec002359c4505c7fa6f7de3e2eb71acd651a242')

# 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 \
    004-fix-loading-ngspice-dll.patch \
    006-ki-6.0-rewrite-kiwin32_rc_for_clang.patch \
    007-llvm-libcxx-19.patch \
    009-clang-needs-explicit-json-implementations.patch
}

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

  export GIT_DIR=/dev/null

  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_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_PREFIX_PATH="${MINGW_PREFIX}" \
      -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}" \
      -DNGSPICE_ROOT_DIR=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}

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

package_kicad() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  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
}

package_kicad-meta() {
  pkgdesc="Meta package for KiCad containing the core application and support libraries (mingw-w64)"

  optdepends=()
  replaces=()
  conflicts=()
  provides=()
  depends=(${MINGW_PACKAGE_PREFIX}-${_realname}
           ${MINGW_PACKAGE_PREFIX}-${_realname}-footprints
           ${MINGW_PACKAGE_PREFIX}-${_realname}-symbols
           ${MINGW_PACKAGE_PREFIX}-${_realname}-templates
           ${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D)
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
