#!/bin/bash

set -e

# Absolute path this script is in
SCRIPT_PATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd )"
CMD=$1

if [ "$PWD" != "$SCRIPT_PATH" ]; then
    echo "Please run this command from the root of your bigquery-etl checkout ($SCRIPT_PATH)"
    exit 1
fi

if ! command -v python3 &> /dev/null; then
    echo "Please install Python (version 3.11)"
    exit 1
fi

if [ "$CMD" == "bootstrap" ]; then
    if [[ ! $(python3 --version) =~ Python\ 3\.11 ]]; then
        echo "bqetl currently only works with Python 3.11 (you can use pyenv to install different versions of Python)"
        exit 1
    fi

    if [ -d "venv" ]; then
        echo 'venv subdirectory already exists, execute "rm -rf venv" and re-run this command if you want to recreate it'
        exit 1
    else
        python3 -m venv venv
    fi

    venv/bin/pip install --no-deps -r requirements.txt
    venv/bin/pip install -e .
    echo "bqetl configured! It should now be ready for use."
    export BIGEYE_API_CRED_FILE='bigeye_credentials.ini'
    exit 0
fi

if [ ! -d "venv" ]; then
    echo "Please run ./bqetl bootstrap"
    exit 1
fi

venv/bin/bqetl "$@"
