#!/bin/sh
# Look near the end of this script for more data that needs to be
# filled in manually. These include  Comments:  and  Change-log:,
# as well as the dependencies section labeled  DEPS.

# Data used to automatically create the info file.
EXTNAME=conntrack-tools
VERSION=1.2.2
DESCRIPTION="View/manage the in-kernel connection tracking state table from userspace."
AUTHOR="Pablo Neira Ayuso"
ORIGSITE=http://www.netfilter.org/projects
COPYING=GPLv2+
EXTBY=Rich
TAGS="library conntrack netlink"
BUILDDATE=`date -I`

# Compiler flags.
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export LDFLAGS="-Wl,-O1"

# Uncomment the second line if you need a startup script
#TCEINSTALLED=""
TCEINSTALLED="/tce.installed"

# Set up work environment
SRCNAME=$EXTNAME-$VERSION.tar.bz2
STARTDIR=`pwd`
WORKDIR=$STARTDIR/$EXTNAME-$VERSION
TCEINSTALLEDDIR=$WORKDIR/tmp/package/usr/local$TCEINSTALLED
rm -rf $WORKDIR
mkdir -p $TCEINSTALLEDDIR
mkdir -p $WORKDIR/tmp/ext
tar xf $SRCNAME
cd $WORKDIR

# Create the startup script if required
if [ -n "$TCEINSTALLED" ]; then
# Add startup commands between #!/bin/sh and INSTALL
cat << INSTALL > $TCEINSTALLEDDIR/$EXTNAME
#!/bin/sh
sudo modprobe nf_conntrack
sudo modprobe nf_conntrack_ipv4
sudo modprobe nf_conntrack_netlink
INSTALL

# Set owner/permissions
	sudo chown -R root:staff $TCEINSTALLEDDIR
	sudo chmod -R 775 $TCEINSTALLEDDIR
fi

# Build the extension
./configure --prefix=/usr/local
if [ "$?" -ne 0 ]; then echo "***** Error: configure failed *****"; exit 1; fi
make install DESTDIR=$WORKDIR/tmp/package
if [ "$?"-ne 0]; then echo "***** Error: make failed *****"; exit 1; fi

# Strip binaries
cd tmp
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

# Create extension files
cd package
find usr -not -type d | sort > ../ext/$EXTNAME.tcz.list
cd ../ext
mksquashfs ../package/ $EXTNAME.tcz -noappend
md5sum $EXTNAME.tcz > $EXTNAME.tcz.md5.txt

# Get size of the tcz file
FILESIZE=`ls -lh $EXTNAME.tcz | cut -f 5 -d " "`

# Create the info file
cat << INFO > $EXTNAME.tcz.info
Title:		$EXTNAME.tcz
Description:	$DESCRIPTION
Version:	$VERSION
Author:		$AUTHOR
Original-site:	$ORIGSITE/$EXTNAME
Copying-policy: $COPYING
Size:		$FILESIZE
Extension_by:	$EXTBY
Tags:		$TAGS
Comments:	The conntrack-tools are a set of tools targeted at system
		administrators. They are conntrack, the userspace command
		line interface, and conntrackd, the userspace daemon. The
		tool conntrack provides a full featured interface that is
		intended to replace the old /proc/net/ip_conntrack
		interface. Using conntrack, you can view and manage the
		in-kernel connection tracking state table from userspace.
		On the other hand, conntrackd covers the specific aspects
		of stateful firewalls to enable highly available scenarios,
		and can be used as statistics collector as well.

		Enter  sudo conntrack -E  then generate some network
		traffic and watch for a response.

		Available man files: conntrack, conntrackd, and nfct
Change-log:     ----
Current:	$BUILDDATE First version, $VERSION (TESTING)
INFO

# Create the dependency file
cat << DEPS > $EXTNAME.tcz.dep
netfilter-KERNEL.tcz
libmnl.tcz
libnfnetlink.tcz
libnetfilter_conntrack.tcz
libnetfilter_cttimeout
DEPS

# Check the extension for errors
submitqc4

# Bundle the files in an archive for submission
tar zcf $EXTNAME.tar.gz *
# Encrypt the archive
yes tinycore | bcrypt $EXTNAME.tar.gz

echo "Extension is in $WORKDIR/tmp/ext"
cd $STARTDIR
