#!/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
# following two extensions loaded as a workaround to prevent python3.6 to become installed.
#tce-load -wi libxml2.tcz
#tce-load -i libxml2-python.tcz
# download and install other dependencies
tce-load -wi libxml2-dev.tcz
tce-load -wi libxslt-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`
PACKAGE="python-lxml"
VERSION="4.5.2"
DESCRIPTION="library for processing XML and HTML in the Python language"
DEVDESCRIPTION="Develoment part of LXML, library for processing XML and HTML in the Python language"
AUTHORS="Stefan Behnel"
HOMEPAGE="https://lxml.de/"
LICENSE="BSD"
ME="rhermsen"
TAGS="python lxml"
DEVTAGS="python lxml"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="2c/4d/3ec1ea8512a7fbf57f02dee3035e2cce2d63d0e9c0ab8e4e376e01452597/lxml-$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 lxml-$VERSION.tar.gz

tar xf /tmp/${PACKAGE}/lxml-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/lxml-${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/lxml-${VERSION}-py2.7-linux-i686.egg/EGG-INFO
mkdir -p  ${TMPDIR}/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html

mkdir -p $TMPDIR/usr/local/share/doc/python-lxml/
mkdir -p ${TMPDIR}/usr/local/tce.installed

cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/EGG-INFO/* ${TMPDIR}/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/EGG-INFO/
cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/
cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/*.so ${TMPDIR}/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/
cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html/*.py ${TMPDIR}/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html
cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html/*.so ${TMPDIR}/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html

cp /tmp/${PACKAGE}/lxml-${VERSION}/LICENSES.txt $TMPDIR/usr/local/share/doc/python-lxml/

###################################################
# 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:   justincb
Tags:           ${TAGS}
Comments:       lxml is a Pythonic, mature binding for the libxml2 and libxslt libraries.
                It provides safe and convenient access to these libraries using the ElementTree API.
                
                2017/09/26 Compiled from source for tinycore 8.x
Change-log:     ${TODAY} update to verion, ${VERSION} (${ME})
Current:        ${TODAY} update to verion, ${VERSION} (${ME})
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
libxslt.tcz
libxml2-bin.tcz
python.tcz
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
echo './lxml-${VERSION}-py2.7-linux-i686.egg' >> /usr/local/lib/python2.7/site-packages/easy-install.pth
EOF

######################################################
# Dev extension                                      #
######################################################
mkdir -p  ${TMPDIR}-dev/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html

cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/*.h ${TMPDIR}-dev/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/
cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/*.pyc ${TMPDIR}-dev/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/
cp /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html/*.pyc ${TMPDIR}-dev/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/html/
cp -r /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/includes ${TMPDIR}-dev/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/
cp -r /usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/isoschematron ${TMPDIR}-dev/usr/local/lib/python2.7/site-packages/lxml-${VERSION}-py2.7-linux-i686.egg/lxml/

###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
cat <<EOF> ${PACKAGE}-dev.tcz.info
Title:          ${PACKAGE}-dev.tcz
Description:    ${DEVDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${DEVTAGS}
Comments:       lxml is a Pythonic, mature binding for the libxml2 and libxslt libraries.
                It provides safe and convenient access to these libraries using the ElementTree API.
                
Change-log:     ${TODAY} verion, ${VERSION}
Current:        ${TODAY} verion, ${VERSION}
EOF

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

###################################################
# Create development extension in temp dir      #
###################################################
cd $TMPDIR-dev
find $TMPDIR-dev -perm 777 -exec chmod 755 {} \;
find $TMPDIR-dev -perm 555 -exec chmod 755 {} \;
find $TMPDIR-dev -perm 444 -exec chmod 644 {} \;
find $TMPDIR-dev -perm 666 -exec chmod 644 {} \;
find $TMPDIR-dev -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR-dev

cd /tmp/submit/

mksquashfs $TMPDIR-dev ${PACKAGE}-dev.tcz

cd $TMPDIR-dev
sudo sh -c "find usr -not -type d > ${PACKAGE}-dev.tcz.list"
sudo mv ../${PACKAGE}-dev.tcz .
sudo mv ../${PACKAGE}-dev.tcz.dep .
sudo mv ../${PACKAGE}-dev.tcz.info .

# Create md5 file
sudo sh -c "md5sum ${PACKAGE}-dev.tcz > ${PACKAGE}-dev.tcz.md5.txt"

# Cleanup temp directory
sudo rm -r -f usr