#                     HelpDocUpdate.pl
#
# PURPOSE
#
#     Generate a .chm help file from ActivePerl's HTML Documentation
#
# REQUIREMENTS
#
#     Active Perl 5.8 and File::Slurp module
#     MS HTML Help WorkShop
#
# USAGE
#
#   usage: HelpDocUpdate.pl [options] <chapters ...>
#      or: HelpDocUpdate.pl [options] default
#
#   Options:
#
#       --no-strip      Do not remove <?xml version ...>
#                       and <script> tags
#       --no-compile    Only create the .hhp and .hhc file
#       --no-fulltext   Don't create a full text index
#       --verbose       Display compile progress
#       --help          This text
#
#   Chapters: (possible chapters are: ap, core, mods, pragmas, misc)
#
#       ap      ...  Active Perl specific documentation
#       core    ...  Perl core documentation
#       mods    ...  Module documentation
#       pragmas ...  Pragma documentation
#
#       If you use "default" you'll get the following order:
#           1. core  2. mods  3. pragmas  4. ap
#
# CONTACT
#
#   Email to Robert Bachmann <rbach (at) rbach.priv.at>.
#   If you are NOT on his contact list please include
#   "[first contact]" in the Subject line.
#
# VERSION HISTORY
#
#     Version 0.2.0  (2007-08-03)
#         by Robert Bachmann
#         <http://rbach.priv.at/2007/Perl-CHM/>
#
#     Version 0.1? (2007-05-17)
#         by Axel Kollmorgen
#         <http://chm.kollm.org/>
#
#     Version 0.1  (2001-12-21)
#         by Simon Flack
#         <http://www.perlmonks.org/?node_id=133810>
#

use strict;
use warnings;

use File::Find;
use File::Slurp qw(read_file write_file);
use File::Basename;
use Config;
use FindBin;

# Adjust these to your needs
my $HTML_Compiler = Win32::GetShortPathName(
    "$ENV{ProgramFiles}\\HTML Help Workshop\\HHC.EXE");
my $OutFilenames = "Perl-" . $Config{version};
my $HtmlPath     = $Config{installhtmldir};

my $version     = '0.2.0';
my $strip_files = 1;
my $verbose     = 0;
my $fulltext    = 1;
my $compile     = 1;
my $add_notes   = 1;

my ( %dir_map,   @dir_list );
my ( %topic_map, @topics_list );
my (@strip_list);

my @chapters;
my @perl_help_files;
my @perl_core_docs;
my @perl_pragmas = qw(attributes attrs autouse base bigint bignum bigrat
    blib bytes charnames constant diagnostics encoding fields
    filetest if integer less lib locale open ops overload re
    sigtrap sort strict subs threads threads::shared utf8
    vars vmsish warnings warnings::register);

main();

sub main {
    for (@ARGV) {
        if (/^--no-fulltext$/) {
            $fulltext = 0;
        }
        if (/^--no-compile$/) {
            $compile = 0;
        }
        elsif (/^--no-strip$/) {
            $strip_files = 0;
        }
        elsif (/^--verbose$/) {
            $verbose = 1;
        }
        elsif (/^--help$/) {
            show_help();
        }
        elsif ( /^ap$/i || /^core$/i || /^mods$/i || /^pragmas$/i ) {
            push @chapters, lc($_);
        }
        elsif (/^default$/i) {
            push @chapters, qw(core mods pragmas ap);
        }
        else {
            print STDERR "Invalid option $_\nTry $0 --help\n";
            exit 1;
        }
    }

    if ( !@chapters ) {
        print STDERR "No chapters selected\nTry $0 --help\n";
        exit 1;
    }

    chdir($HtmlPath);

    print "Looking for HTML files ...\n";
    find( \&add_html_files, '.' );

    chdir($HtmlPath);

    if ($strip_files) {
        strip_file($_) for (@strip_list);
    }

    $dir_map{''} = '';

    @topics_list = sort keys %topic_map;
    @dir_list    = sort keys %dir_map;

    fix_list();

    # Write the Help Project File
    make_hhp_file( "$OutFilenames.hhp", @perl_help_files );

    # Write the Table of Contents
    make_hhc_file("$OutFilenames.hhc");

    # Compile the project
    make_chm_file("$OutFilenames.hhp") if $compile;

    exit 0;
}

