#!/bin/bash

HN=`hostname`

# see if HN contains the domain name
echo $HN | grep '\.' > /dev/null 2>&1

if [ $? == 0 ]
then
        # $HN has a period in it so strip away
        # everything after and including the first period
        HN="`echo $HN | awk -F. '{print $1}'`"
fi

# change /etc/hosts
sed s/"\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*[[:blank:]]*\)$HN"/\\1$1/g < /etc/hosts | sed s/"\([[:blank:]]\+\)$HN$"/\\1$1/g | sed s/"\([[:blank:]]\+\)$HN\(ETH\+\)"/\\1$1\\2/g > /tmp/hosts

# change /etc/sysconfig/network
sed s/"\(HOSTNAME=\).*"/\\1$1/g < /etc/sysconfig/network | sed s/"\(DHCP_HOSTNAME=\).*"/\\1$1/g > /tmp/network

mv /etc/hosts /etc/hosts.bak
mv /etc/sysconfig/network /etc/sysconfig/network.bak

mv /tmp/hosts /etc/hosts
chmod 644 /etc/hosts
mv /tmp/network /etc/sysconfig/network

hostname $1
