#!/bin/bash

wait_for_confgend() {
    local wait_time=120
    echo -e -n "Waiting maximum $wait_time seconds for xivo-confgend container to be healthy..."

    local is_healthy="false"
    for _ in $(seq 1 $wait_time); do
        if [ "$(docker inspect -f '{{.State.Health.Status}}' xivo-confgend-1)" = "healthy" ]; then
            is_healthy="true"
            break
        else
            sleep 1
            echo -e -n "."
        fi
    done
    echo
    if [ "$is_healthy" = "false" ]; then
        echo -e "\e[31mERROR: xivo-confgend container is not healthy.\e[0m"
        exit 1
    fi
}

# xivo-dird resolves its configuration through xivo-confgen once, at startup, and
# is started before 10-run-xivo-dcomp-up replaces the containers: force a restart
# so it reloads the configuration generated by the new xivo-confgend
wait_for_confgend
systemctl restart xivo-dird
echo "xivo-dird was restarted, directory configuration was reloaded"