sub show_help {
    print <<"DOC";
usage: $0 [options] <chapters ...>
   or: $0 [options] default

Options:

    --no-strip      Do not remove <?xml version ...> 
                    and <script> tags
    --no-compile    Only create the .hhp and .hhc file
    --no-fulltext   Don't create a full text index
    --verbose       Display compile progress
    --help          This text

Chapters: (possible chapters are: ap, core, mods, pragmas, misc)
    
    ap      ...  Active Perl specific documentation
    core    ...  Perl core documentation
    mods    ...  Module documentation
    pragmas ...  Pragma documentation

    If you use "default" you'll get the following order:
        1. core  2. mods  3. pragmas  4. ap

DOC
    exit;
}

sub add_html_files {
    my $file = $File::Find::name;
    my $dir  = $File::Find::dir;
    my ( $prefix, $suffix );

    $dir =~ s[\\][/]g;
    $dir =~ s[^\./][];

    $file =~ s[\\][/]g;
    $file =~ s[^\./][];

    push @strip_list, $File::Find::name
        if ( $file =~ /(\.html?)$/i );

    return unless $file =~ m<^(lib|site/lib)>i;
    $prefix = $1;

    $dir  =~ s<^(lib|site/lib)><>i;
    $file =~ s<^(lib|site/lib)><>i;

    if ( $dir ne '.' && $dir ne './site' ) {
        $dir_map{$dir} = '';
    }

    # skip core doc files
    if ( $file =~ m[^/Pod/perl] ) {
        push @perl_core_docs, $file;
        return;
    }

    # skip activeperl files
    if ( $file =~ m[^/Pod/activeperl] ) {
        return;
    }

    if ( $file =~ /(\.html?)$/i ) {
        my $suffix = $1;

        $file =~ s<\.html?$><>i;

        # skip pragmas
        if ( $prefix eq 'lib' && $file =~ m<^/[a-z]> ) {
            my $b = basename($file);

            for ( @perl_pragmas, qw(register shared) ) {
                return if ( $b eq $_ );
            }
        }

        $topic_map{$file} = $File::Find::name;
    }
}

sub make_chm_file {
    my $project_file = shift;
    print "Writing Help File... ...\n";
    system( $HTML_Compiler, $project_file );
}

sub make_hhp_file {

    # Generate the Help Project File
    my ( $output_file, @doc_files ) = @_;
    my $display_compile_progress = $verbose  ? 'Yes' : 'No';
    my $full_text_search         = $fulltext ? 'Yes' : 'No';

    print "Writing Project File: $output_file...\n";

    my $hhp = <<EOT;
[OPTIONS]
Auto Index=Yes
Compatibility=1.1 or later
Compiled file=$OutFilenames.chm
Contents file=$OutFilenames.hhc
Display compile progress=$display_compile_progress
Full-text search=$full_text_search
Language=0x809 English (United Kingdom)
Title=Perl $Config{version} Documentation
Default topic=lib\\Pod\\perl.html

[FILES]
lib\\Pod\\perl.html
EOT

    open my $file, ">", $output_file or die "can't write Project file";
    print $file $hhp;
    close $file;
}

sub make_hhc_file {
    my ($hhc_file) = @_;
    print "Building Table of Contents: $hhc_file...\n";

    my @contents;

    push @contents, <<EOT;
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1">
<!-- Sitemap 1.0 -->
</HEAD><BODY>
<UL>
EOT

    for (@chapters) {
        if (/^core$/) {
            print "Generating chapter: core\n";
            push @contents, toc_core_docs();
        }
        elsif (/^ap$/) {
            print "Generating chapter: ap\n";
            push @contents, toc_active_perl();
        }
        elsif (/^pragmas$/) {
            print "Generating chapter: pragmas\n";
            push @contents, help_folder( 'Pragmas', '', toc_pragmas() );
        }
        elsif (/^mods$/) {
            print "Generating chapter: mods\n";
            mark_folder_topics();
            push @contents, help_folder( "Modules", "", walk('') );
        }
    }

    if ($add_notes) {
        write_notes_txt();
        push @contents, help_topic( 'Notes', '_HelpDocUpdate_notes.txt' );
    }

    push @contents, "</BODY></HEAD></HTML>";

    open my $file, ">", $hhc_file or die "can't write Contents file";
    print $file @contents;
    close $file;
}

sub help_topic {

    # Returns the Markup for a help topic
    # Used in the HHC (contents) file
    my ( $name, $url ) = @_;
    return <<HELP_TOPIC;
    <LI> <OBJECT type="text/sitemap">
        <param name="Name" value="$name">
        <param name="Local" value="$url">
    </OBJECT>

HELP_TOPIC

}

