#!/bin/bash

## Copyright (C) 2026 - 2026 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## age-api-query: print age bracket number to stdout (machine-readable),
##                and a human-readable description to stderr.
## Exit code zero on success. Otherwise non-zero.

set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

## provides: read_integer_file
source /usr/libexec/helper-scripts/strings.bsh

parse_configuration() {
  shopt -s nullglob
  local i
  for i in /etc/age-api.d/*.conf /usr/local/etc/age-api.d/*.conf; do
    bash -n "$i"
    source "$i"
  done

  BRACKET_FILE="${HOME}/.age-api/age-bracket"

  age_bracket="$(read_integer_file "$BRACKET_FILE")"
}

parse_configuration

case "$age_bracket" in
  1) human="Under 13" ;;
  2) human="13 to under 16" ;;
  3) human="16 to under 18" ;;
  4) human="18 or older" ;;
  *)
    printf "age-api-query: invalid bracket '%s' in %s (expected 1-4)\n" "$age_bracket" "$BRACKET_FILE" >&2
    exit 1
    ;;
esac

## Human-readable output to stderr for interactive use/logging.
printf 'Age bracket: %s\n' "$human" >&2

## Machine-readable output to stdout for scripts.
printf '%s\n' "$age_bracket"
