#!/usr/bin/bash
#
# ai-journalctl -- Safe journalctl query wrapper for BlueOnyx AI.
#
# The wrapper only accepts a narrow, positional argument set and runs
# journalctl in a predictable form so sudoers can whitelist it safely.
#
set -e

PATH=/bin:/usr/bin:/usr/local/bin

usage() {
    echo "Usage: $(basename \"$0\") [--unit UNIT] [--since SINCE] [--until UNTIL] [--priority PRIORITY] [--lines N]" >&2
    exit 1
}

UNIT=""
SINCE="today"
UNTIL=""
PRIORITY=""
LINES="50"

while [ $# -gt 0 ]; do
    case "$1" in
        --unit)
            shift
            [ $# -gt 0 ] || usage
            UNIT="$1"
            ;;
        --since)
            shift
            [ $# -gt 0 ] || usage
            SINCE="$1"
            ;;
        --until)
            shift
            [ $# -gt 0 ] || usage
            UNTIL="$1"
            ;;
        --priority|-p)
            shift
            [ $# -gt 0 ] || usage
            PRIORITY="$1"
            ;;
        --lines|-n)
            shift
            [ $# -gt 0 ] || usage
            LINES="$1"
            ;;
        --no-pager)
            ;;
        *)
            echo "ERROR: Unsupported argument: $1" >&2
            usage
            ;;
    esac
    shift
done

case "$LINES" in
    ''|*[!0-9]*)
        echo "ERROR: --lines must be an integer" >&2
        exit 1
        ;;
esac

CMD=(/usr/bin/journalctl --no-pager "-n${LINES}")
if [ -n "$UNIT" ]; then
    CMD+=(--unit "$UNIT")
fi
if [ -n "$SINCE" ]; then
    CMD+=(--since "$SINCE")
fi
if [ -n "$UNTIL" ]; then
    CMD+=(--until "$UNTIL")
fi
if [ -n "$PRIORITY" ]; then
    CMD+=(-p "$PRIORITY")
fi

exec "${CMD[@]}"

# 
# Copyright (c) 2008-2026 Michael Stauber, SOLARSPEED.NET
# Copyright (c) 2008-2026 Team BlueOnyx, BLUEONYX.IT
# All Rights Reserved.
# 
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in 
#    the documentation and/or other materials provided with the 
#    distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its 
#    contributors may be used to endorse or promote products derived 
#    from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
# 
# You acknowledge that this software is not designed or intended for 
# use in the design, construction, operation or maintenance of any 
# nuclear facility.
# 
