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

######################################################
# Prepare extension creation                         #
######################################################
PYVER=3.9
# download and install python3.9
tce-load -wi python${PYVER}.tcz
# download and install other python dependencies
# download and install other dependencies
# 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="setuptools"
PACKAGE="python${PYVER}-$NAME"
VERSION="57.5.0"
DESCRIPTION="Easily download, build, install, upgrade, and uninstall Python packages"
AUTHORS="Python Packaging Authority"
HOMEPAGE="https://github.com/pypa/setuptools"
LICENSE="https://github.com/pypa/setuptools/blob/master/LICENSE"
ME="rhermsen"
TAGS="python python3 setuptools"
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 https://files.pythonhosted.org/packages/20/f7/205a71cc9cc68f23afd9f3083002d0fb0a8c2277baf41a528395552e13c0/${NAME}-${VERSION}.tar.gz

# source is extracted with 1-1-1970 by default. Use the tar -m option to use the current date and time.
# tar -xzf /tmp/${PACKAGE}/${NAME}-${VERSION}.tar.gz
tar -mxzf /tmp/${PACKAGE}/${NAME}-${VERSION}.tar.gz
cd /tmp/${PACKAGE}/${NAME}-${VERSION}
ls -la

######################################################
# Compile extension                                  #
######################################################
sudo rm -r ${DESTDIR}* 2>/dev/null
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/${NAME}/
mkdir -p  ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/
# 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 -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/_distutils_hack/ ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/
cp -r ${DESTDIR}/usr/local/lib/python${PYVER}/site-packages/pkg_resources/ ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/
cp ${DESTDIR}/usr/local/lib/python${PYVER}/site-packages/distutils-precedence.pth ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/
cp -r ${DESTDIR}/usr/local/lib/python${PYVER}/site-packages/${NAME}-${VERSION}-py${PYVER}.egg-info ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/

# cp -r ${DESTDIR}/usr/local/lib/python${PYVER}/site-packages/pkg_resources ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/${NAME}-${VERSION}-py${PYVER}.egg/
# cp ${DESTDIR}/usr/local/lib/python${PYVER}/site-packages/setuptools/command/easy_install.py ${TMPDIR}/usr/local/lib/python${PYVER}/site-packages/
# cp ${DESTDIR}/usr/local/bin/* ${TMPDIR}/usr/local/bin/
cp /tmp/${PACKAGE}/${NAME}-${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:   remus
Tags:           ${TAGS}
Comments:       Setuptools is a fully-featured, actively-maintained, and
                stable library designed to facilitate packaging Python projects,
                where packaging includes:
                - Python package and module definitions
                - Distribution package metadata
                - Test hooks
                - Project installation
                - Platform-specific details
                - Python 3 support
                
Change-log:     2014/12/04 first version (remus)
                2018/03/31 updated 7.0 -> 39.0.1 (juanito)
                2020/03/05 updated 39.0.1 -> 45.2.0 (juanito)
                ${TODAY} first python ${PYVER} verion, ${VERSION} (rhermsen)
Current:        ${TODAY} first python ${PYVER} 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 './setuptools-${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
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