sub help_folder {
    no warnings;

    # Returns the Markup for an expandable help folder.
    # Can contain folders and topics, etc...
    # Used in the HHC (contents) file
    my ( $name, $url, @contents ) = @_;

    my $url_ref = qq[ <param name="Local" value="$url">  \n] if $url;
    return <<HELP_FOLDER;
    <LI> <OBJECT type="text/sitemap">
        <param name="Name" value="$name">
        $url_ref
        <param name="ImageNumber" value="1">
        </OBJECT>
        <UL>
            @contents
        </UL>
HELP_FOLDER
}

sub strip_file {

    # remove xml version and <script> tags from an HTML file
    my $filename = shift;

    print "Stripping $filename ...\n";

    my $data = read_file($filename) or warn "r $filename\n";
    $data =~ s{<\?xml[^>]*>\n?}{}ig;
    $data =~ s{<[ \n\r]*script[^>]*>[\d\D]*?<[ \n\r]*/script[^>]*>}{}g;

    chmod 0644, $filename;
    write_file( $filename, $data ) or warn "w $filename\n";
}

sub toc_core_docs {
    my $prefix = "lib\\Pod";
    my $p      = "lib\\Pod";
    my @core;

    my @operating_systems;
    for (
        qw(
        aix amiga apollo beos bs2000 ce cygwin dgux dos
        epoc freebsd hpux hurd irix linux machten macos
        macosx mint mpeix netware openbsd os2 os390 os400
        plan9 qnx solaris tru64 uts vmesa vms vos win32)
        )
    {
        push @operating_systems, help_topic( $_, "$p\\perl$_.html" );
    }

    my @deltas;
    {
        my @list = qw(delta);
        for ( sort @perl_core_docs ) {
            my $name = $_;
            $name =~ s</Pod/perl><>;
            $name =~ s<\.html$><>;

            push @list, $name if $name =~ m/\d+delta/;
        }
        for (@list) {
            push @deltas, help_topic( $_, "$p\\perl$_.html" );
        }
    }

    my $result = help_folder(
        "Core Perl Documentation",
        "$prefix\\perl.html",
        help_topic( "Overview", "$p\\perlintro.html" ),
        help_topic( "Glossary", "$p\\perlglossary.html" ),
        help_topic(
            "How to execute the Perl interpreter",
            "$p\\perlrun.html"
        ),
        help_folder(
            "Advanced",
            "",
            help_folder(
                "C",
                "",
                help_topic( "api",     "$p\\perlapi.html" ),
                help_topic( "apio",    "$p\\perlapio.html" ),
                help_topic( "call",    "$p\\perlcall.html" ),
                help_topic( "clib",    "$p\\perlclib.html" ),
                help_topic( "debguts", "$p\\perldebguts.html" ),
                help_topic( "embed",   "$p\\perlembed.html" ),
                help_topic( "guts",    "$p\\perlguts.html" ),
                help_topic( "hack",    "$p\\perlhack.html" ),
                help_topic( "intern",  "$p\\perlintern.html" ),
                help_topic( "iol",     "$p\\perliol.html" ),
                help_topic( "xs",      "$p\\perlxs.html" ),
                help_topic( "xstut",   "$p\\perlxstut.html" ),
            ),
            help_folder(
                "Character encoding",
                "",
                help_topic( "ebcdic",   "$p\\perlebcdic.html" ),
                help_topic( "unicode",  "$p\\perlunicode.html" ),
                help_topic( "uniintro", "$p\\perluniintro.html" ),
            ),
            help_folder(
                "References", "",
                help_topic( "ref",    "$p\\perlref.html" ),
                help_topic( "reftut", "$p\\perlreftut.html" ),
            ),
            help_folder(
                "Threading", "",
                help_topic( "othrtut", "$p\\perlothrtut.html" ),
                help_topic( "thrtut",  "$p\\perlthrtut.html" ),
            ),
            help_topic( "compile",   "$p\\perlcompile.html" ),
            help_topic( "dbmfilter", "$p\\perldbmfilter.html" ),
            help_topic( "filter",    "$p\\perlfilter.html" ),
            help_topic( "fork",      "$p\\perlfork.html" ),
            help_topic( "form",      "$p\\perlform.html" ),
            help_topic( "ipc",       "$p\\perlipc.html" ),
            help_topic( "locale",    "$p\\perllocale.html" ),
            help_topic( "packtut",   "$p\\perlpacktut.html" ),
            help_topic( "tie",       "$p\\perltie.html" ),
        ),    # end of Advanced
        help_folder(
            'Data types and structures',
            '',
            help_topic( "data",   "$p\\perldata.html" ),
            help_topic( "dsc",    "$p\\perldsc.html" ),
            help_topic( "lol",    "$p\\perllol.html" ),
            help_topic( "number", "$p\\perlnumber.html" ),
        ),
        help_folder(
            'Debugging and Diagnostics',
            '',
            help_topic( "debtut",  "$p\\perldebtut.html" ),
            help_topic( "debug",   "$p\\perldebug.html" ),
            help_topic( "diag",    "$p\\perldiag.html" ),
            help_topic( "lexwarn", "$p\\perllexwarn.html" ),
        ),
        help_folder(
            'Language',
            '',
            help_topic( "cheat",   "$p\\perlcheat.html" ),
            help_topic( "func",    "$p\\perlfunc.html" ),
            help_topic( "op",      "$p\\perlop.html" ),
            help_topic( "opentut", "$p\\perlopentut.html" ),
            help_topic( "port",    "$p\\perlport.html" ),
            help_topic( "sec",     "$p\\perlsec.html" ),
            help_topic( "style",   "$p\\perlstyle.html" ),
            help_topic( "sub",     "$p\\perlsub.html" ),
            help_topic( "syn",     "$p\\perlsyn.html" ),
            help_topic( "trap",    "$p\\perltrap.html" ),
            help_topic( "var",     "$p\\perlvar.html" ),
        ),
        help_folder(
            'Modules',
            '',
            help_topic( "mod",        "$p\\perlmod.html" ),
            help_topic( "modinstall", "$p\\perlmodinstall.html" ),
            help_topic( "modlib",     "$p\\perlmodlib.html" ),
            help_topic( "modstyle",   "$p\\perlmodstyle.html" ),
            help_topic( "newmod",     "$p\\perlnewmod.html" ),
        ),
        help_folder(
            'OOP',
            '',
            help_topic( "boot", "$p\\perlboot.html" ),
            help_topic( "bot",  "$p\\perlbot.html" ),
            help_topic( "obj",  "$p\\perlobj.html" ),
            help_topic( "tooc", "$p\\perltooc.html" ),
            help_topic( "toot", "$p\\perltoot.html" ),
        ),
        help_folder(
            "Regular expressions",
            "",
            help_topic( "re",      "$p\\perlre.html" ),
            help_topic( "requick", "$p\\perlrequick.html" ),
            help_topic( "reref",   "$p\\perlreref.html" ),
            help_topic( "retut",   "$p\\perlretut.html" ),
        ),
        help_folder(
            'FAQ',
            "$p\\perlfaq.html",
            help_topic( "faq1", "$p\\perlfaq1.html" ),
            help_topic( "faq2", "$p\\perlfaq2.html" ),
            help_topic( "faq3", "$p\\perlfaq3.html" ),
            help_topic( "faq4", "$p\\perlfaq4.html" ),
            help_topic( "faq5", "$p\\perlfaq5.html" ),
            help_topic( "faq6", "$p\\perlfaq6.html" ),
            help_topic( "faq7", "$p\\perlfaq7.html" ),
            help_topic( "faq8", "$p\\perlfaq8.html" ),
            help_topic( "faq9", "$p\\perlfaq9.html" ),
        ),
        help_folder(
            'Miscellaneous',
            '',
            help_folder( 'Deltas',            '', @deltas ),
            help_folder( "Operating systems", "", @operating_systems ),
            help_folder(
                'POD', '',
                help_topic( "pod",     "$p\\perlpod.html" ),
                help_topic( "podspec", "$p\\perlpodspec.html" ),
            ),
            help_folder(
                'Tools',
                '',
                help_topic( "doc",     "$p\\perldoc.html" ),
                help_topic( "util",    "$p\\perlutil.html" ),
                help_topic( "perl5db", "lib\\perl5db.html" ),
            ),
            help_topic( "artistic", "$p\\perlartistic.html" ),
            help_topic( "book",     "$p\\perlbook.html" ),
            help_topic( "gpl",      "$p\\perlgpl.html" ),
            help_topic( "hist",     "$p\\perlhist.html" ),
            help_topic( "todo",     "$p\\perltodo.html" ),
        ),
    );

    return $result;
}

