add logging of PC92A ip addresses
[spider.git] / perl / hlptohtml.pl
1 #!/usr/bin/perl
2
3 # A program to split out the Command_en.hlp file into two
4 # html documents, one for sysops, one for users
5 #
6 # Copyright (c) - 1999 Dirk Koopman G1TLH
7 #
8 #
9 #
10 require 5.004;
11
12 # search local then perl directories
13 BEGIN {
14         # root of directory tree for this system
15         $root = "/spider"; 
16         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
17         
18         unshift @INC, "$root/perl";     # this IS the right way round!
19         unshift @INC, "$root/local";
20 }
21
22 use strict;
23 use IO::File;
24 use DXVars;
25 use Carp;
26
27 my $lang = 'en';
28 $lang = shift @ARGV if @ARGV;
29
30
31 # read in the help file and key it all up and store it
32 my $h = new IO::File;
33
34 if (!open($h, "$main::localcmd/Commands_$lang.hlp")) {
35         if (!open($h, "$main::cmd/Commands_$lang.hlp")) {
36                 die "cannot open $main::cmd/Commands_$lang.hlp $!";
37         }
38 }
39 my $in;
40 my $state = 0;
41 my $ref;
42 my $text;
43 my %c;
44
45 foreach $in (<$h>) {
46         chomp;
47         next if $in =~ /^\#/;
48         $in =~ s/</\&lt;/g;
49         $in =~ s/>/\&gt;/g;
50         if ($in =~ /^===/) {
51                 $text = [ ] if $state != 1;   # new text reference if not in a list
52                 $in =~ s/=== //;
53                 my ($priv, $cmd, $desc) = split /\^/, $in;
54                 $c{$cmd} = { cmd=>$cmd, priv=>$priv, desc=>$desc, text=> $text };
55                 $state = 1;
56                 next;
57         }
58         if ($state > 0) {
59                 confess "no text!" unless $text;
60             push @$text, $in;
61                 $state = 2;
62         }
63 }
64
65 close($h);
66
67 # At this point we should have a hash containing all the useful info on each command
68
69 # Starting with the user file, open it and copy across the top and tail with the 
70 # <data> tag replaced by this system.
71 #
72
73 my $html = "$main::root/html";
74 my $in = new IO::File "$html/user_$lang\_t.html" or die "can't open $html/user_$lang\_t.html $!";
75 my $out = new IO::File ">$html/user_$lang.html" or die "can't open $html/user_$lang.html $!";
76 chmod 0664, "$html/user_$lang.html";
77
78 # copy until <data> is nigh
79 while (<$in>) {
80         last if /<data>/i;
81         print $out $_;
82 }
83
84 my $c;
85 my $count;
86 my %done;
87
88 foreach $c (sort {$a->{cmd} cmp $b->{cmd}} values %c) {
89         next if $c->{priv};
90         $count++;
91     my $label = "L$count";
92         print $out "<li><a name=\"$label\"><b>$c->{cmd}</b></a> $c->{desc}<br><br>\n";
93         printlines($out, $c->{text});
94 }
95
96 # now copy the rest out
97 while (<$in>) {
98         print $out $_;
99 }
100
101 $in->close;
102 $out->close;
103
104 exit(0);
105
106 sub printlines
107 {
108         my $fh = shift;
109         my $ref = shift;
110
111         my $last;
112         my $state = 0;
113         for (@$ref) {
114                 if ($state == 0) {
115                         if (/^\s+\S+/) {
116                                 print $fh "<pre>\n";
117                                 $state = 1;
118                         }
119                 } else {
120                         unless (/^\s+\S+/) {
121                                 print $fh "</pre>\n";
122                                 $state = 0;
123                         }
124                 }
125                 print $fh $_, " ";
126     
127                 if (/^\s*$/) {
128             if ($last =~ /^\s*$/) {
129                                 print $fh "<br>\n";
130                         } else {
131                                 print $fh "<br><br>\n";
132                         }
133                 }
134                 $last = $_;
135         }
136 #    print $fh "<br>\n";
137 }