#!/usr/bin/env bash

set -e

DB_NAME=mev_inspect

function get_kube_secret(){
    kubectl get secrets $1 -o jsonpath="{.data.$2}" | base64 --decode
}

function get_kube_db_secret(){
    kubectl get secrets mev-inspect-db-credentials -o jsonpath="{.data.$1}" | base64 --decode
}

function db(){
    host=$(get_kube_secret "mev-inspect-db-credentials" "host")
    username=$(get_kube_secret "mev-inspect-db-credentials" "username")
    password=$(get_kube_secret "mev-inspect-db-credentials" "password")

    kubectl run -i --rm --tty postgres-client-$RANDOM \
        --env="PGPASSWORD=$password"  \
        --image=jbergknoff/postgresql-client \
        -- $DB_NAME --host=$host --user=$username
}

function redis(){
    echo "To continue, enter 'shift + r'"
    redis_password=$(get_kube_secret "redis" "redis-password")
    kubectl run -i --rm --tty \
        --namespace default redis-client-$RANDOM \
        --env REDIS_PASSWORD=$redis_password \
        --image docker.io/bitnami/redis:6.2.6-debian-10-r0 \
        --command -- redis-cli -h redis-master -a $redis_password
}

case "$1" in
  db)
        echo "Connecting to $DB_NAME"
        db
	;;
  redis)
        echo "Connecting to redis"
        redis
    ;;
  listener)
        kubectl exec -ti deploy/mev-inspect -- ./listener $2
	;;
  backfill)
        start_block_number=$2
        end_block_number=$3

        echo "Backfilling from $start_block_number to $end_block_number"
        kubectl exec -ti deploy/mev-inspect -- poetry run enqueue-many-blocks $start_block_number $end_block_number
	;;
  inspect)
        block_number=$2
        echo "Inspecting block $block_number"
        kubectl exec -ti deploy/mev-inspect -- poetry run inspect-block $block_number
	;;
  inspect-many)
        start_block_number=$2
        end_block_number=$3
        echo "Inspecting from block $start_block_number to $end_block_number"
        kubectl exec -ti deploy/mev-inspect -- \
            poetry run inspect-many-blocks $start_block_number $end_block_number
	;;
  test)
        shift
        echo "Running tests"
        kubectl exec -ti deploy/mev-inspect -- poetry run pytest tests $@
	;;
  fetch)
      block_number=$2
      echo "Fetching block $block_number"
      kubectl exec -ti deploy/mev-inspect -- poetry run fetch-block $block_number
  ;;
  prices)
        shift
        case "$1" in
          fetch-all)
                echo "Running price fetch-all"
                kubectl exec -ti deploy/mev-inspect -- \
                    poetry run fetch-all-prices
            ;;
          *)
            echo "prices usage: "$1" {fetch-all}"
            exit 1
        esac
  ;;
  exec)
        shift
        kubectl exec -ti deploy/mev-inspect -- $@
  ;;
  *)
	echo "Usage: "$1" {db|backfill|inspect|test}"
	exit 1
esac

exit 0
