# Get some variables provided by autosubmit.
# TODO: What do we do to ensure that these variables are defined in the proj file?
WORKDIR=%HPCROOTDIR%
ICON_VERSION=%ICON_VERSION%
SPACK_URL=%spack.url%
SPACK_BRANCH=%spack.branch%
SPACK_COMPILER=%spack.compiler%

# If the workdir directory does not exist create it
if [ ! -d ${WORKDIR} ]; then
  mkdir -p ${WORKDIR}
fi

# Go to the working directory
cd ${WORKDIR}

# Check if experiment's spack installation already exists, if it doesn't, clone it.
SPACK_ENV=spack/share/spack/setup-env.sh
if [ ! -f ${SPACK_ENV} ]; then
  echo "Spack folder not found!"
  exit 1
fi

# Setup the spack environment
source ${SPACK_ENV}

# Use spack to get a recent enough version of python3
if [ $( spack find python@3.8: &>/dev/null
  echo $?
) -ne 0 ]; then
  echo "Installing a version of python3"
  # Compile openmpi with schedulers=slurm
  spack install python@3.8:
else
  echo "python@3.8: already installed!"
fi

# Load the python module
spack load python@3.8:

PYTHON_ENVIRONMENT_FOLDER=%python_environment.folder_name%

if [ ! -d ${PYTHON_ENVIRONMENT_FOLDER} ]; then
  # Create virtual environment
  python3 -m venv --prompt AS ${PYTHON_ENVIRONMENT_FOLDER}
fi

# Load environment
source ${PYTHON_ENVIRONMENT_FOLDER}/bin/activate

# Create a link to the binary
ln -sf $(which python3) ${WORKDIR}/python3

# Install the requirements via pip
# TODO: Due to a incompatibility issue between latest numba and numpy I have to add this here. Hopefully removable soon.
requirements="%python_environment.requirements%"

# Convert list with python format to a bash array
requirements=($( echo ${requirements} | sed "s/'//g" | tr -d '[],'))

# Install requirements.
for requirement in ${requirements[@]} ; do
  python -m pip install ${requirement}
done