#!/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
#tce-load -wi python-setuptools.tcz
# Begin of note regading python-setuptools
# Install python-setuptools with the following steps:
tce-load -wi ca-certificates.tcz
echo "ca_directory = /usr/local/etc/ssl/certs" > ~/.wgetrc
tce-load -wi python.tcz
tce-load -wi compiletc.tcz
tce-load -wi libffi.tcz
cd /tmp
mkdir setuptools
cd /tmp/setuptools
wget https://files.pythonhosted.org/packages/1d/64/a18a487b4391a05b9c7f938b94a16d80305bf0369c6b0b9509e86165e1d3/setuptools-41.0.1.zip
unzip setuptools-41.0.1.zip
cd /tmp/setuptools/setuptools-41.0.1
python setup.py build
sudo python setup.py install
# End of note regarding python-setuptools
tce-load -wi python-jinja2.tcz
tce-load -i libyaml-dev.tcz
tce-load -i python-pyyaml.tcz
tce-load -wi python-ipaddress.tcz
tce-load -wi python-enum34.tcz
tce-load -wi python-six.tcz
tce-load -wi python-asn1crypto.tcz
tce-load -wi python-cffi-dev.tcz
#tce-load -wi python-cffi.tcz
tce-load -wi python-pycparser.tcz
tce-load -wi python-cryptography.tcz
# download and install other dependencies
tce-load -wi libffi.tcz
# 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`
NAME="ansible"
PACKAGE="python-$NAME"
VERSION="2.8"
DESCRIPTION="Ansible is a simple, powerful, agentless IT automation tool"
AUTHORS="Ansible, Inc."
HOMEPAGE="https://ansible.com"
LICENSE="GPLv3+"
ME="rhermsen"
TAGS="ansible automation python python2"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}

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

# Source
git clone https://github.com/ansible/ansible.git
cd /tmp/${NAME}
git checkout stable-${VERSION}

######################################################
# Compile extension                                  #
######################################################
sudo rm -r ${DESTDIR}* 2>/dev/null
python setup.py build
mkdir -p  ${DESTDIR}
sudo python setup.py install --root=/${DESTDIR}
sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/${NAME}-${VERSION}.0.post0-py2.7.egg/${NAME}
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/${NAME}-${VERSION}.0.post0-py2.7.egg/EGG-INFO
mkdir -p  ${TMPDIR}/usr/local/bin
mkdir -p $TMPDIR/usr/local/share/doc/${PACKAGE}/
mkdir -p ${TMPDIR}/usr/local/tce.installed

#cp -r ${DESTDIR}/* ${TMPDIR}
cp ${DESTDIR}/usr/local/lib/python2.7/site-packages/${NAME}-${VERSION}.0.post0-py2.7.egg-info/* ${TMPDIR}/usr/local/lib/python2.7/site-packages/${NAME}-${VERSION}.0.post0-py2.7.egg/EGG-INFO/
cp -r ${DESTDIR}/usr/local/lib/python2.7/site-packages/${NAME} ${TMPDIR}/usr/local/lib/python2.7/site-packages/${NAME}-${VERSION}.0.post0-py2.7.egg/
cp ${DESTDIR}/usr/local/bin/* ${TMPDIR}/usr/local/bin/
cp /tmp/${NAME}/COPYING $TMPDIR/usr/local/share/doc/${PACKAGE}/
find ${TMPDIR} -name "*.pyc" -type f -delete

###################################################
# 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:       Ansible is a radically simple IT automation platform that makes
                your applications and systems easier to deploy. Avoid writing scripts
                or custom code to deploy and update your applications — automate 
                in a language that approaches plain English, using SSH, with no agents
                to install on remote systems.
                
Change-log:     ${TODAY} first verion, ${VERSION} (rhermsen)
Current:        ${TODAY} first verion, ${VERSION} (rhermsen)
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python-jinja2.tcz
python-pyyaml.tcz
python-cryptography.tcz
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
echo './ansible-2.8.0.post0-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
sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
sudo chmod -R 775 $TMPDIR/usr/local/tce.installed
sudo chown -R tc:staff $TMPDIR/usr/local/tce.installed/*
sudo chmod -R 755 $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