# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=kicad
pkgbase=mingw-w64-${_realname}
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-${_realname}"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-meta"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-footprints"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-symbols"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-templates"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D"
)
pkgver=5.1.10
pkgrel=2
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="http://www.kicad-pcb.org"
license=("GPLv3+")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
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}-wxWidgets3.1"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-freeglut"
  "${MINGW_PACKAGE_PREFIX}-zlib"
)
makedepends=(
  "git"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-gcc"
  "${MINGW_PACKAGE_PREFIX}-doxygen"
  "${MINGW_PACKAGE_PREFIX}-make"
  "${MINGW_PACKAGE_PREFIX}-pkg-config"
  "${MINGW_PACKAGE_PREFIX}-swig"
  "${MINGW_PACKAGE_PREFIX}-cairo"
  "${MINGW_PACKAGE_PREFIX}-oce"
)
source=(
  "${_realname}-${pkgver}.tar.gz"::"https://gitlab.com/kicad/code/kicad/-/archive/${pkgver}/kicad-${pkgver}.tar.gz"
  "${_realname}-i18n-${pkgver}.tar.gz"::"https://gitlab.com/kicad/code/kicad-i18n/-/archive/${pkgver}/kicad-i18n-${pkgver}.tar.gz"
  "${_realname}-footprints-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-footprints/-/archive/${pkgver}/kicad-footprints-${pkgver}.tar.gz"
  "${_realname}-symbols-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-symbols/-/archive/${pkgver}/kicad-symbols-${pkgver}.tar.gz"
  "${_realname}-templates-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-templates/-/archive/${pkgver}/kicad-templates-${pkgver}.tar.gz"
  "${_realname}-packages3D-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-packages3D/-/archive/${pkgver}/kicad-packages3D-${pkgver}.tar.gz"
  '0001-fix-FindwxWidgets.cmake.patch'
)
sha256sums=(
  '296341f30ada2c7f4a1c54720eab425835a93349527d4bf08f73f280d19695ed'
  'f07e029717bfbf0b54c59cbf191e1b2b35cccd276c9fac454239b249e0924fdc'
  '65de50a9669e5eb0f2e4eed433802432eee2bd98df1e898905a7dc117cdb3050'
  '5e80c8420a563c76c884b2771792ce7ee69efbe67d11d9b025eeb16b75aee45e'
  'f2c44d6dc69fc3c7db05f57c5caf290721e0bba3fe686e4e3bc2765e3f314668'
  'b79f8de319a463d12972253a125ece118a8000533061d443d5730e1498822bff'
  '3fe6d1126dc3a4e3d720714fc08ca8d10771f3d38f2a2118b95f9ca41ace0c2d'
)

_doc=("ca" "de" "en" "es" "fr" "id" "it" "ja" "nl" "pl" "ru" "zh")
 _sub=("footprints" "symbols" "templates" "packages3D")

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-fix-FindwxWidgets.cmake.patch
}

build() {
  cd "${srcdir}"
  msg2 "Build kicad"
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DCMAKE_PREFIX_PATH=;DEFAULT_INSTALL_PATH=" \
  cmake \
    -G"MinGW Makefiles" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DDEFAULT_INSTALL_PATH=${MINGW_PREFIX} \
    -DOPENSSL_ROOT_DIR=${MINGW_PREFIX} \
    -DKICAD_SCRIPTING=ON \
    -DKICAD_SCRIPTING_MODULES=ON \
    -DKICAD_SCRIPTING_PYTHON3=ON \
    -DKICAD_SCRIPTING_WXPYTHON=ON \
    -DKICAD_SCRIPTING_WXPYTHON_PHOENIX=ON \
    -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}

  mingw32-make

  cd "${srcdir}"
  msg2 "Build translations"
  [[ -d build-i18n ]] && rm -rf build-i18n
  mkdir -p build-i18n && cd build-i18n

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G "MinGW Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-i18n-${pkgver}

  for _s in ${_sub[@]}; do
    cd ${srcdir}
    msg2 "Build ${_s}"
    [[ -d build-${_s} ]] && rm -rf build-${_s}
    mkdir -p build-${_s} && cd build-${_s}

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -G "MinGW Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${_s}-${pkgver}
  done
}

for _doclang in ${_doc[@]}; do _doc_pkg+=("\"${MINGW_PACKAGE_PREFIX}-${_realname}-doc-${_doclang}: Documentation for KiCad (mingw-w64)\""); done
for _feature in ${_sub[@]}; do _sub_pkg+=("\"${MINGW_PACKAGE_PREFIX}-${_realname}-${_feature}\""); done

eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}() {

  msg2 \"Package kicad\"
  optdepends+=(${_doc_pkg[@]} ${_sub_pkg[@]})
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)

  cd \${srcdir}/build-${MINGW_CHOST}
  mingw32-make DESTDIR=\${pkgdir} install

  cd \${srcdir}/build-i18n
  mingw32-make DESTDIR=\${pkgdir} install
}"

eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-meta() {
  depends=(${MINGW_PACKAGE_PREFIX}-${_realname}
           ${_sub_pkg[@]})
}"

for _part in ${_sub[@]}; do
  eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}() {
    options=('!strip')
    conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}-git)
    replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}-git)
    msg2 \"Package ${_part}\"
    cd \${srcdir}/build-${_part}
    mingw32-make DESTDIR=\${pkgdir} install
  }"
done
