# Which kind of machine this is: 'desktop' or 'server'. Sourced, not run.
#
# The role is the one interview answer Panama keeps. Everything else is
# transient by design because it is personal -- a name, an email -- and
# re-asking costs less than a state file that drifts. The role is neither
# personal nor re-askable: `panama update` runs with no interview at all, and
# a server updated as a desktop would acquire Hyprland, so the answer has to
# outlive the run that gave it.
#
# Precedence: an exported PANAMA_ROLE (a fresh install, where the interview
# just asked) beats the recorded file (every later run), which beats the
# default. The default is desktop because every machine that predates roles
# is one.

PANAMA_ROLE_FILE="${XDG_STATE_HOME:-$HOME/.local/state}/panama/role"

panama_role() {
  local role="${PANAMA_ROLE:-}"
  if [[ -z "$role" && -r "$PANAMA_ROLE_FILE" ]]; then
    role="$(<"$PANAMA_ROLE_FILE")"
  fi
  case "$role" in
    server) printf 'server' ;;
    *)      printf 'desktop' ;;
  esac
}

# Anything but 'server' records 'desktop' rather than erroring: the recorded
# file must never hold a value panama_role would refuse to read back.
panama_role_record() {
  local role="$1"
  [[ "$role" == server ]] || role=desktop
  mkdir -p "$(dirname "$PANAMA_ROLE_FILE")"
  printf '%s\n' "$role" >"$PANAMA_ROLE_FILE"
}
