#!/usr/bin/perl

use strict;
use threads;

# the list of grid5000 sites to synchronize with
#my @sync_sites = ("portoalegre.grenoble","bordeaux","grenoble","frontend.lille","nancy","orsay","rennes","sophia","toulouse");
my @sync_sites = ("bordeaux","grenoble","frontend.lille","nancy","orsay","rennes","sophia","toulouse");

# a string with space separated list of files and/or directories to synchronize
my $sync_paths = ".ssh/authorized_keys .ssh/id_dsa .ssh/id_dsa.pub bin/ .emacs .bash_profile .bashrc .inputrc .screenrc images .local .config .execo_conf.py oar_key oar_key.pub";

my $sync_ok = 1;
my @rsync_threads;
for (my $i = 0 ; $i < @sync_sites ; $i++) {
    print "sync $sync_sites[$i]\n";
    $rsync_threads[$i] = threads->create(sub {
        return system("cd ; nice -n 1 rsync -e 'ssh -qo BatchMode=yes -o PasswordAuthentication=no -o StrictHostKeyChecking=no' --delete -aqzR $sync_paths $_[0]:");
    },$sync_sites[$i]);
}
for (my $i = 0 ; $i < @sync_sites ; $i++) {
    if (! $rsync_threads[$i]) {
        print "error spawning sync thread for $sync_sites[$i]\n";
        $sync_ok=0;
    }
    my $thread_retval = $rsync_threads[$i]->join;
    if ($thread_retval != 0 ) {
        print "error sync $sync_sites[$i] (sync process returned $thread_retval)\n";
        $sync_ok=0;
    }
}
if (!$sync_ok) {
    exit 1;
}
exit 0;
