#!/bin/sh

# No -e since some rsync commands are expected to fail

# DO NOT sync /etc/passwd, /etc/ssh*, or any other file that may contain
# different information on different node types!

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

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

login_nodes="`cluster-backup-nodes`"
root_nodes="`cluster-backup-nodes` `cluster-file-servers`"
all_nodes="`cluster-all-nodes`"

# Select nodes of each category from list
if [ $# -gt 0 ]; then
    nodes="$@"
    login_nodes=$(printf "%s\n" $login_nodes $nodes | sort | uniq -d)
    root_nodes=$(printf "%s\n" $root_nodes $nodes | sort | uniq -d)
    all_nodes=$(printf "%s\n" $all_nodes $nodes | sort | uniq -d)
fi

echo $login_nodes $root_nodes $all_nodes

printf "limits.d...\n"
for node in $login_nodes; do
    rsync -av /etc/security/limits.d/* ${node}:/etc/security/limits.d
done

printf "root home...\n"
for node in $all_nodes; do
    rsync /root/.bash_profile /root/.bashrc /root/.login /root/.cshrc \
	${node}:/root 2> /dev/null
done
    
# FIXME: Are these ready to merge?
os_type=`auto-ostype`
case $os_type in
RHEL)
    CONF_DIR=$(auto-pkgsrc-prefix)/etc/spcm
    ;;

FreeBSD)
    LOCALBASE=$(cluster-localbase)
    CONF_DIR=$LOCALBASE/etc/spcm
    ;;

*)
    printf "$0: Not supported on $(auto-ostype).\n"
    exit 1
    ;;

esac

for node in $all_nodes; do
    # /etc/hosts
    printf "$node: hosts "
    rsync -pog /etc/hosts ${node}:/etc

    printf "hosts.allow "
    rsync -pog /etc/hosts.allow ${node}:/etc
    
    # Do not sync sshd_config.  Public-facing servers are different than
    # compute nodes.
    printf "ssh_config "
    rsync -pog /etc/ssh/ssh_config ${node}:/etc/ssh
    
    printf "system-auth "
    # RHEL
    rsync -pog /etc/pam.d/system-auth-ac ${node}:/etc/pam.d 2> /dev/null
    # FreeBSD
    rsync -pog /etc/pam.d/passwd ${node}:/etc/pam.d 2> /dev/null
    
    printf "etc/spcm "
    ssh $node mkdir -p $CONF_DIR
    rsync -rpog $CONF_DIR/fstab* $CONF_DIR/head-node ${node}:$CONF_DIR 2> /dev/null
    printf '\n'
done

cd $CONF_DIR
# Should have been done by cluster-init-node
# ssh $node mkdir -p $CONF_DIR
for file in fstab.* site-mods.sh; do
    if [ -e $file ]; then
	scp -p $file ${node}:$CONF_DIR
    fi
done
