#!/bin/sh
# vim:sw=4:et:
##  This Source Code Form is subject to the terms of the Mozilla Public
##  License, v. 2.0. If a copy of the MPL was not distributed with this
##  file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
##  Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
##

set -e

# Get default settings with user overrides for (RABBITMQ_)<var_name>
# Non-empty defaults should be set in rabbitmq-env
SCRIPTS_DIR=$(dirname "$0")
. "$SCRIPTS_DIR/rabbitmq-env"

[ "$NOTIFY_SOCKET" ] && RUNNING_UNDER_SYSTEMD=true

for opt in "$@"; do
    if [ "$opt" = "-detached" ]; then
        detached="true"
    fi
done

RABBITMQ_DEFAULT_ALLOC_ARGS="+MBas ageffcbf +MHas ageffcbf +MBlmbcs 512 +MHlmbcs 512 +MMmcs 30"

check_start_params() {
    check_not_empty RABBITMQ_BOOT_MODULE
    check_not_empty SASL_BOOT_FILE
}

check_not_empty() {
    local name="${1:?}"
    local value
    eval value=\$$name
    if [ -z "$value" ]; then
        echo "Error: ENV variable should be defined: $1.
       Please check rabbitmq-env, rabbitmq-defaults, and ${RABBITMQ_CONF_ENV_FILE} script files"
        exit 78
    fi
}

start_rabbitmq_server() {
    set -e

    _rmq_env_set_erl_libs

    if [ -n "$RABBITMQ_MAX_OPEN_FILES" ]; then
        if ! ulimit -n ${RABBITMQ_MAX_OPEN_FILES}; then
            echo "Error: Failed to set maximum number of open files to ${RABBITMQ_MAX_OPEN_FILES}" >&2
            exit 1
        fi
    fi

    RABBITMQ_START_RABBIT=
    [ "x" = "x$RABBITMQ_ALLOW_INPUT" ] && RABBITMQ_START_RABBIT=" -noinput"
    if test -z "$RABBITMQ_NODE_ONLY"; then
        if test "$USE_RABBIT_BOOT_SCRIPT"; then
            # TODO: This is experimental and undocumented at this point.
            # It is here just to do simple checks while playing with how
            # RabbitMQ is started.
            "$SCRIPTS_DIR/rabbitmq-rel" gen-boot
            SASL_BOOT_FILE=rabbit
            test -f "$SASL_BOOT_FILE.boot"
            RABBITMQ_START_RABBIT="$RABBITMQ_START_RABBIT -init_debug"
        else
            RABBITMQ_START_RABBIT="$RABBITMQ_START_RABBIT -s $RABBITMQ_BOOT_MODULE boot"
        fi
    fi

    # We need to turn off path expansion because some of the vars,
    # notably RABBITMQ_SERVER_ERL_ARGS, contain terms that look like
    # globs and there is no other way of preventing their expansion.
    set -f

    export ERL_MAX_ETS_TABLES \
        SYS_PREFIX

    check_start_params

    exec /usr/local/bin/erl28 \
        -pa "$RABBITMQ_SERVER_CODE_PATH" \
        ${RABBITMQ_START_RABBIT} \
        -boot "${SASL_BOOT_FILE}" \
        +W w \
        ${RABBITMQ_DEFAULT_ALLOC_ARGS} \
        ${RABBITMQ_SERVER_ERL_ARGS} \
        ${RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS} \
        ${RABBITMQ_SERVER_START_ARGS} \
        -syslog logger '[]' \
        -syslog syslog_error_logger false \
        -kernel prevent_overlapping_partitions false \
        "$@"
}

stop_rabbitmq_server() {
    if test "$rabbitmq_server_pid"; then
        kill -TERM "$rabbitmq_server_pid"
        wait "$rabbitmq_server_pid"
    fi
}

# Note: the docker-library/rabbitmq#778 change exports "RUNNING_UNDER_SYSTEMD=true" to start
# the erl process using exec via the start_rabbitmq_server.
if [ "$RABBITMQ_ALLOW_INPUT" ] || [ "$RUNNING_UNDER_SYSTEMD" ] || [ "$detached" ]; then
    # Run erlang VM directly, completely replacing current shell process
    #
    # This is also the correct mode to run the broker under
    # systemd - there is no need in a proxy process that converts
    # signals to graceful shutdown command, the unit file should already
    # contain instructions for graceful shutdown. Also by removing
    # this additional process we could simply use value returned by
    # `os:getpid/0` for a systemd ready notification.
    start_rabbitmq_server "$@"
else
    # When RabbitMQ runs in the foreground but the Erlang shell is
    # disabled, we setup signal handlers to stop RabbitMQ properly. This
    # is at least useful in the case of Docker.
    # The Erlang VM should ignore SIGINT.
    RABBITMQ_SERVER_START_ARGS="${RABBITMQ_SERVER_START_ARGS} ${RABBITMQ_IGNORE_SIGINT_FLAG}"

    # Signal handlers. They all stop RabbitMQ properly, using
    # rabbitmqctl stop. This script will exit with different exit codes:
    #   SIGHUP, SIGTSTP + SIGCONT
    #     Ignored until we implement a useful behavior.
    #   SIGTERM
    #	  Exits 0 since this is considered a normal process termination.
    #   SIGINT
    #     Exits 128 + $signal_number where $signal_number is 2 for SIGINT (see
    #     https://pubs.opengroup.org/onlinepubs/009695399/utilities/kill.html).
    #     This is considered an abnormal process termination. Normally, we
    #     don't need to specify this exit code because the shell propagates it.
    #     Unfortunately, the signal handler doesn't work as expected in Dash,
    #     thus we need to explicitly restate the exit code.
    #
    # The behaviors below should remain consistent with the
    # equivalent signal handlers in the Erlang code
    # (see apps/rabbitmq_prelaunch/src/rabbit_prelaunch_sighandler.erl).
    trap '' HUP TSTP CONT
    trap "stop_rabbitmq_server; exit 0" TERM
    trap "stop_rabbitmq_server; exit 130" INT

    start_rabbitmq_server "$@" &
    export rabbitmq_server_pid="$!"

    # Block until RabbitMQ exits or a signal is caught.
    # Waits for last command (which is start_rabbitmq_server)
    # In a POSIX Bourne shell, if `wait` is interrupted by a signal, the signal
    # handlers defined above are executed and the script terminates with the
    # exit code of `wait` (unless the signal handler overrides that).
    wait "$rabbitmq_server_pid"
fi
