# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=hidapi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.15.0
pkgrel=1
pkgdesc="Library for communicating with USB and Bluetooth HID devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://libusb.info/hidapi/"
msys2_repository_url="https://github.com/libusb/hidapi"
license=('spdx:GPL-3.0-or-later' 'spdx:BSD-3-Clause' 'custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
source=("https://github.com/libusb/hidapi/archive/${_realname}-${pkgver}.tar.gz"
        0001-hidapi-cmake-add-soversion.patch)
sha256sums=('5d84dec684c27b97b921d2f3b73218cb773cf4ea915caee317ac8fc73cef8136'
            'd689070fece827e0fb7dda42d663c89034b7047f157c365b52a2d212a6ab01f8')

prepare() {
  cd "${_realname}-${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-hidapi-cmake-add-soversion.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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -S "${_realname}-${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-static"

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -S "${_realname}-${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-shared"

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

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

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-${_realname}-${pkgver}/LICENSE{-bsd,-gpl3,-orig,}.txt "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/
}
