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

######################################################
# Prepare extension creation                         #
######################################################
PYVER=3.9
#download and install python
tce-load -wi python${PYVER}.tcz

#download and install other python dependencies
# Sphinx 1.2 dependencies
# tce-load -wi python${PYVER}-setuptools.tcz
tce-load -wi python${PYVER}-docutils.tcz
tce-load -wi python${PYVER}-pygments.tcz
tce-load -wi python${PYVER}-jinja2.tcz
tce-load -wi python${PYVER}-markupsafe.tcz
# additional Sphinx 1.3 dependencies
tce-load -wi python${PYVER}-sphinx_rtd_theme.tcz
tce-load -wi python${PYVER}-alabaster.tcz
tce-load -wi python${PYVER}-babel.tcz
tce-load -wi python${PYVER}-snowballstemmer.tcz
tce-load -wi python${PYVER}-six.tcz
tce-load -wi python${PYVER}-pytz.tcz

# download and install dependencies
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=sphinx
CAPNAME=Sphinx
PACKAGE="python${PYVER}-${NAME}"
# https://pypi.org/project/Sphinx/
VERSION="1.3.6"
SHA256=""
DESCRIPTION="a tool to create documentation for Python"
AUTHORS="Georg Brandl"
HOMEPAGE="http://sphinx-doc.org/"
LICENSE="See Original-site"
ME="rhermsen"
TAGS="python3 sphinx"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="ea/56/9d723915960f50010f5d98874d9c357ff048661611e0b09dc726622c5c3d/${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

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

######################################################
# Compile extension                                  #
######################################################
# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "i686" ]
then
echo
export CFLAGS="-Os -pipe -march=i486 -mtune=i686"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
# no specific arch dependent parameters
elif [ $ARCH == "x86_64" ]
then
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
echo
# no specific arch dependent parameters
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

sudo rm -r ${DESTDIR}* 2>/dev/null
# /usr/local/lib/python3.6/site-packages/setuptools/lib2to3_ex.py:41: SetuptoolsDeprecationWarning:
# 2to3 support is deprecated. If the project still requires Python 2 support, please migrate to a single-
# codebase solution or employ an independent conversion process.
# SetuptoolsDeprecationWarning)
# 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/bin
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/
cp -r ${DESTDIR}/usr/local/bin/sphinx-* ${TMPDIR}/usr/local/bin/

cp /tmp/${PACKAGE}/${CAPNAME}-${VERSION}/LICENSE $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:   ${ME}
Tags:           ${TAGS}
Comments:       Sphinx
                Sphinx is a tool that makes it easy to create intelligent 
                and beautiful documentation, written by Georg Brandl 
                and licensed under the BSD license.
                
Change-log:     ${TODAY} first python ${PYVER} version, ${VERSION} (rhermsen)
Current:        ${TODAY} first python ${PYVER} version, ${VERSION} (rhermsen)
EOF

###################################################
# Create .dep file                                #
###################################################
cat <<EOF> ${PACKAGE}.tcz.dep
python${PYVER}-docutils.tcz
python${PYVER}-pygments.tcz
python${PYVER}-jinja2.tcz
python${PYVER}-markupsafe.tcz
python${PYVER}-six.tcz
python${PYVER}-babel.tcz
python${PYVER}-alabaster.tcz
python${PYVER}-snowballstemmer.tcz
python${PYVER}-sphinx_rtd_theme.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

