#!/bin/bash

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


##########################
function ymc_script_help
{
  echo ""
  echo "Get contact-information in Fliwi"
  echo ""
  echo "Usage: $(basename $0) [OPTIONS] admininistrator"
  echo "====="
  echo ""
  echo "OPTIONS:"
  echo "========"
  echo "--show-[name|full-name|default-email|contact-email|gpg-fingerprints]"
  echo "  Only output a specific type of information. If not given, all available"
  echo "  will be shown."
  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="admins"
dns_zone="contacts"
show_all=1
show_name=0
show_full_name=0
show_default_email=0
show_contact_email=0
show_alert_email=0
show_gpg_fingerprints=0


TEMP=$(getopt -o h --long help,nameserver:,show-name,show-full-name,show-default-email,show-contact-email,show-alert-email,show-gpg-fingerprints \
              -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
    ;;

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

    --show-full-name)
      show_full_name=1
      show_all=0
      shift 1
    ;;

    --show-default-email)
      show_default_email=1
      show_all=0
      shift 1
    ;;

    --show-contact-email)
      show_contact_email=1
      show_all=0
      shift 1
    ;;

    --show-alert-email)
      show_alert_email=1
      show_all=0
      shift 1
    ;;

    --show-gpg-fingerprints)
      show_gpg_fingerprints=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 1 ]; then
  ymc_script_help
  exit 1
fi

found_contacts=$(ymc_get_hostname_from_dns_txt "$dns_part" "$dns_zone")
if [ $? -ne 0 ] || \
   [ "$found_contacts" = '' ] || \
   [ $(ymc_contains "$what_to_lookup_for" "$found_contacts") -ne 1 ]; then
  exit 1
fi


output=""

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

if [ $show_all -eq 1 ] || \
   [ $show_full_name -eq 1 ]; then
  output=$output' '$(ymc_get_hostname_from_dns_txt "full.name" $what_to_lookup_for.$dns_part.$dns_zone) || exit 1
fi

if [ $show_all -eq 1 ] || \
   [ $show_default_email -eq 1 ]; then
  output=$output' '$(ymc_get_hostname_from_dns_txt "default.email" $what_to_lookup_for.$dns_part.$dns_zone) || exit 1
fi

if [ $show_all -eq 1 ] || \
   [ $show_contact_email -eq 1 ]; then
  output=$output' '$(ymc_get_hostname_from_dns_txt "contact.email" $what_to_lookup_for.$dns_part.$dns_zone) || exit 1
fi

if [ $show_all -eq 1 ] || \
   [ $show_alert_email -eq 1 ]; then
  output=$output' '$(ymc_get_hostname_from_dns_txt "alert.email" $what_to_lookup_for.$dns_part.$dns_zone) || exit 1
fi

if [ $show_all -eq 1 ] || \
   [ $show_gpg_fingerprints -eq 1 ]; then
  output=$output' '$(ymc_get_hostname_from_dns_txt "fingerprints.gpg" $what_to_lookup_for.$dns_part.$dns_zone) || exit 1
fi

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