#!/bin/bash

set -e

. ./CLUSTER-SERVER/_mongodb_cluster_common.bash

gs_mongodb_cluster_require_local_role \
    "router/orchestration" \
    "$GS_MONGODB_ROUTER_HOSTNAME"

./CLUSTER-SERVER/CHECK-MONGODB-CLUSTER-SETUP.sh

echo ""
echo "=== Starting and configuring the config server ==="
gs_mongodb_cluster_remote_run \
    "$GS_MONGODB_CONFIG_SERVER_HOSTNAME" \
    RUN-MONGODB-CONFIG-SERVER-START.sh
gs_mongodb_cluster_remote_run \
    "$GS_MONGODB_CONFIG_SERVER_HOSTNAME" \
    CONFIGURE-MONGODB-CONFIG-SERVER.sh

echo ""
echo "=== Starting and configuring the shards ==="
for shard_host in "${GS_MONGODB_SHARD_HOSTNAMES[@]}"; do
    gs_mongodb_cluster_remote_run "$shard_host" RUN-MONGODB-SHARD-START.sh
    gs_mongodb_cluster_remote_run "$shard_host" CONFIGURE-MONGODB-SHARD.sh
done

echo ""
echo "=== Starting and configuring the router ==="
gs_mongodb_cluster_remote_run \
    "$GS_MONGODB_ROUTER_HOSTNAME" \
    RUN-MONGODB-ROUTER-START.sh
gs_mongodb_cluster_remote_run \
    "$GS_MONGODB_ROUTER_HOSTNAME" \
    CONFIGURE-MONGODB-ROUTER.sh

echo ""
./CLUSTER-SERVER/RUN-MONGODB-CLUSTER-STATUS.sh
