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

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/_netpipes.tar.gz

echo "+==============+"
echo "| netpipes-4.2 |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/netpipes-4.2-export.tar.gz
cd netpipes-4.2-export
mkdir -p $PKG/usr/doc/netpipes-4.2-export
cp -a COPYING README *.html $PKG/usr/doc/netpipes-4.2-export
chown root.root $PKG/usr/doc/netpipes-4.2-export/*
chmod 644 $PKG/usr/doc/netpipes-4.2-export/*
make
strip encapsulate faucet getpeername hose sockdown timelimit
for file in encapsulate faucet getpeername hose sockdown timelimit ; do
  cat $file > $PKG/usr/bin/$file
done
mkdir -p $PKG/usr/man/man1
for file in encapsulate faucet getpeername hose sockdown timelimit ; do
  cat $file.1 | gzip -9c > $PKG/usr/man/man1/$file.1.gz
done

# Build the package:
cd $PKG
tar czvf $TMP/netpipes.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/netpipes-4.2-export
  rm -rf $PKG
fi
