#!/bin/bash

set -x
set -e

export H5_LDFLAGS=-L/usr/local/lib
export H5_CFLAGS=-I/usr/local/include

readonly ROOTDIR=${PWD}

# create the wheelhouse directory
mkdir -p ${ROOTDIR}/wheelhouse

# build wheel directly from developer-provided .tar.gz of TREXIO (generated with `python setup.py sdist`)
# note: trexio-VERSION.tar.gz has to be in the root directory of the host machine and provided as an argument to this script

# process input: first argument is the name of the .tar.gz with the source code of the Python API
if [[ -z "$1" ]]; then
    echo "Please specify the name of the TREXIO source code distribution (with .tar.gz suffix)"
    exit 1
fi

# derive TREXIO version from the file name
TREXIO_SOURCE=${1}
# remove prefix that ends with "-"
tmp=${TREXIO_SOURCE#*-}
# remove suffix that ends with ".tar.gz"
TR_VERSION=${tmp%.tar.gz*}
# print the computed version
echo "TREXIO VERSION:" ${TR_VERSION}

# unzip and enter the folder with TREXIO Python API
gzip -cd ${TREXIO_SOURCE} | tar xvf -
cd trexio-${TR_VERSION}

# the function below build manylinux wheels based on the provided version of python (e.g. build_wheel_for_py 36)
function build_wheel_for_py()
{

   if [[ -z "$1" ]]; then
      echo "Empty string provided instead of the Python version"
      exit 1
   fi

   # derive PYVERSION from the input argument
   PYVERSION=${1}

   # derive manylinux glibc tag from the PLAT env variable provided to docker run
   MANYLINUX_TAG=${PLAT:9:4}

   CPYTHON="cp${PYVERSION}-cp${PYVERSION}"

   # create and activate a virtual environment based on CPython version ${PYVERSION}
   /opt/python/${CPYTHON}/bin/python3 -m venv --clear trexio-manylinux-py${PYVERSION}
   source trexio-manylinux-py${PYVERSION}/bin/activate
   python3 --version

   # upgrade pip, otherwise it complains that manylinux wheel is "...not supported wheel on this platform"
   pip install --upgrade pip
   # install dependencies needed to build manylinux wheel
   pip install --upgrade setuptools build

   # produce conventional (non-manylinux) wheel
   python3 -m build --wheel --outdir dist/

   # use auditwheel from PyPA to repair all wheels and make them manylinux-compatible
   auditwheel repair dist/trexio-${TR_VERSION}-${CPYTHON}-*.whl

   # install the produced manylinux wheel in the virtual environment
   python3 -m pip install wheelhouse/trexio-${TR_VERSION}-${CPYTHON}-manylinux*.whl

   # install pytest for testing
   pip install pytest
   # run test script
   pytest -v test/test_api.py

   # cleaning
   rm -rf -- dist/ build/ trexio.egg-info/
   #rm -- test_file_py.h5 unsafe_test_file_py.h5

   # deactivate the current environment
   deactivate

   # remove the virtual environment
   rm -rf -- trexio-manylinux-py${PYVERSION}

   # move the wheelhouse directory to the ROOTDIR
   mv wheelhouse/trexio-${TR_VERSION}-${CPYTHON}-manylinux*.whl ${ROOTDIR}/wheelhouse/
}


# build wheels for all versions of CPython in this container
for CPYVERSION in 38 39 310 311 312 313
do
  build_wheel_for_py ${CPYVERSION}
done
