#!/usr/bin/perl

use strict;
&diffcp($ARGV[0], $ARGV[1]);

sub diffcp {
    my($from, $into) = @_;
    if (-d $from) {
	if (! -e $into) {
	    print STDERR "Directory `$into' does not exist\n";
	    exit -1;
	}
	foreach my $name (`ls $from`) {
	    chop $name;
	    next if $name =~ /~$/;
	    next if $name =~ /^#.*#$/;
	    &diffcp("$from/$name", "$into/$name");
	}
    }
    elsif (-f $from) {
	my $rc = 1;
	$rc = system "diff -q $from $into" if -e $into;
	if ($rc) {
	    system "cp $from $into" || die "Cannot cp `$from' to `$into'";
	    system "touch .diffcp_last_updated";
	}
    }
    else {
	print STDERR "Something wrong with `$from'";
	exit -1;
    }
}
