#!/usr/bin/bash

# test if link already exists
if [ -L "/usr/bin/python" ]; then
	exit 0
fi

# find latest python version
py_ver="$(ls /usr/bin/python* /usr/local/bin/python* 2>/dev/null | grep -E -o 'python[0-9]+(\.[0-9]+)?' | sort -Vr | head -n 1)"

if [ -n "$py_ver" ]; then
	py_bin=$(which "$py_ver")
	echo "No link to /usr/bin/python found. Linking $py_ver as main Python environment"
else
	echo "Error: No Python installations found."
	exit 1
fi

# create link
ln -s "$py_bin" "/usr/bin/python"

# test if link was created
if [ ! -L "/usr/bin/python" ]; then
	echo "Error: Failed to create symbolic link for Python."
	exit 1
fi

exit 0

