#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-find
SRC=/devel/manpagesrc
INFO=$PKG/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/_find.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 "| findutils-4.1 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/findutils-4.1.tar.gz
cd findutils-4.1
./configure --prefix=/usr
zcat $CWD/findutils-4.1.diff.gz | patch
make CFLAGS=-O2 LDFLAGS=-s
cat find/find > $PKG/usr/bin/find
cat locate/bigram > $PKG/usr/bin/bigram
cat locate/code > $PKG/usr/bin/code
cat locate/frcode > $PKG/usr/bin/frcode
cat locate/locate > $PKG/usr/bin/locate
cat locate/updatedb > $PKG/usr/bin/updatedb
cat xargs/xargs > $PKG/usr/bin/xargs
for page in xargs/xargs.1 find/find.1 locate/locate.1 locate/updatedb.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/`basename $page`.gz
done
cat locate/locatedb.5 | gzip -9c > $PKG/usr/man/man5/locatedb.5.gz
mkdir -p $INFO
cat doc/find.info | gzip -9c > $INFO/find.info.gz
cat doc/find.info-1 | gzip -9c > $INFO/find.info-1.gz
cat doc/find.info-2 | gzip -9c > $INFO/find.info-2.gz
cp doc/find.texi $TEX/find.texi

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

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