#!/bin/bash

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


##########################
function ymc_script_help
{
  echo ""
  echo "Get information about additional repositories in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS]"
  echo "====="
  echo ""
  echo "OPTIONS:"
  echo "========"
  echo "--show-[name|address|distribution|components]"
  echo "  Only output a specific type of information. If not given, all available"
  echo "  will be shown."
  echo ""
  echo "--limit=REPOSITORY_NAME"
  echo "  Limit to a specific repository."
  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 ""
}


dns_part="repositories.basics"
show_all=1
show_name=0
show_address=0
show_distribution=0
show_components=0

TEMP=$(getopt -o h --long help,nameserver:,limit:,show-name,show-address,show-distribution,show-components \
              -n "$(basename $0)" -q -- "$@")
if [ $? != 0 ]; then
  ymc_script_help
  exit 1
fi

eval set -- "$TEMP"

while true
do
  case "$1" in
    -h|--help)
      ymc_script_help
      exit 1
    ;;

    --nameserver)
      DEBUG_DNS=$2
      shift 2
    ;;

    --limit)
      limit_string=$2
      shift 2
    ;;

    --show-name)
      show_name=1
      show_all=0
      shift 1
    ;;

    --show-address)
      show_address=1
      show_all=0
      shift 1
    ;;

    --show-distribution)
      show_distribution=1
      show_all=0
      shift 1
    ;;

    --show-components)
      show_components=1
      show_all=0
      shift 1
    ;;

    --)
      shift
      break
    ;;

    *)
      ymc_script_help
      exit 1
    ;;
  esac
done

what_to_lookup_for=$*
if [ "$what_to_lookup_for" != "" ] || \
   [ $(echo $what_to_lookup_for | wc -w) -ne 0 ]; then
  ymc_script_help
  exit 1
fi

repository_names_to_print_details=""

found_repository_names=$(ymc_get_hostname_from_dns_txt "repositories" "basics")
if [ $? -eq 0 ] && \
   [ "$found_repository_names" != '' ]; then
  for repository_name in $found_repository_names
  do
    if [ "$repository_name" != '' ]; then
      if [ "$limit_string" != '' ]; then
        if [ "$limit_string" != "$repository_name" ]; then
          continue
        fi
      fi
      repository_names_to_print_details=$repository_names_to_print_details' '$repository_name
    fi
  done
else
  exit 1
fi

if [ "$repository_names_to_print_details" != "" ]; then
  for repository_name in $repository_names_to_print_details
  do
    output=""

    if [ $show_all -eq 1 ] || \
       [ $show_name -eq 1 ]; then
      output=$output' '$repository_name
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_address -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "address" $repository_name.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_distribution -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "distribution" $repository_name.$dns_part) || continue
    fi

    if [ $show_all -eq 1 ] || \
       [ $show_components -eq 1 ]; then
      output=$output' '$(ymc_get_hostname_from_dns_txt "components" $repository_name.$dns_part) || continue
    fi

    echo $(echo -n $output | sed -r 's/[[:space:]]+/ /')
  done
  exit 0
else
  exit 1
fi
