#!/bin/bash

export SKIP_LOGGING=true # Set to true if you already logged in AZ CLI
export AZ_SA_RG_NAME="tfstates"

export no_proxy="<LIST OF URLs TO BYPASS PROXY>"
export https_proxy="<PROXY URL>"
export http_proxy="<PROXY URL>"

function login() {
    az login -o table
}

function set_subscription() {
    OIFS=$IFS
    IFS=$'\n'
    select subscription in $(az account list --query "[].{name:name,id:id}" -otsv | sort); do
        az account set --subscription $(echo ${subscription} | awk '{print $2}')
        break
    done
    IFS=$OIFS
}

function set_rg_name() {
    select rg in $(az group list --query "[].name" -otsv); do
        export AZ_SA_RG_NAME=$rg
        break
    done
}

function set_sa_name() {
    select sa in $(az storage account list --query "[].{name:name,resourceGroup:resourceGroup}" --resource-group $AZ_SA_RG_NAME -otsv | awk '{print $1}'); do
        export AZ_SA_NAME=$sa
        break
    done
}

function get_sa_key() {
    echo 'export AZ_SA_KEY=$(az storage account keys list --account-name $AZ_SA_NAME --resource-group $AZ_SA_RG_NAME --query "[0].value" -otsv)'
    export AZ_SA_KEY=$(az storage account keys list --account-name $AZ_SA_NAME --resource-group $AZ_SA_RG_NAME --query "[0].value" -otsv)
}

function set_container_name() {
    select container in $(az storage container list --account-name $AZ_SA_NAME --auth-mode login --query '[].name' -o tsv); do
        export AZ_SA_CONTAINER_NAME=$container
        break
    done
}

function set_blob_names() {
    select blob in $(az storage blob list --account-name $AZ_SA_NAME --auth-mode login --container-name $AZ_SA_CONTAINER_NAME --query '[].name' -o tsv | awk '{print $1}'); do
        export AZ_SA_BLOB_NAME=$blob
        break
    done
}

function download_blob() {
    echo 'az storage blob download --account-name $AZ_SA_NAME --auth-mode login --container-name $AZ_SA_CONTAINER_NAME --name $AZ_SA_BLOB_NAME --file $AZ_SA_BLOB_NAME'
    az storage blob download --account-name $AZ_SA_NAME --auth-mode login --container-name $AZ_SA_CONTAINER_NAME --name $AZ_SA_BLOB_NAME --file $AZ_SA_BLOB_NAME
}

function upload_blob() {
    read -p "Do you want to overwrite the blob if it exists? (y/n): " overwrite
    if [[ $overwrite == "y" ]]; then
        OVERWRITE_OPTION="--overwrite"
    else
        OVERWRITE_OPTION=""
    fi
    az storage blob upload --account-name $AZ_SA_NAME --auth-mode login --container-name $AZ_SA_CONTAINER_NAME --name $AZ_SA_BLOB_NAME --file $AZ_SA_BLOB_NAME $OVERWRITE_OPTION
}

function take_snapshot() {
    az storage blob snapshot --account-name $AZ_SA_NAME --auth-mode login --container-name $AZ_SA_CONTAINER_NAME --name $AZ_SA_BLOB_NAME
}

function break_lease() {
    az storage blob lease break --account-name $AZ_SA_NAME --auth-mode login --container-name $AZ_SA_CONTAINER_NAME --blob-name $AZ_SA_BLOB_NAME
}

function select_operation() {
    select operation in "Download" "Upload" "Break lease" "Take snapshot"; do
        case $operation in
        Download)
            download_blob
            break
            ;;
        Upload)
            upload_blob
            break
            ;;
        Break\ lease)
            break_lease
            break
            ;;
        Take\ snapshot)
            take_snapshot
            break
            ;;
        esac
    done
}

[[ $SKIP_LOGGING ]] || login
[[ $SKIP_LOGGING ]] && set_subscription

set_rg_name
set_sa_name
#get_sa_key
set_container_name
set_blob_names
select_operation

unset https_proxy no_proxy http_proxy SKIP_LOGING AZ_SA_NAME AZ_SA_KEY AZ_SA_CONTAINER_NAME AZ_SA_BLOB_NAME
