#!/bin/bash

# cups.SlackBuild
# Heavily based on the original Slackware build script by Patrick Volkerding,
# Modified by Stuart Winter <stuart@armedslack.org>
# 24-Jul-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/a/cups
export PORTCWD=$PWD

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

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION-source.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

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

# Configure:
./configure \
   --disable-pam || failconfig

# Build:
make || failmake

# Start fakeroot server if required:
start_fakeroot

# Install into package:
make BUILDROOT=$PKG install || failinstall

# This will get you the most useful SMB support for free.
( cd $PKG/usr/lib/cups/backend
  if [ ! -r smb ]; then
    ln -sf /usr/bin/smbspool smb
  fi )

# Fix manpages:
( cd $PKG/usr/share/man
  find . -type d -name "cat*" | xargs rm -rf )
( cd $PKG/usr/share ; mv man .. )
slackgzpages -i # compress man & info pages and delete usr/info/dir
#
# Now fix the associated man pages:
mv $PKG/usr/man/man1/cancel.1.gz $PKG/usr/man/man1/cancel-${SUFFIX}.1.gz
mv $PKG/usr/man/man1/lp.1.gz $PKG/usr/man/man1/lp-${SUFFIX}.1.gz
mv $PKG/usr/man/man1/lpq.1.gz $PKG/usr/man/man1/lpq-${SUFFIX}.1.gz
mv $PKG/usr/man/man1/lpr.1.gz $PKG/usr/man/man1/lpr-${SUFFIX}.1.gz
mv $PKG/usr/man/man1/lprm.1.gz $PKG/usr/man/man1/lprm-${SUFFIX}.1.gz
mv $PKG/usr/man/man1/lpstat.1.gz $PKG/usr/man/man1/lpstat-${SUFFIX}.1.gz
mv $PKG/usr/man/man8/lpc.8.gz $PKG/usr/man/man8/lpc-${SUFFIX}.8.gz
( cd $PKG/usr/man/man1
  ln -sf cancel-${SUFFIX}.1.gz cancel.1.gz
  ln -sf lp-${SUFFIX}.1.gz lp.1.gz
  ln -sf lpq-${SUFFIX}.1.gz lpq.1.gz
  ln -sf lpr-${SUFFIX}.1.gz lpr.1.gz
  ln -sf lprm-${SUFFIX}.1.gz lprm.1.gz
  ln -sf lpstat-${SUFFIX}.1.gz lpstat.1.gz )
( cd $PKG/usr/man/man8
  ln -sf lpc-${SUFFIX}.8.gz lpc.8.gz )

# Adjust for BSD init scripts.  Note that rc.cups will have to be
# started from somewhere, like rc.M or rc.inet2.
( cd $PKG/etc/rc.d
  mv init.d/cups rc.cups
  rm -r init.d rc?.d )

# Handle this as a config file, and non-executable in a default install:
mv $PKG/etc/rc.d/rc.cups $PKG/etc/rc.d/rc.cups.new
chmod 644 $PKG/etc/rc.d/rc.cups.new

# Fix ownerships. We don't call slackchown because 
# -rwsr-xr-x lp/sys /usr/bin/lppasswd
# no need to do that anymore but it's worth keeping this note here
# as a remidner that not all files are owned root:root.

# Adjust/expand docs:
( mv $PKG/usr/share/doc/cups $PKG/usr/doc/cups-$VERSION
  cd $PKG/usr/doc
  rm -rf $PKG/usr/share/doc
  ln -sf cups-$VERSION cups )

# Apply no-clobber fix to conffiles:
( cd $PKG/etc/cups
  for file in * ; do
    if [ -f $file ]; then
      mv $file $file.new
    fi
  done )

# Remove .pdf docs (Pat don't like 'em ;-) )
find $PKG/usr/doc -type f -name '*.pdf' -print0 | xargs -0 rm -f

# Use symlinks to certain binaries so that CUPS and LPRng can coexist:
SUFFIX=cups
for file in \
usr/bin/cancel \
usr/bin/lp \
usr/bin/lpq \
usr/bin/lpr \
usr/bin/lprm \
usr/bin/lpstat \
usr/sbin/lpc ; do
  ( cd $PKG
    mv ${file} ${file}-${SUFFIX}
    ( cd `dirname ${file}` ; ln -sf `basename ${file}`-${SUFFIX} `basename ${file}` )
  )
done

# Set Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack644docs    # Set the correct permissions for docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp      # run makepkg
 else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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