#!/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:
#  * Maybe modify the script to work for any URL-based hyperlinks (e.g. <Blackboard/>, <OtagoUniversity/>).  This would mean ditching the automatic "Ora" prefix, but wouldn't require much else to implement.
#  * 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.
 

# I figure this is also a sensible place to store the root of the Oracle documentation on the department's Web server.
# Should this have a trailing slash (appropriate for standalone URL) or not (appropriate for a prefix for composite URLs)?
$oracle_documentation_root = "http://info-nts-12.otago.ac.nz/docs/oracle11gr1/";


# 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's probably sensible 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 => $oracle_documentation_root },
    Admin => { name => 'Database Administrator<apostrophe />s Guide',
               url => $oracle_documentation_root . 'server.111/b28310/toc.htm' },
    AppDev2Day => { name => 'Database 2 Day Developer<apostrophe />s Guide',
                    url => $oracle_documentation_root . 'appdev.111/b28843/toc.htm' },
    AppDevAdv => { name => 'Database Advanced Application Developer<apostrophe />s Guide',
                   url => $oracle_documentation_root . 'appdev.111/b28424/toc.htm' },
    AppDevOR => { name => 'Object-Relational Developer<apostrophe />s Guide',
                  url => $oracle_documentation_root . 'appdev.111/b28371/toc.htm' },
    Concepts => { name => 'Database Concepts',
                  url => $oracle_documentation_root . 'server.111/b28318/toc.htm' },
    DataWarehousing => { name => 'Database Data Warehousing Guide',
                         url => $oracle_documentation_root . 'server.111/b28313/toc.htm' },
    Errors => { name => 'Database Error Messages',
                url => $oracle_documentation_root . 'server.111/b28278/toc.htm' },
    SQL => { name => 'Database SQL Language Reference',
             url => $oracle_documentation_root . 'server.111/b28286/toc.htm' },
    Java => { name => 'Database Java Developer<apostrophe />s Guide',
              url => $oracle_documentation_root . 'java.111/b31225/toc.htm' },
    JDBC => { name => 'Database JDBC Developer<apostrophe />s Guide and Reference',
              url => $oracle_documentation_root . 'java.111/b31224/toc.htm' },
    Glossary => { name => 'Database Master Glossary',
                  url => $oracle_documentation_root . 'mix.111/b14388/toc.htm' },
    Tuning => { name => 'Database Performance Tuning Guide',
                url => $oracle_documentation_root . 'server.111/b28274/toc.htm' },
    PLSQL => { name => 'Database PL/SQL Language Reference',
               url => $oracle_documentation_root . 'appdev.111/b28370/toc.htm' },
    PLSQLPkgTyp => { name => 'Database PL/SQL Packages and Types Reference',
                     url => $oracle_documentation_root . 'appdev.111/b28419/toc.htm' },
    Index => { name => 'Database Master Index',
               url => $oracle_documentation_root . 'mix.111/b14387/toc.htm' },
    Reference => { name => 'Database Reference',
                   url => $oracle_documentation_root . 'server.111/b28320/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]. " />";
}
