#!/usr/bin/env bash

if [[ $# < 1 ]] || [[ $# > 1 ]] ; then
   echo -e "
   \033[31m\033[1mNo hostname or too many parameters given.\033[m

   \033[1mUsage\033[m: $(basename $0) <hostname>
"
   exit 1
fi

hostname=$1


get_domain_by_hostname() {
   
   _domain=$1
   _tmp_domain="${_domain#*.}"

   while [[ "$_tmp_domain" =~ \. ]]; do
      _domain="$_tmp_domain"
      _tmp_domain="${_domain#*.}"
   done

   echo $_domain
}

domain="$(get_domain_by_hostname "$hostname")"

echo ""
echo "   hostname: $hostname"
echo "   domain:   $domain"
echo ""

exit 0
