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

######################################################
# Prepare extension creation                         #
######################################################
# download and install python
tce-load -wi python-dev.tcz
# download and install other python dependencies
tce-load -wi python-setuptools.tcz
tce-load -wi libffi.tcz
tce-load -i gmp-dev.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="pycrypto"
VERSION="2.6.1"
DESCRIPTION="Python Cryptography Toolkit"
DEVDESCRIPTION="Develoment part of pycrypto, Python Cryptography Toolkit"
AUTHORS="Dwayne C. Litzenberger"
HOMEPAGE="https://www.dlitz.net/software/pycrypto/"
LICENSE="Public Domain"
ME="rhermsen"
TAGS="python pycrypto"
DEVTAGS="python pycrypto"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="60/db/645aa9af249f059cc3a368b118de33889219e0362141e75d4eaf6f80f163/pycrypto-$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 pycrypto-$VERSION.tar.gz

tar xf /tmp/${PACKAGE}/pycrypto-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/pycrypto-${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 rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/

mkdir -p $TMPDIR/usr/local/share/doc/${PACKAGE}/

cp /usr/local/lib/python2.7/site-packages/pycrypto-2.6.1-py2.7.egg-info ${TMPDIR}/usr/local/lib/python2.7/site-packages/
cp /usr/local/lib/python2.7/site-packages/Crypto/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/Cipher ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/Hash ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/Protocol ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/PublicKey ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/Random ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/SelfTest ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/Signature ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
cp -r /usr/local/lib/python2.7/site-packages/Crypto/Util ${TMPDIR}/usr/local/lib/python2.7/site-packages/Crypto/
find ${TMPDIR} -name "*.pyc" -type f -delete

cp /tmp/${PACKAGE}/pycrypto-${VERSION}/COPYRIGHT $TMPDIR/usr/local/share/doc/${PACKAGE}/

###################################################
# 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:   juanito
Tags:           ${TAGS}
Comments:       The Python cryptography toolkit is intended to provide a reliable and stable base
                for writing Python programs that require cryptographic functions.
                
Change-log:     2011/01/01 first version
                2017/10/31 updated 2.3 -> 2.6.1
                ${TODAY} recompile on TCL 11.0 to include missig file, ${VERSION} (${ME})
Current:        ${TODAY} recompile on TCL 11.0 to include missing file, ${VERSION} (${ME})
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python.tcz
gmp.tcz
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

