#!/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=libnetfilter_cttimeout
VERSION=1.0.0
DESCRIPTION="Create, update and delete network timeout policies."
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
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:	libnetfilter_cttimeout is the userspace library that provides the
		programming interface to the fine-grain connection tracking timeout
		infrastructure. With this library, you can create, update and
		delete timeout policies that can be attached to traffic flows. This
		library is used by conntrack-tools.
Change-log:     ----
Current:	$BUILDDATE First version, $VERSION
INFO

# Create the dependency file
cat << DEPS > $EXTNAME.tcz.dep
libmnl.tcz
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
