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

_realname=gnuradio
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.10.12.0
pkgrel=3
pkgdesc="General purpose DSP and SDR toolkit (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gnuradio.org/'
msys2_repository_url="https://github.com/gnuradio/gnuradio"
msys2_references=(
  'archlinux: gnuradio'
)
license=(spdx:GPL-3.0-or-later)
depends=("${MINGW_PACKAGE_PREFIX}-codec2"
  "${MINGW_PACKAGE_PREFIX}-boost-libs"
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
  "${MINGW_PACKAGE_PREFIX}-gmp"
  "${MINGW_PACKAGE_PREFIX}-gsl"
  "${MINGW_PACKAGE_PREFIX}-gsm"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-libad9361-iio"
  "${MINGW_PACKAGE_PREFIX}-libiio"
  "${MINGW_PACKAGE_PREFIX}-libuhd" 
  "${MINGW_PACKAGE_PREFIX}-libunwind"
  "${MINGW_PACKAGE_PREFIX}-fftw"
  "${MINGW_PACKAGE_PREFIX}-mathjax2"
  "${MINGW_PACKAGE_PREFIX}-volk"
  "${MINGW_PACKAGE_PREFIX}-python-click"
  "${MINGW_PACKAGE_PREFIX}-python-click-plugins"
  "${MINGW_PACKAGE_PREFIX}-python-gobject"
  "${MINGW_PACKAGE_PREFIX}-python-cairo"
  "${MINGW_PACKAGE_PREFIX}-python-jsonschema"
  "${MINGW_PACKAGE_PREFIX}-python-mako"
  "${MINGW_PACKAGE_PREFIX}-python-matplotlib"
  "${MINGW_PACKAGE_PREFIX}-python-numpy"
  "${MINGW_PACKAGE_PREFIX}-python-pygccxml"
  "${MINGW_PACKAGE_PREFIX}-python-pyaml" 
  "${MINGW_PACKAGE_PREFIX}-python-pyzmq"
  "${MINGW_PACKAGE_PREFIX}-python-scipy"
  "${MINGW_PACKAGE_PREFIX}-python-qtpy"
  "${MINGW_PACKAGE_PREFIX}-python-pyqt5"
  "${MINGW_PACKAGE_PREFIX}-python-pyqtgraph"
  "${MINGW_PACKAGE_PREFIX}-python-lxml"
  "${MINGW_PACKAGE_PREFIX}-python-pyopengl"
  "${MINGW_PACKAGE_PREFIX}-qt5-base"
  "${MINGW_PACKAGE_PREFIX}-angleproject"
  "${MINGW_PACKAGE_PREFIX}-qwt-qt5"
  "${MINGW_PACKAGE_PREFIX}-SDL"
  "${MINGW_PACKAGE_PREFIX}-soapysdr"
  "${MINGW_PACKAGE_PREFIX}-portaudio"
  "${MINGW_PACKAGE_PREFIX}-libsndfile"
  "${MINGW_PACKAGE_PREFIX}-spdlog"
  "${MINGW_PACKAGE_PREFIX}-fmt"
  "${MINGW_PACKAGE_PREFIX}-zeromq")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cppzmq"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python-packaging"
             "${MINGW_PACKAGE_PREFIX}-python-pip")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
              "${MINGW_PACKAGE_PREFIX}-python-pytest")
optdepends=("${MINGW_PACKAGE_PREFIX}-soapyrtlsdr: install to use RTL-SDR devices with gnuradio")
options=('!strip')
source=("https://github.com/${_realname}/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/gnuradio/gnuradio/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz.asc"
        0001-cmake-Install-python-wrapper-exe-for-scripts-on-Wind.patch
        gnuradio-blocks-and-examples-path.hook.in
        gnuradio-blocks-and-examples-path.script.in
        LICENSE.txt)