sub toc_active_perl {
    return help_folder(
        "ActivePerl Documentation",
        "perlmain.html",
        help_folder(
            "Getting Started",
            "",
            help_topic( "Welcome To ActivePerl", "perlmain.html" ),
            help_topic( "Release Notes",         "release.html" ),
            help_topic( "Installation Guide",    "install.html" ),
            help_topic( "Using PPM",             "faq/ActivePerl-faq2.html" ),
            help_topic(
                "Web Server Configuration",
                "faq/Windows\\ActivePerl-Winfaq6.html"
            ),
            help_topic( "Getting Started",           "readme.html" ),
            help_topic( "ActivePerl 5.6 Change Log", "changes-56.html" ),
            help_topic( "ActivePerl 5.8 Change Log", "changes-58.html" ),
            help_topic( "More Resources",            "resources.html" ),
            help_topic( "License and Copyright",     "Copyright.html" ),
        ),
        help_folder(
            "ActivePerl Components",
            "",
            help_topic( "Overview", "Components\\Descriptions.html" ),
            help_folder(
                "Windows Specific",
                "",

 #               help_topic( "OLE Browser", "lib\\Win32\\OLE\\Browser.html" ),
                help_topic(
                    "PerlScript", "Components\\Windows\\PerlScript.html"
                ),

#               help_topic("PerlScript Examples", "..\\eg\\IEExamples\\index.htm"),
                help_topic( "PerlEz", "Components\\Windows\\PerlEz.html" ),
                help_topic(
                    "Perl for ISAPI",
                    "Components\\Windows\\PerlISAPI.html"
                ),
                help_folder(
                    "PerlEx", "",
                    help_folder(
                        "Getting Started",
                        "",
                        help_topic( "Welcome", "PerlEx\\Welcome.html" ),
                        help_topic(
                            "Getting Started",
                            "PerlEx\\QuickStart.html"
                        ),
                        help_topic(
                            "How PerlEx Works",
                            "PerlEx\\HowItWorks.html"
                        ),
                    ),
                    help_folder(
                        "Configuration",
                        "",
                        help_topic(
                            "WebServer Configuration",
                            "PerlEx\\WebServerConfig.html"
                        ),
                        help_topic(
                            "PerlEx Interpreter Classes",
                            "PerlEx\\IntrpClass.html"
                        ),
                        help_topic(
                            "PerlEx Registry Entries",
                            "PerlEx\\RegistryEntries.html"
                        ),
                        help_topic(
                            "Debugging PerlEx Scripts",
                            "PerlEx\\Debugging.html"
                        ),
                    ),
                    help_folder(
                        "Features",
                        "",
                        help_topic(
                            "BEGIN and END Blocks",
                            "PerlEx\\BEGIN-ENDBlocks.html"
                        ),
                        help_topic(
                            "Persistent Connections",
                            "PerlEx\\PersistentConnections.html"
                        ),
                        help_topic(
                            "Embedding Perl in HTML files",
                            "PerlEx\\Embedded.html"
                        ),
                        help_topic(
                            "Reload &amp; ReloadAll",
                            "PerlEx\\Reload.html"
                        ),
                        help_topic(
                            "Coding with PerlEx",
                            "PerlEx\\PerlExCoding.html"
                        ),
                    ),
                    help_folder(
                        "Reference",
                        "",
                        help_topic( "PerlEx FAQ", "PerlEx\\FAQ.html" ),
                        help_topic(
                            "Troubleshooting", "PerlEx\\Troubleshooting.html"
                        ),
                        help_topic(
                            "PerlEx Precompiler",
                            "PerlEx\\Precompiler.html"
                        ),
                        help_topic(
                            "Event Log and Error Messages",
                            "PerlEx\\ErrorMessages.html"
                        ),
                        help_topic( "Reporting Bugs", "PerlEx\\Bugs.html" ),
                    ),
                    help_folder(
                        "Examples",
                        "",
                        help_topic(
                            "Examples",
                            "http://localhost/PerlEx/examples.aspl"
                        ),
                        help_topic(
                            "Benchmarks", "http://localhost/PerlEx/bm.htm"
                        ),
                    ),
                ),
            ),
        ),
        help_folder(
            "ActivePerl FAQ",
            "",
            help_topic( "Introduction", "faq\\ActivePerl-faq.html" ),
            help_topic(
                "Availability &amp; Install",
                "faq\\ActivePerl-faq1.html"
            ),
            help_topic( "Docs &amp; Support", "faq\\ActivePerl-faq3.html" ),
            help_folder(
                "Windows Specific",
                "",
                help_topic(
                    "Perl for ISAPI",
                    "faq\\Windows\\ActivePerl-Winfaq2.html"
                ),
                help_topic(
                    "Windows 9X\\Me\\NT\\200X\\XP",
                    "faq\\Windows\\ActivePerl-Winfaq4.html"
                ),
                help_topic(
                    "Windows Quirks",
                    "faq\\Windows\\ActivePerl-Winfaq5.html"
                ),
                help_topic(
                    "Web Programming",
                    "faq\\Windows\\ActivePerl-Winfaq7.html"
                ),
                help_topic(
                    "Windows Programming",
                    "faq\\Windows\\ActivePerl-Winfaq8.html"
                ),
                help_topic(
                    "Modules &amp; Samples",
                    "faq\\Windows\\ActivePerl-Winfaq9.html"
                ),
                help_topic(
                    "Embedding &amp; Extending",
                    "faq\\Windows\\ActivePerl-Winfaq10.html"
                ),
                help_topic(
                    "Using OLE with Perl",
                    "faq\\Windows\\ActivePerl-Winfaq12.html"
                ),
            ),
        ),
        help_folder(
            "Windows Scripting",
            "",
            help_topic(
                "Active Server Pages",
                "Windows\\ActiveServerPages.html"
            ),
            help_topic(
                "Windows Script Host",
                "Windows\\WindowsScriptHost.html"
            ),
            help_topic(
                "Windows Script Components",
                "Windows\\WindowsScriptComponents.html"
            ),
        ),
    );
}

