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

######################################################
# Prepare extension creation                         #
######################################################
#download and install python
tce-load -wi python.tcz
#download and install other python dependencies
#(python-ipaddr.tcz is a run-time dependency)
tce-load -wi python-ipaddr.tcz
tce-load -wi python-setuptools.tcz
tce-load -wi libffi.tcz
#download and install dependencies
#download and install the compile tools
tce-load -wi git.tcz
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
PACKAGE="python-exabgp"
VERSION="3.4.26"
DESCRIPTION="The BGP swiss army knife of networking"
DOCDESCRIPTION="Documentation part of ExaBGP, the BGP swiss army knife of networking"
DEVDESCRIPTION="Development files part of ExaBGP, the BGP swiss army knife of networking"
AUTHORS="Thomas Mangin"
HOMEPAGE="https://github.com/Exa-Networks/exabgp"
LICENSE="https://github.com/Exa-Networks/exabgp/blob/master/COPYRIGHT"
ME="rhermsen"
TAGS="python python2.7 exabgp bgp"
DOCTAGS="man pages python python2.7 exabgp bgp"
DEVTAGS="development python python2.7 exabgp bgp"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}

# Workdir
sudo rm -r /tmp/${PACKAGE} 2>/dev/null
mkdir /tmp/${PACKAGE}
cd /tmp/${PACKAGE}

# Source
git clone https://github.com/Exa-Networks/exabgp.git
cd exabgp/
git checkout 3.4

######################################################
# Compile extension                                  #
######################################################
python setup.py build
sudo python setup.py install

######################################################
# Base extension                                     #
######################################################
sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/
mkdir -p  ${TMPDIR}/usr/local/share/doc/python-exabgp
mkdir -p ${TMPDIR}/usr/local/tce.installed
mkdir -p  ${TMPDIR}/usr/local/bin
mkdir -p  ${TMPDIR}/usr/local/sbin

#cp -r /usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/EGG-INFO ${TMPDIR}/usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/
cp -r /usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/exabgp ${TMPDIR}/usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/
#all example configs part of doc extension
#cp -r /usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/share ${TMPDIR}/usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/
cp /tmp/${PACKAGE}/exabgp/COPYRIGHT ${TMPDIR}/usr/local/share/doc/python-exabgp/
#.pyc files should not be included
find ${TMPDIR}/usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg -name "*.pyc" -type f -delete
cp /tmp/${PACKAGE}/exabgp/bin/* ${TMPDIR}/usr/local/bin/
cp /tmp/${PACKAGE}/exabgp/sbin/* ${TMPDIR}/usr/local/sbin/

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
Comments:       ExaBGP, Python BGP route injector
                ExaBGP provides a convenient way to implement
                Software Defined Networking by transforming BGP
                messages into friendly plain text or JSON, which can
                then be easily handled by simple scripts or your BSS/OSS.
                
                For example configuration see the ${PACKAGE}-doc.tcz extension.
                
Change-log:     ${TODAY} first verion, ${VERSION}
Current:        ${TODAY} first verion, ${VERSION}
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python-ipaddr.tcz
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
echo './exabgp-3.4.26-py2.7.egg' >> /usr/local/lib/python2.7/site-packages/easy-install.pth
EOF

######################################################
# Doc extension                                      #
######################################################
mkdir -p $TMPDIR-doc/usr/local/share/man/man1
mkdir -p $TMPDIR-doc/usr/local/share/man/man5
mkdir -p $TMPDIR-doc/usr/local/share/doc/python-exabgp/examples/dev
mkdir -p $TMPDIR-doc/usr/local/share/doc/python-exabgp/examples/processes

cp /tmp/${PACKAGE}/exabgp/doc/man/*.1 $TMPDIR-doc/usr/local/share/man/man1/
cp /tmp/${PACKAGE}/exabgp/doc/man/*.5 $TMPDIR-doc/usr/local/share/man/man5/
cp -r /tmp/${PACKAGE}/exabgp/dev/ $TMPDIR-doc/usr/local/share/doc/python-exabgp/examples/
cp /usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/share/exabgp/etc/* $TMPDIR-doc/usr/local/share/doc/python-exabgp/examples/
cp /usr/local/lib/python2.7/site-packages/exabgp-$VERSION-py2.7.egg/share/exabgp/processes/* $TMPDIR-doc/usr/local/share/doc/python-exabgp/examples/processes/

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
cat <<EOF> ${PACKAGE}-doc.tcz.info
Title:          ${PACKAGE}-doc.tcz
Description:    ${DOCDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${DOCTAGS}
Comments:       ExaBGP, Python BGP route injector
                ExaBGP provides a convenient way to implement
                Software Defined Networking by transforming BGP
                messages into friendly plain text or JSON, which can
                then be easily handled by simple scripts or your BSS/OSS.
                
                Includes example configuration in the following path:
                /usr/local/share/doc/python-exabgp/examples/
                
Change-log:     ${TODAY} first verion, ${VERSION}
Current:        ${TODAY} first verion, ${VERSION}
EOF

###################################################
# Create base extension in temp dir               #
###################################################
find $TMPDIR/ -type d | xargs chmod -v 755;

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

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 /tmp/submit/

mksquashfs $TMPDIR ${PACKAGE}.tcz

cd $TMPDIR
sudo sh -c "find usr -not -type d > ${PACKAGE}.tcz.list"
sudo mv ../${PACKAGE}.tcz .
sudo mv ../${PACKAGE}.tcz.dep .
sudo mv ../${PACKAGE}.tcz.info .

# Create md5 file
sudo sh -c "md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt"

# Cleanup temp directory
sudo rm -r -f usr

###################################################
# Create documentation 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 /tmp/submit/

mksquashfs $TMPDIR-doc ${PACKAGE}-doc.tcz

cd $TMPDIR-doc
sudo sh -c "find usr -not -type d > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz .
sudo mv ../${PACKAGE}-doc.tcz.info .

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

# Cleanup temp directory
sudo rm -r -f usr

