#!/usr/bin/env bash

SCRIPT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"


init_rabbitmq() {
  RABBITMQ_CONFIG_DIR=${TEST_CONFIG_DIR}
  RABBITMQ_DOCKER_IMAGE=${RABBITMQ_DOCKER_IMAGE:-rabbitmq}

  print "> RABBITMQ_CONFIG_DIR: ${RABBITMQ_CONFIG_DIR}"
  print "> RABBITMQ_DOCKER_IMAGE: ${RABBITMQ_DOCKER_IMAGE}"
  [[ -z "${OAUTH_SERVER_CONFIG_BASEDIR}" ]] || print "> OAUTH_SERVER_CONFIG_BASEDIR: ${OAUTH_SERVER_CONFIG_BASEDIR}"
  [[ -z "${OAUTH_SERVER_CONFIG_DIR}" ]] || print "> OAUTH_SERVER_CONFIG_DIR: ${OAUTH_SERVER_CONFIG_DIR}"

  if [[ ! -d "${RABBITMQ_CONFIG_DIR}/certs" ]]; then 
    mkdir ${RABBITMQ_CONFIG_DIR}/certs
  fi 
  generate-ca-server-client-kpi rabbitmq $RABBITMQ_CONFIG_DIR/certs
  generate-server-keystore-if-required rabbitmq $RABBITMQ_CONFIG_DIR/certs
  generate-client-keystore-if-required rabbitmq $RABBITMQ_CONFIG_DIR/certs
  generate-truststore-if-required rabbitmq $RABBITMQ_CONFIG_DIR/certs
}

start_rabbitmq() {
  if [[ "$PROFILES" == *"docker"* ]]; then
      if [[ "$PROFILES" == *"cluster"* ]]; then
          start_docker_cluster_rabbitmq
      else
          start_docker_rabbitmq
      fi
  else
    start_local_rabbitmq
  fi
}
stop_rabbitmq() {
  if [[ "$PROFILES" == *"docker"* ]]; then
      if [[ "$PROFILES" == *"cluster"* ]]; then
          docker compose -f $CONF_DIR/rabbitmq/compose.yml kill
      else
          kill_container_if_exist "$component"
      fi
  else
      stop_local_rabbitmq
  fi
}
stop_local_rabbitmq() {
    RABBITMQ_SERVER_ROOT=$(realpath ../)
    gmake --directory=${RABBITMQ_SERVER_ROOT} stop-node
}
save_logs_rabbitmq() {
    if [[ "$PROFILES" == *"docker"* ]]; then
        if [[ "$PROFILES" == *"cluster"* ]]; then
            docker compose -f $CONF_DIR/rabbitmq/compose.yml logs > $LOGS/rabbitmq.log
        else
            save_container_logs "rabbitmq"
        fi
    fi
}
start_local_rabbitmq() {
  begin "Starting rabbitmq ..."

  init_rabbitmq

  RABBITMQ_SERVER_ROOT=$(realpath ../)
  
  MOUNT_RABBITMQ_CONF="/etc/rabbitmq/rabbitmq.conf"
  MOUNT_ADVANCED_CONFIG="/etc/rabbitmq/advanced.config"

  RABBITMQ_TEST_DIR="${RABBITMQ_CONFIG_DIR}" ${BIN_DIR}/gen-rabbitmq-conf ${RABBITMQ_CONFIG_DIR} $ENV_FILE /tmp$MOUNT_RABBITMQ_CONF

  print "> EFFECTIVE RABBITMQ_CONFIG_FILE: /tmp$MOUNT_RABBITMQ_CONF"
  ${BIN_DIR}/gen-advanced-config ${RABBITMQ_CONFIG_DIR} $ENV_FILE /tmp$MOUNT_ADVANCED_CONFIG
  cp ${RABBITMQ_CONFIG_DIR}/enabled_plugins /tmp/etc/rabbitmq/
  RESULT=$?
  cp ${RABBITMQ_CONFIG_DIR}/enabled_plugins /tmp/etc/rabbitmq/
  if [ $RESULT -eq 0 ]; then
    print "> EFFECTIVE RABBITMQ_CONFIG_FILE: /tmp$MOUNT_ADVANCED_CONFIG"
  	gmake --directory=${RABBITMQ_SERVER_ROOT} run-broker \
  		RABBITMQ_ENABLED_PLUGINS_FILE=/tmp/etc/rabbitmq/enabled_plugins \
  		RABBITMQ_CONFIG_FILE=/tmp$MOUNT_RABBITMQ_CONF \
      RABBITMQ_ADVANCED_CONFIG_FILE=/tmp$MOUNT_ADVANCED_CONFIG
  else
    gmake --directory=${RABBITMQ_SERVER_ROOT} run-broker \
  		RABBITMQ_ENABLED_PLUGINS_FILE=/tmp/etc/rabbitmq/enabled_plugins \
  		RABBITMQ_CONFIG_FILE=/tmp$MOUNT_RABBITMQ_CONF
  fi
  print "> RABBITMQ_TEST_DIR: ${RABBITMQ_CONFIG_DIR}"


}
start_docker_cluster_rabbitmq() {
    begin "Starting rabbitmq cluster in docker ..."
    init_rabbitmq
    kill_container_if_exist rabbitmq
    kill_container_if_exist rabbitmq1
    kill_container_if_exist rabbitmq2

    mkdir -p $CONF_DIR/rabbitmq

    RABBITMQ_TEST_DIR="/var/rabbitmq" ${BIN_DIR}/gen-rabbitmq-conf ${RABBITMQ_CONFIG_DIR} $ENV_FILE $CONF_DIR/rabbitmq/rabbitmq.conf
    print "> EFFECTIVE RABBITMQ_CONFIG_FILE: $CONF_DIR/rabbitmq/rabbitmq.conf"
    ${BIN_DIR}/gen-advanced-config ${RABBITMQ_CONFIG_DIR} $ENV_FILE $CONF_DIR/rabbitmq/advanced.config
    RESULT=$?
    if [ $RESULT -eq 0 ]; then
        if [ -s $RESULT ]; then
            print "> EFFECTIVE ADVANCED_CONFIG_FILE: $CONF_DIR/rabbitmq/advanced.config"
        else
            rm $CONF_DIR/rabbitmq/advanced.config
        fi
    fi
    if [ -f ${RABBITMQ_CONFIG_DIR}/logging.conf ]; then
        mkdir -p $CONF_DIR/rabbitmq/conf.d/
        cp ${RABBITMQ_CONFIG_DIR}/logging.conf $CONF_DIR/rabbitmq/conf.d/
    fi
    if [ -f ${RABBITMQ_CONFIG_DIR}/enabled_plugins ]; then
        cp ${RABBITMQ_CONFIG_DIR}/enabled_plugins $CONF_DIR/rabbitmq
    fi
    if [ -d ${RABBITMQ_CONFIG_DIR}/certs ]; then
        cp -r ${RABBITMQ_CONFIG_DIR}/certs $CONF_DIR/rabbitmq
    fi
    if [ -d ${RABBITMQ_CONFIG_DIR}/imports ]; then
        cp -r ${RABBITMQ_CONFIG_DIR}/imports $CONF_DIR/rabbitmq
    fi

    cat > $CONF_DIR/rabbitmq/image_compose.yml <<EOF
x-rabbitmq_image: &rabbitmq_image
    image: ${RABBITMQ_DOCKER_IMAGE}
    volumes:
    - $CONF_DIR/rabbitmq/:/etc/rabbitmq
    - $CONF_DIR/rabbitmq/:/var/rabbitmq
    - ${TEST_DIR}:/config
EOF
    cat $CONF_DIR/rabbitmq/image_compose.yml > $CONF_DIR/rabbitmq/compose.yml
    cat ${BIN_DIR}/components/../rabbit-compose.yml >> $CONF_DIR/rabbitmq/compose.yml

    docker compose -f $CONF_DIR/rabbitmq/compose.yml up -d

    wait_for_message rabbitmq "Server startup complete"
    wait_for_message rabbitmq1 "Server startup complete"
    wait_for_message rabbitmq2 "Server startup complete"
    end "RabbitMQ cluster ready"
}

