#!/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
# download and install other dependencies
tce-load -i libyaml-dev.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`
NAME="pyyaml"
PACKAGE="python-${NAME}"
VERSION="5.1"
DESCRIPTION="YAML parser and emitter for Python"
AUTHORS="Kirill Simonov"
HOMEPAGE="https://github.com/yaml/pyyaml"
LICENSE="MIT"
ME="rhermsen"
TAGS="python yaml ansible"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}

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

# Source
wget http://pyyaml.org/download/pyyaml/PyYAML-$VERSION.tar.gz

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

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

cp ${DESTDIR}/usr/local/lib/python2.7/site-packages/yaml/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/yaml/
cp ${DESTDIR}/usr/local/lib/python2.7/site-packages/PyYAML-5.1-py2.7.egg-info ${TMPDIR}/usr/local/lib/python2.7/site-packages/
cp ${DESTDIR}/usr/local/lib/python2.7/site-packages/_yaml.so ${TMPDIR}/usr/local/lib/python2.7/site-packages/
cp /tmp/${PACKAGE}/PyYAML-${VERSION}/LICENSE $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:   ${ME}
Tags:           ${TAGS}
Comments:       PyYAML
                The next generation YAML parser and emitter for Python.
                YAML is a data serialization format designed for human
                readability and interaction with scripting languages.
                
Change-log:     ${TODAY} first verion, ${VERSION}
Current:        ${TODAY} first verion, ${VERSION}
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python.tcz
libyaml.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
#sudo chown -R tc:staff $TMPDIR/usr/local/etc/${PACKAGE}
#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

