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

<!-- Start -->
<xsl:template match="/">
    <xsl:processing-instruction name="DOCTYPE">COMPLETION SYSTEM "completion.dtd"</xsl:processing-instruction>
    <completion version='.02'>
    <meta>
      <filename>cleartool</filename>
      <includes>
        <include file="clearcase_defines.xml"/>
      </includes>
    </meta>
    <prolog>
        <code>
        local suffixes suf
        local expl curcontext="$curcontext" state line
        typeset -A opt_args
        </code>
    </prolog>
    <services>
        <service name='cleartool'>
            <arguments></arguments>
            <argspecs>
                <argspec>
                  <optspec><flagarg name='-version'></flagarg></optspec>
                  <description>
                    <message>Version information</message>
                  </description>
                </argspec>
                <argspec>
                  <optspec><flagarg name='-verall'></flagarg></optspec>
                  <description>
                    <message>full version information</message>
                  </description>
                </argspec>

                <argspec>
                  <optspec><flagarg name='-e'></flagarg></optspec>
                </argspec>
                <argspec>
                  <optspec><anyposition/><words_only_normal_args/></optspec>
                  <description><message>clearcase command</message>
                    <action>
                      <command>_cc_command</command>
                    </action>
                  </description>
                </argspec>
            </argspecs>
        </service>
    </services>
    <dispatch>
    <code><![CDATA[
# define cleartool command dispatch function.
(( $+functions[_cc_command] )) ||
_cc_command () {
  if (( ! $+_cc_cmds )); then
    typeset -gA _cc_cmds
    _cc_cmds=()
    for a in ]]></code>
    <subcommandlist style="list"/>
    <code><![CDATA[
    do
      _cc_cmds[$a]=" "
    done
    _cc_cmds[checkin]=" ci "
    _cc_cmds[checkout]=" co "
    _cc_cmds[lscheckout]=" lsco "
    _cc_cmds[uncheckout]=" unco "
  fi

  if (( CURRENT == 1 )); then
    _tags commands && { compadd "$@" -k _cc_cmds || compadd "$@" ${(kv)=_cc_cmds} }
  else
    local curcontext="$curcontext"

    cmd="${${(k)_cc_cmds[(R)* $words[1] *]}:-${(k)_cc_cmds[(i)$words[1]]}}"
    if (( $#cmd )); then
      curcontext="${curcontext%:*:*}:cc-${cmd}:"
      _cc_$cmd
    else
      _message "unknown clearcase command: $words[1]"
    fi
  fi
}

# define cleartool command dispatch function.
(( $+functions[_cc_handle] )) ||
_cc_handle () {
}
    ]]></code></dispatch>
    <subcommands prefix="_cc_">
        <xsl:apply-templates select="commands/subcommand"/>
    </subcommands>
    </completion>
</xsl:template>



<xsl:template match="subcommand">
  <xsl:variable name="root" select="."/>
  <subcommand name="{@cmd}">
    <service>
      <arguments/>
      <argspecs>
        <xsl:variable name="flags"
             select="flagset/flag[not(@name = preceding-sibling::flag/@name)]"/>
        <xsl:for-each select="$flags">
          <argspec>
            <xsl:apply-templates select=".">
              <xsl:with-param name="root" select="$root/contents"/>
            </xsl:apply-templates>
          </argspec>
        </xsl:for-each>
        <xsl:for-each select="$root/contents//word[not(name(preceding-sibling::*[1]) = 'flag')]">
        <!-- Comment out trace information:  "command:wordname"
        <xsl:message><xsl:value-of select="$root/@cmd"/>:<xsl:value-of select="@name"/></xsl:message> -->
          <argspec>
            <xsl:choose>
              <xsl:when test="name(following-sibling::*[1]) = 'ellipses'">
                <xsl:call-template name="anyposition">
                    <xsl:with-param name="word" select="."/>
                </xsl:call-template>
              </xsl:when>
              <xsl:when test="true()">
                <xsl:call-template name="position">
                    <xsl:with-param name="word" select="."/>
                    <xsl:with-param name="pos" select="position()"/>
                </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
              </xsl:otherwise>
            </xsl:choose>
          </argspec>
        </xsl:for-each>
      </argspecs>
    </service>
    <states>
    </states>
  </subcommand>
</xsl:template>

<xsl:template name="anyposition">
  <xsl:param name="word"/>

  <optspec>
    <anyposition/>
  </optspec>
  <description>
    <message></message>
    <action tag="{$word/@name}"></action>
  </description>
</xsl:template>

<xsl:template name="position">
  <xsl:param name="word"/>
  <xsl:param name="pos"/>


  <optspec>
    <position position="{$pos}"/>
  </optspec>
  <description>
    <message></message>
    <action tag="{$word/@name}"></action>
  </description>

</xsl:template>


<xsl:template match="flag">
  <xsl:param name="root"/>

  <xsl:variable name="name"     select="@name"/>
  <xsl:variable name="flagNode" select="$root//flag[@name=$name][1]"/>
  <optspec>
    <xsl:call-template name="ExclusionList">
      <xsl:with-param name="flag" select="$flagNode"/>
      <xsl:with-param name="root" select="$root"/>
    </xsl:call-template>
    <flagarg name="{@name}">
      <explanation/>
    </flagarg>
  </optspec>
  <description>
    <message>
    </message>
    <xsl:for-each select="$flagNode/*">
      <xsl:apply-templates select="."/>
    </xsl:for-each>
  </description>
</xsl:template>

<xsl:template match="flag_set">
  <action>
    <fixed_set>
       <xsl:apply-templates select="*" mode="fixed_set"/>
    </fixed_set>
  </action>
</xsl:template>

<xsl:template match="flag_word" mode="fixed_set">
  <fixed_string>
    <value><xsl:value-of select="@name"/></value>
    <description/>
  </fixed_string>
</xsl:template>

<xsl:template match="flag_word">
  <action tag="{@name}">
  </action>
</xsl:template>

<xsl:template match="flag_brace|flag_group|flag_or">
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="flag_ellipses|flag_const"/>

<xsl:template match="group">
</xsl:template>

<xsl:template name="ExclusionList">
  <xsl:param name="flag"/>
  <xsl:param name="root"/>

  <xsl:choose>
    <xsl:when test="$flag/ancestor::group">
      <exclusionlist>
        <xsl:for-each select="$flag/ancestor::group">
          <xsl:for-each select="or">
             <xsl:if test="not(generate-id()=generate-id($flag/ancestor::or))">
                <!--  Bug with non-unique items being found -->
                <xsl:for-each select="descendant::flag">
                  <xsl:if test="@name != $flag/@name">
                    <flag><xsl:value-of select="@name"/></flag>
                  </xsl:if>
                </xsl:for-each>
             </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
      </exclusionlist>
    </xsl:when>
    <xsl:otherwise/>
  </xsl:choose>
</xsl:template>

<xsl:template match="brace|commands|word|ellipses|const">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:choose>
      <xsl:when test="./or">
        <xsl:call-template name="handleOr"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

<xsl:template name="handleOr">
</xsl:template>

<xsl:template name="showPath">
    <xsl:param name="node"/>

    <xsl:choose>
      <xsl:when test="count($node/ancestor::*) &gt; 0">
        <xsl:call-template name="showPath">
          <xsl:with-param name="node" select="$node/ancestor::*[1]"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise/>
    </xsl:choose>
    <xsl:text>/</xsl:text><xsl:value-of select="name($node)"/>
</xsl:template>
</xsl:stylesheet>