<?xml version="1.0"?>
<!--
Copyright (c) 2000-2002 Carnegie Mellon University. All rights reserved.
Redistribution and use in source and binary forms, with or without   
modification, are permitted provided that the following conditions are met:
   
1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.  

3. The name "Carnegie Mellon University" must not be used to endorse or
   promote products derived from this software without prior written
   permission. For permission or any legal details, please contact:  
     Office of Technology Transfer
     Carnegie Mellon University
     5000 Forbes Avenue
     Pittsburgh, PA 15213-3890
     (412) 268-4387, fax: (412) 268-7395
     tech-transfer@andrew.cmu.edu

4. Redistributions of any form whatsoever must retain the following
   acknowledgment: "This product includes software developed by Computing
   Services at Carnegie Mellon University (http://www.cmu.edu/computing/)."

CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE  
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
-->
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0" xmlns:sage="http://www.net.cmu.edu/netsage/xml/main"
  xmlns:attr="http://www.net.cmu.edu/netsage/xml/attributes"
  xmlns:saxon="http://icl.com/saxon" xmlns:exsl="http://exslt.org/common"
  extension-element-prefixes="saxon exsl">
  <xsl:output method="text" encoding="UTF-8" media-type="text/plain" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:param name="server" select="''"/>
  <xsl:param name="file" select="''"/>
  <xsl:key name="exclhost" match="attr:exclude_host" 
    use="concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name)"/>
  <xsl:key name="inclhost" match="attr:include_host" 
    use="concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name,@host)"/>
  <xsl:key name="inclhost2" match="attr:include_host" 
    use="concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name)"/>

  <xsl:key name="exclperiod" match="attr:exclude_period" 
    use="concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name)"/>

  <xsl:key name="traphost" match="attr:authtraphost" 
    use="concat(../../../@server_hostname,../../@file_name)"/>

  <xsl:key name="command" match="attr:authcommand" 
    use="concat(../../../@server_hostname,../../@file_name)"/>
  <xsl:key name="hostid" match="sage:host" use="@id" />
  
  <!-- -->
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="$server != '' and $file != ''">
        <!-- server and file specified -->
        <xsl:apply-templates select="sage:top/sage:server[@server_hostname=$server]/sage:server_file[@file_name=$file]"/>
      </xsl:when>
      <xsl:when test="$server != ''">
        <xsl:message terminate="yes">Please specify which config file should be generated for&#xA;Specifying only a server is no longer supported</xsl:message>
      </xsl:when>
      <xsl:when test="element-available('saxon:output')">
        <xsl:for-each select="sage:top/sage:server/sage:server_file">
          <saxon:output href="{concat(@file_name,'-',../@server_hostname)}" method="text" encoding="UTF-8" 
            media-type="text/plain" omit-xml-declaration="yes">
            <xsl:apply-templates select="current()"/>
          </saxon:output>
        </xsl:for-each>
      </xsl:when>
      <xsl:when test="element-available('exsl:document')">
        <xsl:for-each select="sage:top/sage:server/sage:server_file">
          <exsl:document href="{concat(@file_name,'-',../@server_hostname)}" method="text" encoding="UTF-8" 
            media-type="text/plain" omit-xml-declaration="yes">
            <xsl:apply-templates select="current()"/>
          </exsl:document>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">Please specify which server and filename should be generated&#xA;</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  
  <xsl:template match="sage:server_file">
    <xsl:choose>
      <xsl:when test="@file_name = 'auth.cf'">
        <xsl:text># MON authenication file</xsl:text>
        <xsl:text>&#xA;# Automatically generated -- do not edit</xsl:text>
        <xsl:apply-templates mode="findauthattrs"/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:when>
      <xsl:when test="@file_name = 'mon.cfg'">
        <xsl:text># MON configuration file</xsl:text>
        <xsl:text>&#xA;# Automatically generated -- do not edit</xsl:text>
        <xsl:apply-templates mode="findglobalattrs"/>
        <xsl:apply-templates mode="definehostgroups"/>
        <xsl:apply-templates/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">
          <xsl:value-of select="concat('Unknown config file type ',@file_name)"/>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!-- definehostgroups templates. These templates produce the "hostgroups"
       lists -->
  <xsl:template mode="definehostgroups" match="sage:watchgroup">
    <xsl:text>&#xA;&#xA;hostgroup </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:apply-templates mode="definehostgroups"/>
  </xsl:template>
  
  <xsl:template mode="definehostgroups" match="sage:host">
    <xsl:variable name="hostid" select="@ref"/>
    <xsl:value-of select="concat(' ',key('hostid',$hostid)/@name)"/>
  </xsl:template>
  
  <!-- Normal templates. These templates produce the "watch" lists -->
  <xsl:template match="sage:watchgroup">
    <xsl:text>&#xA;&#xA;watch </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="sage:service">
    <xsl:text>&#xA;    service </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xA;        description </xsl:text>
    <xsl:value-of select="../@description"/>
    <xsl:apply-templates/>
    <xsl:if test="count(/sage:top/sage:server) > 1">
      <xsl:choose>
        <xsl:when test="count(attr:redistribute) > 0">
          <!-- moved to a generic template, since otherwise the ordering
               is wrong -->
        </xsl:when>
        <xsl:when test="count(attr:monitoraction) and
          count(sage:period/attr:alertaction) = 0 and 
          ../../sage:globalattrs/attr:redistribute_default">
          <xsl:text>&#xA;        redistribute </xsl:text>
          <xsl:value-of select="../../sage:globalattrs/attr:redistribute_default/@action"/>
        </xsl:when>
        <!--
        <xsl:when test="count(sage:period/attr:alertaction) = 0">
          <xsl:text>&#xA;        redistribute trapall.alert</xsl:text>
        </xsl:when>
        -->
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:template name="format-subperiod-common">
    <xsl:if test="@weekday_start">
      <xsl:choose>
        <xsl:when test="@weekday_start = @weekday_end">
          <xsl:value-of select="concat(' wd{',@weekday_start,'}')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(' wd{',@weekday_start,'-',@weekday_end,'}')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>

    <xsl:if test="@month_start">
      <xsl:choose>
        <xsl:when test="@month_start = @month_end">
          <xsl:value-of select="concat(' mo{',@month_start,'}')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(' mo{',@month_start,'-',@month_end,'}')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
    <xsl:if test="@monthday_start">
      <xsl:choose>
        <xsl:when test="@monthday_start = @monthday_end">
          <xsl:value-of select="concat(' md{',@monthday_start,'}')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(' md{',@monthday_start,'-',@monthday_end,'}')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
    <xsl:if test="@monthweek_start">
      <xsl:choose>
        <xsl:when test="@monthweek_start = @monthweek_end">
          <xsl:value-of select="concat(' wk{',@monthweek_start,'}')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(' wk{',@monthweek_start,'-',@monthweek_end,'}')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:template name="format-subperiod-1">
    <xsl:choose>
      <xsl:when test="@time_start">
        <xsl:variable name="starthour" select="number(substring-before(@time_start,':'))"/>
        <xsl:variable name="endhour" select="number(substring-before(@time_end,':'))"/>
        <xsl:variable name="startmin" select="number(substring-before(substring-after(@time_start,':'),':'))"/>
        <xsl:variable name="endmin" select="number(substring-before(substring-after(@time_end,':'),':'))"/>
        <xsl:choose>
          <xsl:when test="$startmin > 0 and (($starthour + 1) mod 24 = $endhour)">
            <xsl:call-template name="format-subperiod-common"/>
            <xsl:value-of select="concat(' hr{',$starthour,'}')"/>
            <xsl:value-of select="concat(' min{',$startmin,'-59}')"/>
          </xsl:when>
          <xsl:when test="$startmin > 0 and ($starthour != $endhour or ($starthour = $endhour and $startmin > $endmin))">
            <xsl:call-template name="format-subperiod-common"/>
            <xsl:value-of select="concat(' hr{',$starthour,'}')"/>
            <xsl:value-of select="concat(' min{',$startmin,'-59},')"/>
            <xsl:call-template name="format-subperiod-common"/>
            <xsl:value-of select="concat(' hr{',($starthour +1) mod 24 ,'-',
              ($endhour + 23) mod 24,'}')"/>
          </xsl:when>
          <xsl:when test="$startmin > 0">
            <xsl:call-template name="format-subperiod-common"/>
            <xsl:value-of select="concat(' hr{',$starthour,'}')"/>
            <xsl:value-of select="concat(' min{',$startmin,'-',$endmin,'}')"/>
          </xsl:when>
          <xsl:when test="$starthour = $endhour and $endmin > 0">
            <xsl:call-template name="format-subperiod-common"/>
            <xsl:value-of select="concat(' hr{',$endhour,'}')"/>
            <xsl:value-of select="concat(' min{0-',$endmin,'}')"/>
          </xsl:when>
          <xsl:when test="($starthour + 1) mod 24 = $endhour">
            <xsl:call-template name="format-subperiod-common"/>
            <xsl:value-of select="concat(' hr{',$starthour,'}')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="format-subperiod-common"/>
            <xsl:value-of select="concat(' hr{',$starthour,'-',
              ($endhour + 23) mod 24,'}')"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="format-subperiod-common"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="format-subperiod-2">
    <xsl:if test="@time_start">
      <xsl:variable name="starthour" select="number(substring-before(@time_start,':'))"/>
      <xsl:variable name="endhour" select="number(substring-before(@time_end,':'))"/>
      <xsl:variable name="startmin" select="number(substring-before(substring-after(@time_start,':'),':'))"/>
      <xsl:variable name="endmin" select="number(substring-before(substring-after(@time_end,':'),':'))"/>
      <xsl:if test="$endmin > 0 and ($starthour != $endhour or $startmin > $endmin)">
        <xsl:value-of select="','"/>
        <xsl:call-template name="format-subperiod-common"/>
        <xsl:value-of select="concat(' hr{',$endhour,'}')"/>
        <xsl:value-of select="concat(' min{0-',$endmin,'}')"/>
      </xsl:if>
    </xsl:if>
  </xsl:template>
    
  <xsl:template mode="period" match="sage:subperiod">
    <xsl:if test="position() != 1">
      <xsl:value-of select="','"/>
    </xsl:if>
    <xsl:call-template name="format-subperiod-1"/>
    <xsl:call-template name="format-subperiod-2"/>
  </xsl:template>
    
  <xsl:template match="sage:period">
    <xsl:if test="count(./*) - count(./sage:subperiod) > 0">
      <xsl:text>&#xA;        period</xsl:text>
      <xsl:if test="@name">
        <xsl:value-of select="concat(' ',@name,':')"/>
      </xsl:if>

      <xsl:apply-templates mode="period"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="sage:dependencies">
    <xsl:text>&#xA;        </xsl:text>
    <xsl:value-of select="@type"/>
    <xsl:text>depend </xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="sage:dep-and">
    <xsl:text>(</xsl:text>
    <xsl:for-each select="./*">
      <xsl:apply-templates select="."/>
      <xsl:if test="position() != last()">
        <xsl:text> &amp;&amp; </xsl:text>
      </xsl:if>
      </xsl:for-each>
    <xsl:text>)</xsl:text>
  </xsl:template>

  <xsl:template match="sage:dep-or">
    <xsl:text>(</xsl:text>
    <xsl:for-each select="./*">
      <xsl:apply-templates select="."/>
      <xsl:if test="position() != last()">
        <xsl:text> || </xsl:text>
      </xsl:if>
      </xsl:for-each>
    <xsl:text>)</xsl:text>
  </xsl:template>

  <xsl:template match="sage:dep-not">
    <xsl:text>!</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="sage:dep-svc">
    <xsl:value-of select="concat(@hostgroup,':',@service)"/>
  </xsl:template>
  
  <!-- all elements of the mon namespace that can be children of services
       need to appear here -->
  <xsl:template match="attr:interval">
    <xsl:text>&#xA;        interval </xsl:text>
    <xsl:call-template name="format-time">
      <xsl:with-param name="count" select="@time"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="attr:traptimeout">
    <xsl:text>&#xA;        traptimeout </xsl:text>
    <xsl:call-template name="format-time">
      <xsl:with-param name="count" select="@time"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="attr:trapduration">
    <xsl:text>&#xA;        trapduration </xsl:text>
    <xsl:call-template name="format-time">
      <xsl:with-param name="count" select="@time"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="attr:randomskew">
    <xsl:text>&#xA;        randskew </xsl:text>
    <xsl:call-template name="format-time">
      <xsl:with-param name="count" select="@time"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="attr:monitoraction">
    <xsl:text>&#xA;        monitor </xsl:text>
    <xsl:value-of select="@action"/>
    <xsl:if test="count(../attr:monitorarguments) = 1">
      <xsl:value-of select="concat(' ',../attr:monitorarguments/@string)"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="attr:monitorarguments">
    <xsl:if test="count(../attr:monitoraction) = 0">
      <xsl:message terminate="no">attr:monitorarguments found without attr:monitoraction&#xA;<xsl:value-of select="@string"/></xsl:message>
    </xsl:if>
  </xsl:template>

  <xsl:template  match="attr:description">
    <xsl:text>&#xA;           description  </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>

  <xsl:template match="attr:allow_empty_group">
    <xsl:if test="@boolean=1">
      <xsl:text>&#xA;           allow_empty_group</xsl:text>
    </xsl:if>
  </xsl:template>


  <xsl:template match="attr:exclude_host">
    <!-- this tells us if this is the first exclude_host attribute -->
    <xsl:if test="count(.|key('exclhost',concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name))[1]) = 1">
      <xsl:text>&#xA;        exclude_hosts</xsl:text>        
      <xsl:apply-templates mode="item" select="key('exclhost',concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name))"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="attr:exclude_host" mode="item">
    <xsl:variable name="hostid" select="@host"/>
    <xsl:value-of select="concat(' ',key('hostid',$hostid)/@name)"/>
  </xsl:template>


  <xsl:template match="attr:include_host">
    <!-- this tells us if this is the first include_host attribute -->
    <xsl:if test="count(.|key('inclhost2',concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name))[1]) = 1">
      <xsl:variable name="server" select="../../../../@server_hostname"/>
      <xsl:variable name="servf" select="../../../@file_name"/>
      <xsl:variable name="group" select="../../@name"/>
      <xsl:variable name="service" select="../@name"/>
      <xsl:if test="count(key('inclhost2',concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name))) != count(../../sage:host)">
        <xsl:text>&#xA;        exclude_hosts</xsl:text>        
        <xsl:for-each select="../../sage:host">
          <xsl:if test="count(key('inclhost',concat($server,$servf,$group,$service,@ref))) = 0">
            <xsl:variable name="hostid" select="@ref"/>
            <xsl:value-of select="concat(' ',key('hostid',$hostid)/@name)"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:if>
    </xsl:if>
  </xsl:template>


  <xsl:template match="attr:exclude_period">
    <!-- this tells us if this is the first exclude_host attribute -->
    <xsl:if test="count(.|key('exclperiod',concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name))[1]) = 1">
      <xsl:text>&#xA;        exclude_period</xsl:text>        
      <xsl:apply-templates mode="item" select="key('exclperiod',concat(../../../../@server_hostname,../../../@file_name,../../@name,../@name))"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="attr:exclude_period" mode="item">
    <xsl:if test="position() != 1">
      <xsl:value-of select="','"/>
    </xsl:if>
    <xsl:call-template name="format-subperiod-1"/>
    <xsl:call-template name="format-subperiod-2"/>
  </xsl:template>
  
  <xsl:template match="attr:redistribute">
    <xsl:text>&#xA;        redistribute </xsl:text>
    <xsl:value-of select="@action"/>
  </xsl:template>
  
 <!-- this actually belong outside of the period definition.  its a bug 
      in the mon documentation -->
  <xsl:template match="attr:failure_interval">
    <xsl:text>&#xA;        failure_interval </xsl:text>
    <xsl:call-template name="format-time">
      <xsl:with-param name="count" select="@time"/>
    </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>

 <!-- all elements of the mon namespace that can be children of periods 
   need to appear here -->

  <xsl:template mode="period" match="attr:dep_behavior">
    <xsl:text>&#xA;           dep_behavior = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>

  <xsl:template mode="period" match="attr:unack_summary">
    <xsl:text>&#xA;           unack_summary = </xsl:text>
    <xsl:value-of select="@boolean"/>
  </xsl:template>

  <xsl:template  mode="period" match="attr:numalerts">
    <xsl:text>&#xA;           numalerts  </xsl:text>
    <xsl:value-of select="@number"/>
  </xsl:template>

  <xsl:template mode="period" match="attr:alertevery">
    <xsl:text>&#xA;           alertevery </xsl:text>
    <xsl:call-template name="format-time">
      <xsl:with-param name="count" select="@time"/>
    </xsl:call-template>
    <xsl:if test="@boolean = 1">
      <xsl:text> observe_detail</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template mode="period" match="attr:alertafter">
    <xsl:text>&#xA;           alertafter</xsl:text>
    <xsl:if test="@number">
      <xsl:value-of select="concat(' ',@number)"/>
    </xsl:if>
    <xsl:if test="@time">
      <xsl:text> </xsl:text>
      <xsl:call-template name="format-time">
        <xsl:with-param name="count" select="@time"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template mode="period" match="attr:upalertafter">
    <xsl:text>&#xA;           upalertafter</xsl:text>
    <xsl:if test="@number">
      <xsl:value-of select="concat(' ',@number)"/>
    </xsl:if>
    <xsl:if test="@time">
      <xsl:text> </xsl:text>
      <xsl:call-template name="format-time">
        <xsl:with-param name="count" select="@time"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template mode="period" match="attr:alertaction">
    <xsl:text>&#xA;           alert </xsl:text>
    <xsl:if test="@range_start and @range_start != ''">
      <xsl:value-of select="concat('exit=',@range_start)"/>
      <xsl:if test="@range_end and @range_end != ''">
        <xsl:value-of select="concat('-',@range_end)"/>
      </xsl:if>
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:value-of select="@action"/>
    <xsl:if test="@string and @string != ''">
      <xsl:value-of select="concat(' ',@string)"/>
    </xsl:if>
  </xsl:template>

  <xsl:template mode="period" match="attr:upalertaction">
    <xsl:text>&#xA;           upalert </xsl:text>
    <xsl:if test="@range_start and @range_start != ''">
      <xsl:value-of select="concat('exit=',@range_start)"/>
      <xsl:if test="@range_end and @range_end != ''">
        <xsl:value-of select="concat('-',@range_end)"/>
      </xsl:if>
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:value-of select="@action"/>
    <xsl:if test="@string and @string != ''">
      <xsl:value-of select="concat(' ',@string)"/>
    </xsl:if>
  </xsl:template>

  <xsl:template mode="period" match="attr:startupalertaction">
    <xsl:text>&#xA;           startupalert </xsl:text>
    <xsl:if test="@range_start and @range_start != ''">
      <xsl:value-of select="concat('exit=',@range_start)"/>
      <xsl:if test="@range_end and @range_end != ''">
        <xsl:value-of select="concat('-',@range_end)"/>
      </xsl:if>
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:value-of select="@action"/>
    <xsl:if test="@string and @string != ''">
      <xsl:value-of select="concat(' ',@string)"/>
    </xsl:if>
  </xsl:template>
  <xsl:template mode="period" match="attr:no_comp_alerts">
    <xsl:if test="@boolean=1">
      <xsl:text>&#xA;           no_comp_alerts</xsl:text>
    </xsl:if>
  </xsl:template>
  <xsl:template mode="period" match="attr:alerts_dont_count">
    <xsl:if test="@boolean=1">
      <xsl:text>&#xA;           alerts_dont_count</xsl:text>
    </xsl:if>
  </xsl:template>
  
  
  <!-- global attribute templates -->
  <xsl:template match="sage:globalattrs" mode="findglobalattrs">
    <xsl:apply-templates mode="globalattrsproc"/>
  </xsl:template>

  <!-- all elements of the attr namespace that are global
       need to appear here -->
  <xsl:template mode="globalattrsproc" match="attr:randomstart">
    <xsl:text>&#xA;randstart = </xsl:text>
    <xsl:call-template name="format-time">
      <xsl:with-param name="count" select="@time"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:alertdir">
    <xsl:text>&#xA;alertdir = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:authfile">
    <xsl:text>&#xA;authfile = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:authtype">
    <xsl:text>&#xA;authtype = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:basedir">
    <xsl:text>&#xA;basedir = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:cfbasedir">
    <xsl:text>&#xA;cfbasedir = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:dtlogfile">
    <xsl:text>&#xA;dtlogfile = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:historicfile">
    <xsl:text>&#xA;historicfile = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:logdir">
    <xsl:text>&#xA;logdir = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:mondir">
    <xsl:text>&#xA;mondir = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:pamservice">
    <xsl:text>&#xA;pamservice = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:pidfile">
    <xsl:text>&#xA;pidfile = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:serverbind">
    <xsl:text>&#xA;serverbind = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:snmpport">
    <xsl:text>&#xA;snmpport = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:statedir">
    <xsl:text>&#xA;statedir = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:syslog_facility">
    <xsl:text>&#xA;syslog_facility = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:trapbind">
    <xsl:text>&#xA;trapbind = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:userfile">
    <xsl:text>&#xA;userfile = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>
  

  <xsl:template mode="globalattrsproc" match="attr:default_dep_behavior">
    <xsl:text>&#xA;dep_behavior = </xsl:text>
    <xsl:value-of select="@string"/>
  </xsl:template>

  <xsl:template mode="globalattrsproc" match="attr:default_unack_summary">
    <xsl:text>&#xA;unack_summary = </xsl:text>
    <xsl:value-of select="@boolean"/>
  </xsl:template>

  <xsl:template mode="globalattrsproc" match="attr:dtlogging">
    <xsl:text>&#xA;dtlogging = </xsl:text>
    <xsl:value-of select="@boolean"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:snmp">
    <xsl:text>&#xA;snmp = </xsl:text>
    <xsl:value-of select="@boolean"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:startupalerts_on_reset">
    <xsl:text>&#xA;startupalerts_on_reset = </xsl:text>
    <xsl:value-of select="@boolean"/>
  </xsl:template>
  
  <xsl:template mode="globalattrsproc" match="attr:dep_recur_limit">
    <xsl:text>&#xA;dep_recur_limit = </xsl:text>
    <xsl:value-of select="@number"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:histlength">
    <xsl:text>&#xA;histlength = </xsl:text>
    <xsl:value-of select="@number"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:maxprocs">
    <xsl:text>&#xA;maxprocs = </xsl:text>
    <xsl:value-of select="@number"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:serverport">
    <xsl:text>&#xA;serverport = </xsl:text>
    <xsl:value-of select="@number"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:trapport">
    <xsl:text>&#xA;trapport = </xsl:text>
    <xsl:value-of select="@number"/>
  </xsl:template>
  <xsl:template mode="globalattrsproc" match="attr:redistribute_default">
    <!-- Do nothing. This isn't a real mon attribute -->
  </xsl:template>

  <!-- 
  for your auth.cf questions, i've got an idea
