#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-txtutils
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/_txtutils.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 "| textutils-1.22 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/textutils-1.22.tar.gz
cd textutils-1.22
mkdir $PKG/usr/doc/textutils-1.22
cp -a NEWS README $PKG/usr/doc/textutils-1.22
chmod 644 $PKG/usr/doc/textutils-1.22/*
chown root.root $PKG/usr/doc/textutils-1.22/*
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cd src
strip *
cat cat > $PKG/bin/cat
cat cksum > $PKG/usr/bin/cksum
cat comm > $PKG/usr/bin/comm
cat csplit > $PKG/usr/bin/csplit
cat cut > $PKG/bin/cut
cat expand > $PKG/usr/bin/expand
cat fmt > $PKG/usr/bin/fmt
cat fold > $PKG/usr/bin/fold
cat head > $PKG/bin/head
cat join > $PKG/usr/bin/join
cat md5sum > $PKG/usr/bin/md5sum
cat nl > $PKG/usr/bin/nl
cat od > $PKG/usr/bin/od
cat paste > $PKG/usr/bin/paste
cat pr > $PKG/usr/bin/pr
cat sort > $PKG/usr/bin/sort
cat split > $PKG/usr/bin/split
cat sum > $PKG/usr/bin/sum
cat tac > $PKG/usr/bin/tac
cat tail > $PKG/usr/bin/tail
cat tr > $PKG/usr/bin/tr
cat unexpand > $PKG/usr/bin/unexpand
cat uniq > $PKG/usr/bin/uniq
cat wc > $PKG/usr/bin/wc
cd ../man
for page in cat.1 cksum.1 comm.1 csplit.1 cut.1 expand.1 fmt.1 \
fold.1 head.1 join.1 md5sum.1 nl.1 od.1 paste.1 pr.1 sort.1 \
split.1 sum.1 tac.1 tail.1 tr.1 unexpand.1 uniq.1 wc.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cd ../doc
gzip -9c textutils.info > $PKG/usr/info/textutils.info.gz

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

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