#!/bin/sh

exitcode=0

echo '#############################'
echo 'Running tests in cuda11 container with python3.9 and 3.13'
echo '#############################'
echo 'output will be available in artifacts directory under "log.txt"'

rm -rf ./podman/output/*

# Test just the latest and oldest pythons available in the container
# (check dockerfile).
# Unlikely that it fails on an inbetween version.
GATEAU_TEST_VENVS="/venv3.9 /venv3.13" \
	./podman/test-all.sh test11  \
	2>&1 > /dev/null

if [ "$?" -ne 0 ]
then
	echo 'Something went wrong running test suite, check log.txt!'
	exitcode=2
fi

for result in podman/output/*.exitcode
do
	echo "$result: $(cat "$result")"
	if [ "$(cat "$result")" != '0' ]
	then
		echo "Something went wrong!! Check log.txt!"
		exitcode=2
	fi
done

mv ./podman/output/* "$MINICYCLE_ARTIFACTS"

echo '#############################'
echo 'Running editable install test!!'
echo '#############################'
echo 'output will be available in artifacts directory under "editable-log.txt"'

rm -rf ./podman/output/*

GATEAU_TEST_VENVS="/venv3.9 /venv3.13" \
	GATEAU_TEST_EDITABLE_INSTALL="yes" \
	./podman/test-all.sh test11  \
	2>&1 > /dev/null

if [ "$?" -ne 0 ]
then
	echo 'Something went wrong running test suite, check log.txt!'
	exitcode=2
fi

for result in podman/output/*.exitcode
do
	echo "$result: $(cat "$result")"
	if [ "$(cat "$result")" != '0' ]
	then
		echo "Something went wrong!! Check log.txt!"
		exitcode=2
	fi
done

set -e
for f in $( cd ./podman/output; ls )
do
	mv ./podman/output/$f "$MINICYCLE_ARTIFACTS/editable-$f"
done
set +e

echo '#############################'
echo 'Running ruff check'
echo '#############################'
echo 'output will be available in artifacts directory under "ruff.txt"'

#ruff check . -o "$MINICYCLE_ARTIFACTS/ruff.txt"
./podman/test-all.sh ruff

if [ "$?" -ne 0 ]
then
	echo 'Something went wrong running ruff, check ruff.txt!'
	exitcode=2
fi

mv ./podman/output/ruff.txt "$MINICYCLE_ARTIFACTS/ruff.txt"

echo '#############################'
echo 'Searching for TODOs and FIXMEs'
echo '#############################'
echo 'final count ill be available in artifacts directory under "num-todos.txt"'

num_todos="$(find \
	src tests gtest \
	-name '__pycache__' -prune -o \
	-type f -exec grep -Eo "TODO|FIXME" {} \;  \
	| wc -l )"

echo $num_todos > "$MINICYCLE_ARTIFACTS/num-todos.txt"

echo "Found $num_todos TODOs and FIXMEs"
if [ $num_todos -ne 0 ]
then
	echo "Go fix them!"
fi

echo '#############################'
echo 'BUILDING WHEEL'
echo '#############################'

rm -rf ./podman/output/*

./podman/test-all.sh staticwheel  \
	2>&1 > /dev/null

if [ "$?" -ne 0 ]
then
	echo 'Something went wrong building wheel'
	exitcode=2
fi

#for result in podman/output/*.exitcode
#do
#	echo "$result: $(cat "$result")"
#	if [ "$(cat "$result")" != '0' ]
#	then
#		echo "Something went wrong!!"
#		exitcode=2
#	fi
#done

set -e
for f in $( cd ./podman/output; ls )
do
	mv ./podman/output/$f "$MINICYCLE_ARTIFACTS/wheel-$f"
done
set +e

echo '#############################'
echo 'TESTING WHEEL'
echo '#############################'

rm -rf ./podman/output/*

./podman/test-all.sh test-wheel  \
	2>&1 > /dev/null

if [ "$?" -ne 0 ]
then
	echo 'Something went wrong testing wheel'
	exitcode=2
fi

for result in podman/output/*.exitcode
do
	echo "$result: $(cat "$result")"
	if [ "$(cat "$result")" != '0' ]
	then
		echo "Something went wrong!!"
		exitcode=2
	fi
done

set -e
for f in $( cd ./podman/output; ls )
do
	mv ./podman/output/$f "$MINICYCLE_ARTIFACTS/test-wheel-$f"
done
set +e

exit $exitcode

