#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-lpr
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

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

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+=====+"
echo "| lpr |"
echo "+=====+"
cd $TMP
tar xzvf $CWD/lpr.tar.gz
cd lpr
make
cd lpr/lpc
cat lpc > $PKG/usr/sbin/lpc
man2gz lpc.8 $PKG/usr/man/preformat/cat8/lpc.8.gz $SRC/usr/man/man8/lpc.8
cd ../filters
cat lpf > $PKG/usr/sbin/lpf
cd ../lpd
cat lpd > $PKG/usr/sbin/lpd
man2gz lpd.8 $PKG/usr/man/preformat/cat8/lpd.8.gz $SRC/usr/man/man8/lpd.8
cd ../lpq
cat lpq > $PKG/usr/bin/lpq
man2gz lpq.1 $PKG/usr/man/preformat/cat1/lpq.1.gz $SRC/usr/man/man1/lpq.1
cd ../lpr
cat lpr > $PKG/usr/bin/lpr
man2gz lpr.1 $PKG/usr/man/preformat/cat1/lpr.1.gz $SRC/usr/man/man1/lpr.1
cd ../lprm
cat lprm > $PKG/usr/bin/lprm
man2gz lprm.1 $PKG/usr/man/preformat/cat1/lprm.1.gz $SRC/usr/man/man1/lprm.1
cd ../lptest
cat lptest > $PKG/usr/sbin/lptest
man2gz lptest.1 $PKG/usr/man/preformat/cat1/lptest.1.gz $SRC/usr/man/man1/lptest.1
cd ../pac
cat pac > $PKG/usr/sbin/pac
man2gz pac.8 $PKG/usr/man/preformat/cat8/pac.8.gz $SRC/usr/man/man8/pac.8
cd ..
man2gz printcap.5 $PKG/usr/man/preformat/cat5/printcap.5.gz $SRC/usr/man/man5/printcap.5

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

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