#!/usr/bin/perl
# -*-Fundamental-*-


@binary_exts = split(/\n/, <<EOFEOFEOF);
img
tst
crt
exe
a
tdt
stg
mcp
opt
EOFEOFEOF

$binary_exts = join("|", @binary_exts);

# $Id: //guest/hari_krishna_dara/perforce/utils/cvs2p4/bin/srcdiff#1 $

select STDERR; $| = 1; select STDOUT; $| = 1;

use Carp;
$| = 1;

($Myname = $0) =~ s%^.*/%%;

$Usage = <<LIT;
$Myname: usage:
LIT


sub usage
{
  print STDERR $Usage;
  exit 1;
}


sub help
{
  print STDERR <<LIT;
$Usage
LIT
  exit 1;
}

$First = ".";
$Go = 1;


sub same
{
  my ($f1, $f2) = @_;

  if ($f2 eq "MibObjs.h") { $f1 =~ s/\/[^\/]*$/MibObjs.h/; }

  if ((! (-T $f1 && -T $f2)) || $f1 =~ /\.($binary_exts)\$/ || $f1 =~ /mibobs.h$/i)
    {
      my $cmd = "/usr/bin/cmp $f1 $f2";
#      print STDERR "$Myname: binary compare: $cmd\n";
      return ! (system $cmd);
    }


#  print STDERR "$Myname: text compare: $f1 $f2\n";


  if (! open(Y, "<$f1"))
    { print STDERR "$Myname: can't open \"$f1\": $!\n"; return 0; }
  if (! open(T, "<$f2"))
    { close Y; print STDERR "$Myname: can't open \"$f2\": $!\n"; return 0; }

  $tstash = "";
  yline: while (<Y>)
    {
      $y_ = $_;
      if ($tstash)
        { $t = $tstash; $tstash = ""; }
      else
        { $t = <T>; }
      $t_ = $t;
      
      $y_ =~ s/\$(Author|Date|Header|Id|Locker|Log|Name|RCSfile|Revision|Source|State).*\$/\$XXX\$/g;
      $t_ =~ s/\$(Author|Date|Header|Id|Locker|Log|Name|RCSfile|Revision|Source|State).*\$/\$XXX\$/g;

      if ($y_ ne $t_) { close Y; close T; return 0; }

      if ($t =~ /\$Log[^\$]*\$/)
        {
          <T>; 
          while ($t_ = <T>)
            {
              if ($t_ =~ /Revision/ || $t_ =~ /\*\//)
		 { $tstash = $t_; next yline; }
            }
        }

    }

  $t_ = <T>;

  close Y; close T;
  if ($t_ eq "") { return 1; } else { return 0; } }


sub traverse
{
  local($dir, $lev, $onfile, $ondir, $onsymlink) = @_;
  
  local($dirent);
  local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
       $atime,$mtime,$ctime,$blksize,$blocks);

  local($dirhandle) = "dh$lev";

  opendir($dirhandle, $dir);

  while (($dirent = readdir($dirhandle)))
    {
      if ($dirent eq "." || $dirent eq "..") { next; }
      ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
       $atime,$mtime,$ctime,$blksize,$blocks) = lstat("$dir/$dirent");
      typsw:
        {
          -f _ && do
            { 
              if ($Go && defined(&$onfile)) { &$onfile("$dir", "$dirent", $lev); }
              last typsw;
            } ;
          -d _ && do
            {
              if (defined(&$ondir)) { &$ondir("$dir", "$dirent", $lev); }
              if ($lev == 0 && $dirent =~ /$First/) { $Go = 1; }
              if ($Go) { do traverse("$dir/$dirent", $lev+1, $onfile, $ondir, $onsymlink) if -d _; }
              last typsw;
            } ;
          -l "$dir/$dirent" && do
            {
              if ($Go && defined(&$onsymlink)) { &$onsymlink("$dir", "$dirent", $lev); }
              last typsw;
            } ;
        }
    }
  closedir($dirhandle);
}


$Ndiff = 0;
$Nchecked = 0;

sub dir
{
  my($dir, $file, $lev) = @_;

  if ($file eq "CVS") { return; }

  my($path) = "$dir/$file";
  $path =~ s/^\.\///;
  my($rpath) = "$Here/$path";

  if ($lev < $Tracelev) { printf STDERR "$Myname: checking in $path\n"; } }


sub check
{
  my($dir, $file, $lev) = @_;

  my $dirbase;
  ($dirbase = $dir) =~ s%^.*/%%;

  if ($dirbase eq "CVS") { return; }
  if ($file =~ /\.o$/) { return; }

  my($path) = "$dir/$file";
  $path =~ s/^\.\///;

  my($rpath) = "$p4dir/$path";

#print "check <$path> <$rpath>\n";

  if (! -e $rpath)
    {
      print STDOUT "$Myname: *** Missing: $rpath\n";
      $Ndiff++;
    }
  elsif (! &same("$rpath", "$path"))
    {
      print STDOUT "$Myname: different: $rpath\n";
      $Ndiff++;
    }

  $Nchecked++;
  if (($Nchecked % $Repint) == 0) { &report; } }


sub ts
{
  my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  return sprintf("%04d/%02d/%02d_%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec); }


sub report
{ printf STDERR "$Myname: %s checked $Nchecked files; found $Ndiff diffs.\n", &ts; }


sub signoff
{
  open(LAZ, ">>.lazurus") || die "Can't open \">>.lazurus\": $!";
  printf LAZ "%s $Username signoff\n", &ts;
  close LAZ;
  exit 0;
}


# option switch variables get defaults here...

(@pwent) = getpwuid($<);
if ($#pwent < 7)
  {
    print STDERR "$Myname: can't get your passwd file entry.\n";
    exit 1;
  }
$Username = $pwent[0];

$p4dir = "";
$cvsdir =".";
$Repint = 1000;
$Tracelev = 0;

while ($#ARGV >= 0)
  {
    if ($ARGV[0] eq "-boolopt")    { $Boolopt = 1; shift; next; }
    elsif ($ARGV[0] eq "-repint")
      {
        shift; if ($ARGV[0] < 0) { &usage; }
        $Repint = $ARGV[0]; shift; next;
      }
    elsif ($ARGV[0] eq "-tracelev")
      {
        shift; if ($ARGV[0] < 0) { &usage; }
        $Tracelev = $ARGV[0]; shift; next;
      }
    elsif ($ARGV[0] eq "-cvsdir")
      {
        shift; if ($ARGV[0] < 0) { &usage; }
        $cvsdir = $ARGV[0]; shift; next;
      }
    elsif ($ARGV[0] eq "-p4dir")
      {
        shift; if ($ARGV[0] < 0) { &usage; }
        $p4dir = $ARGV[0]; shift; next;
      }
    elsif ($ARGV[0] eq "-help")
      { &help; }
    elsif ($ARGV[0] =~ /^-/) { &usage; }
    if ($Args ne "") { $Args .= " "; }
    push(@Args, $ARGV[0]);
    shift;
  }

if (! $p4dir) { &usage; }

$Here = `/bin/pwd`; chop $Here;

print STDERR "$Myname: starting...\n";

chdir "$cvsdir" || die "Can't chdir $cvsdir: $!";

&traverse(".", 0, "check", "dir", undef);

&report;

print STDERR "$Myname: done.\n";