]> git.pond.sub.org Git - empserver/blob - info/mksubj.pl
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / info / mksubj.pl
1 #!/usr/bin/perl
2 #
3 #  Empire - A multi-player, client/server Internet based war game.
4 #  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
5 #                           Ken Stevens, Steve McClure
6 #
7 #  This program 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 2 of the License, or
10 #  (at your option) any later version.
11 #
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.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 #
21 #  ---
22 #
23 #  See files README, COPYING and CREDITS in the root of the source
24 #  tree for related information and legal notices.  It is expected
25 #  that future projects/authors will amend these files as needed.
26 #
27 #  ---
28 #
29 #  mksubj.pl: Create the index for a subject
30 #
31 #  Known contributors to this file:
32 #     Ken Stevens (when it was still info.pl)
33 #     Markus Armbruster, 2006
34 #
35
36 # Usage: mksubj.pl OUTFILE INFILE...
37 # The INFILE... contain all the topics belonging to a subject.  Read
38 # and check the information required for the index from them, write
39 # the index to OUTFILE.
40
41 use strict;
42 use warnings;
43
44 # The chapters, in order
45 our @Chapters = qw/Introduction Concept Command Server/;
46
47 our $filename;
48 my (%subject, %level, %desc);
49 my $largest = "";
50
51 my $out = shift @ARGV;
52 $out =~ /([^\/]*)\.t$/
53     or die "Strange subject file name $out";
54 my $subj = $1;
55
56 for (@ARGV) {
57     my ($topic, $chap, $lvl, $desc) = parse_file($_);
58     $largest = $topic if length $topic > length $largest;
59     $subject{$chap} .= "$topic\n";
60     $level{$topic} = $lvl;
61     $desc{$topic} = $desc;
62 }
63
64 open(SUBJ, ">$out")
65     or die "Can't open $out for writing: $!";
66
67 print SUBJ '.\" DO NOT EDIT THIS FILE.  It was automatically generated by mksubj.pl'."\n";
68 print SUBJ ".TH Subject \U$subj\n";
69 $largest =~ s/-/M/g;
70 print SUBJ ".in \\w'$largest", "XX\\0\\0\\0\\0'u\n";
71
72 for my $chap (@Chapters) {
73     next unless exists $subject{$chap};
74     print SUBJ ".s1\n";
75     for (split(/\n/, $subject{$chap})) {
76         print SUBJ ".L \"$_ ";
77         if ($level{$_} eq 'Basic') {
78             print SUBJ "* \"\n";
79         } else {
80             print SUBJ "  \"\n";
81         }
82         print SUBJ "$desc{$_}\n";
83     }
84 }
85
86 print SUBJ <<EOF;
87 .s1
88 .in 0
89 For info on a particular subject, type "info <subject>" where <subject> is
90 one of the subjects listed above.  Subjects marked by * are the most
91 important and should be read by new players.
92 EOF
93 close SUBJ;
94
95
96 # Check .TH, .NA, .LV and .SA.
97 # Parse .NA into %desc and .SA into %see_also
98 sub parse_file {
99     ($filename) = @_;
100     my ($topic, $chap, $lvl, $desc);
101
102     $topic = $filename;
103     $topic =~ s,.*/([^/]*)\.t$,$1,;
104
105     open(F, "<$filename")
106         or die "Can't open $filename: $!";
107
108     $_ = <F>;
109     if (/^\.TH (\S+) (\S.+\S)$/) {
110         if (!grep(/^$1$/, @Chapters)) {
111             error("First argument to .TH was '$1', which is not a known chapter");
112         }
113         $chap = $1;
114         if ($1 eq "Command" && $2 ne "\U$topic") {
115             error("Second argument to .TH was '$2' but it should be '\U$topic'");
116         }
117     } else {
118         error("The first line in the file must be a .TH request");
119     }
120
121     $_ = <F>;
122     if (/^\.NA (\S+) "(\S.+\S)"$/) {
123         if ($topic ne $1) {
124             error("First argument to .NA was '$1' but it should be '$topic'");
125         }
126         $desc = $2;
127     } else {
128         error("The second line in the file must be a .NA request");
129     }
130
131     $_ = <F>;
132     if (/^\.LV (\S+)$/) {
133         if ($1 ne 'Basic' && $1 ne 'Expert') {
134             error("The argument to .LV was '$1' but it must be either 'Basic' or 'Expert'");
135         }
136         $lvl = $1;
137     } else {
138         error("The third line in the file must be a .LV request");
139     }
140
141     close F;
142
143     return ($topic, $chap, $lvl, $desc);
144 }
145
146 # Print an integrity error message and exit with code 1
147 sub error {
148     my ($error) = @_;
149
150     print STDERR "info.pl:$filename:$.: $error\n";
151     exit 1;
152 }