#!/bin/bash
#
#==============================================================
#
#  Abs:Makes emergency MAC address changes to DHCP servers
#
#	Creates a file that contains
#		1) who
#		2) why
#		3) Host
#		4) New MAC address
#
# This file is "monitored" by a process on mccsrv01 and when
# there is a file it is processed and makes a change to the
# DHCP servers
#
#  Name: chg-dhcp
#
#
# Created: 29-Oct-2012; Brobeck
#
#--------------------------------------------------------------
# MODS:
#
# 3-Nov-2015, Brobeck
#   Added eoicdesk as user - Removed softegr
#
#--------------------------------------------------------------
#
if [ "$USER" != "eoicdesk" ]
  then
    printf "\n\n\tMust be run as eoicdesk\n\n"
    sleep 2
    exit
fi
 
########################################
#Make the backspace key work properly
########################################
stty erase ^H
#
DT=`date +%b-%d-%Y-%H:%M:%S`
## Color
Escape="\033";
Reset="${Escape}[0m";
RedF="${Escape}[31m";
BlueF="${Escape}[34m";
GreenF="${Escape}[32DHCPTMm";
## End Of Color

DHCPCHGFILE=/u1/lcls/sys/dhcp/dhcp-changes
DHCPTMP=/u1/lcls/sys/dhcp/dhcp-tmp
DHCPRESULTS=/u1/lcls/sys/dhcp/dhcp-results
DHCPRESULTSREAD=/u1/lcls/sys/dhcp/dhcp-results-$DT
cnt=0

#############################################
# Remove the old DHCPRESULTS file in case
# someone control-c'd out of this script
#############################################
if [ -f $DHCPRESULTS ]; then rm -f $DHCPRESULTS; fi

#####################################################################
# Once the DHCPCHGFILE is created the monitoring script will rename the
# file - so we need to give the monitoring file a chance to read the
# script -LOOP until file is gone
# -This will prevent 2 people trying to make changes at the same time
#####################################################################
while [ -f $DHCPCHGFILE ] 
do
	printf "\n\n\tWaiting for other dhcp changes to get implemented\n"
	ls -l $DHCPCHGFILE
	sleep 5
	cnt=`expr $cnt + 1`
	if [ "$cnt" -gt 7 ]
	  then
	    printf "\n\t$RedF Looks like the DHCP monitoring script is not running"
	    printf "\n\t Please contact Jingchen or Ken\n\n $Reset"
	    exit
	fi
done

#############################################
# Verify MAC address is in the proper format
#############################################
function CHK-MAC {

	CHKMACFMT=`echo $MAC | egrep "^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$" > /dev/null; echo $?`

	if [ "$CHKMACFMT" != 0 ]
	  then
		printf "\n\n\t $RedF Bad MAC format -> $MAC \n\n $Reset"
		MAC=""
	fi	
}

#####################################
# Get the basic information: WHO WHY
#####################################
function GET-INFO {
   while [ "$WHO" == "" ]
   do
     printf "\n\tWho are you? (ex: brobeck) (UNIX ID)-> "
     read WHO
     if [ "$WHO" == "physics" ] || [ "$WHO" == "softegr" ] || [ "$WHO" == "eoicdesk" ]
       then 
        printf "\n\t${RedF} NOT a group account -Your unix id $Reset \n\n"
        WHO=""
     fi
   done
   while [ "$WHY" == "" ]
   do
     WHO=`echo $WHO | awk '{print $1}'`
     printf "\n\tWhy are you making this MAC change? -> "
     read WHY
   done
}

#################
# Print the Menu
#################

printf "\n\n\t$RedF##############################################################"
printf "\n\t   - This script is only to be used in case of emergency to "
printf "\n\t     replace failed hardware in production"
printf "\n\n\t   You must know the following to use this script:"
printf "\n\t     1) Nodename of device being replaced"
printf "\n\t     2) MAC address of NEW device"
printf "\n\t################################################################$Reset\n\n"
sleep 2

printf "\n\tThis script will allow the user to change the MAC address in the DHCP file\n"
GET-INFO

while [ "$HNAME" == "" ]
do
	printf "\n\tPlease enter the hostname -> "
	read HNAME

  #####################################################
  # Verify hostname is in DNS
  #####################################################
  IP=`/usr/bin/host $HNAME | awk '{print $4}'`
  if [ "$IP" == "found:" ]
    then
	printf "\n\n\t$RedF $HNAME is not in DNS $Reset \n\n"
	printf "\tPlease check the name and try again\n\n"
	HNAME=""
  fi
done 

##############################
# Get the NEW MAC address
##############################
while [ "$MAC" == "" ]
do
  printf "\n\tEnter the new MAC address: (xx:xx:xx:xx:xx:xx):-> "
  read MAC
  CHK-MAC
done

#############################
# Create the change file
#############################
rm -f $DHCPTMP
touch $DHCPTMP
printf "$WHO\n"   >> $DHCPTMP
printf "$WHY\n"   >> $DHCPTMP
printf "$HNAME\n" >> $DHCPTMP
printf "$MAC\n\n" >> $DHCPTMP
mv $DHCPTMP $DHCPCHGFILE

#############################
# Keep the user informed
#############################
printf "\n\n\tDHCP file has been created."
printf "\n\tWaiting on DHCP server to read file"
printf "\n\tThis can take up to 30 seconds"
printf "\n\nPlease be patient..."

CNT=0
printf "\nWaiting..."
while [ ! -f $DHCPRESULTS ]
do
  sleep 2
  printf "."
  CNT=`expr $CNT + 1`
  if [ $CNT -gt 45 ]
    then
	printf "\n\nResults can't be verified\n\n"
	printf "Please contact the systems team immediately\n\n"
	exit
  fi
done

################################
# Show the results to the user
# -Save the results
################################
cat $DHCPRESULTS
mv -f $DHCPRESULTS $DHCPRESULTSREAD
exit

