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

######################################################
# Prepare extension creation                         #
######################################################
PYVER=3.6
#download and install python
tce-load -wi python${PYVER}-dev.tcz
#tce-load -wi python${PYVER}.tcz
#download and install other python dependencies
tce-load -wi python${PYVER}-setuptools.tcz
tce-load -wi libffi.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="markupsafe"
CAPNAME="MarkupSafe"
PACKAGE="python${PYVER}-${NAME}"
VERSION="2.0.1"
SHA256=""
DESCRIPTION="HTML string handling library for Python"
AUTHORS="Armin Ronacher"
HOMEPAGE="https://palletsprojects.com/p/markupsafe/"
LICENSE="BSD License (BSD-3-Clause)"
ME="rhermsen"
TAGS="python3 markupsafe html unicode subclass"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="bf/10/ff66fea6d1788c458663a84d88787bae15d45daa16f6b3ef33322a51fc7e/${CAPNAME}-${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 ${CAPNAME}-$VERSION.tar.gz

tar mxzf /tmp/${PACKAGE}/${CAPNAME}-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/${CAPNAME}-${VERSION}

######################################################
# Compile extension                                  #
######################################################
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export LDFLAGS="-Wl,-O1"

python${PYVER} setup.py build

sudo rm -r ${DESTDIR}* 2>/dev/null
# https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html
python${PYVER} setup.py build
mkdir -p  ${DESTDIR}
sudo python${PYVER} setup.py install --root=${DESTDIR}

sudo rm -r ${TMPDIR}* 2>/dev/null
mkdir -p  ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/${CAPNAME}-${VERSION}-py${PYVER}.egg-info/
mkdir -p  ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/${NAME}/
mkdir -p ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mkdir -p ${TMPDIR}/usr/local/tce.installed

cp -r ${DESTDIR}/usr/local/lib/python${PYVER}/site-packages/${NAME}/ ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/
cp -r ${DESTDIR}/usr/local/lib/python${PYVER}/site-packages/${CAPNAME}-${VERSION}-py${PYVER}.egg-info ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/
#unzip -x /usr/local/lib/python${PYVER}/site-packages/${CAPNAME}-${VERSION}-py${PYVER}-linux-i686.egg -d ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/${CAPNAME}-${VERSION}-py${PYVER}-linux-i686.egg/
cp /tmp/${PACKAGE}/${CAPNAME}-${VERSION}/docs/license.rst $TMPDIR/usr/local/share/doc/${PACKAGE}/
#find ${TMPDIR} -name "*.pyc" -type f -delete
find ${TMPDIR} -name "__pycache__" -type d -exec rm -rf {} +


###################################################
# 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:       MarkupSafe implements a text object that escapes
                characters so it is safe to use in HTML and XML.
                Characters that have special meanings are replaced so that
                they display as the actual characters.
                This mitigates injection attacks, meaning untrusted user
                input can safely be displayed on a page.
                Escaping is implemented in C so it is as efficient as possible.
                
Change-log:     2019/11/12 first version 1.0 (juanito)
                ${TODAY} python${PYVER} update to later verion, ${VERSION} (rhermsen)
Current:        ${TODAY} python${PYVER} update to later verion, ${VERSION} (rhermsen)
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python${PYVER}.tcz
EOF

###################################################
# Create install script file                      #
###################################################
cat <<EOF> $TMPDIR/usr/local/tce.installed/${PACKAGE}
#!/bin/sh
echo './${CAPNAME}-${VERSION}-py${PYVER}.egg-info' >> /usr/local/lib/python${PYVER}/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

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

