#!/usr/bin/env bash

set -eu
set -o pipefail > /dev/null 2>&1 || true

if python -c 'import pychart' > /dev/null 2>&1
then
	expected_pypy_time=300
	expected_pyx_time=3000
	expected_psyco_time=5000
	expected_cpython_time=10000
	expected_jython_time=10000

	for record in $(../../this-interpreter --variety all)
	do
		pyver=$(echo "$record" | awk -F'%' ' { print $1 }')
		interp=$(echo "$record" | awk -F'%' ' { print $2 }')

		case "$pyver" in
			usr-bin-python|cpython-2.[56])
				./"test-just-one" "$pyver" "$interp" pyx "$expected_pyx_time"
				./"test-just-one" "$pyver" "$interp" psyco "$expected_psyco_time"
				./"test-just-one" "$pyver" "$interp" py "$expected_cpython_time"
				;;
			cpython-2.7|cpython-3.*|usr-bin-python3)
				./"test-just-one" "$pyver" "$interp" pyx "$expected_pyx_time"
				./"test-just-one" "$pyver" "$interp" py "$expected_cpython_time"
				;;
			pypy-*)
				./"test-just-one" "$pyver" "$interp" py "$expected_pypy_time"
				;;
			jython*)
				./"test-just-one" "$pyver" "$interp" py "$expected_jython_time"
				;;
			*)
				echo "$0: Skipping unrecognized pyver: $pyver" 1>&2
				continue
				;;
		esac
	done

	./check

	cat output | egrep 'duration of' | awk ' { print $4, $6 }' | sort > bar-chart-data

	export PYCHART_OPTIONS='output=bar-chart.pdf'
	#./bar-chart.py > bar-chart.ps
	./bar-chart.py
else
	echo "$0: Skipping performance comparison because $(which python) does not have pychart" 1>&2
fi