#!/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)
#
# Stop the Odoo instance.
#
# Usage: bin/stop
#
# In production: stops the systemd service (requires sudo).
# In dev: no service to stop; reminds the user to Ctrl-C the foreground process.

set -euo pipefail

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

info() { echo "[INFO]  $*"; }

# 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
    info "Dev instance has no service to stop."
    info "Press Ctrl-C in the terminal running bin/run."
    exit 0
fi

info "Stopping ${SERVICE}..."
sudo systemctl stop "$SERVICE"
info "${SERVICE} stopped."