sub walk {
    my $dir = shift;
    my @result;

    my @child_dirs = grep {m<^$dir/[^/]+$>} @dir_list;
    my @subtopics  = grep {m<^$dir/[^/]+$>} @topics_list;

    my %h;

    $h{$_} = 'f' for (@subtopics);
    $h{$_} = 'd' for (@child_dirs);

    for ( sort keys %h ) {
        if ( $h{$_} eq 'd' ) {
            my $link = '';

            if ( $dir_map{$_} eq 'FOLDER_TOPIC' ) {
                $link = $topic_map{$_};
            }

            my $title = basename($_);
            push @result, help_folder( $title, $link, walk($_) );
        }
        else {
            if ( $dir_map{$_} && $dir_map{$_} eq 'FOLDER_TOPIC' ) {
                next;
            }

            my $link = $topic_map{$_};
            push @result, help_topic( basename($_), $link );
        }
    }
    return @result;
}

sub mark_folder_topics {
    for (@topics_list) {
        if ( exists $dir_map{$_} ) {
            $dir_map{$_} = 'FOLDER_TOPIC';
        }
    }
}

sub toc_pragmas {
    my @pragmas;

    my @list;
    for (@perl_pragmas) {
        my $name = $_;
        ( my $filename = $name ) =~ s<::><\\>;

        push @pragmas, help_topic( $name, "lib\\$filename.html" );
    }
    push @pragmas, help_topic( 'version', "site\\lib\\version.html" );

    return sort @pragmas;
}

