#!/usr/bin/env bash
# Banner module (global/user). Runs once per login session.

case "$-" in
  *i*) ;;
  *) return 0 ;;
esac

# Disable switch (per-user/per-shell)
# 1) env: DISABLE_GLOBAL_BANNER=1
# 2) file: ~/.config/ps1/disable-banner
if [[ "${DISABLE_GLOBAL_BANNER:-0}" == "1" ]]; then
  return 0
fi
if [[ -f "$HOME/.config/ps1/disable-banner" ]]; then
  return 0
fi

# Run banner only once per session
if [[ -n "${__BANNER_ALREADY_SHOWN:-}" ]]; then
  return 0
fi
__BANNER_ALREADY_SHOWN=1

# Find tools (be defensive)
FIGLET="$(command -v figlet 2>/dev/null || true)"
LANDSCAPE="$(command -v landscape-sysinfo 2>/dev/null || true)"

# lolcat is often /usr/games/lolcat on Ubuntu
if [[ -x /usr/games/lolcat ]]; then
  LOLCAT="/usr/games/lolcat"
else
  LOLCAT="$(command -v lolcat 2>/dev/null || true)"
fi

HAVE_LOLCAT=0
if [[ -n "${LOLCAT:-}" ]]; then
  HAVE_LOLCAT=1
fi

# Punchline source:
# - global file: /etc/ps1-punchline
# - fallback: "Hello"
PUNCHLINE="Hello"
if [[ -r /etc/ps1-punchline ]]; then
  PUNCHLINE="$(head -n 1 /etc/ps1-punchline 2>/dev/null || echo "Hello")"
  PUNCHLINE="${PUNCHLINE%%$'\r'}"
fi

# Print banner + sysinfo
if [[ -n "${FIGLET:-}" ]]; then
  if [[ "$HAVE_LOLCAT" -eq 1 ]]; then
    "$FIGLET" "$(hostname)" -c | "$LOLCAT"
    "$FIGLET" -f digital "$PUNCHLINE" -c | "$LOLCAT"
    if [[ -n "${LANDSCAPE:-}" ]]; then
      "$LANDSCAPE" | "$LOLCAT"
    fi
  else
    "$FIGLET" "$(hostname)" -c
    "$FIGLET" -f digital "$PUNCHLINE" -c
    if [[ -n "${LANDSCAPE:-}" ]]; then
      "$LANDSCAPE"
    fi
  fi
fi
