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

_realname=libusb
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.24
pkgrel=3
pkgdesc="Library that provides generic access to USB devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://libusb.info/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=("https://github.com/libusb/libusb/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.bz2")
sha256sums=('7efd2685f7b327326dcfb85cee426d9b871fd70e22caa15bb68d595ce2a2b12a')

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

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -e "s/-mwin32//g" -e "s/-Wl,--add-stdcall-alias//g" -i configure.ac
  fi

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  # Copied from llvm-mingw/wrappers/dlltool-wrapper.sh
  case ${CARCH} in
    i686)    M=i386        ;;
    x86_64)  M=i386:x86-64 ;;
    armv7)   M=arm         ;;
    aarch64) M=arm64       ;;
  esac
  export DLLTOOLFLAGS="-m ${M}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
