#!/bin/sh
####################################################################
# (C) Copyright 2005-2014 Hewlett-Packard Development Company, L.P.
# @(#) cmsync - copy file to multiple nodes within configured cluster.
# @(#) Product Name              :  HP Serviceguard
# @(#) Product Version           :  A.12.10.00
# @(#) Patch Name                :  
#####################################################################

#PATH=$SGSBIN:/usr/bin:/usr/sbin:/opt/VRTS/bin

print_usage ()
{
    echo "Usage: cmsync [-r] [ [-n node] ... ]  [ [source_node:]source_file ... ]"
    exit 1
}

# Loop through arguments and options, parsing -n for cmsync use, passing 
# the source file(s) to cmcp and disallowing any other options.
node_list=""
index=0
other_opts=""

while [ $# -gt 0 ]; do
    case "$1" in
        -n) # Check for option argument
            if [ $# -gt 1 ]; then
                node_list="$node_list $2"
                shift 2
            else
                print_usage
            fi ;;
        -r) other_opts="$other_opts $1"
            shift ;;
        -*) print_usage ;;
        *)  source_files[$index]=$1
            index=$(expr $index + 1)
            shift ;;
    esac
done

# Verify a source file was specified
if [ ${#source_files[@]} -eq 0 ]; then
    print_usage
fi

# If no node is specified, automatically select all nodes in local cluster.
if [ -z "$node_list" ]; then
    node_list=`cmviewcl -f line -l node | grep '|name=' | cut -f2 -d'='`
    if [ -z "$node_list" ]; then
        echo "cmsync: Error reading cluster configuration; specify nodes."
        exit 1
    fi
fi

# Execute cmcp for the source file on each node in node_list.
# Destination filename is left blank (to be equivalent to source filename).
ret=0
for node in ${node_list}
do
    echo "## Copying to node ${node}..."
    # Copy each file to each source node to the same path as the source file. 
    # In the case of a remote source file, remove the remote node name.
    for sfile in "${source_files[@]}"
    do    
        # obtain source file name only (no remote node with colon)
        if [ "$other_opts" ]; then
            # If argument is a directory
            is_first_char_slash=`expr match "$sfile" '\/*'`
            if [ !  "$is_first_char_slash" -eq 1  ]; then  # It is relative path
                 sfile="$PWD/$sfile"
            fi
            sff=`echo $sfile | awk '{ d=match($0,":"); print (d==0) ? $0 : substr($0,d+1) }' | awk -F/ '{print $NF}'`
            if [  "$sff" ]; then
                # If path is for e.g /root/TEST
                sff=`echo $sfile | awk '{ d=match($0,":"); print (d==0) ? $0 : substr($0,d+1) }' | rev | cut -d/ -f2- | rev`
            else
                # If path is for e.g /root/TEST/
                sff=`echo $sfile | awk '{ d=match($0,":"); print (d==0) ? $0 : substr($0,d+1) }' |  sed 's/\/*$//'`
                sff=`echo $sff | awk '{ d=match($0,":"); print (d==0) ? $0 : substr($0,d+1) }' | rev | cut -d/ -f2- | rev`
            fi
        else
            # If argument is a file
            sff=`echo $sfile | awk '{ d=match($0,":"); print (d==0) ? $0 : substr($0,d+1) }'`
        fi
 
        echo 'cmcp '$other_opts' "'$sfile'" "'${node}:$sff'"'
        cmcp $other_opts "$sfile" "${node}:$sff"
        if [ $? -ne 0 ]; then
            ret=1
        fi
    done
done
exit $ret
