<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="urn:WebWorks-Engine-Files-Schema"
                              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:wwfiles="urn:WebWorks-Engine-Files-Schema"
                              xmlns:wwenv="urn:WebWorks-XSLT-Extension-Environment"
                              xmlns:wwprojext="urn:WebWorks-XSLT-Extension-Project"
                              xmlns:wwuri="urn:WebWorks-XSLT-Extension-URI"
                              xmlns:wwlog="urn:WebWorks-XSLT-Extension-Log"
                              xmlns:wwfilesystem="urn:WebWorks-XSLT-Extension-FileSystem" 
                              xmlns:wwexec="urn:WebWorks-XSLT-Extension-Execute"
                              exclude-result-prefixes="xsl wwfiles wwenv wwprojext wwuri wwlog wwfilesystem wwexec"
>
 <xsl:param name="GlobalInput" />
 <xsl:param name="GlobalPipelineName" />
 <xsl:param name="GlobalProject" />
 <xsl:param name="GlobalFiles" />
 <xsl:param name="ParameterPublishType" />


 <xsl:output method="xml" encoding="UTF-8" indent="yes" />


 <xsl:template match="/">
  <wwfiles:Files version="1.0">

   <!-- Only perform action if explicitly requested -->
   <!--                                             -->
   <xsl:if test="wwenv:RequestedPipeline($GlobalPipelineName)">
    <!-- Access publishing info -->
    <!--                        -->
    <xsl:variable name="VarPublishWikiURL" select="wwprojext:GetFormatSetting('publish-wiki-url')" />
    <xsl:variable name="VarPublishWikiParentPage" select="wwprojext:GetFormatSetting('publish-wiki-parent-page')" />

    <!-- Determine root publish path -->
    <!--                             -->
    <xsl:variable name="VarPublishBaseURL" select="wwuri:MakeAbsolute($VarPublishWikiURL, concat($VarPublishWikiParentPage, '/'))" />
    <xsl:choose>
     <xsl:when test="string-length($VarPublishBaseURL) &gt; 0">
      <!-- Locate files to publish -->
      <!--                         -->
      <xsl:variable name="VarPublishFiles" select="$GlobalFiles/wwfiles:Files/wwfiles:File[@type = $ParameterPublishType]" />

      <!-- Process each file -->
      <!--                   -->
      <xsl:for-each select="$VarPublishFiles">
       <xsl:variable name="VarPublishFile" select="." />

       <!-- Wiki page name -->
       <!--                -->
       <xsl:variable name="VarWikiPath">
        <!-- Parent page -->
        <!--             -->
        <xsl:if test="string-length($VarPublishWikiParentPage) &gt; 0">
         <xsl:value-of select="$VarPublishWikiParentPage" />
         <xsl:text>/</xsl:text>
        </xsl:if>

        <!-- Directory path as parent pages -->
        <!--                                -->
        <xsl:variable name="VarOutputDirectoryPath" select="wwfilesystem:Combine(wwprojext:GetTargetOutputDirectoryPath(), wwprojext:GetGroupName($VarPublishFile/@groupID))" />
        <xsl:variable name="VarRelativePath" select="wwfilesystem:GetRelativeTo($VarPublishFile/@path, wwfilesystem:Combine($VarOutputDirectoryPath, 'dummy.component'))" />
        <xsl:variable name="VarRelativePathDirectoryName" select="wwfilesystem:GetDirectoryName($VarRelativePath)" />
        <xsl:if test="string-length($VarRelativePathDirectoryName) &gt; 0">
         <xsl:value-of select="translate('\', '/', $VarRelativePathDirectoryName)" />
         <xsl:text>/</xsl:text>
        </xsl:if>

        <xsl:value-of select="wwfilesystem:GetFileNameWithoutExtension($VarPublishFile/@path)"/>
       </xsl:variable>

       <!-- Publish -->
       <!--         -->
       <xsl:variable name="VarExecutablePath" select="wwuri:AsFilePath('wwformat:Tools/wiki-put-page/wiki-put-page.exe')" />
       <xsl:variable name="VarPublishResult" select="wwexec:ExecuteCommand($VarExecutablePath, '--wiki-url', $VarPublishWikiURL, '--wiki-page', $VarWikiPath, '--wiki-text-file', $VarPublishFile/@path)" />
      </xsl:for-each>
     </xsl:when>

     <xsl:otherwise>
      <!-- Warn! -->
      <!--       -->
      <xsl:variable name="VarWarnInvalidBaseURL" select="wwlog:Warning('Invalid URL supplied for Wiki publishing.')" />
      <xsl:variable name="VarWarnPublishWikiURL" select="wwlog:Warning('  publish-wiki-url == ', VarPublishWikiURL)" />
      <xsl:variable name="VarWarnPublishWikiParentPage" select="wwlog:Warning('  publish-wiki-parent-page == ', VarPublishWikiParentPage)" />
     </xsl:otherwise>
    </xsl:choose>
   </xsl:if>

  </wwfiles:Files>
 </xsl:template>
</xsl:stylesheet>
