#!/usr/bin/env bash
# site/list -- read from the registry, never by scanning nginx configs
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 site_m_list)" "$(t site_count): $(nv_site_count)"

if [ "$(nv_site_count)" -eq 0 ]; then
    ui_info "$(t site_none)"
    ui_blank
    exit 0
fi

printf '  %-34s %-8s %-12s %-10s %s\n' \
    "$(t col_domain)" "$(t col_php)" "$(t col_ssl)" "$(t col_cache)" "$(t col_status)"
while IFS=$'\t' read -r domain php ssl health; do
    [ -z "$domain" ] && continue
    cache=$(nv_site_get "$domain" cache_redis 0)
    [ "$cache" = "1" ] && cache='redis' || cache='-'
    printf '  %-34s %-8s %-12s %-10s %s\n' "$domain" "$php" "$ssl" "$cache" "$health"
done < <(nv_site_summary)
ui_blank
exit 0
