#!/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)
#
# Run Odoo in the foreground (not as a service) for debugging.
# Uses the production config — add extra flags after the script if needed.
#
# Usage: bin/run [--log-level=debug]
#
# Examples:
#   bin/run                     Run with current config
#   bin/run --log-level=debug   Run with debug logging
#
# In dev instances (no systemd service), --dev=all is added automatically
# so auto-reload is enabled.  Pass --dev=all explicitly to override in prod.
#
# Run as the instance user.  Press Ctrl-C to stop.

set -euo pipefail

BUILD_DIR="$(cd "$(dirname "$0")/.." && pwd)"
INSTANCE="$(basename "$(dirname "$BUILD_DIR")")"
PYTHON="${BUILD_DIR}/.venv/bin/python"
ODOO_BIN="${BUILD_DIR}/parts/odoo/odoo-bin"
CONFIG="${BUILD_DIR}/etc/odoo.conf"

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

cd "$BUILD_DIR"

# Auto-enable dev mode (auto-reload) in dev instances.
args=()
if [[ "$IS_DEV" -eq 1 ]]; then
    args+=(--dev=all)
fi

exec "${PYTHON}" "${ODOO_BIN}" --config "${CONFIG}" "${args[@]}" "$@"
