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

_realname=avrdude
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.3
pkgrel=3
pkgdesc='Software for programming Atmel AVR Microcontrollers (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.nongnu.org/avrdude/"
license=('GPL')
makedepends=()
depends=("${MINGW_PACKAGE_PREFIX}-libftdi"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libusb-compat"
         "${MINGW_PACKAGE_PREFIX}-libelf")
options=('staticlibs' 'strip')
source=("https://download.savannah.gnu.org/releases/avrdude/avrdude-${pkgver}.tar.gz"{,.sig}
        '01-libtool.patch'
        '02-ddk-headers.patch'
        '03-handle-printing.patch'
        '04-no-giveio.patch')
sha256sums=('0f9f731b6394ca7795b88359689a7fa1fba818c6e1d962513eb28da670e0a196'
            'SKIP'
            '3a5c041f97e8fcd6706b08ae24fe21337fd1df13319479e2d49c3f82529ba054'
            '7b52523834e492281347bc92a064ca924dc5d09e2d653e7076c5a966d03f7768'
            '159fb7d3253931c00a019c43f343f533d4e67c3a9859b7bcdb51878b8dc26451'
            'c52f01656cfece50da9513170549fa5f0c8240544e10e77f67eea7a3ad8d1b50')
validpgpkeys=('EF497ABE47ED91B3FC3D7EA54D902FF7723BDEE9'
              '5E84F980C3CAFD4BB5841070F48CA81B69A85873')

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

  # Delete binaries.
  rm windows/giveio.sys

  # Let's use mingw-w64's hidsdi.h instead of AVRDUDE's.
  rm my_ddk_hidsdi.h

  # Fixes a warning from libtool.  However, a DLL still does not generated.
  patch -p1 -i ../01-libtool.patch

  # Look for headers like hidsdi.h and hidpi.h at the top level,
  # (do not assume they are inside the "ddk" directory).
  patch -p1 -i ../02-ddk-headers.patch

  # Proper printing of 64-bit HANDLE pointers.  The upstream code results
  # in a compiler warning on x86_64 and wouldn't print the whole pointer.
  patch -p1 -i ../03-handle-printing.patch

  # Don't install giveio.sys or the batch scripts associated with it since it is
  # a precompiled binary and unlikely to work on modern versions of Windows
  # anyway due to driver signing requirements.
  patch -p1 -i ../04-no-giveio.patch

  ./bootstrap
}

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  CFLAGS+=" -I${MINGW_PREFIX}/include/libusb-compat"
  "../${_realname}-${pkgver}/configure" \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}"

  make
}

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

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

  cd "${pkgdir}${MINGW_PREFIX}"

  # Remove loaddrv.exe because we aren't distributing any drivers one could use
  # with it and driver signing requirements in modern versions of Windows mean
  # it is unlikely to work (it only claims to support Windows NT/2000/XP).
  # Also, with a generic name like loaddrv, it could cause a name collision.
  rm bin/loaddrv.exe

  # As we can see from confwin.c, AVRDUDE searches for avrdude.conf
  # using the PATH; it does not find it in /etc.
  mv etc/avrdude.conf bin

  cd "${srcdir}/${_realname}-${pkgver}"
  install -D COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
