# Copyright     : Obarun
#------------------------
# Maintainer    : Eric Vidal <eric@obarun.org>
# Maintainer    : Jean-Michel T.Dydak <jean-michel@obarun.org>
#----------------
# Pkgbuild Src  : https://git.obarun.org/pkg/obextra/sane
#--------------------------------------------------------------------------------
# DESCRIPTION ]

pkgname=sane
pkgver=1.1.1
pkgrel=2
pkgdesc="Scanner Access Now Easy"
url='http://www.sane-project.org/'

track="backends"
target="$track-$pkgver"
source=(
    "https://gitlab.com/sane-project/${track}/-/archive/${pkgver}/${target}.tar.gz"
    66-saned.rules
    sane.sysusers
)

#----------------------
# BUILD CONFIGURATION ]

makedepends=(
    'autoconf-archive'
    'curl'
    'glib2'
    'libjpeg-turbo'
    'libtiff'
    'libusb'
    'poppler-glib'
    'python'
    'texlive-latexextra'
)

#------------------------
# INSTALL CONFIGURATION ]

depends=(
    'bash'
    'cairo'
    'gcc-libs'
    'glibc'
    'libgphoto2'
    'libnl'
    'libpng'
    'libxml2'
    'libieee1284'
    'net-snmp'
    'openssl'
    'v4l-utils'
)

provides=(
    'libsane.so'
)

#----------------
# BUILD PREPARE ]

prepare() {
    cd $track-$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 CONTROL ]

_flags=(
    --prefix=/usr
    --docdir=/usr/share/doc/$pkgname
    --localstatedir=/var
    --sbindir=/usr/bin
    --sysconfdir=/etc
    --disable-locking
    --disable-rpath
    --enable-pthread
    --without-avahi
    --without-systemd
    --with-libcurl
    --with-pic
    --with-poppler-glib
    --with-usb
)

#--------
# BUILD ]

build() {
    cd $track-$pkgver

    ./configure "${_flags[@]}"

    ## circumvent overlinking in libraries
    sed -e 's/ -shared / -Wl,-O1,--as-needed\0/g' -i libtool

    make
}

#----------
# PACKAGE ]

package() {
    depends+=(
        'libcurl.so'
        'libgphoto2.so'
        'libgobject-2.0.so'
        'libpoppler-glib.so'
        'libtiff.so'
        'libusb-1.0.so'
)

    cd $track-$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"

    install -vDm 644 "$srcdir"/66-saned.rules "$pkgdir"/usr/lib/udev/rules.d/
    install -vDm 644 "$srcdir"/sane.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) )
}

#--------------------
# ARCH LICENSE AUTH ]

arch=(x86_64)
license=(GPL2)

sha512sums=('')
