<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp   "&#160;">
<!ENTITY lsaquo   "&#8249;">
<!ENTITY rsaquo   "&#8250;">
<!ENTITY laquo  "&#171;">
<!ENTITY raquo  "&#187;">
<!ENTITY copy   "&#169;">
]>
<xsl:stylesheet version="3.0"
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
				xmlns:xs="http://www.w3.org/2001/XMLSchema"
				xmlns:ou="http://omniupdate.com/XSL/Variables"
				xmlns:ouc="http://omniupdate.com/XSL/Variables"
				exclude-result-prefixes="xs ou ouc">

	<!-- get a single profile XML -->
	<!-- has option to add custom xsl attribute to exclude it from the xml output of the file -->
	<xsl:function name="ou:get-profile">
		<xsl:param name="document" />
		<xsl:element name="{$profile-node}">
			<xsl:attribute name="href" select="concat($dirname, replace($ou:filename, $profile-ext, $extension))" />
			<xsl:for-each select="$document/profile/ouc:div[not(@xml = 'exclude')]">
				<xsl:element name="{translate(./@label, ' ', '_')}"><xsl:apply-templates select="node()[not(self::ouc:multiedit)]" /></xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:function>

	<!-- template for getting all the profiles from staging or production depending on what parameters you pass in -->
	<xsl:template name="get-all-profiles">
		<xsl:param name="current-path" select="concat($root,$ou:dirname)" /> <!-- current path -->
		<xsl:param name="skip" select="$ou:stagingpath"/> <!-- what file to skip -->
		<xsl:param name="type" select="'staging'"/> <!-- determine if it is done from staging or production -->
		<xsl:param name="language" select="'php'" /> <!-- determine what server side language the aggregation will be using -->
		<xsl:variable name="skipping" select="tokenize($skip, '/')[last()]" /> <!-- get the word 'index.pcf' or 'default.pcf' -->
		<xsl:if test="not(/document/element()[name() = $profile-node])">
			<xsl:choose>
				<!-- aggregate via server side side code -->
				<xsl:when test="$type = 'production'">
					<xsl:variable name="query-string" select="concat('?dir=', $ou:dirname, '&#38;ext=', $profile-ext, '&#38;node=', $profile-node)" /> <!-- determine the query string -->
					<xsl:variable name="request-location" select="concat($domain, '/_resources/directory')"/> <!-- determine the request location string -->
					<xsl:choose>
						<xsl:when test="$language = 'php'">
							<!-- path to php script -->
							<xsl:variable name="php-loc" select="concat($request-location, '/php/profiles.php', $query-string)" />
							<xsl:if test="not($is-pub)">
								<query-path><xsl:value-of select="$php-loc" /></query-path> <!-- output the query path for debugging -->
							</xsl:if>
							<xsl:copy-of select="document($php-loc)" />
						</xsl:when>
						<xsl:when test="$language = 'csharp'">
							<!-- path to c# script -->
							<xsl:variable name="c-loc" select="concat($request-location, '/cs/profiles.ashx', $query-string)" />
							<xsl:if test="not($is-pub)">
								<query-path><xsl:value-of select="$c-loc" /></query-path> <!-- output the query path for debugging -->
							</xsl:if>
							<xsl:copy-of select="document($c-loc)" />
						</xsl:when>
						<xsl:otherwise>
							<p>Invalid server side language. Please verify/configure <xsl:value-of select="$language" /> is the language you want to use.</p>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<!-- aggregate via XSL -->
				<xsl:otherwise>
					<xsl:element name="{$profile-node}s">
						<xsl:call-template name="profiles-with-xsl">
							<xsl:with-param name="current-path" select="replace($current-path, '/$', '')"/> <!-- current path -->
							<xsl:with-param name="skipping" select="$skipping"/> <!-- current path -->
						</xsl:call-template>
					</xsl:element>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>

	<!-- template to grab all the `profile-ext` xml files from production with XSL -->
	<xsl:template name="profiles-with-xsl">
		<xsl:param name="current-path" /> <!-- current path -->
		<xsl:param name="skipping" /> <!-- what file to skip -->
		<!-- calculate the current path to the production server and replace the root with the domain -->
		<xsl:variable name="domain-dir" select="concat(replace($current-path, $root, $domain), '/')" />
		<xsl:for-each select="doc($current-path)/list/file[contains(text(),'.pcf') and not(contains(text(), $skipping))]">
			<xsl:variable name="file-path" select="concat($domain-dir, replace(text(), 'pcf', $profile-ext))" />
			<xsl:if test="doc-available($file-path)">
				<xsl:copy-of select="doc($file-path)/profile" />
			</xsl:if>
		</xsl:for-each>
		<!-- get all directories -->
		<xsl:for-each select="doc($current-path)/list/directory">
			<xsl:call-template name="profiles-with-xsl">
				<xsl:with-param name="current-path" select="concat($current-path,'/',text())" />
				<xsl:with-param name="skipping" select="$skipping"/>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>

</xsl:stylesheet>
