#!/bin/sh
#
######################################################
## Build script for Core 13.x                          #
##                                                    #
## See .info for details                              #
######################################################

######################################################
# Configure extension creation parameters            #
######################################################

# Variables
EXTNAM=tcpreplay
VERSION="4.4.1" 
#SRCNAM=$EXTNAM-$VERSION.tar.xz
SRCNAM=$EXTNAM-$VERSION.tar.gz
WRKDIR=$EXTNAM-$VERSION
BASEDIR=/tmp/$EXTNAM
SRCDIR=/tmp/$EXTNAM/source
TMPDIR=/tmp/$EXTNAM/package
DESTDIR=/tmp/$EXTNAM/build
TODAY=`date +%Y/%m/%d`
DESCRIPTION="suite for editing and replaying previously captured network traffic"
DOCDESCRIPTION="Documentation part of suite for editing and replaying previously captured network traffic"
DEVDESCRIPTION="Development files part of suite for editing and replaying previously captured network traffic"
AUTHORS="Fred Klassen"
HOMEPAGE="https://tcpreplay.appneta.com/"
LICENSE="GPLv3"
ME="rhermsen"
TAGS="tcpdump replay tcpreplay pcap"
DOCTAGS="man pages tcpdump replay tcpreplay pcap"
DEVTAGS="development tcpdump replay tcpreplay pcap"

######################################################
# Prepare extension creation                         #
######################################################

tce-load -wi ca-certificates.tcz
echo "ca_directory = /usr/local/etc/ssl/certs" > ~/.wgetrc

# Compile tools:
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz
#tce-load -wi autoconf.tcz
#tce-load -wi automake.tcz
#tce-load -wi libtool.tcz
#tce-load -wi libltdl.tcz
#tce-load -wi pkg-config.tcz
#tce-load -wi intltool.tcz
#tce-load -wi libtool-dev.tcz

# Configure dependancy
tce-load -wi tcpdump.tcz
tce-load -wi libpcap-dev.tcz
# tce-load -wi libpcap.tcz
# ARCH=`uname -m`
# if [ $ARCH == "x86" ]
# then
# sudo ln -s /tmp/tcloop/libffi/usr/local/lib/libffi.so.7 /usr/local/lib/libffi.so.5
# elif [ $ARCH == "x86_64" ]
# sudo ln -s /tmp/tcloop/libatomic_ops/usr/local/lib/libatomic_ops.so /usr/local/lib/libatomic_ops.so.0
# fi
tce-load -wi autogen.tcz
tce-load -wi libdnet-dev.tcz

# Other
tce-load -wi ipv6-netfilter-KERNEL.tcz

# Remove dirs and files left from previous creation
rm -r -f $SRCDIR 2>/dev/null
rm -r -f $WRKDIR 2>/dev/null
rm -r -f $DESTDIR 2>/dev/null

sudo rm -r -f $TMPDIR 2>/dev/null
sudo rm -r -f $TMPDIR-doc 2>/dev/null
sudo rm -r -f $TMPDIR-dev 2>/dev/null


######################################################
# Compile extension                                  #
######################################################

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "x86" ]
then
export CFLAGS="-Os -pipe -march=i486 -mtune=i686"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
elif [ $ARCH == "x86_64" ]
then
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

# Download source in SRCDIR
mkdir -p $SRCDIR
cd $SRCDIR
#wget https://github.com/appneta/tcpreplay/releases/download/v${VERSION}/${EXTNAM}-${VERSION}.tar.xz
wget https://github.com/appneta/tcpreplay/releases/download/v${VERSION}/${EXTNAM}-${VERSION}.tar.gz

# Unpack source in current directory
tar -xf $SRCNAM
cd $WRKDIR

# Configure it
./configure  --prefix=/usr/local \
--sysconfdir=/usr/local/etc/$EXTNAM \
--with-libpcap=/usr/local \
--with-libdnet=/usr/local/lib

# Compile
make
mkdir -p $DESTDIR
make DESTDIR=$DESTDIR install

# Install in base temp dir
mkdir -p $TMPDIR/usr/local/bin
mkdir -p $TMPDIR/usr/local/share/doc/${EXTNAM}/

