#!/bin/sh
#VOLDEMORT 0.6.5
#Last revised, S. Timm 2/20/03
#Bug fixes, S. Timm 4/14/03
#Major bug fixes, S. Timm, 11/14/03
#last revised S. Timm 4/28/04
#pullrsync script
#Script will insert /sbin/ in its path
#Helper applications will either be in /sbin (linux RPM) 
#or in $VOLDEMORT_DIR/bin (ups/upd)
#
# Subroutines:
#
printHelp() {
            echo "pullrsync  [-h]"
            echo "           [-c <cluster>] [-f <flavor>]"
            echo "           [ -R -F -S -P ]"
            echo "           [-q -v]"
            echo "          [ -H <hostname>] "
            echo "           [ -M <module> ]"
            echo "          [ -t <target server> ] "
            echo "          [-i | -I ]"
            echo "          [ -w] "
            echo " -c  Pull for this cluster--default is cluster found in nodes.conf"
            echo " -f  Pull for this OS flavor--default is flavor found in nodes.conf"
            echo " -R  Don't pull things in the RPMS directory"
            echo " -F  Don't pull things in the files directory"
            echo " -S  Don't pull things in the scripts directory"
            echo " -P  Don't pull things in the prescripts directory"
            echo " -T  Don't pull things in the tarballs directory"
            echo " -L  Don't pull things in the /etc/Farms directory"
            echo " Default is to pull everything"
            echo " -q  quiet--nothing to stdout"
            echo " -v  verbose (the more v's the more verbose)"
            echo " -H hostname of local node"
	    echo " -M module on rsync server to pull from (default is voldemort or whatever is specified in /etc/voldemort.conf)"
            echo " -t target rsync server (default is fnpcd or specified in 
 /etc/voldemort.conf)"
	    echo " -i Install mode, run new scripts and prescripts"
            echo " -I Install mode, run all scripts and prescripts"
	    echo " -C clear RPM, prescripts, and scripts directory if selected"
            echo " -W Wipe out anything in the tarball area before untarring"
            echo " -w Workgroup, default is in /etc/workgroup"
}
#***************************************************
# Read a file with one item per line and make it into a list that
# fits into one environment variable
#***************************************************
linetovar() {
  MEMLIST=""
  if [ $DEBUG -gt 2 ] 
  then
    echo "in linetovar"
  fi
  for line in $MEMFILE
  do
        if [ $DEBUG -gt 2 ]
        then
          echo "$line"
        fi
	if [ "$line" != "" ] 
	then
	    testchar=`echo $line | cut -c1`
	    if [ "$testchar" != "#" ]
	    then
		MEMLIST="$MEMLIST $line"
	    fi
	fi
    done
}
#*******************************************
random_sleep() {
        RAN=$RANDOM
        if [ "$RAN" = "0" ] ; then
                RAN=1
        fi
        if [ $MAXWAITTIME -le 0 ] ; then
                MAXWAITTIME=1
        fi
        SLEEPTIME=$(($RAN % $MAXWAITTIME))
        if [ $DEBUG -ge 1 ] ; then
                echo "  Sleeping for $SLEEPTIME minutes"
        fi
        sleep ${SLEEPTIME}m
}

#Pull logic
#$host is the hostname of the node you are pulling the 
#files from
#$PUSHDIR is the list of directories to pull (in case of files)
#should not have leading / in the path.

hostpull(){
                case ${category} in
                files)
#
#  Now loop across each directory... in demo it is only /etc but will grow.
#  Note that with rsync -avz any subdirectories of /etc will also 
#  get pushed out and the corresponding subdirectories created on the
#  worker nodes.--nested do 5A
#
		    for dir in $PUSHDIR
		    do
			cd /
                        if [ $DEBUG -gt 1 ]
                        then
			    echo ${dir}
                        fi
#
			$RSYNC_PATH --exclude '*Makefile' $RSYNCOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} .
			RETVAL=$?
			while [ $RETVAL -eq 10 ] 
			do
			    random_sleep
			    ${RSYNC_PATH} --exclude '*Makefile' $RSYNCOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} .
			    RETVAL=$?
			done
			    
#    End of RSYNC_PUSHDIR loop (nested do 5A)
		    done 
                 ;;
                tarballs)
		TMPTAROUT=/tmp/tarballs.out_$$
                TMPTARERR=/tmp/tarballs.err_$$
                TMPTAR=/tmp/tarballs_$$
                TMPTGZ=/tmp/tgz_$$
                cat /dev/null > $TMPTAROUT
		cat /dev/null > $TMPTARERR
		cat /dev/null > $TMPTGZ
		cat /dev/null > $TMPTAR
