#!/usr/bin/perl

#NAME
#  rsdir - rsync directories on a list of mirror hosts

#SYNOPSIS
#  rsdir dir...

#DESCRIPTION
#  This MUST be run from $HOME!  (Unless $base is changed below.)

#  This is how I synchronize my files with a number of machines  that
#  I use as mirrors.

#  This program uses rsync to synchronize  the  list  of  directories
#  with the corresponding directories on several other machines.

#SETUP
#  The following things are hard-coded into this script:

#  The list of mirror hosts:
	@hosts =  qw( jc.tzo.net trillian.mit.edu lochaber.tullochgorm.com );

#  The transport protocol is also fixed:
	$ENV{'RSYNC_RSH'} = 'ssh';

# Here is the base path on remote systems:
	$base = '';	# Null for home directory

#OPTIONS

#EXAMPLES

#FILES

#BUGS
#  While the local directories will be rsync'd with  each  host,  the
#  remote hosts will not necessarily be rsync'd with each other. This
#  is order dependent:  host1's files will end up  on  with  all  the
#  hosts  (including  the  local  host).   But  host2's  files  won't
#  necessarily be on host1. To ensure completion, repeat the command.

#SEE ALSO

#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;	# Don't buffer stdio
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || 1;
$home = $ENV{'HOME'} || ".";

print "Password:$pswd\n" if $V>5;
for $dir (@ARGV) {
	print "\n";
	print "==== $dir/\n";
	$XF = "$dir/.rsExclude";
	if      (-f ($XF = "$dir/.rsExclude")) {
		$Xopt = "--exclude-from=$XF"
	} elsif (-f ($XF = ".rsExclude")) {
		$Xopt = "--exclude-from=$XF"
	} elsif (-f ($XF = "$home/.rsExclude")) {
		$Xopt = "--exclude-from=$XF"
	} else {
		$Xopt = '';
	}
	for $hst (@hosts) {
		print "<=== $hst\n";
		$frcmd = "rsync -Cvurptz $Xopt $hst:$base$dir/ $dir/";
		system $frcmd;
		print "===> $hst\n";
		$tocmd = "rsync -Cvurptz $Xopt $dir/ $hst:$base$dir/";
		system $tocmd;
	}
}
