# Maintainer: Chris Gorman <chrisjohgorman@gmail.com>

_realname=libuhd
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.8.0.0
pkgrel=1
pkgdesc="Universal Software Radio Peripheral (USRP) userspace driver (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://files.ettus.com/manual/'
msys2_repository_url="https://github.com/EttusResearch/uhd"
msys2_references=(
  'archlinux: libuhd'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-mako"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-ruamel-yaml")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-python-poetry"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://github.com/EttusResearch/uhd/archive/v${pkgver}/libuhd-${pkgver}.tar.gz"
        0001-libraries_headers_and_no_pthread_setname_np.patch)
sha256sums=('a2159491949477dca67f5a9b05f5a80d8c2b32e91b95dd7fac8ddd3893e36d09'
            '132767cf9893c17bfd49d81b7b0804f946680fb3a8e350e46f552aef953b91d4')

prepare() {
  cd "${srcdir}"/uhd-${pkgver}

  patch -Np1 -i "${srcdir}"/0001-libraries_headers_and_no_pthread_setname_np.patch
}

build() {
  local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  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=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DPYTHON_EXECUTABLE="${MINGW_PREFIX}"/bin/python \
      -DENABLE_EXAMPLES=ON \
      -DENABLE_PYTHON_API=ON \
      -DUHD_PYTHON_DIR="lib/python${_pyver}/site-packages" \
      -DENABLE_UTILS=ON \
      -DENABLE_TESTS=OFF \
      -DENABLE_E300=ON \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      "${extra_config[@]}" \
      -S "uhd-${pkgver}/host" \
      -B "build-${MSYSTEM}"

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  #run tests with dll directory added to the path
  PATH=$PWD/lib:$PATH \
    "${MINGW_PREFIX}"/bin/ctest.exe --timeout 90 --output-on-failure || warning "Tests failed"
}

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

  cd "${srcdir}"/"build-${MSYSTEM}"/"python"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/uhd*.whl

  # copy uhd_images_downloader.py and usrp2_card_burner.py to ${MINGW_PREFIX}/bin
  # so they can be executed
  if [ -f "${pkgdir}/${MINGW_PREFIX}"/lib/uhd/utils/uhd_images_downloader.py ]; then
    cp "${pkgdir}/${MINGW_PREFIX}"/lib/uhd/utils/uhd_images_downloader.py \
    "${pkgdir}/${MINGW_PREFIX}"/bin/uhd_images_downloader
  fi
  if [ -f "${pkgdir}/${MINGW_PREFIX}"/lib/uhd/utils/usrp2_card_burner.py ]; then
    cp "${pkgdir}/${MINGW_PREFIX}"/lib/uhd/utils/usrp2_card_burner.py \
    "${pkgdir}/${MINGW_PREFIX}"/bin/usrp2_card_burner
  fi

}
