#! /bin/bash
# vim: shiftwidth=4 tabstop=4 expandtab

subcommand__help() {
    if [[ -z "$2" ]] || [[ "$2" == "ls" ]]; then
        echo "$COMMAND check [VM|ALL]"
        if [[ "$1" == "details" ]] && [[ "$2" == "check" ]]; then
            echo "  Check QEmu Guest Agent status on specified VM"
            echo "    VM|ALL      Name of VM to check or 'ALL' to check all VMs"
        fi
    fi
    if [[ -z "$2" ]] || [[ "$2" == "info" ]]; then
        echo "$COMMAND info [VM]"
        if [[ "$1" == "details" ]] && [[ "$2" == "info" ]]; then
            echo "  Show QEmu Guest Agent information of specified VM"
        fi
    fi
}

__check() {
    local hyp=$1 vm=$2

}

subcommand__check() {
    check_generate_script_not_supported
    local vm=$1 hyp
    [[ -z "$vm" ]] && \
        usage "VM name is missing (or the 'ALL' keyword to check all VMs of the cluster)"
    if [[ "$vm" == "ALL" ]]; then
        local hyp_vm
        local -a vms
        mapfile -t vms < <( _list_vms_by_state "running" | awk '{print $1 " " $3 }' )
        for hyp_vm in "${vms[@]}"; do
            hyp=$( awk '{print $1}' <<< "$hyp_vm" )
            vm=$( awk '{print $2}' <<< "$hyp_vm" )
            if response=$(
                run_external ssh "$hyp" "$REAL_VIRSH" qemu-agent-command "$vm" \
                    '{\"execute\": \"guest-ping\"}' 2>&1
            ) && [[ "$response" == '{"return":{}}' ]]; then
                sprint -c green -s bold "[OK]" -r all "$vm"
            else
                sprint -c red -s bold "[ERROR]" -r all "$vm" -s dim "($response)"
            fi
        done
        return
    fi
    hyp=$( locate "$vm" )
    check_hyp "$hyp" || usage "Unknown VM $vm"
    if response=$(
        run_external ssh "$hyp" "$REAL_VIRSH" qemu-agent-command "$vm" '{\"execute\": \"guest-ping\"}'
    ) && [[ "$response" == '{"return":{}}' ]]; then
        display "QEmu Guest Agent is running on VM $vm."
        return
    fi
    error "QEmu Guest Agent is NOT running on VM $vm."
    exit 1
}

subcommand__info() {
    check_generate_script_not_supported
    local vm="" json=0 opt hyp
    for opt in "${@:1}"; do
        case $opt in
            -J|--json)
                json=1
                ;;
            *)
                if [[ -z "$vm" ]]; then
                    vm=$opt
                else
                    usage "Unknown parameter '$opt'"
                fi
                ;;
        esac
    done
    [[ -z "$vm" ]] && usage "VM name is missing"
    hyp=$( locate "$vm" )
    check_hyp "$hyp" || usage "Unknown VM $vm"
    data=$(
        run_external ssh "$hyp" "$REAL_VIRSH" qemu-agent-command "$vm" '{\"execute\": \"guest-info\"}'
    ) || fatal_error "Failed to retrieve QEmu Guest Agent information"
    if [[ "$json" -eq 1 ]]; then
        jq <<< "$data"
        return
    fi
    local version
    version=$( jq -r .return.version <<< "$data" )
    display "QEmu Guest Agent version: $version"
    local -a enabled_command disabled_commands
    mapfile -t enabled_command < <(
        jq -r '.return.supported_commands | map(select(.enabled == true) | .name) | .[]' \
            <<< "$data" | sort
    )
    mapfile -t disabled_commands < <(
        jq -r '.return.supported_commands | map(select(.enabled == false) | .name) | .[]' \
            <<< "$data" | sort
    )

    if [[ "${#enabled_command}" -gt 0 ]]; then
        display "Enabled command: $( implode ", " "${enabled_command[@]}" )"
    else
        echo "No enabled command"
    fi

    if [[ "${#disabled_commands}" -gt 0 ]]; then
        display "Disabled commands: $( implode ", " "${disabled_commands[@]}" )"
    else
        echo "No disabled command"
    fi
}

run_subcommand "${COMMAND_ARGS[@]}"
