#!/bin/bash
#**********************************************
# default variables
#**********************************************
HOSTNAME="defaultname"
SERVER="fnpcd.fnal.gov"
DIR="/linux/Workgroups/Farms"

#**********************************************
# print out the help for config
#**********************************************

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 (fnpcd.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. "
	echo "	  default (/linux/Workgroups/Farms)"
	echo " "
}

#**********************************************
# the actual installation
#**********************************************
doinstall(){
	runprescripts
	installrpms
	installfiles
	runpostscripts
}


#**********************************************
# run pre install scripts for the machine
#**********************************************
runprescripts(){
	echo "******************************"
	echo "Running PreInstall Scripts"
	echo "******************************"
	if ! [ -d /root/bin/scripts ] ; then
		mkdir /root/bin/scripts
	fi
	cd /root/bin/scripts
	/usr/bin/ncftpget ftp://$SERVER$DIR/nodes/$HOSTNAME/prescriptslist
	cat prescriptslist | {
		while read line
		do
			/usr/bin/ncftpget ftp://$SERVER$DIR/nodes/$HOSTNAME/$line
			chmod 744 $line
			./$line $HOSTNAME
		done
	}
}

#**********************************************
# run post install scripts for the machine
#**********************************************
runpostscripts(){
	echo "******************************"
	echo "Running PostInstall Scripts"
	echo "******************************"
	if ! [ -d /root/bin/scripts ] ; then
		mkdir /root/bin/scripts
	fi
	cd /root/bin/scripts
	/usr/bin/ncftpget ftp://$SERVER$DIR/nodes/$HOSTNAME/postscriptslist
	cat postscriptslist | {
		while read line
		do
			/usr/bin/ncftpget ftp://$SERVER$DIR/nodes/$HOSTNAME/$line
			chmod 744 $line
			./$line $HOSTNAME
		done
	}
}

#**********************************************
# Install rpms relavant to this machine
#**********************************************
installrpms(){
	echo "******************************"
	echo "Installing other rpm's for this machine"
	echo "******************************"
	cd /root/bin/ 
	/usr/bin/ncftpget ftp://$SERVER$DIR/nodes/$HOSTNAME/rpmlist
	cat rpmlist | {
		while read line
		do
			rpm -Uvh ftp://$SERVER$DIR/nodes/$HOSTNAME/$line
		done
	}
}

#**********************************************
# Install files relavant to this machine
#**********************************************
installfiles(){
	echo "******************************"
	echo "Installing files for this machine"
	echo "******************************"
	if ! [ -d /root/bin/files ] ; then
		mkdir /root/bin/files
	fi
	cd /root/bin/files
	/usr/bin/ncftpget ftp://$SERVER/$DIR/nodes/$HOSTNAME/filelist
	cat filelist | {
		while read line
		do
			source="$(echo $line | cut -d' ' -f1)"
			dest="$(echo $line | cut -d' ' -f2)"
			mod="$(echo $line | cut -d' ' -f3)"
			/usr/bin/ncftpget ftp://$SERVER/$DIR/nodes/$HOSTNAME/$source
			mv $source $dest
			/bin/chmod $mod $dest
		done
	}
}

#**********************************************
# Start of the Main Program
#**********************************************

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

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

HOSTNAME="${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
doinstall
exit 0;
