#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-rcs
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/_rcs.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 "| rcs5.6.0.1 |"
echo "+============+"
cd $TMP
tar xzvf $CWD/rcs5.6.0.1.tar.gz
cd rcs5.6.0.1
cd src
cp conf.h.debian conf.h
cd ..
zcat $CWD/rcs5.6.0.1.diff.gz | patch
make
cd src
cat ci > $PKG/usr/bin/ci
cat co > $PKG/usr/bin/co
cat ident > $PKG/usr/bin/ident
cat merge > $PKG/usr/bin/merge
cat rcs > $PKG/usr/bin/rcs
cat rcsdiff > $PKG/usr/bin/rcsdiff
cat rcsmerge > $PKG/usr/bin/rcsmerge
cat rcstest > $PKG/usr/bin/rcstest
cat rlog > $PKG/usr/bin/rlog
cd ../man
man2gz ci.1 $PKG/usr/man/preformat/cat1/ci.1.gz $SRC/usr/man/man1/ci.1
man2gz co.1 $PKG/usr/man/preformat/cat1/co.1.gz $SRC/usr/man/man1/co.1
man2gz ident.1 $PKG/usr/man/preformat/cat1/ident.1.gz $SRC/usr/man/man1/ident.1
man2gz merge.1 $PKG/usr/man/preformat/cat1/merge.1.gz $SRC/usr/man/man1/merge.1
man2gz rcs.1 $PKG/usr/man/preformat/cat1/rcs.1.gz $SRC/usr/man/man1/rcs.1
man2gz rcsclean.1 $PKG/usr/man/preformat/cat1/rcsclean.1.gz $SRC/usr/man/man1/rcsclean.1
man2gz rcsdiff.1 $PKG/usr/man/preformat/cat1/rcsdiff.1.gz $SRC/usr/man/man1/rcsdiff.1
man2gz rcsfile.5 $PKG/usr/man/preformat/cat5/rcsfile.5.gz $SRC/usr/man/man5/rcsfile.5
man2gz rcsfreeze.1 $PKG/usr/man/preformat/cat1/rcsfreeze.1.gz $SRC/usr/man/man1/rcsfreeze.1
man2gz rcsintro.1 $PKG/usr/man/preformat/cat1/rcsintro.1.gz $SRC/usr/man/man1/rcsintro.1
man2gz rcsmerge.1 $PKG/usr/man/preformat/cat1/rcsmerge.1.gz $SRC/usr/man/man1/rcsmerge.1
man2gz rlog.1 $PKG/usr/man/preformat/cat1/rlog.1.gz $SRC/usr/man/man1/rlog.1

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

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