#!/usr/bin/env bash
# Metadata and help utilities for scripts
# Provides simple helpers to print script usage and header information.

# Print a standardized usage/help message using optional variables:
#   SCRIPT_NAME, SCRIPT_DESC, SCRIPT_USAGE, SCRIPT_OPTIONS, SCRIPT_ENVVARS, SCRIPT_REQUIREMENTS, SCRIPT_EXAMPLE
script_usage() {
    local name="${SCRIPT_NAME:-${0##*/}}"
    echo "${name}"
    [ -n "${SCRIPT_DESC:-}" ] && echo "Description: ${SCRIPT_DESC}"
    if [ -n "${SCRIPT_USAGE:-}" ]; then
        echo "Usage: ${SCRIPT_USAGE}"
    else
        echo "Usage: ${name} [options]"
    fi
    if [ -n "${SCRIPT_OPTIONS:-}" ]; then
        echo
        echo "Options:"
        # shellcheck disable=SC2001
        echo "${SCRIPT_OPTIONS}" | sed 's/^/  /'
    fi
    if [ -n "${SCRIPT_ENVVARS:-}" ]; then
        echo
        echo "Environment:"
        # shellcheck disable=SC2001
        echo "${SCRIPT_ENVVARS}" | sed 's/^/  /'
    fi
    if [ -n "${SCRIPT_REQUIREMENTS:-}" ]; then
        echo
        echo "Requires: ${SCRIPT_REQUIREMENTS}"
    fi
    if [ -n "${SCRIPT_EXAMPLE:-}" ]; then
        echo
        echo "Example:"
        # shellcheck disable=SC2001
        echo "${SCRIPT_EXAMPLE}" | sed 's/^/  /'
    fi
}

# Handle -h/--help; call after sourcing init and setting metadata vars
handle_help() {
    case "${1:-}" in
        -h|--help)
            script_usage
            exit 0
            ;;
    esac
}
