#!/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.02 - 5th May 1999

use strict;

require 5.004;

use diagnostics;

use Gedcom 1.02;

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

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...\n";
  return unless $ged->validate;
  print "normalising dates...\n";
  $ged->normalise_dates("%E %b %Y");
# $ged->normalise_dates;
  print "renumbering...\n";
  $ged->renumber;
  print "ordering...\n";
  $ged->order;
  if (0)
  {
    print "adding rins...\n";
    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 "validating...\n";
  return unless $ged->validate;
  print "writing...\n";
  $ged->write("$gedcom_file.new");
}
