#!/bin/bash

set -euo pipefail

if [[ ! -e o2_nrConfs.txt ]]; then
  echo "o2_nrConfs.txt not found, exiting"
  exit 1
fi
NR_CONFS=$(cat o2_nrConfs.txt)
rm scratch/o4/compile*.log


# sequential version:
echo "Compiling all versions, logs will be written to scratch/o4/."
echo ""
for i in $(seq 0 ${NR_CONFS}); do
  echo -ne "\r  $i / ${NR_CONFS}"
  DIR=scratch/$(printf "var%05d" $i)
  make -j4 -C ${DIR} &> scratch/o4/compile-$i.log
  mv ${DIR}/exastencils ${DIR}/exastencils.exe
  make -C ${DIR} clean &> /dev/null
done
echo -e "\rdone"
#####################


# parallel version using slurm:
#DEP=$(cat o3.sid)
#NAME=$(basename $(pwd))
#
#sbatch -C chimaira -n 1 -c 2 --array=0-$((${NR_CONFS} / 20)) -o scratch/o4/compile_blob-%a.log -e scratch/o4/compile_blob-%a.log --parsable --nice=250 --job-name "pExpl comp ${NAME}" --time=120 --dependency=afterany:${DEP} "$@" <<EOF | tee o4.sid
##!/bin/bash
#
#ID=\$(( \$SLURM_ARRAY_TASK_ID * 20 ))
#MAX=\$(( (\$ID + 19) >= $NR_CONFS ? $NR_CONFS : (\$ID + 19) ))
#for i in \$(seq \$ID \$MAX); do
#  srun ../helper-compileSingle.sh \$i no_chk
#done
#EOF
#####################
