#!/bin/sh
#
######################################################
# Build script for Core 15.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
tce-load -wi python${PYVER}-setuptools.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="asciidoc"
CAPNAME="asciidoc"
PACKAGE="python${PYVER}-${NAME}"
VERSION="10.2.1"
SHA256=""
DESCRIPTION="A Text Document Manager"
AUTHORS="asciidoc team"
HOMEPAGE="https://github.com/asciidoc-py/asciidoc-py"
LICENSE="GPL-2.0"
ME="rhermsen"
TAGS="asciidoc ascii text doc python python3"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
DOWNLOAD="1d/e7/315a82f2d256e9270977aa3c15e8fe281fd7c40b8e2a0b97e0cb61ca8fa0/${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 variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "i686" ]
then
export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
# export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=i486 -mtune=i686"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
elif [ $ARCH == "x86_64" ]
then
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

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
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/bin ${TMPDIR}/usr/local
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 /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                                #
###################################################
if [ $ARCH == "i686" ]
then
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:       AsciiDoc is a text document format for writing notes, documentation,
                articles, books, ebooks, slideshows, web pages, man pages and blogs.
                AsciiDoc files can be translated to many formats including HTML, PDF, EPUB, 
                man page.
                
Change-log:     ${TODAY} python${PYVER} first version, ${VERSION} (rhermsen)
Current:        ${TODAY} python${PYVER} first version, ${VERSION} (rhermsen)
EOF
elif [ $ARCH == "x86_64" ]
then
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:       AsciiDoc is a text document format for writing notes, documentation,
                articles, books, ebooks, slideshows, web pages, man pages and blogs.
                AsciiDoc files can be translated to many formats including HTML, PDF, EPUB, 
                man page.
                
Change-log:     2017/12/21 first version 8.6.9 (juanito)
                ${TODAY} python${PYVER} update to later version, ${VERSION} (rhermsen)
Current:        ${TODAY} python${PYVER} update to later version, ${VERSION} (rhermsen)
EOF
fi

###################################################
# 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

