#!/bin/bash

changeHostName() {
	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

	echo $HN $1
	# 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\([[:blank:]]\+\)"/\\1$1\\2/g > /tmp/hosts

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

	cp -f /etc/hosts /etc/hosts.bak
	cp -f /etc/sysconfig/network /etc/sysconfig/network.bak

	mv -f /tmp/hosts /etc/hosts
	mv -f /tmp/network /etc/sysconfig/network

	hostname $1
}

changeDomainName() {
	echo "`date`" >> /var/log/LAN.log
	IP=`hostname -i`
	if [ $? -eq 0 ]; then
	echo "Info: Domain Name Update is in progress... New DomainName=$1" >> /var/log/LAN.log
	DN=`dnsdomainname`
	DN="`echo $DN | awk -F' ' '{print $1}'`"

	# change /etc/hosts
	if [ "$DN" == "" ]; 
	then
		HN=`hostname`
		sed s/"$HN\."/$HN.$1/g /etc/hosts > /tmp/hosts
	else
		sed s/$DN/$1/g /etc/hosts > /tmp/hosts
	fi

	cp -f /etc/hosts /etc/hosts.bak
	mv -f /tmp/hosts /etc/hosts
	else
		echo "Error: Domain Name Update. Error Detail: There is no IP Address" >> /var/log/LAN.log
	fi
}

changeIPAddress() {
	IP=`hostname -i`
	# if error in getting the hostname, do nothing
	if [ $? -eq 0 ]; then
		IP="`echo $IP | awk -F' ' '{print $1}'`"

		# update /etc/hosts
		sed s/$IP/$1/g /etc/hosts > /tmp/hosts

		cp -f /etc/hosts /etc/hosts.bak
		mv -f /tmp/hosts /etc/hosts
	fi
}

changeRouterAddress() {
        # update /etc/hosts
	sed s/"[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\([[:blank:]]*router\)"/$1\\1/g < /etc/hosts > /tmp/hosts

        cp -f /etc/hosts /etc/hosts.bak
        mv -f /tmp/hosts /etc/hosts
}

removeIPAddress() {
        # update /etc/hosts
        sed s/"^$1[A-z \t.]*"//g < /etc/hosts

        cp -f /etc/hosts /etc/hosts.bak
        mv -f /tmp/hosts /etc/hosts
}

addIPAddress() {
        # update /etc/hosts
        echo $* >> /etc/hosts
}

changeIPAddress_TUX() {
		# update /etc/hosts
		sed s/$1/$2/g /etc/hosts > /tmp/hosts

		cp -f /etc/hosts /etc/hosts.bak
		mv -f /tmp/hosts /etc/hosts
}

changeIPAddress_TUX_Eth0() {
	echo "hosts file changes starts at - `date`" >> /var/log/LAN.log
	IP=`hostname -i`
	# if error in getting the hostname, do nothing
	if [ ! "$1" = "" ]; then
	if [ ! "$IP" == "" ]; then
		echo "Info: hostname -i is success...IPAddress in hosts file=$IP New IPAddress=$1" >> /var/log/LAN.log
		IP="`echo $IP | awk -F' ' '{print $1}'`"
		# update /etc/hosts
		sed s/$IP/$1/g /etc/hosts > /tmp/hosts

		cp -f /etc/hosts /etc/hosts.bak
		mv -f /tmp/hosts /etc/hosts
	else
		echo "Error: hostname -i is failed... Blank IP Address found in hosts file" >> /var/log/LAN.log
		HOSTNAME1=`grep -w HOSTNAME /etc/sysconfig/network | cut -f2 -d"="`
		IP=`grep -w $HOSTNAME1 /etc/hosts`
		echo "Info: ETH0 entry in hosts file...$IP" >> /var/log/LAN.log
		HOSTNAME2=`grep -w $HOSTNAME1 /etc/hosts | cut -f1 -d"."`
		FULL_NAME=""
		if [ "$HOSTNAME2" == "	$HOSTNAME1" ]; then
			FULL_NAME="$1$IP"
			echo "Info: This is a tab..." >> /var/log/LAN.log
		else
			FULL_NAME="$1	$IP"
			echo "Info: This not a tab...." >> /var/log/LAN.log
			echo $FULL_NAME
		fi
			IP1=`grep -w $HOSTNAME1 /etc/hosts | cut -f1 -d"	"`
			echo "Adding the New IPAddress..." >> /var/log/LAN.log
			sed s/"$IP"/"$FULL_NAME"/g /etc/hosts > /tmp/hosts
			cp -f /etc/hosts /etc/hosts.bak
			mv -f /tmp/hosts /etc/hosts
			echo "Info: hosts file has been updated... New IPAddress=$1" >> /var/log/LAN.log
	fi
	else
		echo "Error: Argument passed to this file is void...." >> /var/log/LAN.log
	fi
	echo "hosts file changes ends at - `date`" >> /var/log/LAN.log
}

case "$1" in
  hostname)
        changeHostName $2
        ;;
  domainname)
        changeDomainName $2
        ;;
  ipaddress)
        changeIPAddress $2
        ;;
  routeraddress)
        changeRouterAddress $2
        ;;
  removeip)
        removeIPAddress $2
        ;;
  addip)
        addIPAddress $2
        ;;
  ipaddress_TUX)
        changeIPAddress_TUX $2 $3
        ;;
  ipaddress_TUX_Eth0)
        changeIPAddress_TUX_Eth0 $2
        ;;
  *)
        echo $"Usage: $0 {hostname|domainname|ipaddress|removeip|ipaddress_TUX|ipaddress_TUX_Eth0|addip <value>}"
        exit 1
esac


