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