#!/bin/bash

# exit if any command fails
set -e

# Change to the root of the project
cd "$(dirname "$0")"/../..

# Check if the virtual environment directory exists
# and that there is a /bin/activate command
if [ ! -d "./venv" ] || [ ! -f "./venv/bin/activate" ]; then
  echo "Virtual environment not found. Creating one..."
  python3 -m venv ./venv

  # Ensure pip is installed inside the venv
  echo "Ensuring pip is available in the virtual environment..."
  ./venv/bin/python3 -m ensurepip --default-pip
  ./venv/bin/python3 -m pip install --upgrade pip setuptools wheel
fi

# Activate the virtual environment
source ./venv/bin/activate

# Upgrade pip to the latest version
python3 -m pip install --upgrade pip

# If CLEAN parameter is set to true, remove the existing packages
if [ "$CLEAN" = "true" ]; then
  pip freeze | grep -v "@" | xargs pip uninstall -y
fi

# Install the required packages
pip install .

echo "Virtual environment setup complete."
