#!/usr/local/bin/bash
#/ Usage: ghe-cluster-hostnames <host> <prefix>
#/
#/ Finds all nodes of the cluster using the config on <host>.
#/ If it is a 2.8 cluster the results are returned as prefix-uuid,
#/ otherwise the configured hostnames are returned.
#/
#/ Note: This script typically isn't called directly. It's invoked by the
#/ ghe-restore-* commands when restoring into a cluster.
set -e

# Bring in the backup configuration
. $( dirname "${BASH_SOURCE[0]}" )/ghe-backup-config

# Show usage and bail with no arguments
[ -z "$*" ] && print_usage

GHE_HOSTNAME="$1"
prefix="$2"

if ghe-ssh "$GHE_HOSTNAME" test -f /data/user/common/uuid; then
  node_uuids=$(ghe-ssh "$GHE_HOSTNAME" ghe-config --get-regexp cluster.*.uuid | cut -d ' ' -f 2)
  hostnames=''
  for uuid in $node_uuids; do
    hostnames+="$prefix-$uuid "
  done
else
  hostnames=$(ghe-ssh "$GHE_HOSTNAME" ghe-config --get-regexp cluster.*.hostname | cut -d ' ' -f 2)
fi

echo "$hostnames"
