#!/usr/local/bin/perl
if ($#ARGV <= 0 ) {
    die;
} elsif($#ARGV == 1 && -f $ARGV[0] && -f $ARGV[1]) {
    &move_if_changed($ARGV[0],$ARGV[1]);
} else {
    $target_dir = pop(@ARGV);
    while ($f = shift(@ARGV)) {
	&move_if_changed($f, "$target_dir/$f");
    }
}

sub move_if_changed {
    my($s,$t) = @_;
    if(-f $s) {
	if( -f $t) {
	    if(-M $s < -M $t) { 
		if(system("cmp -s $s $t")) {
		    print "move $s $t\n";
		    rename($s,$t);
		} else {
		    #print "$t is not changed\n";
		}
	    } else {
		#print "$s is older than $t\n";
	    }
	} else {
	    print "move $s $t\n";
	    rename($s,$t);
	}
    }
}
