#!/bin/sh -e
#
# Author: Petter Reinholdtsen <pere@td.org.uit.no>
# Date:   2002-08-06
#
# Set current hostname to match the IP address on eth0.  This is
# useful when getting the IP address from DHCP.

PATH="/sbin:$PATH"

INTERFACE=eth0

# Extract current IP
IP=`ifconfig $INTERFACE 2>&1 |grep 'inet addr:'|tr a-zA-Z: " "|awk '{print $1}'`

if [ "$IP" ] ; then

    DNSNAME=`host $IP | grep Name: | awk '{ print $2 }'`

    if [ "$DNSNAME" ] ; then

	if hostname $DNSNAME 2>/dev/null ; then
	    echo "info: Setting hostname to DNS name $DNSNAME [$IP]."
	    echo $DNSNAME > /etc/hostname
	else
	    echo "error: Unable to set hostname to $DNSNAME."
	fi
    else
	echo "error: Unable to look up $IP using DNS."
    fi
else
    echo "error: Unable to determine current IP address."
fi
