#!/bin/bash

# GNU Enterprise Application Server - GCD to HTML converter script
#
# Copyright 2007 Free Software Foundation
#
# This file is part of GNU Enterprise.
#
# GNU Enterprise is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 3, or (at your option) any later version.
#
# GNU Enterprise is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with program; see the file COPYING. If not,
# write to the Free Software Foundation, Inc., 59 Temple Place
# - Suite 330, Boston, MA 02111-1307, USA.
#
# $Id: gnue-gcd2html 9953 2009-10-11 18:50:17Z reinhard $

# -----------------------------------------------------------------------------
# This script is a quick hack that allows to generate HTML documentation from
# GCD files.
# -----------------------------------------------------------------------------

xsl='<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <xsl:template match="module">
    <xsl:text>
::MODULE::
</xsl:text><xsl:value-of select="@name"/><xsl:text>
</xsl:text>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>

  <xsl:template match="class">
    <xsl:text>
::CLASS::
</xsl:text><xsl:value-of select="@name"/><xsl:text>
</xsl:text>
    <xsl:element name="h2">
      <xsl:text>Class </xsl:text>
      <xsl:value-of select="@name"/>
    </xsl:element>
    <xsl:element name="p">
      <xsl:value-of select="@comment"/>
    </xsl:element>
    <xsl:element name="dl">
      <xsl:for-each select="property">
        <xsl:element name="dt">
          <xsl:value-of select="@name"/>
          <xsl:text> - </xsl:text>
          <xsl:element name="em">
            <xsl:choose>
              <xsl:when test="contains(@type,'"'_'"')">
                <xsl:text>Reference to </xsl:text>
                <xsl:element name="a">
                  <xsl:attribute name="href">../<xsl:value-of select="translate(@type,'"'_'"','"'/'"')"/>.html</xsl:attribute>
                  <xsl:value-of select="@type"/>
                </xsl:element>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="@type"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:element>
        </xsl:element>
        <xsl:element name="dd">
          <xsl:value-of select="@comment"/>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*|node()" priority="-1"/>
</xsl:stylesheet>'

for gcd in $*; do
  module=""
  class=""
  echo "$xsl" | xsltproc - "$gcd" | while read line; do
    test -n "$line" || continue
    if [ "$line" == "::MODULE::" ]; then
      read module
      mkdir -p $module
      continue
    fi
    if [ "$line" == "::CLASS::" ]; then
      read class
      continue
    fi
    test -n "$module" || continue
    test -n "$class" || continue
    echo "$line" >> $module/$class.html
  done
done
