Stefan Seyfried <seife+obs@b1-systems.com>
[vpnc.git] / makeman.pl
blobb0de2780e5c859fd74b191dcd2a8c3d183ac09c1
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 $date = strftime( '%B %Y', localtime );
97 open my $VERSION, '<', './VERSION';
98 my $vpnc_version = <$VERSION>;
99 close $VERSION;
100 chomp $vpnc_version;
102 open my $TEMPLATE, '<', './vpnc.8.template';
103 open my $MANPAGE , '>', './vpnc.8';
104 my $magic_found;
105 my $MAGIC_FOR_HEADER = qq(.\\" ###makeman.pl: Replace header here!\n);
106 my $MAGIC_FOR_OPTIONS = qq(.\\" ###makeman.pl: Insert options from help-output here!\n);
108 # Skip the template-header
109 while (<$TEMPLATE>) {
110 last if ($magic_found = ($_ eq $MAGIC_FOR_HEADER));
112 die "$0: Missing magic: $MAGIC_FOR_HEADER" if not $magic_found;
114 print {$MANPAGE} <<"END_MANPAGE_HEADER";
115 .\\" This manpage is generated!
116 .\\" Please edit the template-file in the source-distribution only.
117 .TH VPNC "8" "$date" "vpnc version $vpnc_version" "System Administration Utilities"
118 END_MANPAGE_HEADER
120 $magic_found = 0;
122 while (<$TEMPLATE>) {
123 if ($_ ne $MAGIC_FOR_OPTIONS) {
124 print {$MANPAGE} $_;
125 } else {
126 print {$MANPAGE} $vpnc_options;
127 $magic_found = 1;
130 die "$0: Missing magic: $MAGIC_FOR_OPTIONS" if not $magic_found;
132 close $TEMPLATE;
133 close $MANPAGE;