#!/bin/sh
#VOLDEMORT 0.6
#rsync_clone_pull script, to clone the whole RSYNC_HOME
#directory of the master server onto a slave server
#modified from pullrsync, S. Timm, 4/24/03
#
#
# Subroutines:
#
printHelp() {
            echo "rsync_clone_pull  [-h]"
            echo "           [-q -v]"
            echo "           [ -M <module> ]"
            echo "          [ -t <target server> ] "
            echo " -q  quiet--nothing to stdout"
            echo " -v  verbose (the more v's the more verbose)"
	    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 " -D directory other than $RSYNC_HOME (specified in /etc/voldemort_push.conf) to sync the files into"
	    echo " -y  skips the are-you-sure question"
}
#***************************************************
# 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(){
if [ $DEBUG -le 0 ]
then
    $RSYNC_PATH --exclude '*Makefile' -aqz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/* .
else
    $RSYNC_PATH --exclude '*Makefile' -avz rsync://${RSYNC_SERVER}/${RSYNC_MODULE}/* .
fi
}
#
#  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.
#
CMDLINE_SERVER=""; export CMDLINE_SERVER
CMDLINE_MODULE=""; export CMDLINE_MODULE
CMDLINE_DIRECTORY=""; export CMDLINE_DIRECTORY
AREYOUSURE="no"; export AREYOUSURE
#
#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 ;;
            -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 ;;
            -q) DEBUG=0 ; shift 1 ;;
            -v*) PAR=$1 ; DEBUG=`echo ${PAR:1} | wc -c` ; echo "Debug level $DEBUG" ; shift 1 ;;
	     -y) AREYOUSURE="yes" ; export AREYOUSURE ; shift 1;;
	     -D) test $# -lt 2 && ( printHelp ; exit 1 ; )
		 CMDLINE_DIRECTORY="$2" ; export CMDLINE_DIRECTORY ; shift 2 ;;
             *) printHelp; break
	esac
done
#Source the global config file
#For the rsync_clone_pull  client, a properly configured system will 
# set variables RSYNC_SERVER, RSYNC_BACKUP, RSYNC_MODULE
if [ -x /etc/voldemort.conf ]
then
    . /etc/voldemort.conf
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
if [ -x /etc/voldemort_push.conf ] 
then
    . /etc/voldemort_push.conf
else
    if [ $DEBUG -gt 0 ] 
    then
	echo "Error: /etc/voldemort_push.conf not found"
    fi
    if [ "${VOLDEMORT_DIR}" != "" ] 
    then
	RSYNC_HOME="${VOLDEMORT_DIR}"
    else
	if [ -d /usr/local/voldemort ]
	then
	    RSYNC_HOME="/usr/local/voldemort"
	fi
     fi
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_DIRECTORY" != "" ]
then
    if [ "$CMDLINE_DIRECTORY" != "$RSYNC_HOME" ]
    then
	if [ $DEBUG -gt 1 ]
	then
	    echo "server $CMDLINE_DIRECTORY on command line different than default server $RSYNC_HOME specified in config file"
	fi
    fi
    RSYNC_HOME="$CMDLINE_DIRECTORY"
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
#
#   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`"
cleanerr="`grep -v 'Warning: time of day goes back' /tmp/ping.err`"
if [ "$cleanerr" != "" ] || [ "$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
    pingcount="`grep received /tmp/ping.out | cut -d' ' -f4`"
    cleanerr="`grep -v 'Warning: time of day goes back' /tmp/ping.err`"
    if [ "$cleanerr" != "" ]  || [ "$pingcount" != "1" ]
    then
	echo "$RSYNC_BACKUP is not reachable"
        echo "try again when the network is up"
        exit
    else
	RSYNC_SERVER="$RSYNC_BACKUP"
    fi
fi
if [ ! -d "$RSYNC_HOME" ] 
then
    echo "$RSYNC_HOME does not exist, exiting"
    exit
fi
###
### Print the are-you-sure message
echo "This utility will delete everything in and under directory $RSYNC_HOME"
echo "and pull the contents of the voldemort tree from $RSYNC_SERVER"
if [ "$AREYOUSURE" == "no" ]
then
    echo "Are you sure? (yes/no)"
    read AREYOUSURE
    if [ "$AREYOUSURE" != "yes" ]
    then
        exit
    fi
fi
###
cd $RSYNC_HOME
rm -Rf *
hostpull
