#!/bin/bash

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


##########################
function ymc_script_help
{
  echo ""
  echo "Get virtual IPs in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS] service"
  echo "====="
  echo ""
  echo "DEBUGGING OPTIONS:"
  echo "=================="
  echo "NOTE: The following 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')

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

  service=$(eval echo \$$#)
  if [ "$service" == "$0" ]; then
    help=1;
  fi

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




ymc_initialise_script $*



vips_found=0;
vips_to_output=''

dns_part="vips"

found_vips=$(ymc_get_hostname_from_dns_txt "$service" "$dns_part")
if [ $? -eq 0 ] && [ "$found_vips" != '' ]; then
  for vip in $found_vips
  do
    if [ "$vip" != '' ]; then
      vips_found=$(expr $vips_found + 1)
      vips_to_output=$vips_to_output' '$vip
    fi
  done
else
  exit 1
fi

if [ $vips_found -gt 0 ]; then
  for vip_to_output in $vips_to_output
  do
    echo $vip_to_output
  done
  exit 0
else
  exit 1
fi
