#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-inn
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
tar xzvf $CWD/_inn.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 "| inn1.4 |"
echo "+========+"
cd $TMP
tar xzvf $CWD/inn1.4.tar.gz
cd inn
make all
cd frontends
cat inews > $PKG/usr/lib/news/inews
cat rnews > $PKG/usr/lib/news/rnews
cat ctlinnd > $PKG/usr/lib/news/bin/ctlinnd
cat getlist > $PKG/usr/lib/news/bin/getlist
cat innconfval > $PKG/usr/lib/news/bin/innconfval
cat encode > $PKG/usr/lib/news/bin/rnews/encode
cat decode > $PKG/usr/lib/news/bin/rnews/decode
cd ../backends
cat archive > $PKG/usr/lib/news/bin/archive
cat batcher > $PKG/usr/lib/news/bin/batcher
cat buffchan > $PKG/usr/lib/news/bin/buffchan
cat cvtbatch > $PKG/usr/lib/news/bin/cvtbatch
cat filechan > $PKG/usr/lib/news/bin/filechan
cat innxmit > $PKG/usr/lib/news/bin/innxmit
cat nntpget > $PKG/usr/lib/news/bin/nntpget
cat overchan > $PKG/usr/lib/news/bin/overchan
cat shlock > $PKG/usr/lib/news/bin/shlock
cat shrinkfile > $PKG/usr/lib/news/bin/shrinkfile
cd ../expire
cat convdate > $PKG/usr/lib/news/bin/convdate
cat expire > $PKG/usr/lib/news/bin/expire
cat expireover > $PKG/usr/lib/news/bin/expireover
cat fastrm > $PKG/usr/lib/news/bin/fastrm
cat grephistory > $PKG/usr/lib/news/bin/grephistory
cat makeactive > $PKG/usr/lib/news/bin/makeactive
cat makehistory > $PKG/usr/lib/news/bin/makehistory
cat newsrequeue > $PKG/usr/lib/news/bin/newsrequeue
cat prunehistory > $PKG/usr/lib/news/bin/prunehistory
cd ../innd
cat innd > $PKG/usr/lib/news/etc/innd
cat inndstart > $PKG/usr/lib/news/etc/inndstart
cd ../nnrpd
cat nnrpd > $PKG/usr/lib/news/etc/in.nnrpd
cd ../doc
man2gz convdate.1 $PKG/usr/man/preformat/cat1/convdate.1.gz $SRC/usr/man/man1/convdate.1
man2gz getlist.1 $PKG/usr/man/preformat/cat1/getlist.1.gz $SRC/usr/man/man1/getlist.1
man2gz grephistory.1 $PKG/usr/man/preformat/cat1/grephistory.1.gz $SRC/usr/man/man1/grephistory.1
man2gz inews.1 $PKG/usr/man/preformat/cat1/inews.1.gz $SRC/usr/man/man1/inews.1
man2gz innconfval.1 $PKG/usr/man/preformat/cat1/innconfval.1.gz $SRC/usr/man/man1/innconfval.1
man2gz installit.1 $PKG/usr/man/preformat/cat1/installit.1.gz $SRC/usr/man/man1/installit.1
man2gz nntpget.1 $PKG/usr/man/preformat/cat1/nntpget.1.gz $SRC/usr/man/man1/nntpget.1
man2gz rnews.1 $PKG/usr/man/preformat/cat1/rnews.1.gz $SRC/usr/man/man1/rnews.1
man2gz shlock.1 $PKG/usr/man/preformat/cat1/shlock.1.gz $SRC/usr/man/man1/shlock.1
man2gz shrinkfile.1 $PKG/usr/man/preformat/cat1/shrinkfile.1.gz $SRC/usr/man/man1/shrinkfile.1
man2gz subst.1 $PKG/usr/man/preformat/cat1/subst.1.gz $SRC/usr/man/man1/subst.1
man2gz clientlib.3 $PKG/usr/man/preformat/cat3/clientlib.3.gz $SRC/usr/man/man3/clientlib.3
man2gz dbz.3 $PKG/usr/man/preformat/cat3/dbz.3.gz $SRC/usr/man/man3/dbz.3
man2gz inndcomm.3 $PKG/usr/man/preformat/cat3/inndcomm.3.gz $SRC/usr/man/man3/inndcomm.3
man2gz libinn.3 $PKG/usr/man/preformat/cat3/libinn.3.gz $SRC/usr/man/man3/libinn.3
man2gz parsedate.3 $PKG/usr/man/preformat/cat3/parsedate.3.gz $SRC/usr/man/man3/parsedate.3
man2gz qio.3 $PKG/usr/man/preformat/cat3/qio.3.gz $SRC/usr/man/man3/qio.3
man2gz strcasecmp.3 $PKG/usr/man/preformat/cat3/strcasecmp.3.gz $SRC/usr/man/man3/strcasecmp.3
man2gz wildmat.3 $PKG/usr/man/preformat/cat3/wildmat.3.gz $SRC/usr/man/man3/wildmat.3
man2gz active.5 $PKG/usr/man/preformat/cat5/active.5.gz $SRC/usr/man/man5/active.5
man2gz control.ctl.5 $PKG/usr/man/preformat/cat5/control.ctl.5.gz $SRC/usr/man/man5/control.ctl.5
man2gz distrib.pats.5 $PKG/usr/man/preformat/cat5/distrib.pats.5.gz $SRC/usr/man/man5/distrib.pats.5
man2gz expire.ctl.5 $PKG/usr/man/preformat/cat5/expire.ctl.5.gz $SRC/usr/man/man5/expire.ctl.5
man2gz history.5 $PKG/usr/man/preformat/cat5/history.5.gz $SRC/usr/man/man5/history.5
man2gz hosts.nntp.5 $PKG/usr/man/preformat/cat5/hosts.nntp.5.gz $SRC/usr/man/man5/hosts.nntp.5
man2gz inn.conf.5 $PKG/usr/man/preformat/cat5/inn.conf.5.gz $SRC/usr/man/man5/inn.conf.5
man2gz innwatch.ctl.5 $PKG/usr/man/preformat/cat5/innwatch.ctl.5.gz $SRC/usr/man/man5/innwatch.ctl.5
man2gz moderators.5 $PKG/usr/man/preformat/cat5/moderators.5.gz $SRC/usr/man/man5/moderators.5
man2gz newsfeeds.5 $PKG/usr/man/preformat/cat5/newsfeeds.5.gz $SRC/usr/man/man5/newsfeeds.5
man2gz newslog.5 $PKG/usr/man/preformat/cat5/newslog.5.gz $SRC/usr/man/man5/newslog.5
man2gz nnrp.access.5 $PKG/usr/man/preformat/cat5/nnrp.access.5.gz $SRC/usr/man/man5/nnrp.access.5
man2gz nntpsend.ctl.5 $PKG/usr/man/preformat/cat5/nntpsend.ctl.5.gz $SRC/usr/man/man5/nntpsend.ctl.5
man2gz overview.fmt.5 $PKG/usr/man/preformat/cat5/overview.fmt.5.gz $SRC/usr/man/man5/overview.fmt.5
man2gz passwd.nntp.5 $PKG/usr/man/preformat/cat5/passwd.nntp.5.gz $SRC/usr/man/man5/passwd.nntp.5
man2gz archive.8 $PKG/usr/man/preformat/cat8/archive.8.gz $SRC/usr/man/man8/archive.8
man2gz batcher.8 $PKG/usr/man/preformat/cat8/batcher.8.gz $SRC/usr/man/man8/batcher.8
man2gz buffchan.8 $PKG/usr/man/preformat/cat8/buffchan.8.gz $SRC/usr/man/man8/buffchan.8
man2gz ctlinnd.8 $PKG/usr/man/preformat/cat8/ctlinnd.8.gz $SRC/usr/man/man8/ctlinnd.8
man2gz cvtbatch.8 $PKG/usr/man/preformat/cat8/cvtbatch.8.gz $SRC/usr/man/man8/cvtbatch.8
man2gz expire.8 $PKG/usr/man/preformat/cat8/expire.8.gz $SRC/usr/man/man8/expire.8
man2gz expireover.8 $PKG/usr/man/preformat/cat8/expireover.8.gz $SRC/usr/man/man8/expireover.8
man2gz fastrm.8 $PKG/usr/man/preformat/cat8/fastrm.8.gz $SRC/usr/man/man8/fastrm.8
man2gz filechan.8 $PKG/usr/man/preformat/cat8/filechan.8.gz $SRC/usr/man/man8/filechan.8
man2gz innd.8 $PKG/usr/man/preformat/cat8/innd.8.gz $SRC/usr/man/man8/innd.8
man2gz innxmit.8 $PKG/usr/man/preformat/cat8/innxmit.8.gz $SRC/usr/man/man8/innxmit.8
man2gz news-recovery.8 $PKG/usr/man/preformat/cat8/news-recovery.8.gz $SRC/usr/man/man8/news-recovery.8
man2gz news.daily.8 $PKG/usr/man/preformat/cat8/news.daily.8.gz $SRC/usr/man/man8/news.daily.8
man2gz newslog.8 $PKG/usr/man/preformat/cat8/newslog.8.gz $SRC/usr/man/man8/newslog.8
man2gz nnrpd.8 $PKG/usr/man/preformat/cat8/nnrpd.8.gz $SRC/usr/man/man8/nnrpd.8
man2gz nntpsend.8 $PKG/usr/man/preformat/cat8/nntpsend.8.gz $SRC/usr/man/man8/nntpsend.8
man2gz overchan.8 $PKG/usr/man/preformat/cat8/overchan.8.gz $SRC/usr/man/man8/overchan.8
man2gz prunehistory.8 $PKG/usr/man/preformat/cat8/prunehistory.8.gz $SRC/usr/man/man8/prunehistory.8

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

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