# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=sane
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.4.0
pkgrel=4
pkgdesc="Scanner Access Now Easy ― Backends. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='http://www.sane-project.org'
msys2_repository_url="https://gitlab.com/sane-project/backends"
msys2_references=(
  'archlinux: sane'
  "cpe: cpe:2.3:a:sane-backends_project:sane-backends"
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-libieee1284"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-poppler")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "autoconf-archive")
source=("https://gitlab.com/sane-project/backends/-/archive/${pkgver}/backends-${pkgver}.tar.bz2"
        "001-fix-build-on-mingw.patch")
sha256sums=('813ef8818a498cbb11615f657cd6dc66536ef34df4a557d9cd63086622f6123d'
            'c7d84a80d4f91000024562500b95de165b8200bae66f48d807e9f07e7e8f4bb2')

prepare() {
  cd "backends-${pkgver}"
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch

  # https://gitlab.com/sane-project/backends/-/issues/440
  printf "%s\n" "$pkgver" > .tarball-version
  printf "%s\n" "$pkgver" > .version

  autoreconf -fiv
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  export BACKENDS="abaton apple artec bh canon canon630u canon_dr canon_lide70 canon_pp cardscan coolscan2 coolscan3 dell1600n_net dmc epjitsu epson epson2 epsonds fujitsu genesys gt68xx hp3900 hp4200 hp5400 hp5590 hpsj5s hpljm1005 hs2p ibm kodak kvs1025 kvs20xx leo lexmark ma1509 matsushita microtek mustek_usb mustek_usb2 nec niash ricoh ricoh2 rts8891 s9036 sceptre sharp sm3600 sm3840 st400 stv680 teco1 teco2 teco3 umax_pp xerox_mfp p5"
  CFLAGS+=" -Wno-implicit-function-declaration -Wno-int-conversion -Wno-incompatible-pointer-types" \
  ../backends-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --disable-locking \
    --disable-rpath \
    --disable-preload \
    --with-libcurl \
    --with-pic \
    --with-poppler-glib \
    --with-usb \
    LIBS="-lws2_32"

  make || make -C tools || make
}

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

  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"

  install -Dm644 "${srcdir}/backends-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
