#!/bin/zsh
#
# Create or update a virtual environment on a MacOS development system

# assume this script is in the "tools" subdirectory; change to project directory
cd $(dirname $0)/..
PROJECT_DIR=$(pwd)
PROJECT_NAME=$(basename ${PROJECT_DIR})
VENV_DIR=${PROJECT_DIR}/venv

# check the virtual environment version on MacOS
if [[ "$(uname)" == "Darwin" ]]; then

    # Make sure Homebrew Python 3.12 is installed
    # First check for Apple silicon location
    if [[ -e /opt/homebrew/bin/python3.12  ]]; then
        echo "Using Homebrew Python 3.12 on Apple silicon"
        PYBINDIR=/opt/homebrew/bin

    # Make sure Homebrew Python 3.12 is installed
    elif [[ -e /usr/local/bin/python3.12  ]]; then
        echo "Using Homebrew Python 3.12 on Intel"
        PYBINDIR=/usr/local/bin

    # Make sure Homebrew Python 3.12 is installed
    else
        echo "Python 3.12 is not installed"
        exit 1
    fi

    # If the virtual environment exists, make sure the Python 3.12 version matches
    if [[ -d ${VENV_DIR} && "$(${PYBINDIR}/python3.12 -V)" != "$(${VENV_DIR}/bin/python3 -V)" ]]; then
        echo Removing obsolete $(${VENV_DIR}/bin/python3 -V) virtual environment
        rm -rf ${VENV_DIR}
    fi
fi


# create virtual environment if it doesn't exist
if [[ ! -d ${VENV_DIR} ]]; then
    # only implemented for MacOS
    if [[ "$(uname)" == "Darwin" ]]; then
        ${PYBINDIR}/python3.12 -m venv ${VENV_DIR} --prompt "${PROJECT_NAME}-dev"
        echo initialized virtual environment ${VENV_DIR}

    else
        echo "setupPythonVenv does not support OS $(uname)"
        exit 1
    fi
fi

# activate virtual environment
source ${VENV_DIR}/bin/activate
echo activating virtual environment ${VENV_DIR}

# read requirements.txt file and install or upgrade Python modules
if [[ -f "${PROJECT_DIR}/requirements.txt" ]]; then
    echo updating Python modules
    # If we're using a proxy, download pysocks first as pip requires it
    if [ -n "${ALL_PROXY}" ]
    then
        ALL_PROXY="" ${VENV_DIR}/bin/pip3 install --disable-pip-version-check pysocks
    fi

    # install setuptools and wheel first
    ${VENV_DIR}/bin/pip3 install --disable-pip-version-check setuptools wheel
    # now install all packages in the requirements file
    cat ${PROJECT_DIR}/requirements.txt | xargs ${VENV_DIR}/bin/pip3 install --disable-pip-version-check
    echo Python modules installed
fi

