#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-nn-nntp
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/_nn-nntp.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 "| nn-6.4.18 for NNTP |"
echo "+====================+"
cd $TMP
tar xzvf $CWD/nn-6.4.18.tar.gz
cd nn
zcat $CWD/nn-6.4.18.nntp.diff.gz | patch -p1
make
cat nn > $PKG/usr/bin/nn
cat nnmaster > $PKG/usr/lib/nn/nnmaster
cat usercheck > $PKG/usr/lib/nn/usercheck
cd man
cat nn.1 | gzip -9c > $PKG/usr/man/man1/nn.1.gz
cat nnacct.1m | gzip -9c > $PKG/usr/man/man1/nnacct.1.gz
cat nnadmin.1m | gzip -9c > $PKG/usr/man/man1/nnadmin.1.gz
cat nncheck.1 | gzip -9c > $PKG/usr/man/man1/nncheck.1.gz
cat nngoback.1 | gzip -9c > $PKG/usr/man/man1/nngoback.1.gz
cat nngrab.1 | gzip -9c > $PKG/usr/man/man1/nngrab.1.gz
cat nngrep.1 | gzip -9c > $PKG/usr/man/man1/nngrep.1.gz
cat nnmaster.8 | gzip -9c > $PKG/usr/man/man8/nnmaster.8.gz
cat nnpost.1 | gzip -9c > $PKG/usr/man/man1/nnpost.1.gz
cat nnspew.8 | gzip -9c > $PKG/usr/man/man8/nnspew.8.gz
cat nnstats.1m | gzip -9c > $PKG/usr/man/man1/nnstats.1.gz
cat nntidy.1 | gzip -9c > $PKG/usr/man/man1/nntidy.1.gz
cat nnusage.1m | gzip -9c > $PKG/usr/man/man1/nnusage.1.gz

# Build the package:
cd $PKG
tar czvf $TMP/nn-nntp.tgz .

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