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

PKG=$TMP/package-ipfwadm

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

# Find the size of a file:
filesize() {
 SIZE=`ls -l -d -G $1 | cut -b23-32`
 echo -n $SIZE
}

echo "+======================+"
echo "| ipfwadm-2.3.0.tar.gz |"
echo "+======================+"
cd $TMP
tar xzvf $CWD/ipfwadm-2.3.0.tar.gz
cd ipfwadm-2.3.0
mkdir -p $PKG/usr/doc/ipfwadm-2.3.0
cp -a COPYING ChangeLog README ipfwadm.lsm $PKG/usr/doc/ipfwadm-2.3.0
chmod 644 $PKG/usr/doc/ipfwadm-2.3.0/*
chown root.root $PKG/usr/doc/ipfwadm-2.3.0/*
# These don't build under glibc.  It's getting phased out anyway in the near
# future unless a Linux 2.2.x version is developed.
#make
#strip ipfwadm
cat ipfwadm > $PKG/sbin/ipfwadm-2.3.0
cat ipfwadm.8 | gzip -9c > $PKG/usr/man/man8/ipfwadm.8.gz
cat ipfw.4 | gzip -9c > $PKG/usr/man/man4/ipfw.4.gz

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