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

######################################################
# Prepare extension creation                         #
######################################################
# download and install python
tce-load -wi python-dev.tcz
#tce-load -wi python.tcz
# download and install other python dependencies
tce-load -wi python-setuptools.tcz
tce-load -wi libffi.tcz
tce-load -wi python-six.tcz
tce-load -i python-ipaddress.tcz
tce-load -i python-enum34.tcz
tce-load -i python-asn1crypto.tcz
tce-load -i python-cffi-dev.tcz
#tce-load -i python-cffi.tcz
tce-load -i python-pycparser.tcz
# 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-cryptography"
VERSION="2.6.1"
SHA256=""
DESCRIPTION="provides cryptographic recipes and primitives to Python developers"
AUTHORS="The cryptography developers"
HOMEPAGE="https://github.com/pyca/cryptography"
LICENSE="https://github.com/pyca/cryptography/blob/master/LICENSE"
ME="rhermsen"
TAGS="python cryptography ansible"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="07/ca/bc827c5e55918ad223d59d299fff92f3563476c3b00d0a9157d9c0217449/cryptography-$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 cryptography-$VERSION.tar.gz

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

######################################################
# Compile extension                                  #
######################################################
python setup.py build
sudo python setup.py install
sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/cryptography-$VERSION-py2.7-linux-i686.egg/
mkdir -p $TMPDIR/usr/local/share/doc/python-cryptography/
mkdir -p ${TMPDIR}/usr/local/tce.installed


cp -r /usr/local/lib/python2.7/site-packages/cryptography-$VERSION-py2.7-linux-i686.egg/ ${TMPDIR}/usr/local/lib/python2.7/site-packages/
find ${TMPDIR}/usr/local/lib/python2.7/site-packages/cryptography-$VERSION-py2.7-linux-i686.egg -name "*.pyc" -type f -delete
cp /tmp/${PACKAGE}/cryptography-${VERSION}/LICENSE* $TMPDIR/usr/local/share/doc/python-cryptography/

###################################################
# 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:       cryptography is a package which provides cryptographic
                recipes and primitives to Python developers. Our goal is for
                it to be your “cryptographic standard library”. It supports
                Python 2.7, Python 3.4+, and PyPy 5.4+.
                
Change-log:     ${TODAY} first verion, ${VERSION}
Current:        ${TODAY} first verion, ${VERSION}
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python-asn1crypto.tcz
python-six.tcz
python-cffi.tcz
python-enum34.tcz
python-ipaddress.tcz
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
echo './cryptography-2.6.1-py2.7-linux-i686.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
sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
sudo chmod -R 775 $TMPDIR/usr/local/tce.installed

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