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

######################################################
# Prepare extension creation                         #
######################################################
# download and install python
tce-load -wi python.tcz
# download and install other python dependencies
tce-load -wi python-setuptools.tcz
tce-load -wi python-paramiko.tcz
tce-load -wi libxml2-bin.tcz
tce-load -i python-lxml.tcz
tce-load -wi python-libxml2.tcz
tce-load -wi libxslt.tcz
tce-load -wi python-selectors2.tcz
tce-load -wi libffi.tcz
tce-load -wi python-six.tcz
# download and install other dependencies
# download and install the compile tools
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
PACKAGE="python-ncclient"
VERSION="0.6.9"
DESCRIPTION="Python library for NETCONF clients"
DEVDESCRIPTION="Develoment part of LXML, Python library for NETCONF clients"
AUTHORS="Shikhar Bhushan, Leonidas Poulopoulos, Ebben Aries, Einar Nilsen-Nygaard"
HOMEPAGE="https://github.com/ncclient/ncclient"
LICENSE="Apache 2.0"
ME="rhermsen"
TAGS="python ncclient"
DEVTAGS="python ncclient"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="72/fd/ccae38393c22099229b68e8cdf061408b824e09ee207e2401c224398c5b0/ncclient-$VERSION.tar.gz"

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

# Source
wget https://files.pythonhosted.org/packages/$DOWNLOAD -O ncclient-$VERSION.tar.gz

tar xf /tmp/${PACKAGE}/ncclient-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/ncclient-${VERSION}

######################################################
# Compile extension                                  #
######################################################
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export LDFLAGS="-Wl,-O1"

python setup.py build
sudo python setup.py install
cd /usr/local/lib/python2.7/site-packages/
sudo unzip -oq ncclient-${VERSION}-py2.7.egg

sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/EGG-INFO
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/devices
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/operations
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/transport

mkdir -p $TMPDIR/usr/local/share/doc/python-ncclient/
mkdir -p ${TMPDIR}/usr/local/tce.installed

cp /usr/local/lib/python2.7/site-packages/EGG-INFO/* ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/EGG-INFO/
cp /usr/local/lib/python2.7/site-packages/ncclient/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/
cp /usr/local/lib/python2.7/site-packages/ncclient/devices/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/devices/
cp /usr/local/lib/python2.7/site-packages/ncclient/operations/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/operations/
cp /usr/local/lib/python2.7/site-packages/ncclient/transport/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/transport/
cp -r /usr/local/lib/python2.7/site-packages/ncclient/operations/third_party ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/operations/
cp -r /usr/local/lib/python2.7/site-packages/ncclient/transport/third_party ${TMPDIR}/usr/local/lib/python2.7/site-packages/ncclient-${VERSION}-py2.7.egg/ncclient/transport/
find ${TMPDIR} -name "*.pyc" -type f -delete

cp /tmp/${PACKAGE}/ncclient-${VERSION}/LICENSE $TMPDIR/usr/local/share/doc/python-ncclient/

###################################################
# 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:       ncclient is a Python library that facilitates client-side scripting and application
                development around the NETCONF protocol.
                It aims to offer an intuitive API that sensibly maps the XML-encoded nature of NETCONF
                to Python constructs and idioms, and make writing network-management scripts easier.
                
Change-log:     ${TODAY} update to verion, ${VERSION}
Current:        ${TODAY} update to verion, ${VERSION}
EOF

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

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

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