mv $DESTDIR/usr/local/bin/* $TMPDIR/usr/local/bin

cp /tmp/${EXTNAM}/source/${EXTNAM}-${VERSION}/docs/LICENSE $TMPDIR/usr/local/share/doc/${EXTNAM}/

###################################################
# Create info file                                #
###################################################
cat <<EOF> /tmp/$EXTNAM/${EXTNAM}.tcz.info
Title:          ${EXTNAM}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
Comments:       Tcpreplay is a suite of GPLv3 licensed utilities for editing and
                finally replay the traffic back onto the network and through other devices
                such as switches, routers, firewalls, NIDS and IPS's. Tcpreplay supports
                both single and dual NIC modes for testing both sniffing and in-line devices.

                The IPv6 kernel modules are not automatically installed.
                If IPv6 is desired, install ipv6-netfilter-x.x.x-tinycore.tcz via
                the Apps GUI, or cli (tce-load -wi ipv6-netfilter-KERNEL).

Change-log:     ${TODAY} Initial version, $VERSION
Current:        ${TODAY} Initial version, $VERSION
EOF

###################################################                            
# Create .dep file                                #
###################################################
cat <<EOF> /tmp/${EXTNAM}/${EXTNAM}.tcz.dep
tcpdump.tcz
libpcap.tcz
libdnet.tcz
EOF


# Delete compilation work directory
cd ..
#rm -r -f $WRKDIR

# Adjust directory access rigths
find $TMPDIR/ -type d | xargs chmod -v 755;

# Strip executables
find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded


# Move files to doc extension
mkdir -p $TMPDIR-doc/usr/local/share/man/man1

mv $DESTDIR/usr/local/share/man/man1/* $TMPDIR-doc/usr/local/share/man/man1

###################################################
# Create info file                                #
###################################################
cat <<EOF> /tmp/${EXTNAM}/${EXTNAM}-doc.tcz.info
Title:          ${EXTNAM}-doc.tcz
Description:    ${DOCDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${DOCTAGS}
Comments:       Tcpreplay is a suite of GPLv3 licensed utilities for editing and
                finally replay the traffic back onto the network and through other devices
                such as switches, routers, firewalls, NIDS and IPS's. Tcpreplay supports
                both single and dual NIC modes for testing both sniffing and in-line devices.

Change-log:     ${TODAY} Initial version, $VERSION
Current:        ${TODAY} Initial version, $VERSION
EOF


###################################################
# Create base extension in temp dir               #
###################################################

cd $TMPDIR
find $TMPDIR -perm 777 -exec chmod 755 {} \;
find $TMPDIR -perm 555 -exec chmod 755 {} \;
find $TMPDIR -perm 444 -exec chmod 644 {} \;
find $TMPDIR -perm 666 -exec chmod 644 {} \;
find $TMPDIR -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR

cd ..
mksquashfs $TMPDIR $EXTNAM.tcz
cd $TMPDIR
sudo sh -c "find usr -not -type d > $EXTNAM.tcz.list"
sudo mv ../$EXTNAM.tcz* .
#sudo mv /tmp/${EXTNAM}/${EXTNAM}.tcz.info .

# Create md5 file
sudo sh -c "md5sum $EXTNAM.tcz > $EXTNAM.tcz.md5.txt"

# Cleanup temp directory
sudo rm -r -f usr


###################################################
# Create doc extension in temp dir                #
###################################################

cd $TMPDIR-doc
find $TMPDIR-doc -perm 777 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 555 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 444 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 666 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR-doc
cd ..
mksquashfs $TMPDIR-doc $EXTNAM-doc.tcz
cd $TMPDIR-doc
sudo sh -c "find usr -not -type d > $EXTNAM-doc.tcz.list"
sudo mv ../$EXTNAM-doc.tcz* .
#sudo mv /tmp/${EXTNAM}/${EXTNAM}-doc.tcz.info .

# Create md5 file
sudo sh -c "md5sum $EXTNAM-doc.tcz > $EXTNAM-doc.tcz.md5.txt"

# Cleanup temp directory
sudo rm -r -f usr

