#!/usr/bin/perl
#
# Basic script for generating XML markup for the major sections of the Oracle Documentation, and XSLT templates (and meta-templates, for both output formats) for transforming it.
# 
# To do:
#  * Generalise so that the LaTeX output is split to use a separate hyperlink for each word in the link label (to allow the links to be wrapped across lines).  HTML version doesn't need to be changed.
 

# This is a link to the root page of the documentation, not a URL prefix.
$oracle_documentation_root = 'https://docs.oracle.com/cd/E11882_01/index.html';


# Here's the raw data.
# Each entry has a name (marked up as XML), a unique, short, space-less code name for it, and the actual URL.
# It makes more sense to have the full URL here, rather than prefixing the Oracle document root, because it's easier to copy the entire URL from the Web browser when updating.
%references =
(
    Docs => { name => 'Database Documentation Library',
              url => 'https://docs.oracle.com/cd/E11882_01/nav/portal_booklist.htm' },
    Admin => { name => 'Database Administrator<apostrophe />s Guide',
               url => 'https://docs.oracle.com/cd/E11882_01/server.112/e25494/toc.htm' },
#     AppDev2Day => { name => 'Database 2 Day Developer<apostrophe />s Guide',
#                     url => 'https://docs.oracle.com/cd/E11882_01/appdev.112/b28843/toc.htm' },
    AppDevAdv => { name => 'Database Advanced Application Developer<apostrophe />s Guide',
                   url => 'https://docs.oracle.com/cd/E11882_01/appdev.112/e41502/toc.htm' },
    AppDevOR => { name => 'Object-Relational Developer<apostrophe />s Guide',
                  url => 'https://docs.oracle.com/cd/E11882_01/appdev.112/e11822/toc.htm' },
    Concepts => { name => 'Database Concepts',
                  url => 'https://docs.oracle.com/cd/E11882_01/server.112/e40540/toc.htm' },
    DataWarehousing => { name => 'Database Data Warehousing Guide',
                         url => 'https://docs.oracle.com/cd/E11882_01/server.112/e25554/toc.htm' },
    Errors => { name => 'Database Error Messages',
                url => 'https://docs.oracle.com/cd/E11882_01/server.112/e17766/toc.htm' },
    SQL => { name => 'Database SQL Language Reference',
             url => 'https://docs.oracle.com/cd/E11882_01/server.112/e41084/toc.htm' },
    Java => { name => 'Database Java Developer<apostrophe />s Guide',
              url => 'https://docs.oracle.com/cd/E11882_01/java.112/e10588/toc.htm' },
    JDBC => { name => 'Database JDBC Developer<apostrophe />s Guide',
              url => 'https://docs.oracle.com/cd/E11882_01/java.112/e16548/toc.htm' },
    JDBCRef => { name => 'Database JDBC Java API Reference (Javadoc)',
              url => 'https://docs.oracle.com/cd/E11882_01/Database JDBC Java API Reference (Javadoc)' },
#     Glossary => { name => 'Database Master Glossary',
#                   url => 'https://docs.oracle.com/cd/E11882_01/mix.112/b14388/toc.htm' },
    Tuning => { name => 'Database Performance Tuning Guide',
                url => 'https://docs.oracle.com/cd/E11882_01/e41573/toc.htm' },
    PLSQL => { name => 'Database PL/SQL Language Reference',
               url => 'https://docs.oracle.com/cd/E11882_01/e25519/toc.htm' },
    PLSQLPkgTyp => { name => 'Database PL/SQL Packages and Types Reference',
                     url => 'https://docs.oracle.com/cd/E11882_01/e40758/toc.htm' },
#     Index => { name => 'Database Master Index',
#                url => 'https://docs.oracle.com/cd/E11882_01/mix.112/b14387/toc.htm' },
    Reference => { name => 'Database Reference',
                   url => 'https://docs.oracle.com/cd/E11882_01/e40402/toc.htm' },
);

# Prefix to use in the generated template names:
$oracle_prefix = "Ora";

if ($#ARGV != 1)
{
    warn "usage: oracle-docs <section-code-regexp> (xml | xslt | xslt_html | xslt_latex | link | all)\n";
    exit;
}

$section_code = $ARGV[0];
$output_type = $ARGV[1];

if ($output_type eq "xslt") {
    print <<END_OF_HEADER
<?xml version="1.0"?>
<!-- Do not edit!  Automatically generated by oracle-docs.perl! -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

END_OF_HEADER
;
}

# One-off hard-coded template for a link to the root of the documentation set, using the URL as the anchor text.
# This is used in several places in the course book and updating them is more of a pain than implementing this!
if ($output_type eq "xslt") {
    print <<END_OF_TEMPLATE
    <xsl:template name="OraDocsURL" match="OraDocsURL">
        <xsl:variable name="OracleURL">
            <xsl:element name="hyperlink">
                <xsl:attribute name="url">
                    <xsl:text>${oracle_documentation_root}</xsl:text>
                </xsl:attribute>
            </xsl:element>
        </xsl:variable>
        <xsl:apply-templates select="\$OracleURL" />
    </xsl:template>

END_OF_TEMPLATE
;
}
    
foreach $doccode (keys %references) {
    $docname = $references{$doccode}{'name'};
    $docurl = $references{$doccode}{'url'};
    if ($doccode =~ /$section_code/) {
        # Print the requested stuff:
        if ($output_type eq "xml") {
            print generate_xml($doccode) . "\n";
        } elsif ($output_type eq "xslt") {
            print generate_xsl_template($docname, $doccode, $docurl) . "\n";
        } elsif ($output_type eq "all") {
            print generate_xsl_template($docname, $doccode, $docurl) . "\n";
        } else {
            warn "Sorry - I don't know anything about a \"$output_type\" format.\n";
            exit;
        }
    }
}

if ($output_type eq "xslt") {
    print "</xsl:stylesheet>\n";
}



# Generate an XSLT template for generating a hyperlink to the specified Oracle document.
# Arguments: (<descriptive-label>, <entry-code>, <url>)
sub generate_xsl_template {
    return <<END_OF_TEMPLATE
    <xsl:template name="${oracle_prefix}${_[1]}" match="${oracle_prefix}${_[1]}">
        <xsl:variable name="OracleURL">
            <xsl:element name="hyperlink">
                <xsl:attribute name="url">
                    <xsl:text>${_[2]}</xsl:text>
                </xsl:attribute>
                <xsl:call-template name="OracleServer" />
                <xsl:text> </xsl:text>
                ${_[0]}
            </xsl:element>
        </xsl:variable>
        <xsl:apply-templates select="\$OracleURL" />
    </xsl:template>
END_OF_TEMPLATE
;
}


# Produce the actual XML markup to be used in authoring (just to provide a handy list of them all):
# Arguments: (<entry-code>)
sub generate_xml {
    return "<" . $oracle_prefix . $_[0]. " />";
}
