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

######################################################
# Prepare extension creation                         #
######################################################
# download and install python
tce-load -wi python.tcz
# download and install other python 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-ipaddress"
VERSION="1.0.22"
SHA256=""
DESCRIPTION="Port of the 3.3+ ipaddress module to 2.6, 2.7, 3.2"
AUTHORS="Philipp Hagemeister"
HOMEPAGE="https://github.com/phihag/ipaddress"
LICENSE="Python Software Foundation License"
ME="rhermsen"
TAGS="python ipaddress"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="97/8d/77b8cedcfbf93676148518036c6b1ce7f8e14bf07e95d7fd4ddcb8cc052f/ipaddress-$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 ipaddress-$VERSION.tar.gz

tar xf /tmp/${PACKAGE}/ipaddress-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/ipaddress-${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/ipaddress-${VERSION}-py2.7.egg-info
#mkdir -p ${TMPDIR}/usr/local/tce.installed
mkdir -p $TMPDIR/usr/local/share/doc/python-ipaddress/

cp /usr/local/lib/python2.7/site-packages/ipaddress.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/
cp /usr/local/lib/python2.7/site-packages/ipaddress-1.0.22-py2.7.egg-info ${TMPDIR}/usr/local/lib/python2.7/site-packages/ipaddress-${VERSION}-py2.7.egg-info/PKG-INFO
cp /tmp/${PACKAGE}/ipaddress-${VERSION}/LICENSE $TMPDIR/usr/local/share/doc/python-ipaddress/
cd ${TMPDIR}/usr/local/lib/python2.7/site-packages/ipaddress-${VERSION}-py2.7.egg-info/
cat <<EOF> SOURCES.txt
LICENSE
MANIFEST.in
Makefile
README
README.md
ipaddress.py
setup.cfg
setup.py
test_ipaddress.py
ipaddress.egg-info/PKG-INFO
ipaddress.egg-info/SOURCES.txt
ipaddress.egg-info/dependency_links.txt
ipaddress.egg-info/top_level.txt
EOF
cat <<EOF> dependency_links.txt


EOF
cat <<EOF> top_level.txt
ipaddress

EOF

###################################################
# 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:       ipaddress
                Python 3.3+'s ipaddress for Python 2.6, 2.7, 3.2.
                
Change-log:     ${TODAY} first verion, ${VERSION}
Current:        ${TODAY} first verion, ${VERSION}
EOF

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

