#!/usr/bin/env bash

set -o errexit
set -o pipefail

if [[ -z "$1" ]] || [[ "$1" == "--current" ]]; then
  mode="current"
  shift
elif [[ "$1" == "--all" ]]; then
  mode="all"
  shift
else
  echo "Usage: $0 [--current|--all] [journalctl options...]"
  exit 1
fi

if [[ "$mode" == "current" ]]; then
  invocation_id="$(sudo systemctl show -p InvocationID --value aembit_agent_proxy.service)"
  if [[ "$invocation_id" == "0" ]]; then
    echo "Aembit Agent Proxy is not running."
    exit 1
  fi
  sudo journalctl --namespace aembit_agent_proxy "$@" _SYSTEMD_INVOCATION_ID="$invocation_id"
elif [[ "$mode" == "all" ]]; then
  sudo journalctl --namespace aembit_agent_proxy "$@"
fi
