#!/bin/bash

set -e

# Check if a supported python executable exists
# Eligible versions are listed at # https://developer.lsst.io/coding/python_style_guide.html#python-version
if hash python 2>/dev/null; then
	# Check the version by running a small Python program
	python -c 'import sys
minver2=7
minver3=6
vmaj = sys.version_info[0]
vmin = sys.version_info[1]
if (vmaj == 2 and vmin >= minver2) or (vmaj == 3 and vmin >= minver3):
    exit(0)
print("""check-python: Python2 >= {} or Python3 >= {} not found (version reported by '{}' is '{}').
check-python: Please ensure a compatible python is at the front of your PATH.""".format(minver2, minver3,
                                                                                        sys.executable,
                                                                                        sys.version))
exit(-1)'
	if [ "$?" -ne 0 ]; then
		exit -1
	fi
else
	echo "check-python: no python executable detected (looked for 'python')." 2>&1
	exit -1
fi

echo "Using $(which python) ($(python -V 2>&1))."

exit 0
