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

_realname=libftdi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5
pkgrel=10
pkgdesc='Library to talk to FTDI chips, with Python 3 bindings (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.intra2net.com/en/developer/libftdi/"
license=('spdx:LGPL-2.1-only AND GPL-2.0-only')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-confuse"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings to libftdi")
source=("https://www.intra2net.com/en/developer/libftdi/download/libftdi1-${pkgver}.tar.bz2"
        "0001-cmake-fix-libdir.patch"
        "0002-libftdi-include-sys-time-timeval.patch"
        "0003-no-distutils.patch")
sha256sums=('7c7091e9c86196148bd41177b4590dccb1510bfe6cea5bf7407ff194482eb049'
            '71d5a8de43a61c7e26531c722862dc7c4135e4b494498d2bd140019f9693741c'
            '71e542e4847642ec38e7929c208e61183ef2ff7c65ae21200101bb0a02635735'
            '8bdf184d7f0e6be2e7802202f9a415652eb05d84f324202632d10abdb68c0190')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}1-${pkgver}
  apply_patch_with_msg \
    0001-cmake-fix-libdir.patch \
    0002-libftdi-include-sys-time-timeval.patch \
    0003-no-distutils.patch
}

build() {
  rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DEXAMPLES=OFF \
    -DPYTHON_BINDINGS=ON \
    -DLINK_PYTHON_LIBRARY=ON \
    -DDOCUMENTATION=OFF \
    "../${_realname}1-${pkgver}/"

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  PATH=$PATH:"${srcdir}/build-${MSYSTEM}/src" make check || true
}

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

  cd "${srcdir}/${_realname}1-${pkgver}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  cp COPYING* "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  sed -i "s;$(cygpath -m /);/;" \
      "${pkgdir}${MINGW_PREFIX}/lib/cmake/${_realname}1"/*.cmake
}
