#!/usr/bin/perl
while (my $line = <>) { 
    if($w) { 
        $line =~ /^\[(\d+)\]\s+(.*)$/; $footnote[$index{$1}] = $2;
    } else { 
        $w = 1 if $line =~ /^\@footnotes/;
        @hits = $line =~ /\[(\d+)\]/g; 
        foreach my $hit (@hits) { 
            if( not defined $index{$hit}) { $index{$hit} = ++$i }; 
            $line =~ s/\[$hit\]/\[$index{$hit}\]/g;
        }
        print $line;
    }
}
for my $i (1..$#footnote+1) { 
    printf( "[%d] %s\n", $i, $footnote[$i] ? $footnote[$i] : 'not defined' ) if defined($footnote[$i]) or defined($index{$i}) 
}
