#!/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.9 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/textutils-1.9.tar.gz
cd textutils-1.9
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cd src
cat cat > $PKG/bin/cat
cat cut > $PKG/bin/cut
cat head > $PKG/bin/head
cat cksum > $PKG/usr/bin/cksum
cat comm > $PKG/usr/bin/comm
cat csplit > $PKG/usr/bin/csplit
cat expand > $PKG/usr/bin/expand
cat fold > $PKG/usr/bin/fold
cat join > $PKG/usr/bin/join
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 fold.1 head.1 join.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

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

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