#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# cups.SlackBuild
# Heavily based on the original Slackware build script by Patrick Volkerding,
# Modified by Stuart Winter <mozes@slackware.com>
# 24-Jul-2004
#
# Copyright 2008-2026  Patrick J. Volkerding, Sebeka, Minnesota, USA
# 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.

# 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 xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-*/ || failextract
slackhousekeeping

# Create package framework:
mkdir -p $PKG/{etc/cups,var/spool,usr/doc}

# Turn off stack protector.
#grep -Flr -- '-fstack-protector' . | xargs sed -i 's?-fstack-protector?-fno-stack-protector?g'

# Apply patches:
sed -i.orig -e 's#$exec_prefix/lib/cups#$libdir/cups#g' configure

# Choose correct options depending on whether PAM is installed:
if [ -L /lib${LIBDIRSUFFIX}/libpam.so.? ]; then
   PAM_OPTIONS="--enable-pam"
   unset SHADOW_OPTIONS
else
   unset PAM_OPTIONS
   SHADOW_OPTIONS="--disable-pam"
fi

# Configure:
#aclocal && autoconf || failconfig
CC=gcc \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-pkgconfpath=/usr/lib${LIBDIRSUFFIX}/pkgconfig \
   --enable-cdsassl=no \
   --docdir=/usr/doc \
   --mandir=/usr/man \
   --with-rundir=/run/cups \
   $PAM_OPTIONS \
   $SHADOW_OPTIONS \
   --build=${SLK_ARCH_BUILD} || failconfig

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

# Install into package:
mkdir -p $PKG/etc/cups
mkdir -p $PKG/var/spool
make BUILDROOT=$PKG install || failinstall

# These files are lost with a restart, so don't package them:
rm -r $PKG/run

if [ ! -z "$PAM_OPTIONS" ]; then
   # Allow pam config files to be edited by root:
   chmod 644 $PKG/etc/pam.d/*
   # Make PAM file .new:
   mv -fv $PKG/etc/pam.d/cups $PKG/etc/pam.d/cups.new
fi

# Remove files that overlap with the cups-filters package:
( cd $PKG
  rm -f \
  usr/share/cups/banners/classified \
  usr/share/cups/banners/confidential \
  usr/share/cups/banners/secret \
  usr/share/cups/banners/standard \
  usr/share/cups/banners/topsecret \
  usr/share/cups/banners/unclassified \
  usr/share/cups/data/testprint
)

# Hey, what's with the gigantic test files?  Bloat is bad.
rm -f $PKG/usr/share/cups/ipptool/*.{pdf,ps,jpg}

# I've added so many things like /etc/init.d/ to Slackware that CUPS
# is now installing init scripts to the Red Hat locations.  We'll move
# them to the usual locations:
mkdir -p $PKG/etc/rc.d
# Handle this as a config file, and non-executable in a default install:
mv $PKG/etc/init.d/cups $PKG/etc/rc.d/rc.cups.new
chmod 644 $PKG/etc/rc.d/rc.cups.new
# Clear out the additions:
rm -rf $PKG/etc/init.d $PKG/etc/rc{0,2,3,5}.d

# Move the dbus config file to the system directory
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/

# For full CUPS SMB support, you'll need to install the cups-samba
# package from the source in this directory.  There's no easy way
# to add that to a package build, and the requests aren't pouring in,
# so you'll have to install it yourself.  It's easy to do.

# However, this will get you the most useful SMB support for free.
# Thanks to Boris Kurktchiev for the tip.  :-)
( cd $PKG/usr/lib${LIBDIRSUFFIX}/cups/backend
  if [ ! -e smb ]; then
    ln -sf /usr/bin/smbspool smb
  fi )

# Sanity check:
if [ ! -f $PKG/etc/rc.d/rc.cups.new ]; then
   echo "ERROR: /etc/rc.d/rc.cups.new is missing"
   exit 1
fi

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

# Adjust/expand docs:
( mkdir -p $PKG/usr/doc
  mv $PKG/usr/share/doc/cups $PKG/usr/doc/cups-$VERSION
  rmdir $PKG/usr/share/doc
  cd $PKG/usr/doc
  ln -sf cups-$VERSION cups )
cp -fav \
    CHANGES* CONTRIBUTING* CREDITS* DEVELOPING* INSTALL* LICENSE* NOTICE* README* SECURITY* \
    $PKG/usr/doc/${PKGNAM}-$VERSION
changelogliposuction CHANGES.md $PKGNAM $VERSION

# I'm sorry, but PDF files are almost as bloated and annoying as
# MS Word documents.  We'll retain the HTML files in /usr/doc.
( cd $PKG/usr/doc
  find . -name "*.pdf" -exec rm -f {} \; )

# In /etc/cups/cupsd.conf, the IdleExitTimeout directive will throw an error
# in the logs if HAVE_ONDEMAND (systemd related...) is not defined. So if
# we see that situation, edit the conf file:
if grep -q "undef HAVE_ONDEMAND" config.h ; then
  sed -i '/Timeout after cupsd exits if idle/d' $PKG/etc/cups/cupsd.conf
  sed -i '/IdleExitTimeout/,+1d' $PKG/etc/cups/cupsd.conf
  install -vpm644 $PKG/etc/cups/cupsd.conf $PKG/etc/cups/cupsd.conf.default
fi

# Handle .conf files with config() in the install script.
# Not .conf.default copies, though.
( cd $PKG/etc/cups
  for file in *.conf ; do
    if [ -f $file ]; then
      mv -fv $file $file.new
    fi
  done )

# Set Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slack644docs    # Set the correct permissions for docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

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