# Support libraries for KiCad

_realname=kicad
pkgbase=mingw-w64-${_realname}-library
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-footprints"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-symbols"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-templates"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D")
pkgver=9.0.6
pkgrel=1
pkgdesc="Support libraries for KiCad (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.kicad.org/'
msys2_repository_url="https://gitlab.com/kicad/libraries"
msys2_references=(
  'archlinux: kicad-library'
)
license=("spdx:GPL-3.0-or-later")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('!strip')
source=("https://gitlab.com/kicad/libraries/kicad-footprints/-/archive/${pkgver}/kicad-footprints-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-symbols/-/archive/${pkgver}/kicad-symbols-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-templates/-/archive/${pkgver}/kicad-templates-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-packages3D/-/archive/${pkgver}/kicad-packages3D-${pkgver}.tar.bz2")
sha256sums=('9de7909bb9baf7082697a21b4750eef43243866eaec3ef19f53b64f1091dde10'
            '6dd526dc6b17880fdfe42b3ae5076575349927a7de31f5a1abdde4723cf8f959'
            '8d691bee0e407050d0ef8d280f8f6edce08fd715968d69040a45a6c9d0c0f698'
            'e27f00a4a75b17b57ddbbaf26d95ee9d0a442b58c9cceff6f7aedeb4449de139')

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

  for _s in "footprints" "symbols" "templates" "packages3D"; do
    msg2 "Build kicad-${_s}"
    mkdir -p "${srcdir}/build-${_s}-${MSYSTEM}" && cd "${srcdir}/build-${_s}-${MSYSTEM}"

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      "${MINGW_PREFIX}"/bin/cmake.exe \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
        "${_extra_config[@]}" \
        ../${_realname}-${_s}-${pkgver}

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

package_footprints() {
  pkgdesc="KiCad footprints library (mingw-w64)"
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-footprints-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_symbols() {
  pkgdesc="KiCad symbols library (mingw-w64)"
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-symbols-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_templates() {
  pkgdesc="KiCad templates library (mingw-w64)"

  cd "${srcdir}/build-templates-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_packages3D() {
  pkgdesc="KiCad 3D models library (mingw-w64)"

  cd "${srcdir}/build-packages3D-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

# 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}-${_realname}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
