

_realname=hackrf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2024.02.1
pkgrel=2
pkgdesc="Low cost software radio platform (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/greatscottgadgets/hackrf"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-fftw")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/greatscottgadgets/hackrf/releases/download/v${pkgver}/hackrf-${pkgver}.tar.xz"
        "001-libusb.patch"
        "002-fftw.patch")
sha256sums=('d9ced67e6b801cd02c18d0c4654ed18a4bcb36c24a64330c347dfccbd859ad16'
            'bdb446fb8ae8a5505ef18767b02af171ff8f00fc8d98347ba3978c644fcae3a6'
            'a48be6ca06c3cb1e464b988215000ebba68e8b7494a9f008749ffb78af1b6f75')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}"/001-libusb.patch
  patch -Np1 -i "${srcdir}"/002-fftw.patch
}

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      "${extra_config[@]}" \
      -GNinja \
      ../${_realname}-${pkgver}/host

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"

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

  mv "${pkgdir}${MINGW_PREFIX}/bin/"*.a "${pkgdir}${MINGW_PREFIX}/lib/"

  cd "${srcdir}"/${_realname}-${pkgver}
  install -vDm644 "firmware/cpld/sgpio_if/default.xsvf" \
    "${pkgdir}${MINGW_PREFIX}/share/hackrf/hackrf_cpld_default.xsvf"

  cd firmware-bin
  for i in *.{bin,dfu}; do
    install -vDm644 "${i}" "${pkgdir}${MINGW_PREFIX}/share/hackrf/${i}"
  done
}
