#!/usr/local/bin/perl -w

# Copyright 1998-1999, Paul Johnson (pjcj@transeda.com)

# This software is free.  It is licensed under the same terms as Perl itself.

# The latest version of this software should be available from my homepage:
# http://www.transeda.com/pjcj

# Version 1.05 - 20th July 1999

use strict;

require 5.005;

# use diagnostics;

use Gedcom 1.05;

use vars qw( $VERSION );
$VERSION = "1.05";

eval "use Date::Manip";
Date_Init("DateFormat=UK") if $INC{"Date/Manip.pm"};

$SIG{__WARN__} = sub { print "\n@_" };

main();

sub main()
{
  my $gedcom_file = shift @ARGV;
  $| = 1;
  print "reading...";
  my $ged = Gedcom->new
  (
    gedcom_file  => $gedcom_file,
#   grammar_file => "gedcom-5.5.grammar",
    callback     => sub { print "." }
  );
  print "\nvalidating...";
  my %x;
  my $vcb = sub
  {
   my ($r) = @_;
   my $t = $r->{xref};
   print "." if $t && !$x{$t}++;
  };
  $ged->validate($vcb);
  print "\nnormalising dates...";
  $ged->normalise_dates("%E %b %Y");
# $ged->normalise_dates;
  print "\nrenumbering...";
  $ged->renumber;
  print "\nordering...";
  $ged->order;
  if (0)
  {
    print "\nadding rins...";
    my $rin = 1;
    for (@{$ged->{record}{children}})
    {
      push @{$_->{children}}, $_->new(tag => "RIN", value => $rin++)
        unless $_->{tag} eq "HEAD" || $_->{tag} eq "TRLR";
    }
  }
  $ged->unresolve_xrefs;
  print "\nvalidating...";
  $ged->validate;
  print "\nwriting...";
  $ged->write("$gedcom_file.new");
}
