### this file is sourced not run
PKGVER=3.23.12
PKGBUILD=1
PKGARCH=x86_64

# source: james + arch linux
TARBALL=hplip-$PKGVER.tar.gz
MD5SUM=86be3fb32bb360a16db08139aacb2a70
SRC_URL=http://downloads.sourceforge.net/hplip/$TARBALL
BUNDLE=

SLACKREQ='ghostscript python3-pyqt5 python3-dbus'
SLACKDESC="hplip: hplip $PKGVER (HP Printer drivers)
hplip: 
hplip: The HP Linux Printing and Imaging System provides full support for 
hplip: printing on most HP SFP (single function peripheral) inkjets and many 
hplip: LaserJets, and for scanning, sending faxes and for photo-card access 
hplip: on most HP MFP (multi-function peripheral) printers.
hplip: 
hplip: https://hplipopensource.com
hplip: 
hplip: 
hplip:
"
SLACKSUG='sane-backend'

### default pkg_download

### prepare
pkg_prepare() {
	pkg_prepare_default &&
	cd tmp/hplip* &&

	### these are ArchLinux patchs
	# disable insecure update - https://bugs.archlinux.org/task/38083
	patch -Np0 -i ../pkg/disable_upgrade.patch &&

	# add missing 'include <cups/ppd.h>' at various places
	patch -Np1 -i ../pkg/0022-Add-include-cups-ppd.h-in-various-places-as-CUPS-2.2.patch &&

	# PYTHON 3 ONLY
	# fix some handling unicode file names FS#58412
	patch -Np1 -i ../pkg/0023-Fix-handling-of-unicode-filenames-in-sixext.py.patch &&

	# PYTHON 3 ONLY
	# Workaround patch for missing Python3 transition of the old
	# (pre-USB-storage) photo memory card support (pcardext) - Debian patch
	patch -Np1 -i ../pkg/python3.diff &&

	# fix model support / Debian patch / FS#74942
	patch -Np1 -i ../pkg/0003-models.dat-Re-add-drivers-missing-from-3.19.1.patch &&

	# PYTHON 3 ONLY
	# https://bugs.launchpad.net/hplip/+bug/1879445
	# broken scanning - https://bugs.archlinux.org/task/66704
	patch -Np1 -i ../pkg/hplip-configure-python.patch &&

	# allow non-jpeg scanning on all-in-one devices - FS#78135
	patch -Np1 -i ../pkg/0018-Allow-non-JPEG-scanning-on-the-HP-DeskJet-3520-All-i.patch &&

	# PYTHON 3 ONLY
	# hp-firmware: module 'locale' not longer provides method 'format', causing traceback #4
	patch -Np1 -i ../pkg/hplip-locale-format.patch &&

	# make gzip creation reproducible by removing the timestamp
	patch -Np1 -i ../pkg/reproducible-gzip.patch &&

	# PYTHON 3 ONLY
	# https://gitlab.archlinux.org/archlinux/packaging/packages/hplip/-/issues/3
	patch -Np1 -i ../pkg/hplip-1_3_23_12-4-python3_12-fix.patch &&

	return 0
}

### default pkg_package
### build
pkg_build() {
	# qt5	
	export PATH="$PATH:/opt/qt5/bin"
	export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/qt5/$LIBDIR"
	export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/opt/qt5/$LIBDIR/pkgconfig"
	export QT5_PATH="/opt/qt5/bin"
	export QT5_LD_LIBRARY_PATH="/opt/qt5/$LIBDIR"
	export QT5_PKG_CONFIG_PATH="/opt/qt5/$LIBDIR/pkgconfig"
	export QT5_CPLUS_INCLUDE_PATH="/opt/qt5/include" 

	CONFFLAGS="$CONFFLAGS \
		--disable-network-build    \
		--enable-qt5 --disable-qt4 \
		--with-cupsfilterdir=/usr/$LIBDIR/cups/filter   \
		--with-cupsbackenddir=/usr/$LIBDIR/cups/backend \
		--enable-pp-build \
	"
	cd /tmp/hplip* &&

	export PYTHON=python3 # note we use python3 becase we only build pyqt5 for python3
	export AUTOMAKE='automake --foreign'
	autoreconf -fi                                           &&
	./configure $CONFFLAGS                                   &&
	sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool &&
	make $MAKEFLAGS && make install                          &&

	# remove config provided by sane and autostart of hp-daemon
	rm -rf /etc/{sane.d,xdg}                &&
	install -dm755 /etc/sane.d/dll.d        &&
	echo hpaio > /etc/sane.d/dll.d/hpaio    &&

	# remove HAL .fdi file because HAL is no longer used
	rm -vrf "$pkgdir"/usr/share/hal         &&

	# convert all python to python3
	for p in /usr/share/hplip/*.py; do
		sed -i -e '1 s/python/python3/' $p || return 1
	done &&
	
	# Compile Python bytecode:
	python3 -m compileall -d /usr/share/hplip /usr/share/hplip    &&
	python3 -O -m compileall -d /usr/share/hplip /usr/share/hplip &&

	# done
	pkg_build_slackdesc
}
