Print a warning if an illegal value is used for the spi but continue
[vpnc.git] / makeman.pl
blobcf241a184e96f33aa5492fca45d03d781512c000
1 #! /usr/bin/env perl
3 # $Id$
5 # Written by Wolfram Sang (wolfram@the-dreams.de) in 2007,
6 # some inspiration from help2man by Brendan O'Dea and from Perl::Critic
8 # Generate the vpnc-manpage from a template and the --long-help-output.
9 # Version 0.2
11 # Command-line options: none
12 # Files needed : ./vpnc ./vpnc.8.template ./VERSION
13 # Files created : ./vpnc.8
14 # Exit status : errno-values or 255 (Magic string not found)
16 # Distributed under the same licence as vpnc.
18 use strict;
19 use warnings;
20 use Fatal qw(open close);
21 use filetest qw(access); # to always get errno-values on filetests
22 use POSIX qw(strftime setlocale LC_ALL);
24 my $vpnc = './vpnc';
25 -e $vpnc or die "$0: Can't find $vpnc. Did you compile it?\n";
26 -x $vpnc or die "$0: Can't execute $vpnc. Please check permissions.\n";
28 # The code converting the help-output to manpage format is lots of
29 # regex-fiddling, sorry. It got a bit more complicated by additionally
30 # indenting lists (those originally starting with an asterisk). I hope
31 # this pays off when converting the manpage to HTML or such.
33 open my $LONGHELP, '-|', "$vpnc --long-help";
34 my $vpnc_options = '';
35 my $relative_indent = 0;
36 my $indent_needed = 0;
38 while (<$LONGHELP>) {
39 if (/^ /) {
41 # Check if additional indent needs to be finished by comparing the
42 # amount of spaces at the beginning. A bit ugly, but I don't see a
43 # better way to do it.
44 if ($relative_indent) {
45 /^( *)/;
46 if (length($1) < $relative_indent) {
47 $vpnc_options .= ".RE\n";
48 $relative_indent = 0;
49 $indent_needed = 1;
53 # Highlight the option and make an optional argument italic.
54 if (s/^ *(--[\w-]+)/\n.TP\n.BI "$1"/) {
55 s/(<.+>)/ " $1"/;
58 # Highlight conffile-only options.
59 s/^ *(\(configfile only option\))/\n.TP\n.B $1/;
61 # Position the Default-string
62 s/^ *(Default:)/.IP\n$1/;
64 # Highlight the conf-variable and make an optional argument italic.
65 if (s/^ *(conf-variable:) (.+?) ?([<\n])/.P\n$1\n.BI "$2"$3/) {
66 s/(<.+>)/ " $1"/;
69 # Replace asterisk with bulletin; indent if needed.
70 if (s/^( +)\* /.IP \\(bu\n/) {
71 if (not $relative_indent) {
72 $vpnc_options .= ".RS\n";
73 $relative_indent = length $1;
77 # Do we need to add an .IP-command after .RE or is there already one?
78 if ($indent_needed and not /^\n?\.[TI]?P/) {
79 $vpnc_options .= ".IP\n";
80 $indent_needed = 0;
83 # Finalize string and add it to buffer
84 s/^ *//;
85 s/ *$//;
86 s/-/\\-/g;
87 $vpnc_options .= $_;
90 close $LONGHELP;
92 # Hopefully the code speaks for itself from now on...
94 setlocale( LC_ALL, 'C' );
95 my $write_secs = (stat("./vpnc.8.template"))[9];
96 my $date = strftime( '%B %Y', localtime($write_secs) );
98 open my $VERSION, '<', './VERSION';
99 my $vpnc_version = <$VERSION>;
100 close $VERSION;
101 chomp $vpnc_version;
103 open my $TEMPLATE, '<', './vpnc.8.template';
104 open my $MANPAGE , '>', './vpnc.8';
105 my $magic_found;
106 my $MAGIC_FOR_HEADER = qq(.\\" ###makeman.pl: Replace header here!\n);
107 my $MAGIC_FOR_OPTIONS = qq(.\\" ###makeman.pl: Insert options from help-output here!\n);
109 # Skip the template-header
110 while (<$TEMPLATE>) {
111 last if ($magic_found = ($_ eq $MAGIC_FOR_HEADER));
113 die "$0: Missing magic: $MAGIC_FOR_HEADER" if not $magic_found;
115 print {$MANPAGE} <<"END_MANPAGE_HEADER";
116 .\\" This manpage is generated!
117 .\\" Please edit the template-file in the source-distribution only.
118 .TH VPNC "8" "$date" "vpnc version $vpnc_version" "System Administration Utilities"
119 END_MANPAGE_HEADER
121 $magic_found = 0;
123 while (<$TEMPLATE>) {
124 if ($_ ne $MAGIC_FOR_OPTIONS) {
125 print {$MANPAGE} $_;
126 } else {
127 print {$MANPAGE} $vpnc_options;
128 $magic_found = 1;
131 die "$0: Missing magic: $MAGIC_FOR_OPTIONS" if not $magic_found;
133 close $TEMPLATE;
134 close $MANPAGE;