#!/bin/sh
# hostname-fqdn --- try to guess fully-qualified hostname
# Author: Noah Friedman <friedman@splode.com>
# Created: 1991-12-26
# Public domain

# $Id: hostname-fqdn,v 1.3 1999/10/10 02:35:24 friedman Exp $

# Commentary:

# This script tries to guarantee that a full host+domain name is given for
# the current host.  Some brane-damaged machines only give the host
# nickname when calling hostname, and it's necessary to use domainname to
# get the rest of the domain.

# On linux machines, there is a `dnsdomainname' command that is more likely
# to return the host domain, rather than the NIS domain.

# Code:

hostname=`hostname`

case "$hostname" in
  *.* ) echo "$hostname" ;;
  * )
    # Try to use DNS to resolve the hostname.
    if (perl -v > /dev/null) 2> /dev/null; then
      n=`perl -e '($n) = gethostbyname ($ARGV[0]); print "$n\n";' "$hostname"`
      case "$n" in
        *.* ) echo "$n"; exit 0 ;;
      esac
    fi

    domainname="`(dnsdomainname) 2> /dev/null || domainname`"
    echo "$hostname.$domainname"
   ;;
esac

# hostname-fqdn ends here
