#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-lprng
SRC=/devel/manpagesrc
INFO=$PKG/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/_lprng.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 "| LPRng-3.1.3 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/LPRng-3.1.3.tar.gz
cd LPRng-3.1.3
CFLAGS='-O2 -I/usr/include/ncurses' LDFLAGS=-s LIBS=-lncurses \
  ./configure --prefix=/usr i586-slackware-linux
make
mkdir -p $PKG/usr/sbin
mkdir -p $PKG/usr/bin
cd src
for file in lpd lpf lpbanner ; do
 cat $file > $PKG/usr/sbin/$file
 strip $PKG/usr/sbin/$file
 chown root.bin $PKG/usr/sbin/$file
 chmod 755 $PKG/usr/sbin/$file
done
for file in lpq lprm lpr lpc checkpc lpraccnt readfilecount removeoneline \
authenticate_pgp setupauth ; do
 cat $file > $PKG/usr/bin/$file
 strip $PKG/usr/bin/$file
 chown root.bin $PKG/usr/bin/$file
 chmod 755 $PKG/usr/bin/$file
done
cd ../man
mkdir -p $PKG/usr/man/man1
mkdir -p $PKG/usr/man/man5
mkdir -p $PKG/usr/man/man8
for file in *.1 ; do
 cat $file | gzip -9c > $PKG/usr/man/man1/$file.gz
done
for file in *.5 ; do
 cat $file | gzip -9c > $PKG/usr/man/man5/$file.gz
done
for file in *.8 ; do
 cat $file | gzip -9c > $PKG/usr/man/man8/$file.gz
done

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

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