start_docker_rabbitmq() {
  begin "Starting rabbitmq in docker ..."

  init_rabbitmq
  kill_container_if_exist rabbitmq

  mkdir -p $CONF_DIR/rabbitmq

  RABBITMQ_TEST_DIR="/var/rabbitmq" ${BIN_DIR}/gen-rabbitmq-conf ${RABBITMQ_CONFIG_DIR} $ENV_FILE $CONF_DIR/rabbitmq/rabbitmq.conf
  print "> EFFECTIVE RABBITMQ_CONFIG_FILE: $CONF_DIR/rabbitmq/rabbitmq.conf"
  ${BIN_DIR}/gen-advanced-config ${RABBITMQ_CONFIG_DIR} $ENV_FILE $CONF_DIR/rabbitmq/advanced.config
  RESULT=$?
  if [ $RESULT -eq 0 ]; then
      if [ -s $RESULT ]; then
          print "> EFFECTIVE ADVANCED_CONFIG_FILE: $CONF_DIR/rabbitmq/advanced.config"
      else
          rm $CONF_DIR/rabbitmq/advanced.config
      fi
  fi
  if [ -f ${RABBITMQ_CONFIG_DIR}/logging.conf ]; then
      mkdir -p $CONF_DIR/rabbitmq/conf.d/
      cp ${RABBITMQ_CONFIG_DIR}/logging.conf $CONF_DIR/rabbitmq/conf.d/
  fi
  if [ -f ${RABBITMQ_CONFIG_DIR}/enabled_plugins ]; then
      cp ${RABBITMQ_CONFIG_DIR}/enabled_plugins $CONF_DIR/rabbitmq
  fi
  if [ -d "${RABBITMQ_CONFIG_DIR}/certs" ]; then      
      cp -r ${RABBITMQ_CONFIG_DIR}/certs $CONF_DIR/rabbitmq
  fi
  if [ -d ${RABBITMQ_CONFIG_DIR}/imports ]; then
      cp -r ${RABBITMQ_CONFIG_DIR}/imports $CONF_DIR/rabbitmq
  fi

  print "> RABBITMQ_TEST_DIR: /var/rabbitmq"

  docker run \
    --detach \
    --name rabbitmq \
    --net ${DOCKER_NETWORK} \
    -p 5672:5672 \
    -p 5671:5671 \
    -p 15672:15672 \
    -p 15671:15671 \
    -v $CONF_DIR/rabbitmq/:/etc/rabbitmq \
    -v $CONF_DIR/rabbitmq/imports:/var/rabbitmq/imports \
    -v ${TEST_DIR}:/config \
    ${RABBITMQ_DOCKER_IMAGE}
    
  wait_for_message rabbitmq "Server startup complete"
  end "RabbitMQ ready"
}
