#!/usr/bin/env bash
# tool/diagnose -- build a support bundle from the menu
set -o pipefail
SELF=$(readlink -f "${BASH_SOURCE[0]}")
NV_BASE=$(cd "$(dirname "$SELF")/../../.." && pwd)
export NV_BASE
# shellcheck source=/dev/null
. "$NV_BASE/lib/core/bootstrap.sh"

ui_header "$(t tool_m_diagnose)"
ui_step "$(t diag_collect)"

out=$(bash "$NV_BASE/install/diagnose.sh" 2>&1)
bundle=$(printf '%s\n' "$out" | tail -n 1)
if [ -f "$bundle" ]; then
    ui_ok "$(t diag_done "$bundle")"
    ui_kv "$(t col_size)" "$(du -h "$bundle" | cut -f1)"
else
    ui_err "$(t diag_failed)"
    printf '%s\n' "$out" | tail -n 5 | sed 's/^/    /'
    exit 1
fi
exit 0
