#!/bin/bash
#
# getConfigRPM 1.0
# description: 
#	getConfigRPM is a program that really has only one function,  it
#	  is supposed to get the correct configuration RPM from the correct
#	  place, then run it.
#	A working network connection must be in place for this to work.  
#	The machine must also be able to do correct name lookups.
#
#**********************************************
# variables
#**********************************************
SERVER="linux.fnal.gov"
DIR="/linux/Workgroups/Farms/"
NODENAME=""

#**********************************************
# print out the help for getConfigRPM
#**********************************************

printHelp(){
	echo " "
	echo "getConfigRPM [-h] <node name> [-s <server>] [-d <dir>]"
	echo " "
	echo " -h Help"
	echo "	  Display this help message"
	echo " -s Server"
	echo "	  What machine is the rpm on"
	echo "	  default (linux.fnal.gov)"
	echo " -d directory"
	echo "	  Where your ICABOD directory structure starts"
	echo "	    below this directory there is usually more directories"
	echo "	    such a nodes, rpms, srpms.  The actual config rpms is"
	echo "	    at <dir>/nodes/<nodename>/config.rpm"
	echo "	  default (/linux/Workgroups/Farms)"
	echo " "
}

#**********************************************
# get and run the config file
#**********************************************

getNrun(){
	ip=`nslookup ${NODENAME} |tail -2|head -1|awk -F: '{print $2}'|tr -d " "`
	rpm -Uvh ftp://${SERVER}${DIR}/nodes/${NODENAME}/config.rpm
	if [ $? -ne "0" ]; then
		echo "You had a problem installing ${NODENAME}'s config.rpm"
	fi
	
	/root/bin/config $NODENAME $ip $SERVER $DIR
	
	if [ $? -ne "0" ]; then
		echo "config $NODENAME $ip $SERVER $DIR did not finished idealy"
	fi
}

#**********************************************
# We always hit here
#**********************************************

if [ $# = 0 ] ; then
	printHelp 
	exit 0;
fi	

if [ "${1}" = "-h" ] ; then
	printHelp 
	exit 0;
fi

NODENAME="${1}"
shift 1
while test $# != 0
do
	case $1 in
		-s) test $# -lt 2 && { printHelp ; exit 1 ; }
			SERVER="$2" ;shift 2 ;;
		-d)  test $# -lt 2 && { printHelp ; exit 1 ; }
			DIR="$2" ;shift 2 ;;
		-h | -help | --help | --h) printHelp ; exit 0 ;;
		*) printHelp ; exit 1;
	esac
done

getNrun
exit 0;
