-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-reference-list.pl
executable file
·94 lines (77 loc) · 2.61 KB
/
build-reference-list.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env perl
# Breaks a NAVADMIN record message (passed as a filename on the command line)
# into a header and then a main body.
use v5.28;
use feature 'signatures';
use lib 'modules';
use MsgReader qw(read_navadmin_from_file);
use List::Util qw(first);
use Mojo::JSON qw(encode_json);
# Get list of all available NAVADMINs, preferring .ctxt version if available
my @files = Mojo::File->new('NAVADMIN')
->list
->grep(qr/\.txt$/)
->map(sub ($f) {
my $base = $f->basename('.txt');
my $simplified = $f->sibling("$base.ctxt");
(-e $simplified->to_string)
? $simplified
: $f
;
})
->each;
my %refs = (
NAVADMIN => { },
OPNAVINST => { },
SECNAVINST => { },
MILPERSMAN => { },
BUPERSINST => { },
DODINST => { },
);
# Each key should map to a regex with a capture group that pulls out the
# series/ID of the instruction for the type that corresponds to the key
my %ref_scanners = (
NAVADMIN => qr/^NAVADMIN ([0-9]{3} *[\/-]? *[0-9]{2})\b/,
BUPERSINST => qr/^BUPERSINST *([0-9]{4,5}\.[0-9]{1,2}[A-Z]?)\b/,
OPNAVINST => qr/^OPNAVINST *([0-9]{4,5}\.[0-9]{1,2}[A-Z]?)\b/,
SECNAVINST => qr/^SECNAVINST *([0-9]{4,5}\.[0-9]{1,2}[A-Z]?)\b/,
MILPERSMAN => qr/^MILPERSMAN *([0-9]{4,5}-[0-9]{3})\b/,
DODINST => qr/^(?:DODINST|DEPARTMENT OF DEFENSE INSTRUCTION) *([0-9]{4,5}\.[0-9]{1,2})\b/,
);
foreach my $path (@files) {
say STDERR "Reading $path";
my $data = eval {
read_navadmin_from_file($path);
};
# Filter out issues
if ($@) {
say STDERR "Failed to read from $path: $@";
next;
}
if (!exists $data->{fields} || !exists $data->{fields}->{REF}) {
say STDERR "No references for $path";
next;
}
my $cur_navadmin = $data->{fields}->{NAVADMIN} // '';
if (!$cur_navadmin || $cur_navadmin !~ /[0-9]{3}\/[0-9]{2}\b/) {
say STDERR "Unclear which NAVADMIN $path is";
next;
}
$cur_navadmin =~ s/-/\//;
# Read in references to central dict
foreach my $ref (@{$data->{fields}->{REF}}) {
my $ampn = $ref->{ampn} // undef;
next unless $ampn;
my $dest_ref;
my $dest_dictionary =
first {
($dest_ref) = $ampn =~ $ref_scanners{$_};
} keys %ref_scanners;
next unless $dest_dictionary;
# NAVADMIN 123-92 rather than 123/92
$dest_ref =~ s/-/\// if $dest_dictionary eq 'NAVADMIN';
$refs{$dest_dictionary}->{$dest_ref} //= [ ];
push @{$refs{$dest_dictionary}->{$dest_ref}}, $cur_navadmin;
}
}
say encode_json(\%refs);