#!/usr/bin/env bash
set -e
# install uv if needed
install_uv_if_needed() {
# add PATH before uv
grep -Fxq 'export PATH="$HOME/.local/bin:$PATH"' ~/.bashrc || echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
# ensure PATH for current session
export PATH="$HOME/.local/bin:$PATH"
# install uv
if ! command -v uv >/dev/null 2>&1; then
curl -LsSf https://astral.sh/uv/install.sh | sh
fi
[ -f "$HOME/.local/bin/env" ] && source "$HOME/.local/bin/env"
grep -Fxq 'eval "$(uv generate-shell-completion bash)"' ~/.bashrc || echo 'eval "$(uv generate-shell-completion bash)"' >> ~/.bashrc
grep -Fxq 'eval "$(uvx --generate-shell-completion bash)"' ~/.bashrc || echo 'eval "$(uvx --generate-shell-completion bash)"' >> ~/.bashrc
uv tool install tox --with tox-uv
}

# install nvm if needed
install_nvm_if_needed() {
export NVM_DIR="$HOME/.nvm"
if [ -s "$NVM_DIR/nvm.sh" ]; then
\. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
else
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
fi
nvm install --lts
}

install_uv_if_needed
install_nvm_if_needed

uv sync
npm install
