#!/bin/bash -e
# Setup the repository.

cd "$(dirname "$0")/.."

# Add default vscode settings if not existing
SETTINGS_FILE=./.vscode/settings.json
SETTINGS_TEMPLATE_FILE=./contrib/vscode/settings.json
if [ ! -f "$SETTINGS_FILE" ]; then
    mkdir ./.vscode
    echo "Copy $SETTINGS_TEMPLATE_FILE to $SETTINGS_FILE."
    cp "$SETTINGS_TEMPLATE_FILE" "$SETTINGS_FILE"
fi

#if interactive install build deps
if [ -n "$PS1" ] || [[ $- == *i* ]] || [ -f /dev/.cros_milestone ]; then
    read -p  "Install build dependencies? (y/n) " question
    if [ "$question" = "y" ]; then
        DEPS=$(./contrib/ci/generate_dependencies.py)
        OS=$(python3 -c "import distro; print(distro.linux_distribution()[0].split()[0].lower())")
        if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ]; then
            if ! python3 -c "import venv"; then
                DEPS="python3-venv"
            fi
            sudo apt install $DEPS
        elif [ "$OS" = "fedora" ]; then
            sudo dnf install $DEPS
        elif [ "$OS" = "arch" ]; then
            pacman -Syu --noconfirm --needed $DEPS
        fi
    fi
fi

python3 -m venv venv
source venv/bin/activate

python3 -m pip install pre-commit
pre-commit install
