#!/usr/bin/env bash
# Set up ESPHome dev environment

set -e

cd "$(dirname "$0")/.."
if [ -n "$VIRTUAL_ENV" ]; then
  # A virtual environment is already active (e.g. the devcontainer's pre-provisioned
  # esphome-venv). Install into it rather than creating a ./venv in the workspace.
  created_venv=false
else
  created_venv=true
  if [ -x "$(command -v uv)" ]; then
    uv venv --seed venv
  else
    python3 -m venv venv
  fi
  source venv/bin/activate
fi

if ! [ -x "$(command -v uv)" ]; then
  python3 -m pip install uv
fi

uv pip install setuptools wheel
uv pip install -e ".[dev,test]" --config-settings editable_mode=compat

pre-commit install

mkdir -p .temp

echo
echo
if [ "$created_venv" = true ]; then
  echo "Virtual environment created at ./venv. Run 'source venv/bin/activate' to use it."
else
  echo "Dependencies installed into the active virtual environment:"
  echo "  $VIRTUAL_ENV"
  echo "It is already active in this shell, so no 'source venv/bin/activate' is needed."
fi
