# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=picotool
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.1
pkgrel=1
_pico_sdk_version=2.1.1
pkgdesc="Tool for inspecting RP2040 binaries and interacting with RP2040 devices. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/raspberrypi/picotool"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libusb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/raspberrypi/picotool/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/raspberrypi/pico-sdk/archive/${_pico_sdk_version}/pico-sdk-${_pico_sdk_version}.tar.gz")
sha256sums=('19200c6dc4be5acd6fb53de3d7f35c826af596c18879d56f214b795300100260'
            '179c5531e75ac7f82d0679e70b6e8881f871738dc0ef17baf3b4ff4679291c41')

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

  PICO_SDK_PATH="${srcdir}/pico-sdk-${_pico_sdk_version}" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      ../${_realname}-${pkgver}

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

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

  # Install docs
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/README.md" -t "${pkgdir}/${MINGW_PREFIX}/share/doc/${_realname}"

  # Install license
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.TXT" -t "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}"
}
