#!/bin/bash

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

if [ ! -d "$HOME/py$major$minor" ]; then
    cp changes_in "changes$major.$minor.plist"
    sed -i "" "s/_VERSION_/$major.$minor/g" "changes$major.$minor.plist"
    curl -o "python$major.$minor-installer.pkg" "https://www.python.org/ftp/python/$1/python-$1-macos11.pkg"
    sudo installer -pkg "./python$major.$minor-installer.pkg" -applyChoiceChangesXML "./changes$major.$minor.plist" -target /
    "/Library/Frameworks/Python.framework/Versions/$major.$minor/bin/python$major.$minor" -m venv "$HOME/py$major$minor"
    source "$HOME/py$major$minor/bin/activate"
    python -m pip install --upgrade pip
    deactivate
    rm "python$major.$minor-installer.pkg"
    echo "python version $major.$minor installed"
else
    echo "python version $major.$minor previously installed"
fi
