#!/bin/sh -e

##########################################################################
#   Script description:
#       
#   Arguments:
#       
#   Returns:
#       
#   History:
#   Date        Name        Modification
#   2014-01-01  root        Begin
##########################################################################

usage()
{
    printf "Usage: $0 local-path remote-path perms backup|compute|io|vis|all\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

auto-root-check $0
cluster-head-check $0

set +e

if [ $# -lt 4 ]; then
    usage
fi

if ! cluster-check-cron-updates; then
    exit 0
fi

file=$1
dest_dir=$2
perms=$3
shift; shift; shift

node_types="$*"
if [ "$node_types" = all ]; then
    node_types="backup compute io vis"
fi

for node_type in $node_types; do
    for node in `cluster-$node_type-nodes`; do
	printf "$node...\n"
	scp $file ${node}:$dest_dir
	ssh $node chmod $perms $dest_dir/`basename $file`
    done
done

