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

######################################################
# Prepare extension creation                         #
######################################################
PYVER=3.9
#download and install python
tce-load -wi python${PYVER}.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${PYVER}-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${PYVER}-exabgp"
VERSION="4.2.21"
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/main/LICENCE.txt"
ME="rhermsen"
TAGS="python python${PYVER} exabgp bgp"
DOCTAGS="man pages python python${PYVER} exabgp bgp"
DEVTAGS="development python python${PYVER} 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 4.2

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

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "i686" ]
then
echo
# no specific arch dependent parameters
elif [ $ARCH == "x86_64" ]
then
echo
# no specific arch dependent parameters
else
exit 1
fi

python${PYVER} setup.py build
sudo python${PYVER} setup.py install

######################################################
# Base extension                                     #
######################################################
sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/
mkdir -p  ${TMPDIR}/usr/local/share/doc/python${PYVER}-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/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/EGG-INFO ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/
cp -r /usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/exabgp ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/
#all example configs part of doc extension
#cp -r /usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/share ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/
cp /tmp/${PACKAGE}/exabgp/LICENCE.txt ${TMPDIR}/usr/local/share/doc/python${PYVER}-exabgp/
# __pycache__ directories should not be included
find ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg -name "__pycache__" -type d -exec rm -rf {} \;
#.pyc files should not be included
find ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg -name "*.pyc" -type f -delete
# cp /tmp/${PACKAGE}/exabgp/bin/* ${TMPDIR}/usr/local/bin/
# cp /tmp/${PACKAGE}/exabgp/sbin/* ${TMPDIR}/usr/local/sbin/
cp /usr/local/bin/exabgp* ${TMPDIR}/usr/local/bin/

###################################################
# 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.

                Add paramter '--env /usr/local/etc/exabgp/exabgp.env' to use the default environment values.
                
                For example configuration see the ${PACKAGE}-doc.tcz extension.
                
Change-log:     2019/02/03 first verion, 3.4.26
                ${TODAY} major version update, ${VERSION}
Current:        ${TODAY} major version update, ${VERSION}
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
echo './exabgp-${VERSION}-py${PYVER}.egg' >> /usr/local/lib/python${PYVER}/site-packages/easy-install.pth
if [ ! -d /usr/local/etc/exabgp ]; then
    mkdir -p /usr/local/etc/exabgp
    # chown nobody:nogroup /usr/local/etc/exabgp
fi
if [ ! -f /usr/local/etc/exabgp/exabgp.env ]; then
    exabgp --fi | tee /usr/local/etc/exabgp/exabgp.env > /dev/null
    sed -i "s/pid = ''/pid = '\/var\/run\/exabgp.pid'/g" /usr/local/etc/exabgp/exabgp.env
    # chown nobody:nogroup /usr/local/etc/exabgp/exabgp.env
fi
if [ ! -f /var/log/exabgp.in ]; then
    mkfifo /var/run/exabgp.in
    # chmod 600 /var/run/exabgp.in
    chmod 4777 /var/run/exabgp.in
    chown nobody:nogroup /var/run/exabgp.in
fi
if [ ! -f /var/log/exabgp.out ]; then
    mkfifo /var/run/exabgp.out
    # chmod 600 /var/run/exabgp.out
    chmod 4777 /var/run/exabgp.out
    chown nobody:nogroup /var/run/exabgp.out
fi
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python${PYVER}.tcz
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${PYVER}-exabgp/examples/dev
#mkdir -p $TMPDIR-doc/usr/local/share/doc/python${PYVER}-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${PYVER}-exabgp/examples/
cp /usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/etc/exabgp/examples/*.* $TMPDIR-doc/usr/local/share/doc/python${PYVER}-exabgp/examples/
#cp /usr/local/lib/python${PYVER}/site-packages/exabgp-$VERSION-py${PYVER}.egg/share/exabgp/processes/* $TMPDIR-doc/usr/local/share/doc/python${PYVER}-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${PYVER}-exabgp/examples/
                
Change-log:     2019/02/03 first verion, 3.4.26
                ${TODAY} major version update, ${VERSION}
Current:        ${TODAY} major version update, ${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

