#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobextra/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

_name=backends
pkgname=sane
pkgver=1.1.1
pkgrel=02
pkgdesc="Scanner Access Now Easy w/o systemd or avahi"
arch=(x86_64)
url="http://www.sane-project.org/"
# NOTE: libgphoto2 can be moved to makedepends once the package also provides
# libgphoto2_port.so
depends=(bash cairo gcc-libs glibc libgphoto2 libnl libpng libxml2 libieee1284
net-snmp openssl v4l-utils)
makedepends=(autoconf-archive glib2 libjpeg-turbo libtiff libusb python texlive-latexextra)
provides=(libsane.so)
source=("https://gitlab.com/sane-project/${_name}/-/archive/${pkgver}/${_name}-${pkgver}.tar.gz"
        "66-${pkgname}d.rules"
        "${pkgname}.sysusers")
#  From initial build 1.1.1-02
# ==> WARNING: Library listed in 'depends' is not required by any files: libcurl.so
# ==> WARNING: Library listed in 'depends' is not required by any files: libgobject-2.0.so
# ==> WARNING: Library listed in 'depends' is not required by any files: libpoppler-glib.so
# 

prepare() {
  cd "${_name}-${pkgver}"
  # create version files, so that autotools macros can use them:
  # https://gitlab.com/sane-project/backends/-/issues/440
  echo "${pkgver}" > .tarball-version
  echo "${pkgver}" > .version
  autoreconf -fiv
}

build() {
  cd "${_name}-${pkgver}"

  ./configure --prefix=/usr \
    --disable-locking \
    --disable-rpath \
    --docdir="/usr/share/doc/${pkgname}" \
    --enable-pthread \
    --localstatedir=/var \
    --sbindir=/usr/bin \
    --sysconfdir=/etc \
    --without-avahi \
    --without-libcurl \
    --with-pic \
    --without-poppler-glib \
    --without-systemd \
    --with-usb
  # circumvent overlinking in libraries
  sed -e 's/ -shared / -Wl,-O1,--as-needed\0/g' -i libtool
  make
}

package() {
  depends+=(libgphoto2.so libtiff.so libusb-1.0.so)

  cd "${_name}-${pkgver}"

  make DESTDIR="${pkgdir}" install

  # generate udev udev+hwdb
  install -vdm 755 "${pkgdir}/usr/lib/udev/rules.d/"
  tools/sane-desc -m udev+hwdb -s doc/descriptions/ > "${pkgdir}/usr/lib/udev/rules.d/65-${pkgname}.rules"
  tools/sane-desc -m udev+hwdb -s doc/descriptions-external/ >> "${pkgdir}/usr/lib/udev/rules.d/65-${pkgname}.rules"
  # generate udev hwdb
  install -vdm 755 "${pkgdir}/usr/lib/udev/hwdb.d/"
  tools/sane-desc -m hwdb -s doc/descriptions/ > "${pkgdir}/usr/lib/udev/hwdb.d/20-${pkgname}.hwdb"
  # NOTE: an empty new line is required between the two .desc collections
  printf "\n" >> "${pkgdir}/usr/lib/udev/hwdb.d/20-${pkgname}.hwdb"
  tools/sane-desc -m hwdb -s doc/descriptions-external/ >> "${pkgdir}/usr/lib/udev/hwdb.d/20-${pkgname}.hwdb"

#  # systemd integration
#  install -vDm 644 "../${pkgname}d.socket" \
#      -t "${pkgdir}/usr/lib/systemd/system/"
#  install -vDm 644 "../${pkgname}d.service" \
#      "${pkgdir}/usr/lib/systemd/system/${pkgname}d@.service"
  install -vDm 644 "../66-${pkgname}d.rules" "${pkgdir}/usr/lib/udev/rules.d/"
  # sysusers.d
  install -vDm 644 "../${pkgname}.sysusers" "${pkgdir}/usr/lib/sysusers.d/${pkgname}.conf"

  # remove old ChangeLogs
  rm -rvf "${pkgdir}/usr/share/doc/${pkgname}/ChangeLogs/"

  # add files below /etc/sane.d to backup array
  cd "${pkgdir}"
  # trick extract_function_variable() in makepkg into not detecting the
  # backup array modification and adding remaining configuration files
  [[ /usr/bin/true ]] && backup=( ${backup[@]} $(find "etc/${pkgname}.d/" -type f) )
}


#---- license gpg-key sha256sums ----

license=(GPL2)

sha256sums=(4caa9155b797e8b83abc88bcbfc8212155d388311c3ba077bbcf9c98ed78ca0e  # backends-1.1.1.tar.gz
	0e98982ff1550b16b098f7563569c203aab5f7b4172717bec0d42eab15fb875b  # 66-saned.rules
	ac9183a0f4379106550c972374b8e36277d8e7f480ca931f0af87550892942e7) # sane.sysusers

