#!/bin/bash

CURR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# shellcheck disable=SC1091
# shellcheck source=lib/devstack-heat
source "${CURR_DIR}/lib/devstack-heat"

function sub_stack() {
    local deployment
    local environment
    local key
    local parameters
    local positional

    parameters=()
    positional=()
    while [[ $# -gt 0 ]]; do
        key="$1"
        case $key in
            -e|--environment)
                environment="$2"
                shift
                shift
                ;;
            --parameter)
                parameters+=("$key")
                parameters+=("$2")
                shift
                shift
                ;;
            *)
                positional+=("$1")
                shift
                ;;
        esac
    done
    if [[ ${#positional[@]} -eq 0 ]]; then
        deployment=master_
    else
        deployment="${positional[0]}"
    fi
    deployment=$(_generate_deployment_name "${deployment}")

    if [[ -z "$environment" ]]; then
        environment="hot/parameters.yml"
    fi

    # create stack
    _confirm_or_exit "Deploying the stack ${deployment}"
    echo "Starting..."

    set -- "${parameters[@]}"
    set -x
    openstack stack create -e "$environment" -t hot/devstack_heat_template.yml "$@" "$deployment"
    set +x

    sub_show "$deployment"
}

function sub_unstack()
{
    local deployment
    deployment=${1:-master_}
    if [[ "${deployment}" == "master_" ]]; then
        (>&2 echo "You must put the whole stack name for unstacking")
        exit 1
    fi

    _confirm_or_exit "Deleting the stack ${deployment}"
    openstack stack delete "$deployment"
}

function sub_show() {
    local deployment
    deployment=${1:-master_}
    if [[ "${deployment}" == "master_" ]]; then
        (>&2 echo "You must put the whole stack name for showing the stack")
        exit 1
    fi

    _wait_for_after_in_progress "${1:-master_}"
    echo "VM subnet:     $(openstack stack output show "${deployment}" vm_subnet -f json | jq -r '.output_value')"
    echo "Nodes FIPs:    $(openstack stack output show "${deployment}" node_fips -f json | jq -r '.output_value' | jq -r '.[]?' | xargs echo)"
    printf "\n"
}



function sub_help() {
    local myname
    myname=$(basename "$0")

    printf "Usage: %s <subcommand> [options]\n" "$myname"
    printf "Subcommands:\n"
    printf "    stack gerrit_change_number     Create Heat stack\n"
    printf "    unstack my_stack_name   Delete Heat stack\n"
    printf "    show my_stack_name      Show important info about the deployed Heat stack\n"
    printf "    getkey my_stack_name    Output the Heat stack instances privkey to stdout\n"
    printf "    ssh my_stack_name       gets the key and sshs into the stack user of the Stack\n"
}

command=$1
case $command in
    "" | "-h" | "--help")
        sub_help
        ;;
    *)
        shift
        "sub_${command}" "$@"
        if [ $? = 127 ]; then
            echo "Error: '$command' is not a known $(basename "$0") command." >&2
            echo "       Run \'$(basename "$0")  --help\' for a list of known subcommands." >&2
            exit 1
        fi
        ;;
esac
