#!/bin/sh

########################################################
# Runs the appropriate set of rsync commands to synchronize 
#   a series of folders.  Folders to synchronize are loaded
#   from a file (passed as a parameter).
#
# Version 3
########################################################

SCRIPT_NAME="./rsync_batch.sh"
TARGET_SCRIPT="./rsync.sh"
OPTIONS_ALL=""
OPTIONS1_CHECK="--dry-run"
OPTIONS1_SYNC=""
OPTIONS2_TIMESTAMP=""
OPTIONS2_CHECKSUM="--checksum"

# check that we have at least 3 args
if [ $# -gt 2 ]; then
        # build options
        compiled_options="$OPTIONS_ALL"
        
        # retrieve first parameter and build rsync options based off of it
        if [ "$1" == 'check' ]; then
                compiled_options="$compiled_options $OPTIONS1_CHECK"
        elif [ "$1" == 'sync' ]; then
                compiled_options="$compiled_options $OPTIONS1_SYNC"
        else
                echo 'Error - parameter 1:  Neither "check" nor "sync" specified.  Aborting.'
                echo ''
                echo "Usage: $SCRIPT_NAME check|sync timestamp|checksum rules_file [additional rsync options]"
                echo "$TARGET_SCRIPT must be present and executable!"
                exit -2
        fi
        
        # retrieve second parameter and build rsync options based off of it
        if [ "$2" == 'timestamp' ]; then
                compiled_options="$compiled_options $OPTIONS2_TIMESTAMP"
        elif [ "$2" == 'checksum' ]; then
                compiled_options="$compiled_options $OPTIONS2_CHECKSUM"
        else
                echo 'Error - parameter 2:  Neither "timestamp" nor "checksum" specified.  Aborting.'
                echo ''
                echo "Usage: $SCRIPT_NAME check|sync timestamp|checksum rules_file [additional rsync options]"
                echo "$TARGET_SCRIPT must be present and executable!"
                exit -3
        fi
        
        # third parameter is "rules file"
        rulesFilePath="$3"
        
        # remove the first three parameters passed to script.  What's left over is additional rsync options passed by user.
        shift 3
        
        # check that supplied rules file exists
        if [ -f "$rulesFilePath" ]; then
                
                # process each line in rules file
                ruleFile=`sed '/^\#/d' < $rulesFilePath`
                echo "$ruleFile" | { while read line; do
                        # if line is not blank
                        if [ -n "$line" ] ; then
                                # parse three parameters from each line (each field is separated by a comma)
                                rs_mode=`echo "$line" | awk -F"," '{print $1}'`   # mode (either "local" or "remote")
                                rs_source=`echo "$line" | awk -F"," '{print $2}'` # source path
                                rs_dest=`echo "$line" | awk -F"," '{print $3}'`   # destination path
                                
                                # check that we were able to retrieve all three required items from rules file
                                if [ -n "$rs_mode" ] && [ -n "$rs_source" ] && [ -n "$rs_dest" ]; then
                                        
                                        # execute our target script with the read-in rule
                                        $TARGET_SCRIPT $rs_mode $compiled_options "$@" "$rs_source" "$rs_dest"
                                        if [ $? -ne 0 ]; then exit $? ; fi   # quit if rsync encountered an error
                                        
                                # if error getting all three required items from rules file...
                                else
                                        echo 'Error - rules file:  Error reading following rule line: $line'
                                        echo 'Aborting...'
                                        exit -10
                                fi
                        fi
                done; exit 0; } # exit 0 (no error) if we made it to the end of the rules file
                
                # exit the main shell with the same code as the sub-shell
                exit $?
                
        else
                echo 'Error - parameter 3:  Rules file does not exist.  Aborting.'
                echo ''
                echo "Usage: $SCRIPT_NAME check|sync timestamp|checksum rules_file [additional rsync options]"
                echo "$TARGET_SCRIPT must be present and executable!"
                exit -5
        fi
        
# no args
else
        echo "Usage: $SCRIPT_NAME check|sync timestamp|checksum rules_file [additional rsync options]"
        echo "$TARGET_SCRIPT must be present and executable!"
        exit -1
fi
