#!/bin/bash
# Copyright 2022 The Wazo Authors  (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

set -e
set -u  # fail if variable is undefined
set -o pipefail  # fail if command before pipe fails

SENTINEL="/var/lib/wazo-upgrade/remove-rabbitmq-consul-config"

[ -e "${SENTINEL}" ] && exit 0

RABBITMQ_CONF='/etc/rabbitmq/rabbitmq.conf'
# If RabbitMQ config was customized, do nothing
# The sha256sum comes from the file generated by the Ansible installer until Wazo 22.17
# File content:
# cluster_formation.peer_discovery_backend = rabbit_peer_discovery_consul
#
# cluster_formation.consul.host = localhost
# cluster_formation.consul.port = 8500
# cluster_formation.consul.scheme = http
if [ -f "${RABBITMQ_CONF}" ] && [ "$(sha256sum ${RABBITMQ_CONF} | awk '{print $1}')" != \
     "3cf1008ac2b93e08896c863a41509e75536d339dd4d6b5a743c1a31b62e4cf59" ] ; then
    exit 0
fi

rm -f ${RABBITMQ_CONF}
systemctl restart rabbitmq-server

touch "${SENTINEL}"
