#!/bin/bash
ulimit -s unlimited
shopt -s extglob
# Fail on undeclared variables and broken pipes:
#set -u -o pipefail

# hplip.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Slackware build script for hplip
# Copyright 2006-2026  Robby Workman  (http://rlworkman.net)
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
#tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-*/ || failextract
slackhousekeeping

# Apply patches:
#
# Upgrades should not be done outside the package system:
zcat $CWD/hplip.no.upgrade.diff.gz | patch -p1 --verbose || exit 1

# Set LC_ALL=C to fix issues with CUPS output parsing in hp-setup:
zcat $CWD/setup.py.lc_all.c.diff.gz | patch -p1 --verbose || exit 1

# Fix a few .py files lacking #!/usr/bin/python3:
zcat $CWD/hplip.python3.shebang.diff.gz | patch -p1 --verbose || exit 1

# Fix the desktop file to show correct icon and category
zcat $CWD/fix-desktop-file.patch.gz | patch -p1 --verbose || exit 1

# GCC 14 fixes:
cat $CWD/hplip-pserror-c99.patch | patch -p1 --verbose || exit 1
cat $CWD/hplip-hpaio-gcc14.patch | patch -p1 --verbose || exit 1
cat $CWD/hplip-covscan.patch | patch -p1 --verbose || exit 1
cat $CWD/hplip-scan-orblite-c99.patch | patch -p1 --verbose || exit 1
cat $CWD/hplip-scan-hpaio-include.patch | patch -p1 --verbose || exit 1

cat $CWD/hplip-gcc15-stdc23.patch | patch -p1 --verbose || exit 1

# This extension was never ported to python3:
cat $CWD/hplip-pcardext-disable.patch | patch -p1 --verbose || exit 1
# Fix a crash that hplip causes in the colord service:
cat $CWD/hplip.colord.segfault.fix.patch | patch -p1 --verbose || exit 1

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

# Add a cups-2.2.x header since some definitions were moved:
zcat $CWD/0021-Add-include-cups-ppd.h-in-various-places-as-CUPS-2.2.patch.gz | patch -p1 -l --verbose || exit 1

# Forget it folks - this ImageProcessor closed-source blob is broken, and there
# is no way to fix it. We will use this patch from Debian to remove it from
# hplip, reverting to the way things worked in the 3.18.6 release. If HP ever
# sees fit to make ImageProcessor mandatory, we'll likely stick with the last
# hplip release that can be made to work without it, and any HP printers that
# require a newer version of hplip will not be supported.
#
# https://src.fedoraproject.org/rpms/hplip/tree/rawhide
zcat $CWD/0025-Remove-all-ImageProcessor-functionality-which-is-clo.patch.gz | patch -p1 -l --verbose || exit 1

# autoreconf will fail if these files do not exist:
for file in NEWS README AUTHORS ChangeLog ; do
  if [ ! -r $file ]; then
    touch $file
  fi
done

# Needed because Makefile.am was patched:
autoreconf -vif || exit 1

# Configure:
CFLAGS="$SLKCFLAGS -I$(ls -d /usr/include/python3.*)" \
CXXFLAGS="$SLKCFLAGS" \
LDFLAGS="-lpthread" \
PYTHON=/usr/bin/python3 \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-cupsbackenddir=/usr/lib${LIBDIRSUFFIX}/cups/backend \
   --with-cupsfilterdir=/usr/lib${LIBDIRSUFFIX}/cups/filter \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --with-docdir=/usr/doc/$PKGNAM-$VERSION \
   --with-htmldir=/usr/doc/$PKGNAM-$VERSION/html \
   --with-hpppddir=/usr/share/ppd/HP \
   --with-drvdir=/usr/share/cups/drv/HP \
   --enable-hpijs-install \
   --enable-scan-build \
   --enable-gui-build \
   --enable-fax-build \
   --enable-doc-build \
   --enable-foomatic-rip-hplip-install \
   --enable-pp-build \
   --disable-foomatic-ppd-install \
   --disable-imageProcessor-build \
   --enable-foomatic-drv-install \
   --enable-network-build=yes \
   --enable-qt5 \
   --enable-policykit \
   --enable-cups-ppd-install \
   --build=${SLK_ARCH_BUILD} \
   --host=${SLK_ARCH_HOST} || failconfig

# just in case the latest rip out patch does not work, here's what does:
##
## HACK! Rip out this crap for ARM.  We could do an autoreconf, but it breaks
## and I'm in no mood to figure it out when I can hack it out as a temporary fix.
##
#grep -Flr -- '-lImageProcessor' . | xargs sed -i 's?-lImageProcessor??g'
#sed -i 's?prnt/hpcups/libImageProcessor-x86_64.so??g' Makefile
#sed -i 's?prnt/hpcups/libImageProcessor-x86_32.so??g' Makefile
#sed -i 's?-lImageProcessor??g' Makefile

# Build:
make $NUMJOBS || make || failmake

# Install into package:
# We fail over to make -i because there are some overlapping PPD files
# that cause make install to fail.  For some reason this doesn't fail
# on ARM but does on AArch64.
make install DESTDIR=$PKG rulesdir=/lib/udev/rules.d || \
make -i install DESTDIR=$PKG rulesdir=/lib/udev/rules.d

# Move dbus configs to system location:
mkdir -vpm755 $PKG/usr/share/dbus-1/system.d/
mv -fv $PKG/etc/dbus-1/system.d/* $PKG/usr/share/dbus-1/system.d/
rmdir --parents $PKG/etc/dbus-1/system.d/

# 3.16.8 is not making this directory even though several scripts
# continue to make use of it:
mkdir -p $PKG/var/lib/hp/

# HAL is long gone:
rm -rf $PKG/usr/share/hal

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp -p      # run makepkg -l y -c n --prepend

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
