#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-find
INFO=$PKG/usr/info

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

echo "+===============+"
echo "| findutils-4.1 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/findutils-4.1.tar.gz
cd findutils-4.1
zcat $CWD/findutils-4.1.diff.gz | patch -p1 -E
./configure --prefix=/usr
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 $CWD/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

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

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

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