#!/usr/bin/env bash

pkg_path="/home/mepland/chance_of_showers"

# Models to run, see model_kwarg_list in drive_bayesian_opt.py
# prod
i_model_min=0
i_model_max=34
# dev
# i_model_min=30 # 0, 3, 20, 25, 30
# i_model_max=$i_model_min

# Total number of points to generate
# prod
max_points=200
# dev
# max_points=5

# Turn on tdqm
# unset TQDM_DISABLE
# Turn off tqdm
export TQDM_DISABLE=1

# Maximum number of iterations of Bayesian optimization to perform.
# Will stop script if reached before max_points, preventing infinite loops.
max_iter=999
max_iter=$((max_points < max_iter ? max_points : max_iter))

# Check for running drive_bayesian_opt.py
if pgrep -fc "python.*?drive_bayesian_opt.py" > /dev/null 2>&1; then
	echo "Found python drive_bayesian_opt.py running, doing nothing"
	unset TQDM_DISABLE
	exit
fi

# Run over all models
for ((i_model = i_model_min; i_model <= i_model_max; i_model++)); do
	printf "Starting i_model = %d, running to i_model = %d\n\n" "$i_model" "$i_model_max"

	# Run drive_bayesian_opt.py max_iter times, or until max_points is reached
	n_points=-1
	for ((i_iter = 1; i_iter <= max_iter; i_iter++)); do
		# Can add \r to start to backup one line, if drive_bayesian_opt.py is printing nothing out
		printf "Starting drive_bayesian_opt.py i_iter = %d of %d for i_model = %d, currently have %d points\n" "$i_iter" "$max_iter" "$i_model" "$n_points"

		poetry run python -u $pkg_path/ana/drive_bayesian_opt.py +i_model=$i_model +max_points=$max_points
		status=$?
		n_points=$((status - 10))

		if [[ $status -lt 10 ]]; then
			echo "On i_iter = $i_iter, found status = $status, an exception occurred, breaking to next model!"
			break
		elif [[ $max_points -le $n_points ]]; then
			echo "On i_iter = $i_iter, found max_points = $max_points <= $n_points points, breaking to next model"
			break
		fi

	done

done

echo "Completed all runs"
unset TQDM_DISABLE
exit
