#!/bin/sh
set -eu

DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
. "$DIR/lib/common.sh"

unit=""
# optional; if omitted, we default to 400 via _ac_uint_clamp
n=""
since=""
until=""
priority=""
boot=""

usage() {
  echo "Usage: JournalctlUnit --unit UNIT [--n N] [--since TIME] [--until TIME] [--priority PRIO] [--boot BOOT]" >&2
}

_ac_handle_help usage "$@"
_ac_require journalctl

# Parse args (ac-ops passes --<param> flags)
_ac_parse_args \
  unit:str \
  n:int \
  since:str \
  until:str \
  priority:str \
  boot:int \
  -- "$@"

# Validate
_ac_require_nonempty unit
_ac_uint_clamp n 1 2000 400

# boot may be negative (optional)
if [ -n "$boot" ]; then
  _ac_require_int boot
fi

# Force an LLM-friendly, stable format:
# - no pager
# - UTC timestamps
# - short-iso output (human readable, has timestamps)
set -- journalctl --no-pager --utc -o short-iso -u "$unit" -n "$n"
[ -n "$since" ] && set -- "$@" --since "$since"
[ -n "$until" ] && set -- "$@" --until "$until"
[ -n "$priority" ] && set -- "$@" -p "$priority"
[ -n "$boot" ] && set -- "$@" -b "$boot"

exec "$@"
