#!/usr/bin/env bash

# ./btrix: Browsertrix dev environment utility
#
# Test commands require installing pytest first, e.g.:
# python3 -m pip install pytest
#
# Usage:
#
# $ ./btrix setup
# Copy local config to expected location
# Local config must exist for other commands to work
#
# $ ./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
# Uninstall, delete data, then bootstrap
# Optional args:
#   -microk8s: Preface kubectl/helm commands with microk8s
#   -wait: Wait until pods are ready
#
# $ ./btrix down
# Uninstall and delete data
# Optional args:
#   -microk8s: Preface kubectl/helm commands with microk8s
#
# $ ./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 "Stopping all crawls & profile browsers"
    kubectl delete cjs -n crawlers --all
    kubectl delete pjs -n crawlers --all

    echo "Uninstalling..."
    helm uninstall btrix

    echo "Deleting data..."
    kubectl delete pvc --all
    kubectl delete cronjob -n crawlers --all
    kubectl delete configmap -n crawlers -l btrix.crawlconfig
}

resetMicrok8s(){
    echo "Stopping all crawls & profile browsers"
    microk8s kubectl delete cjs -n crawlers --all
    microk8s kubectl delete pjs -n crawlers --all

    echo "Uninstalling..."
    microk8s helm uninstall btrix

    echo "Deleting data..."
    microk8s kubectl delete pvc --all
    microk8s kubectl delete cronjob -n crawlers --all
    microk8s kubectl delete configmap -n crawlers -l btrix.crawlconfig
}

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

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

setupLocalConfig() {
    if [ -f ./chart/local.yaml ]; then
        echo "Local config file already exists at ./chart/local.yaml"
        exit 1
    fi
    echo "Copying local example config to ./chart/local.yaml"
    cp ./chart/examples/local-config.yaml ./chart/local.yaml
    echo "Local config file created. Edit ./chart/local.yaml to set local overrides"
    exit 0
}

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

if [[ $1 = "setup" ]]; then
    setupLocalConfig
fi

if [ ! -f ./chart/local.yaml ]; then
    echo "Local config file not found. Run './btrix setup' to configure"
    exit 1
fi

# 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. All running crawls will first be canceled. 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

# down: stop and uninstall browsertrix
if [[ $1 = "down" ]]; then
    if [[ $2 = "$MICROK8S" ]] ; then
        resetMicrok8s
    else
        reset
    fi
fi

echo "Done"
