#!/bin/bash

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


##########################
function ymc_script_help
{
  echo ""
  echo "Get the ssh-port for a specific service 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="ssh-port.services"

found_ports=$(ymc_get_hostname_from_dns_txt "$service" "$dns_part")
if [ $? -eq 0 ] && [ "$found_ports" != '' ]; then
  for port in $found_ports
  do
    if [ "$port" != '' ]; then
      ports_found=$(expr $ports_found + 1)
      ports_to_output=$ports_to_output' '$port
    fi
  done
else
  exit 1
fi

if [ $ports_found -eq 1 ]; then
  for port_to_output in $ports_to_output
  do
    echo $port_to_output
  done
  exit 0
else
  exit 1
fi
