3 # Empire - A multi-player, client/server Internet based war game.
4 # Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
5 # Ken Stevens, Steve McClure, Markus Armbruster
7 # Empire is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # See files README, COPYING and CREDITS in the root of the source
23 # tree for related information and legal notices. It is expected
24 # that future projects/authors will amend these files as needed.
28 # mksubj.pl: Update the subject index pages
30 # Known contributors to this file:
31 # Ken Stevens (when it was still info.pl)
32 # Markus Armbruster, 2006-2013
34 # Usage: mksubj.pl SUBJECT... INFO-FILE...
36 # Read the INFO-FILE..., update info/SUBJECT.t for each SUBJECT.
41 # The chapters, in order
42 my @Chapters = qw/Introduction Concept Command Server/;
44 my @Levels = qw/Basic Expert Obsolete/;
46 # $Subjects{SUBJECT} is a reference to an an anonymous array
47 # containing SUBJECT's topics
50 # $filename{TOPIC} is TOPIC's file name
52 # $lines{TOPIC} is the number of lines in $filename{TOPIC}
54 # $chapter{TOPIC} is TOPIC's chapter (first arg to .TH)
56 # $desc{TOPIC} is a one line description of TOPIC (second arg to .NA)
58 # $level{TOPIC} is TOPIC's difficulty level (arg to .LV)
64 while ($#ARGV >= 0 && $ARGV[0] !~ /\.t$/) {
65 $Subjects{shift @ARGV} = [];
69 $filename{fn2topic($_)} = $_;
76 for (keys %Subjects) {
82 $fn =~ s,.*/([^/]*)\.t$,$1,;
87 # Set $filename, $lines{TOPIC}, $chapter{TOPIC}, $desc{TOPIC},
92 my $topic = fn2topic($filename);
95 or die "Can't open $filename: $!";
98 if (/^\.TH (\S+) (\S.+\S)$/) {
99 if (!grep(/^$1$/, @Chapters)) {
100 error("First argument to .TH was '$1', which is not a known chapter");
102 $chapter{$topic} = $1;
103 if ($1 eq "Command" && $2 ne "\U$topic") {
104 error("Second argument to .TH was '$2' but it should be '\U$topic'");
107 error("The first line in the file must be a .TH request");
111 if (/^\.NA (\S+) "(\S.+\S)"$/) {
113 error("First argument to .NA was '$1' but it should be '$topic'");
117 error("The second line in the file must be a .NA request");
121 if (/^\.LV (\S+)$/) {
122 if (!grep(/^$1$/, @Levels)) {
123 error("The argument to .LV was '$1', which is not a known level");
127 error("The third line in the file must be a .LV request");
135 if (/^\.SA "([^\"]*)"/) {
136 parse_see_also($topic, $1);
138 error("Incorrect .SA Syntax. Syntax should be '.SA \"item1, item2\"'");
142 error("Multiple .SA requests. Each file may contain at most one.") if /^\.SA/;
145 error(".SA request is missing");
153 my ($topic, $sa) = @_;
154 my $wanted = $chapter{$topic};
155 my $found; # found a subject?
157 $wanted = undef if $wanted eq 'Concept' or $wanted eq 'Command';
159 for (split(/, /, $sa)) {
160 next if exists $filename{$_};
161 error("Unknown topic $_ in .SA") unless exists $Subjects{$_};
162 push @{$Subjects{$_}}, $topic;
164 if ($wanted && $_ eq $wanted) {
169 error("No subject listed in .SA") unless $found;
170 error("Chapter $wanted not listed in .SA") if $wanted;
173 # Update a Subject.t file
176 my $fname = "info/$subj.t";
177 my @topics = @{$Subjects{$subj}};
179 my ($any_topic, $any_basic, $any_obsolete, $any_long);
182 for my $topic (@topics) {
183 $largest = $topic if length $topic > length $largest;
186 $out .= '.\" DO NOT EDIT THIS FILE. It was automatically generated by mksubj.pl'."\n";
187 $out .= ".TH Subject \U$subj\n";
189 $out .= ".in \\w'$largest" . "XX\\0\\0\\0\\0'u\n";
191 for my $chap (@Chapters) {
193 for my $topic (@topics) {
195 next if $chapter{$topic} ne $chap;
196 $out .= ".s1\n" if $empty;
199 if ($level{$topic} eq 'Basic') {
203 if ($level{$topic} eq 'Obsolete') {
207 if ($lines{$topic} > 300) {
208 # TODO use formatted line count
212 $flags = sprintf("%-2s", $flags);
213 $out .= ".L \"$topic $flags\"\n";
214 $out .= "$desc{$topic}\n";
217 unless ($any_topic) {
218 print STDERR "$0: Subject $subj has no topics\n";
223 . "For info on a particular subject, type \"info <subject>\" where <subject> is\n"
224 . "one of the subjects listed above.\n";
225 $out .= "Subjects marked by * are the most important and should be read by new players.\n"
227 $out .= "Subjects marked by + are obsolete.\n"
229 $out .= "Unusually long subjects are marked with a !.\n"
232 return if (same_contents($fname, $out));
233 open(SUBJ, ">$fname")
234 or die "Can't open $fname for writing: $!";
240 my ($fname, $contents) = @_;
243 if (!open(SUBJ, "<$fname")) {
244 return 0 if ($!{ENOENT});
245 die "Can't open $fname for reading: $!";
249 return $contents eq $old;
252 # Print an integrity error message and exit with code 1
256 print STDERR "mksubj.pl:$filename:$.: $error\n";