#!/usr/bin/perl

# Takes two arguments, the paths to the tops of the two directory trees

sub seeya {
  print @_, "\n";
  exit(1);
}

if(scalar(@ARGV) != 2) { 
  seeya "Need two arguments";
}

$d1 = $ARGV[0];
$d2 = $ARGV[1];

(-d $d1) or seeya "$d1 is not a directory";
(-d $d2) or seeya "$d2 is not a directory";
 
sub myrd {
  my $md = shift;
  my @allf;

  opendir TD, $md or die "Hey";
# Skip . and ..
  @allf = grep !/^\.\.?/, readdir(TD);
  closedir TD;
# Skip emacs backups
  @allf = grep !/~$/, @allf;
# Skip web backups
  @allf = grep !/\.bak$/, @allf;
# Skip .def backups
  @allf = grep !/\.def$/, @allf;
}
  
sub dodir {
  my $cdir = shift;
  my @cf = myrd("$d1/$cdir");
  my $ff;
  for $ff (@cf) {
		# Am I looking at a link?
		if(-l "$d1/$cdir/$ff") { next;}
    # Does it exist?
    if(not (-e "$d2/$cdir/$ff")) {
      print "$ff does not exist in $d2/$cdir\n";
      next;
    }
    # Is it a directory?
    if(-d "$d1/$cdir/$ff") {
      # Yes, recurse into it
      dodir("$cdir/$ff");
    } else {
      # it is a file, do the diff
			my $dff = `diff $d1/$cdir/$ff $d2/$cdir/$ff`;
			if($dff) {
				print "Versions of $cdir/$ff differ - ";
				my ($t1, $t2);
				$t1 = (-M "$d1/$cdir/$ff");
				$t2 = (-M "$d2/$cdir/$ff");
				my $who = ($t1<$t2) ? "$d1/$cdir" : "$d2/$cdir";
				print "$who is newer\n";
			}
    }
  }

    
}

dodir(".");

exit(0);

@allf = myrd($mdd);

for $f (@allf) {
  if(-d $f) { print "d ";} else {print "  ";}
  if(-l $f) { print "l ";} else {print "  ";}
  if($f eq ".") {print " is dot";}
  if($f eq "..") {print " is dot dot";}
  print "$f ";
  print "\n";
}
