#!/bin/sh
#VOLDEMORT 0.6
#Last revised, S. Timm 2/20/03
#Bug fixes, S. Timm 4/14/03
#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 " -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
}
#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
#
# Allow for the fact that rsync on sgi machines is in /usr/local/bin
# /usr/sbin/rsync which is also there is something totally different.

			if [ $DEBUG -le 0 ]
                        then
				$RSYNC_PATH --exclude '*Makefile' -aqz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} .
			else
				$RSYNC_PATH --exclude '*Makefile' -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} .
                        fi
#    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
#
# Allow for the fact that rsync on sgi machines is in /usr/local/bin
# /usr/sbin/rsync which is also there is something totally different.

			if [ $DEBUG -le 0 ]
                        then 
				$RSYNC_PATH --exclude '*Makefile' -aqz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} . >> $TMPTAROUT 2>> $TMPTARERR
			else
				$RSYNC_PATH --exclude '*Makefile' -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/${dir} . >> $TMPTAROUT 2>> $TMPTARERR
                        fi
#    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
			    run.tarballs
			fi
	            fi
		    ;;
#    presuming only linux flavors exist for RPMS
		RPMS)
		    if [ $DEBUG -le 0 ]
                    then
                        cd /root/bin/RPMS
			$RSYNC_PATH --exclude '*Makefile' -aqz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/*.rpm .
			run.RPMS
		    else
                    echo "${cluster}"
		        cd /root/bin/RPMS
			$RSYNC_PATH --exclude '*Makefile' -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/*.rpm .
			run.RPMS 
		    fi
                    ;;
		prescripts) 
                    cd /root/bin/prescripts
		    if [ $DEBUG -le 0 ]
		    then
			$RSYNC_PATH --exclude '*Makefile' -qlptgoDz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .

		    else
			$RSYNC_PATH --exclude '*Makefile' -lptgoDvz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .
                    fi
                    if [ "$INSTALLMODE" = "new" ]
		    then
			run.prescripts
		    fi
		    if [ "$INSTALLMODE" = "all" ]
		    then
			run.prescripts -I
		    fi
		    ;;
# For scripts and prescripts, don't use "r" recursive option
# "a" option is equal to rlptgoD	
		scripts)
                    cd /root/bin/scripts 
		    if [ $DEBUG -le 0 ]
		    then
			$RSYNC_PATH --exclude '*Makefile' -qlptgoDz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .
		    else
			$RSYNC_PATH --exclude '*Makefile' -lptgoDvz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/${PULLDIR}/* .
                    fi
                    if [ "$INSTALLMODE" = "new" ]
		    then
			run.scripts
		    fi
		    if [ "$INSTALLMODE" = "all" ]
		    then
			run.scripts -I
		    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
#
# /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
#
#
#Parse the options passed to the program
while [ $# != 0 ]
do 
	case $1 in 
            -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 ;;
            -w) WORKGROUP="$2" ; export WORKGROUP ; shift 2 ;;
             *) printHelp; break
	esac
done
#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
#
# 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


#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
    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
#
#Database values only override the local configuration file if
#the local variables are null
#
#       03-A
	if [ "$RSYNC_CLUSTER" = "" ]
	then
	    RSYNC_CLUSTER="$DB_CLUSTER"
	fi
#       03-B
	if [ "$RSYNC_FLAVOR" = "" ]
	then
	    RSYNC_FLAVOR="$DB_FLAVOR"
	fi
#       03-C
	if [ "$RSYNC_NODESPEC" = "" ]
	then 
	    RSYNC_NODESPEC="$DB_NODESPEC"
	fi
#       03-D
	if [ "$RSYNC_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
#               05-A
		if [ "${RSYNC_HOSTNAME:0:5}" = "fnpc1" ] && [ ${#RSYNC_HOSTNAME} -eq 7 ]
		then
		    RSYNC_CLUSTER="fnpcb"
		else
		    RSYNC_CLUSTER="fnsfo"
		fi
#              end 05-A
	    fi
#           end 04-C
        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" ]
		    then
			if [ "${RHREL}" = "7.3.1" ] 
			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.20" ]
			then
			    RSYNC_FLAVOR="Linux+2.4.20"
			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
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
        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="731"
	    elif [ "$flavor" == "Linux+2.4" ] 
	    then
		linuxrelease="711"
	    elif [ "$flavor" == "Linux+2.2" ] 
	    then
		linuxrelease="612"
            else
                linuxrelease="$flavor"
            fi
            if [ "${category}" == "files" ] || [ "${category}" == "tarballs" ]
            then
#
# Get the directory list from rsync server
#
		rsync -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/.pushdir /tmp/.pushdir
		MEMFILE="`cat /tmp/.pushdir`"
                linetovar
                RSYNC_PUSHDIR="$MEMLIST"
                if [ -e /tmp/.pushdir ]
                then
			rm /tmp/.pushdir
                fi
             fi
#
#Loop across each host--fourth nested do
#
            if [ "$category" == "linux" ]
	    then
	       cd /etc/${WORKGROUP}
	       rsync -avz rsync://${RSYNC_SERVER}/linux/${linuxrelease}/i386/Fermi/workgroups/${WORKGROUP}/ .
               /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
#
#    Here is where we add logic for host-specific files
                if [ "$RSYNC_NODESPEC" = "Y" ]
                then
		    if [ ${category} = "files" ]
		    then
			MEMLIST=""
			HOST_PUSHDIR=""
			rsync -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/${host}/.pushdir /tmp/.pushdir
			MEMFILE="`cat /tmp/.pushdir`"
			linetovar
			HOST_PUSHDIR="$MEMLIST"
			PUSHDIR="$HOST_PUSHDIR"
                        if [ -e /tmp/.pushdir ]
                        then
				rm /tmp/.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
#    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 -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/clusters/${cluster}/${category}/${flavor}/${subcluster}/.pushdir /tmp/.pushdir
			    MEMFILE="`cat /tmp/.pushdir`"
			    linetovar
                            SUBHOST_PUSHDIR="$MEMLIST"
                            if [ -e /tmp/.pushdir ]
                            then
				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
#    End of IF construct 1A
#		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
