#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-cnews
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/_cnews.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 "| c-news |"
echo "+========+"
cd $TMP
tar xzvf $CWD/c-news.tar.gz
cd cnews
zcat $CWD/c-news.patch1.gz | patch -p1
cd conf
sh doit.root
sh doit.bin
cd ../include
zcat $CWD/c-news.patch2.gz | patch
cd ../conf
sh doit.bin
cd ../expire
zcat $CWD/c-news.patch3.gz | patch
cd ../conf
sh doit.bin
cd ../expire
gcc -O2 -I../include  -s expire.o  ../libcnews.a  -o expire -ldbm
cd ../conf
sh doit.bin
cd ../relay
gcc -I../include -I. -DVOID=int -DFLUSHEVERY=6 -O2 -s relaynews.o active.o article.o caches.o mkdirs.o control.o fileart.o hdrdefs.o hdrparse.o hdrmunge.o history.o io.o msgs.o procart.o sys.o transmit.o trbatch.o ihave.o morefds.o  ../libcnews.a  -o relaynews -ldbm
cd ../conf
sh doit.bin
chmod 755 doit.news
su news -c doit.news
sh again.root
cd ../misc
cat canonhdr > $PKG/usr/lib/newsbin/canonhdr
cat ctime > $PKG/usr/lib/newsbin/ctime
man2gz ctime.1 $PKG/usr/man/preformat/cat1/ctime.1.gz $SRC/usr/man/man1/ctime.1
cat getabsdate > $PKG/usr/lib/newsbin/getabsdate
cat getdate > $PKG/usr/lib/newsbin/getdate
cat gngp > $PKG/usr/lib/newsbin/gngp
cat newslock > $PKG/usr/lib/newsbin/newslock
cat sizeof > $PKG/usr/lib/newsbin/sizeof
cd ../batch
cat batcher > $PKG/usr/lib/newsbin/batch/batcher
cat bencode > $PKG/usr/lib/newsbin/batch/bencode
man2gz bencode.1 $PKG/usr/man/preformat/cat1/bencode.1.gz $SRC/usr/man/man1/bencode.1
cat c7encode > $PKG/usr/lib/newsbin/batch/c7encode
cd ../expire
cat expire > $PKG/usr/lib/newsbin/expire/expire
cat histinfo > $PKG/usr/lib/newsbin/expire/histinfo
cat histslash > $PKG/usr/lib/newsbin/expire/histslash
cd ../inject
cat actflag > $PKG/usr/lib/newsbin/inject/actflag
cat defaults > $PKG/usr/lib/newsbin/inject/defaults
cd ../input
cat bdecode > $PKG/usr/lib/newsbin/input/bdecode
cat c7decode > $PKG/usr/lib/newsbin/input/c7decode
cat newsspool > $PKG/usr/lib/newsbin/input/newsspool
cd ../misc
cat locknews > $PKG/usr/lib/newsbin/maint/locknews
cd ../explode
cat explode > $PKG/usr/lib/newsbin/relay/explode
cd ../relay
cat relaynews > $PKG/usr/lib/newsbin/relay/relaynews
cd ../dbz
cat dbz > $PKG/usr/lib/newsbin/dbz
man2gz dbz.1 $PKG/usr/man/preformat/cat1/dbz.1.gz $SRC/usr/man/man1/dbz.1
cd ../conf
cat dostatfs > $PKG/usr/lib/newsbin/dostatfs
cd ../man
man2gz active.times.5 $PKG/usr/man/preformat/cat5/active.times.5.gz $SRC/usr/man/man5/active.times.5
man2gz checknews.1 $PKG/usr/man/preformat/cat1/checknews.1.gz $SRC/usr/man/man1/checknews.1
man2gz expire.8 $PKG/usr/man/preformat/cat8/expire.8.gz $SRC/usr/man/man8/expire.8
man2gz explode.8 $PKG/usr/man/preformat/cat8/explode.8.gz $SRC/usr/man/man8/explode.8
man2gz injnews.1 $PKG/usr/man/preformat/cat1/injnews.1.gz $SRC/usr/man/man1/injnews.1
man2gz news.5 $PKG/usr/man/preformat/cat5/news.5.gz $SRC/usr/man/man5/news.5
man2gz newsaux.8 $PKG/usr/man/preformat/cat8/newsaux.8.gz $SRC/usr/man/man8/newsaux.8
man2gz newsbatch.8 $PKG/usr/man/preformat/cat8/newsbatch.8.gz $SRC/usr/man/man8/newsbatch.8
man2gz newsctl.5 $PKG/usr/man/preformat/cat5/newsctl.5.gz $SRC/usr/man/man5/newsctl.5
man2gz newsdb.5 $PKG/usr/man/preformat/cat5/newsdb.5.gz $SRC/usr/man/man5/newsdb.5
man2gz newsmail.8 $PKG/usr/man/preformat/cat8/newsmail.8.gz $SRC/usr/man/man8/newsmail.8
man2gz newsmaint.8 $PKG/usr/man/preformat/cat8/newsmaint.8.gz $SRC/usr/man/man8/newsmaint.8
man2gz newssys.5 $PKG/usr/man/preformat/cat5/newssys.5.gz $SRC/usr/man/man5/newssys.5
man2gz postnews.1 $PKG/usr/man/preformat/cat1/postnews.1.gz $SRC/usr/man/man1/postnews.1
man2gz relaynews.8 $PKG/usr/man/preformat/cat8/relaynews.8.gz $SRC/usr/man/man8/relaynews.8
man2gz rnews.8 $PKG/usr/man/preformat/cat8/rnews.8.gz $SRC/usr/man/man8/rnews.8

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

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