validpgpkeys=(
  'B90DDFAC56989BF62262EB812987C77CBB8ED9B2'  # GNU Radio Project (Admin) <admin@gnuradio.org>
  'D74F9F146E7F755783583158B343B2BA293E5174'  # Marcus Müller (GNU Radio Maintainer) <mmueller@gnuradio.org>
  '723EC3A2B90533C6B93DFBC8ED797743F7951435'  # GNU Radio (Software Signing Key) <info@gnuradio.org>
)
sha256sums=('fe78ad9f74c8ebf93d5c8ad6fa2c13236af330f3c67149d91a0647b3dc6f3958'
            'SKIP'
            'aa9c79f6ab923c566f57d90117fa7e82549c26230538ffb3a2b1c0246fc31c19'
            'b2f014a69b32afcb207e4d2e8e163fc04968594d7d4c3184b9ea5c8cf2a29329'
            'caa9a6cc253d508abcd01545f26b46df0d708d24f69b7dc59183d631872721ed'
            '86dc4bc79c9fa4d5dc3ae5558441d5bb8b8736403e70997cf9ae8f671ab5d0ca')
 
prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # Patch number 0001 is credited to Ryan Volz <ryan.volz@gmail.com> and licensed under the 
  # BSD-3-Clause license
  patch -Np1 -i "${srcdir}/0001-cmake-Install-python-wrapper-exe-for-scripts-on-Wind.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

  # FIXME remove -DENABLE_CTRLPORT_THRIFT=OFF when issue 7390 is fixed
  # https://github.com/gnuradio/gnuradio/issues/7390

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBoost_ATOMIC_LIBRARY_RELEASE="${MINGW_PREFIX}/bin/libboost_atomic-mt.dll" \
      -DBoost_CHRONO_LIBRARY_RELEASE="${MINGW_PREFIX}/bin/libboost_chrono-mt.dll" \
      -DBoost_DATE_TIME_LIBRARY_RELEASE="${MINGW_PREFIX}/bin/libboost_date_time-mt.dll" \
      -DBoost_PROGRAM_OPTIONS_LIBRARY_RELEASE="${MINGW_PREFIX}/bin/libboost_program_options-mt.dll" \
      -DBoost_THREAD_LIBRARY_RELEASE="${MINGW_PREFIX}/bin/libboost_thread-mt.dll" \
      -DBoost_UNIT_TEST_FRAMEWORK_LIBRARY_RELEASE="${MINGW_PREFIX}/bin/libboost_unit_test_framework-mt.dll" \
      -DENABLE_CTRLPORT_THRIFT=OFF \
      -S "${_realname}-${pkgver}" \
      -DMATHJAX2_JS_PATH="${MINGW_PREFIX}/share/mathjax2" \
      -B "build-${MSYSTEM}"

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

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

  #copy all dlls to a directory
  if [ ! -d temp_lib ]; then
    mkdir temp_lib
    find . -name "*.dll" ! -path '*temp_lib*' -exec cp {} temp_lib \;
  fi

  #add a call to os.add_dll_directory() to load python dlls
  python_dll_prefix=`cygpath.exe -m -a .`
  python_dll_suffix="/gnuradio-runtime/python/gnuradio/gr/bindings"

  #run the sed regex
  find . -name "*_test.bat" -exec sed.exe -i 's|^\(.*python3.exe\)|\1 -c "import os; os.add_dll_directory('\'${python_dll_prefix}${python_dll_suffix}\'');"|g' {} \;

  #run tests with dll directory added to the path
  PATH=$PWD/temp_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}"

  #install hook and script for setting global_blocks_path and examples_path in grc.conf
  hook_path="${srcdir}"/${MINGW_PACKAGE_PREFIX}-gnuradio-blocks-and-examples-path.hook
  cp "${srcdir}/gnuradio-blocks-and-examples-path.hook.in" "${hook_path}"
  sed -s "s|@MINGW_PACKAGE_PREFIX@|${MINGW_PACKAGE_PREFIX}|g" -i "${hook_path}"
  install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"

  script_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-gnuradio-blocks-and-examples-path.script";
  cp "${srcdir}/gnuradio-blocks-and-examples-path.script.in" "${script_path}"
  sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${script_path}"
  install -Dt "$pkgdir/usr/share/libalpm/scripts" -m755 "${script_path}"
 
  #install licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