#
#
#  Now loop across each directory... in demo it is only /etc but will grow.
#  Note that with rsync -avz any subdirectories of /etc will also 
#  get pushed out and the corresponding subdirectories created on the
#  worker nodes.--nested do 5A
#
		    for dir in $PUSHDIR
		    do
                    cd /
                        if [ $DEBUG -gt 1 ]
                        then
			    echo ${dir}
                        fi
#

			$RSYNC_PATH --exclude '*Makefile' $RSYNCOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} . >> $TMPTAROUT 2>> $TMPTARERR
			RETVAL=$?
			while [ $RETVAL -eq 10 ] 
			do
			    random_sleep
			    $RSYNC_PATH --exclude '*Makefile' $RSYNCOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} . >> $TMPTAROUT 2>> $TMPTARERR
			    RETVAL=$?
			done

#    End of RSYNC_PUSHDIR loop (nested do )5A
		    done 
		    if [ -s $TMPTAROUT ] 
		    then
			grep tar $TMPTAROUT > $TMPTAR
			grep tgz $TMPTAROUT > $TMPTGZ
			if [ -s $TMPTAR ] || [ -s $TMPTGZ ] 
			then
			    cp $TMPTAR /root/bin/tarballs/tar.list
			    cp $TMPTGZ /root/bin/tarballs/tgz.list
			    if [ ${WIPEOUT_TARBALLS} == "yes" ] 
			    then
				run.tarballs -W
			    else
			    	run.tarballs
                            fi
			fi
	            fi
		    ;;
		RPMS)
                        cd /root/bin/RPMS
			if [ "${CLEAR}" == "yes" ] 
			then
				rm /root/bin/RPMS/*
			fi
# Test to see if there are any rpms in the directory (should check for
# timeout here too--until structure maybe)
			rpmcount=`$RSYNC_PATH $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/ | grep rpm | wc -l`
			RETVAL=$?
			while [ $RETVAL -eq 10 ]
			do
				random_sleep
				rpmcount=`$RSYNC_PATH $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/ | grep rpm | wc -l`
				RETVAL=$?
			done
			if [ $rpmcount -gt 0 ] 
			then
			    $RSYNC_PATH --exclude '*Makefile' $RSYNCOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/*.rpm .
			    RETVAL=$?
			    while [ $RETVAL -eq 10 ] 
			    do
				random_sleep
				$RSYNC_PATH --exclude '*Makefile' $RSYNCOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/*.rpm .
				RETVAL=$?
			    done
			    run.RPMS
			fi
                    ;;
		prescripts) 
                    cd /root/bin/prescripts
		    if [ "${CLEAR}" == "yes" ]
		    then
			rm /root/bin/prescripts/*
                    fi
#check if there is anything in this directory by doing an list first
#will always return at least one (.) if directory exists

		    precount=`$RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/ | grep -v drwx | grep -v 'bytes/sec' | grep -v 'speedup is' | wc -l`
		    RETVAL=$?
		    while [ $RETVAL -eq 10 ]
		    do
			random_sleep
			precount=`$RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/ | grep -v drwx | grep -v 'bytes/sec' | grep -v 'speedup is' | wc -l `
			RETVAL=$?
	            done
		    if [ $precount -gt 0 ] 
		    then
			$RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .
			RETVAL=$?
			while [ $RETVAL -eq 10 ] 
			do
			    random_sleep
			    $RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .
			    RETVAL=$?
			done

			if [ "$INSTALLMODE" = "new" ]
			then
			    run.prescripts
			fi
			if [ "$INSTALLMODE" = "all" ]
			then
			    run.prescripts -I
			fi
		    fi
		    ;;
# For scripts and prescripts, don't use "r" recursive option
# "a" option is equal to rlptgoD	
		scripts)
                    cd /root/bin/scripts 
		    if [ "${CLEAR}" == "yes" ] 
		    then
			rm /root/bin/scripts/*
                    fi
#Check to see if there are any scripts by doing a list
#will always get at least one entry in list

		    postcount=`$RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/ | grep -v drwx | grep -v 'bytes/sec' | grep -v 'speedup is'  | wc -l`
		    RETVAL=$?
		    while [ $RETVAL -eq 10 ]
		    do 
			random_sleep
			postcount=`$RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/ | grep -v drwx | grep -v 'bytes/sec' | grep -v 'speedup is' |  wc -l`
			RETVAL=$?
	            done
		    if [ $postcount -gt 0 ]
		    then
			$RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .
			RETVAL=$?
			while [ $RETVAL -eq 10 ] 
			do
			    random_sleep
			    $RSYNC_PATH --exclude '*Makefile' $RSYNCSINGLEOPT rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .
			    RETVAL=$?
			done

			if [ "$INSTALLMODE" = "new" ]
			then
			    run.scripts
			fi
			if [ "$INSTALLMODE" = "all" ]
			then
			    run.scripts -I
			fi
		    fi
		    ;;
                esac
}
#This subroutine searches through the file /tmp/nodes.conf
#which has been pulled over from the rsync server
#to try to determine the cluster, OS level, and subclusters of a node name
#if applicable
#Fields are colon-separated
#nodename:cluster:OS:nodespec:<subcluster1><subcluster2> (subclusters are space-separated if more than one)
dbsearch() {
DBLINE="`grep "^${RSYNC_HOSTNAME}:" /tmp/db/nodes.conf `"
if [ $DEBUG -gt 1 ]
then
    echo $DBLINE
fi
DB_CLUSTER="$(echo $DBLINE | cut -d ':' -f2)"
DB_FLAVOR="$(echo $DBLINE | cut -d ':' -f3)"
DB_DISK="$(echo $DBLINE | cut -d ':' -f4)"
DB_BAUDRATE="$(echo $DBLINE | cut -d ':' -f5)"
DB_APIC="$(echo $DBLINE | cut -d ':' -f6)"
DB_NODESPEC="$(echo $DBLINE | cut -d ':' -f7)"
DB_SUBCLUSTER="$(echo $DBLINE | cut -d ':' -f8)"
}
#
#  BEGIN MAIN PROGRAM EXECUTION
#
# Order of options should be the following:
# options entered on the command line
# options found in local /etc/voldemort.conf
# options found in database on fnpcd
# Best guess at flavor, generic cluster name
#
#First initialize all variables that could be set by cmdline options.
#
PRESCRIPTPUSH="yes"; export PRESCRIPTPUSH
RPMPUSH="yes"; export RPMPUSH
FILEPUSH="yes"; export FILEPUSH
SCRIPTPUSH="yes"; export SCRIPTPUSH
TARPUSH="yes"; export TARPUSH
LINUXPUSH="yes"; export LINUXPUSH
INSTALLMODE="none"; export INSTALLMODE
CLEAR="no" ; export CLEAR
WIPEOUT_TARBALLS="no" ; export WIPEOUT_TARBALLS
MAXWAITTIME=5; export MAXWAITTIME
#
# /etc/workgroup only there in Fermi Linux
#
if [ -e /etc/workgroup ]
then
    WORKGROUP="`cat /etc/workgroup`" ; export WORKGROUP
else
    WORKGROUP=""
fi
CMDLINE_CLUSTER=""; export CMDLINE_CLUSTER
CMDLINE_FLAVOR=""; export CMDLINE_FLAVOR
CMDLINE_HOSTNAME=""; export CMDLINE_HOSTNAME
CMDLINE_SERVER=""; export CMDLINE_SERVER
CMDLINE_MODULE=""; export CMDLINE_MODULE
#
#Check for the presence of /usr/bin/rsync and /usr/local/bin/rsync
#
if [ -x /usr/bin/rsync ]
then
    RSYNC_PATH="/usr/bin/rsync"
else
    if [ -x /usr/local/bin/rsync ]
    then
	RSYNC_PATH="/usr/local/bin/rsync"
    else
        echo "Rsync executable not found in /usr/bin/rsync or"
	echo "/usr/local/bin/rsync, exiting"
	exit
    fi
fi
RSYNC_ALTPATH="/usr/local/bin/rsync" ; export RSYNC_ALTPATH
DEBUG=1; export DEBUG

#Verify that /sbin is in the path
SBINLINE="`echo $PATH | grep ':/sbin'`"
if [ "$SBINLINE" == "" ]
then
   PATH="$PATH:/sbin"
   export PATH
fi
#Source the global config file
#For the pullrsync client, a properly configured system will 
# set variables RSYNC_SERVER, RSYNC_BACKUP, RSYNC_MODULE, RSYNC_CLUSTER,
#RSYNC_FLAVOR, RSYNC_NODESPEC,RSYNC_SUBCLUSTER.
#If the system has not been configured, RSYNC_CLUSTER,
#RSYNC_FLAVOR, RSYNC_NODESPEC, and RSYNC_SUBCLUSTER will be not defined.
WRITECONF="N"
if [ -x /etc/voldemort.conf ]
then
    . /etc/voldemort.conf
# Check if a RSYNC_FLAVOR line was there
    CLUS_LINE="$(grep RSYNC_FLAVOR /etc/voldemort.conf)"
    if [ "$CLUS_LINE" == "" ]
    then
	WRITECONF="Y"
    fi
else
    if [ $DEBUG -gt 0 ] 
    then
	echo "Error: /etc/voldemort.conf not found"
	echo "setting RSYNC_SERVER=fnpcd.fnal.gov"
        echo "setting RSYNC_BACKUP=fnpcb.fnal.gov"
	echo "setting default module voldemort"
    fi
	RSYNC_SERVER=fnpcd.fnal.gov
	RSYNC_BACKUP=fnpcb.fnal.gov
	RSYNC_MODULE=voldemort
	RSYNC_HOSTNAME="$(grep HOSTNAME /etc/sysconfig/network | grep -v DHCP | cut -d'=' -f2 | cut -d'.' -f1)"
fi


# Handle the case of default options in the config file as follows:
# If the environment variable RSYNC_DEFAULT_OPTIONS is nonzero, 
# call rsync_push again with these extra arguments appended
# to the command line and the "-x" option prepended to the command 
# line to stop it from recursing again.

if [ "$RSYNC_DEFAULT_OPTIONS" != "" ] && [ "$1" != "-x" ]
then
    pullrsync -x $* ${RSYNC_DEFAULT_OPTIONS}
    exit
fi

#Parse the options passed to the program
while [ $# != 0 ]
do 
	case $1 in 
	    -x) shift 1 ;;
            -h | -help | --help) printHelp ; exit 0 ;;
            -c) test $# -lt 2 && { printHelp ; exit 1 ; }
                CMDLINE_CLUSTER="$2" ; export CMDLINE_CLUSTER ; shift 2 ;;
            -f) test $# -lt 2 && { printHelp ; exit 1 ; }
                CMDLINE_FLAVOR="$2" ; export CMDLINE_FLAVOR ; shift 2 ;;
	    -H) test $# -lt 2 && { printHelp ; exit 1 ; }
                CMDLINE_HOSTNAME="$2" ; export CMDLINE_HOSTNAME ; shift 2 ;;
            -M) test $# -lt 2 && { printHelp ; exit 1 ; } 
		CMDLINE_MODULE="$2" ; export CMDLINE_MODULE ; shift 2 ;;
	    -t) test $# -lt 2 && { printHelp ; exit 1 ; }
                CMDLINE_SERVER="$2" ; export CMDLINE_SERVER ; shift 2 ;;
            -P) PRESCRIPTPUSH="no" ; export PRESCRIPTPUSH ; shift 1 ;;
            -R) RPMPUSH="no" ; export RPMPUSH ; shift 1 ;;
            -F) FILEPUSH="no" ; export FILEPUSH ; shift 1 ;;
            -S) SCRIPTPUSH="no" ; export SCRIPTPUSH ; shift 1 ;;
            -T) TARPUSH="no" ; export TARPUSH ; shift 1 ;;
            -L) LINUXPUSH="no" ; export LINUXPUSH ; shift 1 ;;
            -q) DEBUG=0 ; shift 1 ;;
            -v*) PAR=$1 ; DEBUG=`echo ${PAR:1} | wc -c` ; echo "Debug level $DEBUG" ; shift 1 ;;
            -i) INSTALLMODE="new" ; export INSTALLMODE ; shift 1 ;;
	    -I) INSTALLMODE="all" ; export INSTALLMODE ; shift 1 ;;
	    -C) CLEAR="yes" ; export CLEAR ; shift 1 ;;
            -W) WIPEOUT_TARBALLS="yes" ; export WIPEOUT_TARBALLS ; shift 1 ;;
            -w) WORKGROUP="$2" ; export WORKGROUP ; shift 2 ;;
             *) printHelp; break
	esac
done

# Set the rsync options
if [ $DEBUG -eq 0 ]
then
    RSYNCOPT="-aqz"
    RSYNCSINGLEOPT="-qlptgoDz"
else
    RSYNCOPT="-avz"
    RSYNCSINGLEOPT="-lptgoDvz"
fi


#
# Compare command line arguments against /etc/voldemort.conf
# Command line arguments take precedence if they are different
#
if [ "$CMDLINE_SERVER" != "" ]
then
    if [ "$CMDLINE_SERVER" != "$RSYNC_SERVER" ]
    then
	if [ $DEBUG -gt 1 ]
	then
	    echo "server $CMDLINE_SERVER on command line different than default server $RSYNC_SERVER specified in config file"
	fi
    fi
    RSYNC_SERVER="$CMDLINE_SERVER"
fi
if [ "$CMDLINE_MODULE" != "" ]
then
    if [ "$CMDLINE_MODULE" != "$RSYNC_MODULE" ]
    then
	if [ $DEBUG -gt 1 ]
	then
	    echo "module $CMDLINE_MODULE on command line different than default module  $RSYNC_MODULE specified in config file"
	fi
    fi
    RSYNC_MODULE="$CMDLINE_MODULE"
fi
if [ "$CMDLINE_HOSTNAME" != "" ]
then
   if [ "$CMDLINE_HOSTNAME" != "$RSYNC_HOSTNAME" ]
   then
	if [ $DEBUG -gt 0 ]
	then
	    echo "Warning--hostname on command line different than hostname of this host"
	fi
   fi
   RSYNC_HOSTNAME="$CMDLINE_HOSTNAME"
else
    if [ $RSYNC_HOSTNAME = "" ] 
    then
	if [ $DEBUG -gt 0 ] 
	then
	    echo "-H option for hostname is required"
	    echo "exiting now because of null hostname"
	    printHelp
	fi
	exit 1
    fi
fi
if [ "$CMDLINE_CLUSTER" != "" ]
then
    if [ "$CMDLINE_CLUSTER" != "$RSYNC_CLUSTER" ]
    then
	if [ "$RSYNC_CLUSTER" != "" ]
	then
	    if [ $DEBUG -gt 1 ]
	    then
		echo "cluster $CMDLINE_CLUSTER on command line different than default cluster $RSYNC_CLUSTER specified in config file"
	    fi
	fi
    fi
    RSYNC_CLUSTER="$CMDLINE_CLUSTER"
fi
if [ "$CMDLINE_FLAVOR" != "" ]
then
    if [ "$CMDLINE_FLAVOR" != "$RSYNC_FLAVOR" ]
    then
	if [ "$RSYNC_FLAVOR" != "" ]
	then
	    if [ $DEBUG -gt 1 ]
	    then
		echo "flavor $CMDLINE_FLAVOR on command line different than default flavor $RSYNC_FLAVOR specified in config file"
	    fi
	fi
    fi
   RSYNC_FLAVOR="$CMDLINE_FLAVOR"
fi
#
#   Check to see if the server is on the site, and if we are
#   only if RSYNC_DOMAIN and RSYNC_LOCALIP are set 
#  
#
if [ "$RSYNC_DOMAIN" != "" ] && [ "$RSYNC_LOCALIP" != "" ]
then 
    SERVLINE=`echo $RSYNC_SERVER | grep $RSYNC_DOMAIN`
    if [ "$SERVLINE" != "" ] 
    then
	ONSITE=`ifconfig | grep $RSYNC_LOCALIP`
	if [ "$ONSITE" == "" ]
	then
	    echo "pullrsync will only work on site, exiting"
	    exit
        fi
    fi
fi
#
#    Test a ping to be sure the network is up.
#
if [ -e /tmp/ping.err ]
then
    rm /tmp/ping.err
fi
if [ -e /tmp/ping.out ]
then
    rm /tmp/ping.out
fi
ping -c1 -w2 $RSYNC_SERVER > /tmp/ping.out 2> /tmp/ping.err
pingcount="`grep received /tmp/ping.out | cut -d' ' -f4`"
if [ -s /tmp/ping.err ] || [ "$pingcount" != "1" ]
then
    echo "$RSYNC_SERVER is not reachable"
    echo "Trying $RSYNC_BACKUP"
    rm /tmp/ping.err /tmp/ping.out
    ping -c1 -w2 $RSYNC_BACKUP > /tmp/ping.out 2> /tmp/ping.err
    if [ -s /tmp/ping.err ] 
    then
	echo "$RSYNC_BACKUP is not reachable"
        echo "try again when the network is up"
        exit
    else
	RSYNC_SERVER="$RSYNC_BACKUP"
    fi
fi

##Do the database search unconditionally.
##  Previously only did it if 
#any one of the four variables RSYNC_CLUSTER, RSYNC_FLAVOR,
#RSYNC_NODESPEC, or RSYNC_SUBCLUSTER is null, do the database search.
#This is most likely to happen during the first time pullrsync is run
#and the /etc/voldemort.conf is not in good shape yet.
#01-A
if [ $DEBUG -gt 2 ] 
then
echo "$RSYNC_CLUSTER"
echo "$RSYNC_FLAVOR"
echo "$RSYNC_NODESPEC"
echo "$RSYNC_SUBCLUSTER"
fi

#if [ "$RSYNC_CLUSTER" = "" ] || [ "$RSYNC_FLAVOR" = "" ] || [ "$RSYNC_NODESPEC" = "" ] || [ "$RSYNC_SUBCLUSTER" = "" ]
#then
    if [ ! -d /tmp/db ]
    then
	mkdir /tmp/db
    fi
    $RSYNC_PATH  rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/common/db/nodes.conf /tmp/db
    RETVAL=$?
    while [ $RETVAL -eq 10 ] 
    do
	random_sleep
	$RSYNC_PATH  rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/common/db/nodes.conf /tmp/db
    	RETVAL=$?
    done
    dbsearch
#
#    Did the DB search succeed?
#
#   02-A
    if [ $DEBUG -gt 2 ] 
    then
	echo "$DB_CLUSTER $DB_FLAVOR $DB_NODESPEC"
    fi
#   End 02-A
#   02-B 
    if [ "$DB_CLUSTER" != "" ] && [ "$DB_FLAVOR" != "" ] && [ "$DB_NODESPEC" != "" ]
    then
#
#Used to be database values only override the local configuration file if
#the local variables are null
#
# Now database values override other values (including /etc/voldemort.conf) 
# if the
# command line variables are null.  (used to be RSYNC_CLUSTER, etc
# inside the if statements)
#
#
#       03-A
	if [ "$CMDLINE_CLUSTER" = "" ]
	then
	    RSYNC_CLUSTER="$DB_CLUSTER"
	fi
#       03-B
	if [ "$CMDLINE_FLAVOR" = "" ]
	then
	    RSYNC_FLAVOR="$DB_FLAVOR"
	fi
#       03-C
	if [ "$CMDLINE_NODESPEC" = "" ]
	then 
	    RSYNC_NODESPEC="$DB_NODESPEC"
	fi
#       03-D
	if [ "$CMDLINE_SUBCLUSTER" = "" ]
	then
	    RSYNC_SUBCLUSTER="$DB_SUBCLUSTER"
	fi
   else
#
#  DB search was unsuccessful...now we have to guess cluster
#  and flavor--but only if the variable is empty.
#       03-E
        if [ "$RSYNC_CLUSTER" = "" ]
        then
#           04-A
	    if [ "${RSYNC_HOSTNAME:0:5}" = "fncdf" ]
	    then
		RSYNC_CLUSTER="cdffarm1"
	    fi
#           04-B
	    if [ "${RSYNC_HOSTNAME:0:4}" = "fnd0" ]
	    then
		RSYNC_CLUSTER="d0bbin"
	    fi
#           04-C
	    if [ "${RSYNC_HOSTNAME:0:4}" = "fnpc" ]
	    then
		RSYNC_CLUSTER="fnsfo"
	    fi
#           04-D
	    if [ "${RSYNC_HOSTNAME:0:4}" = "fnpt" ]
	    then
		RSYNC_CLUSTER="fnpt0"
	    fi
#           end 04-D
        fi
#       end 03-E
#       03-F
	if [ "$RSYNC_FLAVOR" = "" ]
	then
	    OS="`uname`"
	    REV="`uname -r`"
#           04-D
	    if [ "$OS" = "SunOS" ] && [ "$REV" = "5.8" ]
	    then
		RSYNC_FLAVOR="SunOS+5.8"
	    fi
#           04-E
	    if [ "$OS" = "IRIX64" ] && [ "${REV:0:3}" = "6.5" ]
	    then
		RSYNC_FLAVOR="IRIX+6.5"
	    fi
#           04-F
	    if [ "$OS" = "Linux" ] 
	    then
		RHREL="`cat /etc/redhat-release | cut -d ' ' -f4`"
#               05-B
		if [ "${REV:0:3}" = "2.2" ] 
		then
		    RSYNC_FLAVOR="Linux+2.2"
		else
		    if [ "${REV:0:6}" = "2.4.18" ] || [ "${REV:0:6}" = "2.4.20" ]
		    then
			if [ "${RHREL}" = "7.3.1" ] || [ "${RHREL}" = "7.3.2" ]
			then
			    RSYNC_FLAVOR="Linux+2.4.18"
			else
			    if [ "${RHREL}" = "7.1.2" ]
			    then
				RSYNC_FLAVOR="Linux+2.4.18-712"
			    fi
			fi
                    else
			if [ "${REV:0:6}" = "2.4.21" ]
			then
			    RSYNC_FLAVOR="Linux+2.4.21"
			else
			    if [ "${REV:0:3}" = "2.4" ]
			    then
				RSYNC_FLAVOR="Linux+2.4"
			    fi
                        fi
		    fi
		fi
#               end 05-B
	    fi
#           End 04-F
	fi
#       End 03-F
# assume there's no node-specific info
#       03-G
	if [ "$RSYNC_NODESPEC" = "" ]
	then
	    RSYNC_NODESPEC="N"
	fi
#       End 03-G
   fi 
#  end 02-B
    rm /tmp/db/nodes.conf
# outer if now commented out, db search is unconditional
#fi
#End 01-A

CLUSLIST="$RSYNC_CLUSTER"

#      loop over the clusters in CLUSLIST
#      First nested DO

for cluster in $CLUSLIST
do
    if [ $DEBUG -gt 2 ]
    then
	echo ${cluster}
    fi

#       Find out which categories we are doing.
#       Default is to loop over prescripts, RPMS, files, scripts
#       in that order, unless flags are set for us to do otherwise.
#       First clear the category list from the previous cluster.
    CATLIST=""
    if [ $DEBUG -gt 1 ]
    then 
        echo ${PRESCRIPTPUSH} ${RPMPUSH} ${FILEPUSH} ${SCRIPTPUSH} ${TARPUSH} ${LINUXPUSH}
    fi
    if [ ${PRESCRIPTPUSH} = "yes" ]
    then              
	CATLIST="prescripts "
    fi
    if [ ${RPMPUSH} = "yes" ]
    then              
	CATLIST="${CATLIST} RPMS"
    fi
    if [ ${FILEPUSH} = "yes" ]
    then              
	CATLIST="${CATLIST} files"
    fi
    if [ ${SCRIPTPUSH} = "yes" ]
    then              
	CATLIST="${CATLIST} scripts"
    fi
    if [ ${TARPUSH} = "yes" ]
    then              
	CATLIST="${CATLIST} tarballs"
    fi
    if [ ${LINUXPUSH} = "yes" ]
    then              
	CATLIST="${CATLIST} linux"
    fi
    if [ $DEBUG -gt 0 ] 
    then
	echo "pulling for cluster" $cluster "categories " $CATLIST
    fi

#   Second nested DO--category

    for category in $CATLIST
    do
	if [ $DEBUG -gt 2 ] 
	then
	    echo "category: ${category}"
        fi
        FLAVLIST="$RSYNC_FLAVOR"

#Begin the flavor loop.  Third nested DO

        for flavor in $FLAVLIST
        do
# for "files" only:
# Reset the list of directories
# Then determine directories that should be pushed for this cluster and flavor
            if [ "$flavor" == "Linux+2.4.18" ] 
	    then
		linuxrelease="732"
	    elif [ "$flavor" == "Linux+2.4" ] 
	    then
		linuxrelease="711"
	    elif [ "$flavor" == "Linux+2.2" ] 
	    then
		linuxrelease="612"
	    elif [ "$flavor" == "Linux+2.4.21" ]
	    then
		linuxrelease="lts301"
            else
                linuxrelease="$flavor"
            fi
            if [ "${category}" == "files" ] || [ "${category}" == "tarballs" ]
            then
#
# Get the directory list from rsync server
#
		$RSYNC_PATH -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/.pushdir /tmp/.pushdir
		RETVAL=$?
		while [ $RETVAL -eq 10 ]
		do 
		    random_sleep
		    $RSYNC_PATH -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/.pushdir /tmp/.pushdir
		    RETVAL=$?
                done
# if no .pushdir file in this directory RETVAL is 23
# will cause error message:client: nothing to do: perhaps you need to specify 
# some filenames or the --recursive option?
	        if [ $RETVAL -ne 23 ] && [ -e /tmp/.pushdir ] 
		then
		    MEMFILE="`cat /tmp/.pushdir`"
		    linetovar
		    RSYNC_PUSHDIR="$MEMLIST"
		    rm /tmp/.pushdir
		else
		    RSYNC_PUSHDIR=""
                fi
             fi
#
#
#
            if [ "$category" == "linux" ]
	    then
	       cd /etc/${WORKGROUP}
	       rsync -avz rsync://${RSYNC_SERVER}/linux/${linuxrelease}/i386/Fermi/workgroups/${WORKGROUP}/ .
	       RETVAL=$?
		while [ $RETVAL -eq 10 ]
	        do
		    random_sleep
	       	    $RSYNC_PATH -avz rsync://${RSYNC_SERVER}/linux/${linuxrelease}/i386/Fermi/workgroups/${WORKGROUP}/ .
                    RETVAL=$?
                done
               /sbin/run.comps
            else
            HOSTLIST="$RSYNC_HOSTNAME"
	    for host in $HOSTLIST
	    do
                if [ $DEBUG -gt 1 ]
                then
		    echo ${host}
                fi
                PUSHDIR="$RSYNC_PUSHDIR"
		PULLDIR="clusters/${cluster}/${category}/${flavor}"
                hostpull
#    Now look for subcluster files
                SUBCLUSLIST="$RSYNC_SUBCLUSTER"
#    
#               Loop over the subclusters  (Nested do #4B)
		for subcluster in $SUBCLUSLIST
		do 
		    if [ $DEBUG -gt 1 ]
                    then 
			echo ${subcluster}
                    fi
		    if [ "$category" = "files" ]
		    then 
			SUBHOSTDIRLIST=""
			SUBHOST_PUSHDIR=""
			MEMFILE=""
			$RSYNC_PATH -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/${subcluster}/.pushdir /tmp/.pushdir
			RETVAL=$?
			while [ $RETVAL -eq 10 ]
			do
				random_sleep
				$RSYNC_PATH -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/${subcluster}/.pushdir /tmp/.pushdir
				RETVAL=$?
			done
			if [ $RETVAL -ne 23 ] && [ -e /tmp/.pushdir ]
			then
			    MEMFILE="`cat /tmp/.pushdir`"
			    linetovar
			    SUBHOST_PUSHDIR="$MEMLIST"
			    rm /tmp/.pushdir
                        fi
		    fi
# Push out the subcluster-specific files:
#
		    PUSHDIR="$SUBHOST_PUSHDIR"
		    PULLDIR="clusters/${cluster}/${category}/${flavor}/${subcluster}"
		    hostpull
#    End of SUBCLUSTER loop       (Nested do 4b)
		done
#
#    Here is where we add logic for host-specific files
#    presume no host-specific tarballs
                if [ "$RSYNC_NODESPEC" = "Y" ]
                then
		    if [ ${category} = "files" ]
		    then
			MEMLIST=""
			HOST_PUSHDIR=""
			$RSYNC_PATH -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/${host}/.pushdir /tmp/.pushdir
			RETVAL=$?
			while [ $RETVAL -eq 10 ] 
			do 
				random_sleep
				$RSYNC_PATH -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/${host}/.pushdir /tmp/.pushdir
				RETVAL=$?
			done
			if [ $RETVAL -ne 23 ] && [ -e /tmp/.pushdir ]
			then
			    MEMFILE="`cat /tmp/.pushdir`"
			    linetovar
			    HOST_PUSHDIR="$MEMLIST"
			    PUSHDIR="$HOST_PUSHDIR"
			    rm /tmp/.pushdir
			else
			    PUSHDIR=""
                        fi
                    else
			PUSHDIR=""
                    fi
		    PULLDIR="clusters/${cluster}/${category}/${flavor}/${host}"
                    hostpull
#End of host-specific files construct
                fi
#    End of host loop (nested do #4)
	    done
	    fi
#    End of flavor loop (third nested do)
	done
#    End of category (prescript,rpm, files,postscript ) loop (second)
    done
#    End of cluster loop (first nested do)
done
# Final step--check to see if we need to write /etc/voldemort.conf
if [ "$WRITECONF" == "Y" ] 
then
    echo "#`date`" >> /etc/voldemort.conf
    echo "RSYNC_CLUSTER=$RSYNC_CLUSTER" >> /etc/voldemort.conf
    echo "RSYNC_FLAVOR=$RSYNC_FLAVOR" >> /etc/voldemort.conf
    echo "RSYNC_NODESPEC=$RSYNC_NODESPEC" >> /etc/voldemort.conf
    echo "RSYNC_SUBCLUSTER=\"$RSYNC_SUBCLUSTER\"" >> /etc/voldemort.conf
fi
