#!/bin/bash

# Slackware build script for cups 

# Copyright 2026 Frank Honolka <info@snukware.com>
# 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.

cd $(dirname $0) ; CWD=$(pwd)
PRGNAM=cups			
PRGNAM2=libcups			
VERSION=${VERSION:-2.4.19}	
BUILD=${BUILD:-1}
TAG=${TAG:-_snuk}		
PKGTYPE=${PKGTYPE:-txz}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/var/cache/snukware/tmp}	
PKG=$TMP/package-$PRGNAM
PKG2=$TMP/package-$PRGNAM2
OUTPUT=${OUTPUT:-/var/cache/snukware/ap}	

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e 

rm -rf $PKG $PKG2
mkdir -p $TMP $PKG $PKG2 $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.?z
cd $PRGNAM-$VERSION

# move /var/run -> /run for pid file
cat "${CWD}"/patches/cups-2.4.0-statedir.patch | patch -p1 --verbose || exit 1

  # bug fixes

# https://github.com/OpenPrinting/cups/issues/53
# use IP_FREEBIND, because cupsd cannot bind to not yet existing IP address
cat "${$CWD}"/patches/cups-freebind.patch | patch -p1 --verbose || exit 1

# FS#56818 - https://github.com/apple/cups/issues/5236
cat "${CWD}"/patches/guid.patch | patch -p1 --verbose || exit 1

# Rebuild configure script
aclocal -I config-scripts
autoconf -I config-scripts

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
  # use fixed cups user (id 209) since systemd adds "lp" group without a fixed id
  ./configure --prefix=/usr \
     --sysconfdir=/etc \
     --localstatedir=/var \
     --sbindir=/usr/bin \
     --libdir=/usr/lib \
     --with-logdir=/var/log/cups \
     --with-docdir=/usr/share/cups/doc \
     --with-exe-file-perm=0755 \
     --with-cups-user=209 \
     --with-cups-group=209 \
     --with-max-log-size=0 \
     --enable-pam=yes \
     --enable-raw-printing \
     --enable-dbus=yes \
     --with-tls=gnutls \
     --with-dbusdir=/usr/share/dbus-1 \
     --enable-relro \
     --enable-libpaper \
     --with-optim="$SLKCFLAGS" #--help
  make

make BUILDROOT="${PKG}" install-data install-exec

# this one we ship in the libcups pkg
cp $PKG/usr/bin/cups-config $CWD/ || exit 1
rm -f "${PKG}"/usr/bin/cups-config || exit 1

# some modifications adopted by Arch Linux and Fedora
# kill the sysv stuff
rm -rf "${PKG}"/etc/rc*.d || exit 1
rm -rf "${PKG}"/etc/init.d || exit 1
install -D -m644 $CWD/files/cups.logrotate "${PKG}"/etc/logrotate.d/cups || exit 1
install -D -m644 $CWD/files/cups.pam "${PKG}"/etc/pam.d/cups || exit 1

# fix perms on /var/spool and /etc
chmod 755 "${PKG}"/var/spool
chmod 755 "${PKG}"/etc

  # use cups group FS#36769
install -Dm644 "$CWD"/files/cups.sysusers "${PKG}/usr/lib/sysusers.d/$PRGNAM.conf" || exit 1
sed -i "s:#User 209:User 209:" "${PKG}"/etc/cups/cups-files.conf{,.default} || exit 1
sed -i "s:#Group 209:Group 209:" "${PKG}"/etc/cups/cups-files.conf{,.default} || exit 1

# install ssl directory where to store the certs, solves some samba issues
install -dm700 -g 209 "${PKG}"/etc/cups/ssl || exit 1
# remove directory from package, it will be recreated at each server start
rm -rf "${PKG}"/run || exit 1

# install some more configuration files that will get filled by cupsd
touch "${PKG}"/etc/cups/printers.conf  
touch "${PKG}"/etc/cups/classes.conf
touch "${PKG}"/etc/cups/subscriptions.conf
chgrp -R 209 "${PKG}"/etc/cups

sed -i 's|^Exec=htmlview http://localhost:631/|Exec=xdg-open http://localhost:631/|g' "${PKG}"/usr/share/applications/cups.desktop || exit 1

# compress some driver files, adopted from Fedora
find "${PKG}"/usr/share/cups/model -name "*.ppd" | xargs gzip -n9f || exit 1

# remove client.conf man page
rm -f "${PKG}"/usr/share/man/man5/client.conf.5 || exit 1

# comment out removed filters that are now part of cups-filters
perl -p -i -e 's:^(.*\s+bannertops\s*)$:#\1:' "$PKG"/usr/share/cups/mime/mime.convs || exit 1

# comment out unnecessary PageLogFormat entry
sed -i -e 's:PageLogFormat:#PageLogFormat:' "$PKG"/etc/cups/cupsd.conf* || exit 1

# no more xinetd support
rm -rf "${PKG}"/etc/xinetd.d || exit 1

# Don't ship .la files:
rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

if [ -d $PKG/usr/share/man ]; then
	mv $PKG/usr/share/man $PKG/usr/ 
fi

if [ -d $PKG/usr/man ]; then
find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
fi

find $PKG -name perllocal.pod \
  -o -name ".packlist" \
  -o -name "*.bs" \
  | xargs rm -f

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
DOCS="LICENSE NOTICE"
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

pushd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.txz
/sbin/upgradepkg --install-new --reinstall $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.txz
popd


make BUILDROOT="${PKG2}" install-headers install-libs

# put this into the libs pkg to make other software find the libs(no pkg-config file included)
mkdir -p "${PKG2}"/usr/bin
install -m755 "${CWD}"/cups-config "${PKG2}"/usr/bin/cups-config || exit 1

mkdir -p $PKG2/usr/doc/${PRGNAM2}-$VERSION

DOCS="LICENSE NOTICE"
cp -a $DOCS $PKG2/usr/doc/$PRGNAM2-$VERSION

mkdir -p $PKG2/install
cat $CWD/$PRGNAM2.slack-desc > $PKG2/install/slack-desc
cd $PKG2
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM2-$VERSION-$ARCH-$BUILD$TAG.txz
/sbin/upgradepkg --install-new --reinstall $OUTPUT/$PRGNAM2-$VERSION-$ARCH-$BUILD$TAG.txz
