#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-lpr

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
explodepkg $CWD/_lpr.tar.gz

echo "+==========+"
echo "| lpr-0.48 |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/lpr_0.48.orig.tar.gz
cd lpr-0.48
zcat $CWD/lpr_0.48-1.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig
zcat $CWD/lpr-maxwidth.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig
zcat $CWD/lpr-acctfile.diff.gz | patch -p1 -E --verbose --backup --suffix=.orig
make
cd lpc
cat lpc > $PKG/usr/sbin/lpc
cat lpc.8 | gzip -9c > $PKG/usr/man/man8/lpc.8.gz
cd ../filters
cat lpf > $PKG/usr/sbin/lpf
cd ../lpd
cat lpd > $PKG/usr/sbin/bsdlpd
cat lpd.8 | gzip -9c > $PKG/usr/man/man8/lpd.8.gz
cd ../lpq
cat lpq > $PKG/usr/bin/lpq
cat lpq.1 | gzip -9c > $PKG/usr/man/man1/lpq.1.gz
cd ../lpr
cat lpr > $PKG/usr/bin/lpr
cat lpr.1 | gzip -9c > $PKG/usr/man/man1/lpr.1.gz
cd ../lprm
cat lprm > $PKG/usr/bin/lprm
cat lprm.1 | gzip -9c > $PKG/usr/man/man1/lprm.1.gz
cd ../lptest
cat lptest > $PKG/usr/sbin/lptest
cat lptest.1 | gzip -9c > $PKG/usr/man/man1/lptest.1.gz
cd ../pac
cat pac > $PKG/usr/sbin/pac
cat pac.8 | gzip -9c > $PKG/usr/man/man8/pac.8.gz
cd ..
cat printcap.5 | gzip -9c > $PKG/usr/man/man5/printcap.5.gz
strip $PKG/usr/bin/*
strip $PKG/usr/sbin/*

# Build the package:
cd $PKG
tar czvf $TMP/lpr.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/lpr-0.48
  rm -rf $PKG
fi