we'll have two attributes
one is 'authcommand' with value_has_string ('reset', 'reload', etc)
and value_has_usergroup (shit, haven't added that yet... )
the other is 'authtraphost' with value_has_host
all authtraphost entries get formatted as 'hostname * @string' after the 
'trap section' header
oh, and I fixed the trap section parsing bug... same stupid thing as 
exclude_period.  poor decision logic in a elsif chain
-->
  <!-- global attribute templates -->
  <xsl:template match="sage:globalattrs" mode="findauthattrs">
    <xsl:apply-templates mode="authattrsprocg" select="attr:authcommand[1]"/>
    <xsl:apply-templates mode="authattrsproc" select="attr:authtraphost"/>
  </xsl:template>

  <!-- all elements of the attr namespace that go in the authfile
       need to appear here -->
  <xsl:template mode="authattrsprocg" match="attr:authcommand">
    <!-- Means "the first authcommand in this server/file combo -->
    <xsl:if test="count(.|key('command',concat(../../../@server_hostname,../../@file_name))[1]) = 1">
      <xsl:text>&#xA;&#xA;command section</xsl:text>
    </xsl:if>
    <xsl:variable name="command" select="@string"/>
    <xsl:value-of select="concat('&#xA;', $command, ': ')"/>
    <xsl:apply-templates mode="authattrsproci" select="."/>
    <xsl:apply-templates mode="authattrsprocg" 
      select="following-sibling::attr:authcommand[@string != $command][1]"/>
  </xsl:template>
  <xsl:template mode="authattrsproci" match="attr:authcommand">
    <xsl:variable name="command" select="@string"/>
    <xsl:if test="preceding-sibling::attr:authcommand[@string=$command]">
      <xsl:value-of select="','"/>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="@user and @user != ''">
        <xsl:value-of select="@user"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'all'"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:apply-templates mode="authattrsproci" select="following-sibling::attr:authcommand[@string=$command][1]"/> 
  </xsl:template>
    
  <xsl:template mode="authattrsproc" match="attr:authtraphost">
    <!-- Means "the first authtraphost in this server/file combo -->
    <xsl:if test="count(.|key('traphost',concat(../../../@server_hostname,../../@file_name))[1]) = 1">
      <xsl:text>&#xA;&#xA;trap section</xsl:text>
    </xsl:if>
    <xsl:variable name="hostid" select="@host"/>
    <xsl:value-of select="concat('&#xA;', key('hostid', $hostid)/@name, ' * ', @string)"/>
  </xsl:template>



  <!-- Utility templates -->

                      
  <xsl:template name="format-time">
    <xsl:param name="count" select="."/>
    <xsl:param name="unitlist" select="'hms'"/>
    <xsl:param name="divlist" select="'3600 60 1'"/>
    <xsl:choose>
      <xsl:when test="not(contains($divlist, ' '))">
        <xsl:value-of select="$count"/>
        <xsl:value-of select="substring($unitlist,1,1)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="divisor" select="number(substring-before($divlist,' '))"/>
        <xsl:choose>
          <xsl:when test="($count mod $divisor) = 0">
            <xsl:value-of select="$count div $divisor"/>
            <xsl:value-of select="substring($unitlist,1,1)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="format-time">
              <xsl:with-param name="count" select="$count"/>
              <xsl:with-param name="unitlist" select="substring($unitlist,2)"/>
              <xsl:with-param name="divlist" select="substring-after($divlist, ' ')"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:transform>


