#!/bin/bash

mds_replication_slot_pattern="main\\_%"

list_media_servers() {
    sudo -u postgres psql -Aqt -d asterisk -c "SELECT name FROM mediaserver WHERE name <> 'default' ORDER BY name;" 2> /dev/null
}

list_mds_replication_slots() {
    sudo -u postgres psql -Aqt -c "SELECT slot_name FROM pg_replication_slots WHERE slot_type = 'logical' AND slot_name LIKE '${mds_replication_slot_pattern}';" 2> /dev/null
}

display_detached_mds_reminder() {
    local media_servers="${1}"; shift

    echo ""
    echo "******************************************************************************"
    echo "*  IMPORTANT: the media servers replication is detached                      *"
    echo "*                                                                            *"
    echo "*  The following media servers are still running on a frozen replica:        *"
    echo "${media_servers}" | while read -r media_server; do
        printf '*    - %-70s*\n' "${media_server}"
    done
    echo "*                                                                            *"
    echo "*  Their database is NOT replicated anymore: any configuration change made   *"
    echo "*  on the XiVO Main will not reach them until they are upgraded.             *"
    echo "*                                                                            *"
    echo "*  You MUST now upgrade the XiVOCC, then run 'mds-upgrade' on each of these  *"
    echo "*  media servers. Run them all at once for the shortest maintenance          *"
    echo "*  window, or one after the other to keep the calls served by those not      *"
    echo "*  yet upgraded.                                                             *"
    echo "******************************************************************************"
    echo ""
}

display_still_replicating_mds_reminder() {
    local media_servers="${1}"; shift

    echo ""
    echo "******************************************************************************"
    echo "*  INFO: the media servers replication was kept                              *"
    echo "*                                                                            *"
    echo "*  This upgrade left the database schema unchanged, so the following media   *"
    echo "*  servers keep replicating the XiVO Main and stay up to date:               *"
    echo "${media_servers}" | while read -r media_server; do
        printf '*    - %-70s*\n' "${media_server}"
    done
    echo "*                                                                            *"
    echo "*  They still run the previous version: upgrade the XiVOCC, then run         *"
    echo "*  'mds-upgrade' on each of them at your convenience.                        *"
    echo "******************************************************************************"
    echo ""
}

media_servers=$(list_media_servers)

if [ -z "${media_servers}" ]; then
    exit 0
fi

if [ -z "$(list_mds_replication_slots)" ]; then
    display_detached_mds_reminder "${media_servers}"
else
    display_still_replicating_mds_reminder "${media_servers}"
fi