sub write_notes_txt {
    my $src  = read_file( $FindBin::Bin . '/' . $FindBin::Script );
    my $date = localtime;
    my $chaps;

    $chaps .= "$_ " for (@chapters);

    my $text = <<"END";
Perl $Config{version} Documentation

Generated on $date with HelpDocUpdate.pl (Version $version)
Chapters: $chaps

-- -- -- -- Source code of HelpDocUpdate.pl -- -- -- --

$src
END

    write_file( '_HelpDocUpdate_notes.txt', $text );
}

sub fix_list {
    for (@topics_list) {
        my $key = $_;

        if (m<^/Inline-(API|FAQ|Support)$>) {
            print "Renaming $_ to ";
            s<^/Inline-(API|FAQ|Support)$></Inline/Inline-$1>;
            print "$_\n";
        }
        elsif (m<^/(lwptut|lwpcook)>) {
            print "Renaming $_ to ";
            s<^/(lwptut|lwpcook)$></LWP/$1>;
            print "$_\n";
        }
        elsif (m<^/perl5db$>) {

            # silently ignore this one, since it can
            # be found at Core/Miscellaneous/Tools
            $_ = '';
        }
        elsif (m<^/version$>) {

            # silently ignore this one, since it can
            # be found at Pragmas
            $_ = '';
        }
        elsif (m<^/[a-z]>) {
            print "Ignoring $_\n";
            $_ = '';
        }
        else {
            next;
        }

        my $file = $topic_map{$key};
        delete $topic_map{$key};
        $topic_map{$_} = $file if $_;
    }
    @topics_list = sort keys %topic_map;
}

# End of source code
