| 1 |
#!/usr/local/bin/perl -w |
| 2 |
use strict; |
| 3 |
my $sec; |
| 4 |
my %cat; |
| 5 |
my @cat; |
| 6 |
while (<>) |
| 7 |
{ |
| 8 |
if (/^beginBookmark\s+'(.*)'\s*$/) |
| 9 |
{ |
| 10 |
$sec = $1; |
| 11 |
unless (exists $cat{$sec}) |
| 12 |
{ |
| 13 |
$cat{$sec} = []; |
| 14 |
push(@cat,$sec); |
| 15 |
} |
| 16 |
} |
| 17 |
elsif (/^file\s+(\S+)/) |
| 18 |
{ |
| 19 |
my $file = $1; |
| 20 |
push(@{$cat{$sec}},$file); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
$^I = ".bak"; |
| 25 |
foreach my $sec (@cat) |
| 26 |
{ |
| 27 |
foreach my $file (@{$cat{$sec}}) |
| 28 |
{ |
| 29 |
my $undo = 0; |
| 30 |
$/ = ""; |
| 31 |
@ARGV = ($file); |
| 32 |
while (<>) |
| 33 |
{ |
| 34 |
if (/^=head1\sNAME/) |
| 35 |
{ |
| 36 |
do { |
| 37 |
print if (defined $^I); |
| 38 |
$_ = <>; |
| 39 |
} until (eof || /^=\w+/); |
| 40 |
if (/^=for\s+category\s+(.*)\n+/) |
| 41 |
{ |
| 42 |
if ($1 eq $sec) |
| 43 |
{ |
| 44 |
warn "$file okay\n"; |
| 45 |
$undo = 1; |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
warn "Change $file $1 -> $sec\n"; |
| 50 |
$_ = "=for category $sec\n\n"; |
| 51 |
} |
| 52 |
} |
| 53 |
else |
| 54 |
{ |
| 55 |
warn "$_ Add $file -> $sec\n"; |
| 56 |
print "=for category $sec\n\n"; |
| 57 |
} |
| 58 |
} |
| 59 |
print if (defined $^I); |
| 60 |
} |
| 61 |
if ($undo && defined $^I) |
| 62 |
{ |
| 63 |
unlink($file); |
| 64 |
rename("$file$^I",$file); |
| 65 |
} |
| 66 |
} |
| 67 |
} |