makedepends=(avahi ghostscript foomatic-db foomatic-db-engine usbutils sane cups cups-filters cups-pk-helper libusb net-snmp  polkit libjpeg-turbo2 rpcbind wget xsane python-setuptools)
run=(dbus-python python-distro python-pillow python-qt python-reportlab python-gobject sip)

description="Drivers for HP DeskJet, OfficeJet, Photosmart, Business Inkjet and some LaserJet"
url="http://hplipopensource.com"

packager="Great-OS Team"
maintainer="Great-OS Team"

name=hplip
version=3.25.8
release=4

source=(http://downloads.sourceforge.net/$name/$name-$version.tar.gz
       0022-Add-include-cups-ppd.h-in-various-places-as-CUPS-2.2.patch
       0023-Fix-handling-of-unicode-filenames-in-sixext.py.patch
       python3.diff
       0018-Allow-non-JPEG-scanning-on-the-HP-DeskJet-3520-All-i.patch
       hplip-configure-python.patch
       0003-models.dat-Re-add-drivers-missing-from-3.19.1.patch
       hplip-locale-format.patch
       reproducible-gzip.patch
       hplip-hpaio-gcc14.patch
       hplip-pserror-gcc14.patch
       hplip-gcc15-stdc23.patch)

prepare() {
  cd $name-$version
	
  # add missing 'include <cups/ppd.h>' at various places
  patch -Np1 -i ../0022-Add-include-cups-ppd.h-in-various-places-as-CUPS-2.2.patch

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

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

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

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

  #  allow non-jpeg scanning on all-in-one devices - FS#78135
  patch -Np1 -i ../0018-Allow-non-JPEG-scanning-on-the-HP-DeskJet-3520-All-i.patch
  # hp-firmware: module 'locale' not longer provides method 'format', causing traceback #4
  patch -Np1 -i ../hplip-locale-format.patch

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

  # Fix building with gcc 14
  patch -Np1 -i ../hplip-hpaio-gcc14.patch
  patch -Np1 -i ../hplip-pserror-gcc14.patch

  #Fix gcc isue
  patch -Np1 -i ../hplip-gcc15-stdc23.patch

  # Change shebang /usr/bin/env python -> /usr/bin/python3 (bug #618351).
  find -name '*.py' -print0 | xargs -0 \
     sed -i.env-python -e 's,^#!/usr/bin/env python,#!/usr/bin/python,'
  find -name '*.py' -print0 | xargs -0 \
     sed -i.env-python -e 's,^#!/usr/bin/python,#!/usr/bin/python3,'
  sed -i.env-python -e 's,^#!/usr/bin/env python,#!/usr/bin/python3,' \
    prnt/filters/hpps \
    fax/filters/pstotiff

  export AUTOMAKE='automake --foreign'
  autoreconf --force --install

}

build() {

  cd $name-$version

  export CFLAGS+=" -Wno-deprecated-declarations -Wno-implicit-function-declaration -Wno-return-mismatch"

  PYTHON=python3 ./configure --prefix=/usr \
    --enable-qt5  \
    --disable-qt4 \
    --enable-hpcups-install \
    --enable-cups-drv-install \
    --disable-imageProcessor-build \
    --disable-pp-build

  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make

}

package() {

  cd $name-$version

  make -j1 rulesdir=/usr/lib/udev/rules.d DESTDIR=$PKG install
 
  # remove config provided by sane and autostart of hp-daemon
  rm -rf $PKG/etc/{sane.d,xdg}
  install -dm755 $PKG/etc/sane.d/dll.d
  echo hpaio > $PKG/etc/sane.d/dll.d/hpaio
 
  # remove HAL .fdi file because HAL is no longer used
  rm -vrf $PKG/usr/share/hal
 
  # remove rc script
  rm -vrf $PKG/etc/init.d

  # add mixed license file
  install -Dt $PKG/usr/share/licenses/${name} -m644 COPYING

  # Compile Python bytecode:
  python3 -m compileall -d /usr/share $PKG/usr/share
  python3 -O -m compileall -d /usr/share $PKG/usr/share

}
