#!/bin/bash

# ./btrix: Browsertrix Cloud dev environment utility
#
# Note: btrix helper expects a local.yaml file to exist in
# the chart directory alongside values.yaml.
#
# Test commands require installing pytest first, e.g.:
# python3 -m pip install pytest
#
# Usage:
#
# $ ./btrix bootstrap
# Build frontend and backend and upgrade
# Optional args:
#   -microk8s: Preface kubectl/helm commands with microk8s
#   -wait: Wait until pods are ready
#
# $ ./btrix reset
# Uinstall, delete data, then bootstrap
# Optional args:
#   -microk8s: Preface kubectl/helm commands with microk8s
#   -wait: Wait until pods are ready
#
# $ ./btrix test
# Run backend tests
#
# $ ./btrix nightly
# Run nightly backend tests


bootstrap(){
    echo "Building backend..."
    ./scripts/build-backend.sh

    echo "Building frontend..."
    ./scripts/build-frontend.sh

    echo "Installing..."
    helm upgrade --install -f ./chart/values.yaml -f ./chart/local.yaml btrix ./chart
}

bootstrapMicrok8s(){
    echo "Building backend..."
    ./scripts/build-backend.sh

    echo "Building frontend..."
    ./scripts/build-frontend.sh

    echo "Installing..."
    microk8s helm3 upgrade --install -f ./chart/values.yaml -f ./chart/local.yaml btrix ./chart
}

waitUntilReady(){
    echo "Waiting until ready..."
    kubectl wait --for=condition=ready pod --all --timeout=300s
}

waitUntilReadyMicrok8s(){
    echo "Waiting until ready..."
    microk8s kubectl wait --for=condition=ready pod --all --timeout=300s
}

reset(){
    echo "Uninstalling..."
    helm uninstall btrix

    echo "Deleting data..."
    kubectl delete pvc --all
}

resetMicrok8s(){
    echo "Uninstalling..."
    microk8s helm uninstall btrix

    echo "Deleting data..."
    microk8s kubectl delete pvc --all
}

runTests() {
    echo "Running backend tests..."
    python3 -m pytest backend/test/*.py
}

runNightlyTests() {
    echo "Running nightly backend tests..."
    python3 -m pytest backend/test_nightly/*.py
}

CONTEXT=$(cat ~/.kube/config | grep "current-context:" | sed "s/current-context: //")
MICROK8S="-microk8s"
WAIT="-wait"

# bootstrap: build frontend and backend, upgrade and wait until ready
if [[ $1 = "bootstrap" ]]; then

    echo "Current context: $CONTEXT"
    echo "Are you sure you want to update this context?"
    if [[ "$(read -e -p '[y/N] > '; echo $REPLY)" == [Yy]* ]] ; then
        echo Continuing
    else
        echo Stopping
        exit 1
    fi

    if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
        bootstrapMicrok8s
    else
        bootstrap
    fi

    
    if [[ $2 = "$WAIT" || $3 = "$WAIT" ]]; then
        if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
            waitUntilReadyMicrok8s
        else
            waitUntilReady
        fi
    fi
fi

# reset: uninstall, delete data, then bootstrap
if [[ $1 = "reset" ]]; then
    
    echo "Current context: $CONTEXT"
    echo "Resetting k8s cluster will delete the database. Are you sure you want to do this?"
    if [[ "$(read -e -p '[y/N] > '; echo $REPLY)" == [Yy]* ]] ; then
        echo Continuing
    else
        echo Stopping
        exit 1
    fi

    if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
        resetMicrok8s
        bootstrapMicrok8s
    else
        reset
        bootstrap
    fi

    if [[ $2 = "$WAIT" || $3 = "$WAIT" ]] ; then
        if [[ $2 = "$MICROK8S" || $3 = "$MICROK8S" ]] ; then
            waitUntilReadyMicrok8s
        else
            waitUntilReady
        fi
    fi
fi

# test: run backend tests
if [[ $1 = "test" ]]; then
    runTests
fi

# nightly: run nightly backend tests
if [[ $1 = "nightly" ]]; then
    runNightlyTests
fi

echo "Done"
