# Maintainer: David Grayson <davidegrayson@gmail.com>

_realname=hclient
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=540.f0adcda0
pkgrel=1
_commit='f0adcda012820b1cd44a8b3a1953baf478029738'
pkgdesc='Sample GUI for communicating with HIDs (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/Microsoft/Windows-driver-samples/tree/master/hid/hclient'
license=('custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "git")
depends=()
source=("wds::git+https://github.com/Microsoft/Windows-driver-samples#commit=${_commit}"
        '01-includes.patch'
        '02-intsafe.patch'
        '03-read-thread-proc.patch'
        '04-bad-comparison.patch'
        '05-other-bad-comparison.patch'
        '06-size-t.patch'
        '07-dialog-proc.patch')
sha256sums=('SKIP'
            'c5e16ddaa76e5d2dbcf3d8b5083f9a2dfca74f869419c2e5be9b7444b05a72db'
            '6c73b234ff46a20cae7de86e6d96f53629624c9b3d005d72030500e95616b826'
            '6f2f2ffe160cf71c43fb0224efabf045490ef0202921600f79c45b0bab527e45'
            '8c879599a1cf95ed838be8030acb2d74d85989de10a61c7ac1e9243266d1c361'
            'e673215729689b21fd4b6b246e70ed4a8d75fea871dac6b0ec410bf4fc87d60c'
            '94b4ebd39a372cf105d45daa1be9c5286dc307a82325273c9925253985c736c8'
            'c2caed5841c2212ec43b7799b0140fcdd5e217b0488f8cfe9318a19841e0eb32')

pkgver() {
  cd "${srcdir}/wds"
  printf "%s.%s" "$(git rev-list --count "${_commit}")" "$(git rev-parse --short "${_commit}")"
}

prepare() {
  cd "${srcdir}/wds/hid/${_realname}"

  patch -p1 -i "${srcdir}/01-includes.patch"

  # GCC distinguishes between unsigned long and unsigned int.
  patch -p1 -i "${srcdir}/02-intsafe.patch"

  # GCC distinguishes between void pointers and other pointers.
  patch -p1 -i "${srcdir}/03-read-thread-proc.patch"

  # GCC warns about this because sizeof() for an array parameter
  # just returns the size of a pointer.
  patch -p1 -i "${srcdir}/04-bad-comparison.patch"

  # Fix a compile warning from comparing two different pointer types without a
  # cast.
  patch -p1 -i "${srcdir}/05-other-bad-comparison.patch"

  # size_t is different from SIZE_T
  patch -p1 -i "${srcdir}/06-size-t.patch"

  # DlgProcs are supposed to return INT_PTR, which is different from LRESULT.
  patch -p1 -i "${srcdir}/07-dialog-proc.patch"
}

build() {
  cd "${srcdir}"
  rm -rf "build-${MSYSTEM}"
  mkdir -p "build-${MSYSTEM}"
  cd "build-${MSYSTEM}"

  windres ../wds/hid/hclient/hclient.rc rc.so

  ${CC} -mwindows --std=c99 ${CFLAGS} ${LDFLAGS} \
    ../wds/hid/hclient/*.c rc.so \
    -lsetupapi -lhid \
    -o hclient.exe
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm755 hclient.exe "${pkgdir}${MINGW_PREFIX}/bin"

  cd "${srcdir}/wds"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
