#!/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=libmnl
VERSION=1.0.3
DESCRIPTION="Library oriented to Netlink developers, required by conntrack"
AUTHOR="Pablo Neira Ayuso, Jozsef Kadlecsik, Jan Engelhardt"
ORIGSITE=http://www.netfilter.org/projects
COPYING=LGPLv2.1+
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:	libmnl is a minimalistic user-space library oriented
		to Netlink developers. There are a lot of common tasks
		in parsing, validating, constructing of both the Netlink
		header and TLVs that are repetitive and easy to get
		wrong. This library aims to provide simple helpers that
		allows you to re-use code and to avoid re-inventing
		the wheel.
Change-log:     ----
Current:	$BUILDDATE First version, $VERSION
INFO

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