#!/bin/bash
#
# input is a python version SemVer, e.g. 3.10.11

IFS='.'
read -ra newarr <<< "$1"
count=0
for val in "${newarr[@]}"; do
    if [ $count == 0 ]; then
        major="$val"
    fi
    if [ $count == 1 ]; then
        minor="$val"
    fi
    ((count=count+1))
done

int_minor=$((minor))

if [ ! -d "$HOME/py$major$minor" ]; then
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt update
    sudo apt install -y python$major.$minor-dev
    if [ int_minor -lt 12 ]; then
        sudo apt install -y python$major.$minor-distutils
    fi
    sudo apt install -y python$major.$minor-venv
    cd $HOME
    "python$major.$minor" -m venv py$major$minor
    source py$major$minor/bin/activate
    pip install --upgrade pip
    deactivate
    echo "python version $major.$minor installed"
else
    echo "python version $major.$minor previously installed"
fi
