#!/bin/bash

### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash

##########################
function ymc_script_help
{
  echo ""
  echo "Get all services in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS]"
  echo "====="
  echo ""
  echo "OPTIONS:"
  echo "========"
  echo "--as-hostnames"
  echo "  Output hostnames instead of services, which means output all hosts."
  echo ""
  echo ""
  echo "DEBUGGING OPTIONS:"
  echo "=================="
  echo "NOTE: The following options are most likely only useful for debugging purposes."
  echo "      Try not to use them on any productive environment!"
  echo ""
  echo "--nameserver=<someIP>"
  echo "  Overrides the nameserver used in automatic detection of settings."
  echo "  Default is to use the systems default nameserver."
  echo ""
}

function ymc_initialise_script
{
  help=$(echo "$*" | grep -c '\--help')
  as_hostnames=$(echo "$*" | grep -c '\--as-hostnames')

  DEBUG_DNS=$(echo "$*" | grep -E -e '--nameserver=' | perl -pi -e 's/.*--nameserver=([^ ]+).*/$1/')

  if [ $help -eq 1 ]; then
    ymc_script_help 1>&2
    exit 1
  fi
}




ymc_initialise_script $*

if [ $as_hostnames -eq 1 ]; then
  lookup_part="hosts"
else
  lookup_part="services"
fi
found_roles=$(ymc_get_hostname_from_dns_txt "$lookup_part" "cluster")
if [ $? -eq 0 ] && [ "$found_roles" != '' ]; then
  for role in $found_roles
  do
    if [ "$role" != '' ]; then
      roles_found=$(expr $roles_found + 1)
      echo $role
    fi
  done
else
  exit 1
fi

if [ $roles_found -gt 0 ]; then
  exit 0
else
  exit 1
fi
