#!/usr/bin/env bash
# Copyright 2026 - Ronald Portier (Open Eye Development)
# SPDX-License-Identifier: AGPL-3.0-or-later  (https://www.gnu.org/licenses/agpl.html)
#
# Show the status of the Odoo instance.
#
# Usage: bin/status
#
# In production: shows systemd service status.
# In dev: checks if an Odoo process is running for this instance.

set -euo pipefail

BUILD_DIR="$(cd "$(dirname "$0")/.." && pwd)"
INSTANCE="$(basename "$(dirname "$BUILD_DIR")")"
SERVICE="odoo-${INSTANCE}"

# Dev instances have no systemd service file.
SVC_FILE="/etc/systemd/system/${SERVICE}.service"
[[ -f "$SVC_FILE" ]] && IS_DEV=0 || IS_DEV=1

if [[ "$IS_DEV" -eq 1 ]]; then
    pids=$(pgrep -f "odoo-bin.*${INSTANCE}" 2>/dev/null || true)
    if [[ -n "$pids" ]]; then
        echo "[INFO]  Odoo is running (PID: $(echo "$pids" | tr '\n' ' '))"
        ps -o pid,etime,args -p $pids
    else
        echo "[INFO]  Odoo is not running. Start with: bin/run"
        exit 1
    fi
    exit 0
fi

systemctl status "$SERVICE"
