#!/usr/local/bin/perl5
##
##       This utility is part of AIPS++, a software project centered at
##       the National Radio Astronomy Observatory.
##
##       Copyright (C) 1995,1997,1998,1999,2000
##       Associated Universities, Inc. Washington DC, USA.
## 
##       This utility is free software; you can redistribute it and/or modify
##       it under the same terms as Perl itself, i.e., under the terms of the
##       "Artistic License" or the "GNU General Public License". 
##
## This script compares DBM checkpoints created by patch_chkpoint. It requires
## two directories which contain the release checkpoints to be compared. The
## second must be newer than the first:
##
##	patch_diff /the/older/dir /the/newer/dir
##
## The differences are deposited in the newer directory.
##
BEGIN {@AnyDBM_File::ISA = qw(GDBM_File NDBM_File DB_File SDBM_File ODBM_File);}
use AnyDBM_File;
use POSIX;

#http://www.engelschall.com/u/sb/download/Date-Calc/
use Date::Calc qw( Delta_DHMS Decode_Date_EU );

$first = shift(@ARGV);
$second = shift(@ARGV);

die "no (1st) input directory specified" unless $first;
die "no (2nd) input directory specified" unless $second;

die "can't attach to \"$first/objects\", $!"
  unless tie( %objs1, AnyDBM_File, "$first/objects", O_RDONLY, 0644);
die "can't attach to \"$first/symbols\", $!"
  unless tie( %symbols1, AnyDBM_File, "$first/symbols", O_RDONLY, 0644);
die "can't attach to \"$second/objects\", $!"
  unless tie( %objs2, AnyDBM_File, "$second/objects", O_RDONLY, 0644);
die "can't attach to \"$second/symbols\", $!"
  unless tie( %symbols2, AnyDBM_File, "$second/symbols", O_RDONLY, 0644);

die "can't attach to \"$second/delta\", $!"
  unless tie( %delta, AnyDBM_File, "$second/delta", O_RDWR | O_CREAT, 0644);
%delta=();

foreach $symbol ( keys %symbols1 ) {

    ## symbol removed, we don't note this because we assume no binary will
    ## continue to reference it (though we could flag this as an error).
    ! defined $symbols2{$symbol} && next;

    die "mismatch between symbol and object tables, $symbol"
      unless $info1 = $objs1{$symbols1{$symbol}};
    die "mismatch between symbol and object tables"
      unless $info2 = $objs2{$symbols2{$symbol}};

    ($size1, $date1, $time1) = split( /\s+/, $info1 );
    ($size2, $date2, $time2) = split( /\s+/, $info2 );

    die "bad date string, \"$date1\"" unless
      ($year1, $month1, $day1) = Decode_Date_EU($date1);
    die "bad date string, \"$date2\"" unless
      ($year2, $month2, $day2) = Decode_Date_EU($date2);

    die "bad time string, \"$time1\"" unless $time1 =~ m/^(\d+):(\d+)$/;
    $hour1 = $1;
    $minute1 = $2;

    die "bad time string, \"$time2\"" unless $time2 =~ m/^(\d+):(\d+)$/;
    $hour2 = $1;
    $minute2 = $2;

    ($dDays, $dHours, $dMinutes, $dSeconds) = Delta_DHMS( $year1, $month1, $day1, $hour1, $minute1, "30",
							  $year2, $month2, $day2, $hour2, $minute2, "30" );

    die "found timestamp in 2nd checkpoint earlier than its mate in the 1st" unless
      $dDays >= 0 && $dHours >= 0 && $Dminutes >= 0;

    $delta{$symbol} = $symbols2{$symbol} if
      $dDays > 0 || $dHours > 0 || $dMinutes > 0;

}

foreach $symbol ( keys %symbols2 ) {

    # symbol created
    ! defined $symbols1{$symbol} && ( $delta{$symbol} = $symbols2{$symbol}, next );

}

untie( %objs1 );
untie( %symbols1 );
untie( %objs2 );
untie( %symbols2 );
untie( %delta );
