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

PKG=$TMP/package-ipchains

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

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

echo "+=======================+"
echo "| ipchains-1.3.10.tar.gz |"
echo "+=======================+"
cd $TMP
tar xzvf $CWD/ipchains-1.3.10.tar.gz
cd ipchains-1.3.10
make clean
make
strip ipchains
cat ipchains > $PKG/sbin/ipchains
cat ipchains.8 | gzip -9c > $PKG/usr/man/man8/ipchains.8.gz
mkdir -p $PKG/usr/man/man4
cat ipfw.4 | gzip -9c > $PKG/usr/man/man4/ipfw.4.gz
mkdir -p $PKG/usr/doc/ipchains-1.3.10
cp -a COPYING HOWTO.txt README $PKG/usr/doc/ipchains-1.3.10
chmod 644 $PKG/usr/doc/ipchains-1.3.10/*
chown root.root $PKG/usr/doc/ipchains-1.3.10/*

echo "+===============================+"
echo "| ipchains-scripts-1.1.2.tar.gz |"
echo "+===============================+"
cd $TMP
tar xzvf $CWD/ipchains-scripts-1.1.2.tar.gz
cd ipchains-scripts-1.1.2
zcat $CWD/ipchains-scripts-1.1.2.diff.gz | patch -p1 --backup
for file in ipchains-restore ipchains-save ipfwadm-wrapper ; do
  cat $file > $PKG/sbin/$file
  chmod 755 $PKG/sbin/$file
  chown root.bin $PKG/sbin/$file
  cat $file.8 | gzip -9c > $PKG/usr/man/man8/$file.8.gz
done

echo "+========================+"
echo "| ipmasqadm-0.4.2.tar.gz |"
echo "+========================+"
cd $TMP
tar xzvf $CWD/ipmasqadm-0.4.2.tar.gz
cd ipmasqadm-0.4.2
make KSRC=/usr/src/linux/
cp modules/*.so $PKG/usr/lib/ipmasqadm
strip ipmasqadm/ipmasqadm
cat ipmasqadm/ipmasqadm > $PKG/usr/sbin/ipmasqadm
cat man/ipmasqadm.8 | gzip -9c > $PKG/usr/man/man8/ipmasqadm.8.gz
mkdir -p $PKG/usr/doc/ipmasqadm-0.4.2
cp -a doc/* $PKG/usr/doc/ipmasqadm-0.4.2
chmod 644 $PKG/usr/doc/ipmasqadm-0.4.2/*
chown -R root.root $PKG/usr/doc/ipmasqadm-0.4.2

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/ipchains-1.3.10-i386-1.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
 for dir in \
  ipchains-1.3.10.tar.gz ipchains-scripts-1.1.2 \
  ipmasqadm-0.4.2 ; do
  rm -rf $TMP/$dir
 done
 rm -rf $PKG
fi