#!/bin/sh
###############################################################################
#
#          Dell Inc. PROPRIETARY INFORMATION
# This software is supplied under the terms of a license agreement or
# nondisclosure agreement with Dell Inc. and may not
# be copied or disclosed except in accordance with the terms of that
# agreement.
#
# Copyright (c) 2010 Dell Inc. All Rights Reserved.
#
# Module Name: linux_hostnameupdates.sh
# Module Version: 1.0.0.1
# Environment: Linux
#
#
# Abstract/Purpose:
#
#   This script will perform the following tasks on RHEL 6.x and SLES 11 only:
#       Run "chkconfig NetworkManager off"
#       Append the following to the /etc/hosts file 
#           "<IP Address> <hostname>"
#       (IP Address above is the first valid IPv4 address from "ifconfig"
#       output.)
###############################################################################

if [ -e /etc/redhat-release ]; then
    LINUX_DISTRO=rhel
    LINUX_REV=$(cat /etc/redhat-release | sed 's/.*release //' | sed 's/ .*//')
    LINUX_REV_MAJOR=$(echo $LINUX_REV | sed 's/[\.][0-9]*//')
    if [ $LINUX_REV_MAJOR -lt 6 ]; then
        exit 0;
    fi
    chkconfig NetworkManager off
    IPADDR_TO_ADD=
    IPADDR_FOUND=0
    for LINE in $(ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | awk '{ print $2 }'); do
	#	echo IP addr line: $LINE
        IPADDR=`echo "$LINE" | sed "s/addr://"`
	#	echo IP addr found: $IPADDR
	grep "$IPADDR" /etc/hosts | grep `hostname` 1>/dev/null 2>/dev/null
	if [ $? -eq 0 ]; then
	#           echo "IP Addr found in /etc/hosts file."
            IPADDR_FOUND=1
            exit 0;
        else
            if [ "X" == "X$IPADDR_TO_ADD" ]; then
                IPADDR_TO_ADD=$IPADDR
            fi
	fi
   done
   if [ $IPADDR_FOUND -eq 0 ]; then
       if [ "X" != "X$IPADDR_TO_ADD" ]; then
           echo "$IPADDR_TO_ADD" `hostname` >> /etc/hosts 2> /dev/null
       fi
   fi
elif [ -e /etc/SuSE-release ]; then 
    LINUX_DISTRO=sles
    LINUX_KERNEL_REV=$(uname -r)
    LINUX_ARCH=$(uname -i)
    LINUX_REV_MAJOR=$(cat /etc/SuSE-release | tr '\n' ' ' | sed 's/.*VERSION = //' | sed 's/ .*//')
	LINUX_REV_MINOR=$(cat /etc/SuSE-release | tr '\n' ' ' | sed 's/.*PATCHLEVEL = //' | sed 's/ .*//')
    LINUX_REV=${LINUX_REV_MAJOR}.${LINUX_REV_MINOR}
	if [ "${LINUX_REV_MAJOR}" != "11" ]; then
        exit 0;
    fi
    # chkconfig NetworkManager off
    IPADDR_TO_ADD=
    IPADDR_FOUND=0
    for LINE in $(ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | awk '{ print $2 }'); do
	#	echo IP addr line: $LINE
        IPADDR=`echo "$LINE" | sed "s/addr://"`
	#	echo IP addr found: $IPADDR
	grep "$IPADDR" /etc/hosts | grep `hostname` 1>/dev/null 2>/dev/null
	if [ $? -eq 0 ]; then
	#           echo "IP Addr found in /etc/hosts file."
            IPADDR_FOUND=1
            exit 0;
        else
            if [ "X" == "X$IPADDR_TO_ADD" ]; then
                IPADDR_TO_ADD=$IPADDR
            fi
	fi
   done
   if [ $IPADDR_FOUND -eq 0 ]; then
       if [ "X" != "X$IPADDR_TO_ADD" ]; then
           echo "$IPADDR_TO_ADD" `hostname` >> /etc/hosts 2> /dev/null
       fi
   fi
fi
exit 0;
