<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                xmlns:html="http://www.w3.org/1999/xhtml"
                exclude-result-prefixes="html exsl"
                extension-element-prefixes="exsl"
                version="1.0">

<!--
# duptree - produce a shell script for copying files from Gallery XML files
#
# Version 1.2
#
# $Id: duptree.xsl,v 1.8 2002/09/18 11:53:05 ndw Exp $
#
# Copyright (C) 2002 Norman Walsh, All Rights Reserved.
#
# This program 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; version 2 dated June, 1991.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-->

<xsl:output method="text"/>

<xsl:param name="base" select="'/share/online/www/albums/'"/>
<xsl:param name="root" select="'/tmp/x/'"/>
<xsl:param name="indexname" select="'index'"/>
<xsl:param name="html.ext" select="'.html'"/>

<xsl:param name="dup" select="'cp'"/>
<!-- use dup=copy on Windows ... -->
<!-- use dup=ln -s to make a link farm -->

<xsl:strip-space elements="*"/>

<xsl:key name="albums" match="/albumTree/album" use="name"/>

<xsl:template match="albumTree">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="album">
  <xsl:variable name="numItems" select="count(.//nestedAlbum|.//photo)"/>
  <xsl:variable name="itemsPerPage" select="rows * cols"/>
  <xsl:variable name="numPages"
                select="floor(($numItems + $itemsPerPage - 1) div $itemsPerPage)"/>

  <xsl:text>mkdir </xsl:text>
  <xsl:value-of select="$root"/>
  <xsl:value-of select="name"/>
  <xsl:text>&#10;</xsl:text>

  <xsl:call-template name="write.album.pages">
    <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
    <xsl:with-param name="numPages" select="$numPages"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="write.album.pages">
  <xsl:param name="itemsPerPage" select="9"/>
  <xsl:param name="numPages" select="1"/>
  <xsl:param name="curPage" select="1"/>

  <xsl:apply-templates select="." mode="pages">
    <xsl:with-param name="curPage" select="$curPage"/>
    <xsl:with-param name="numPages" select="$numPages"/>
    <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
  </xsl:apply-templates>

  <xsl:if test="$curPage &lt; $numPages">
    <xsl:call-template name="write.album.pages">
      <xsl:with-param name="itemsPerPage" select="$itemsPerPage"/>
      <xsl:with-param name="numPages" select="$numPages"/>
      <xsl:with-param name="curPage" select="$curPage + 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template match="album" mode="pages">
  <xsl:param name="curPage" select="1"/>
  <xsl:param name="numPages" select="1"/>
  <xsl:param name="itemsPerPage" select="9"/>

  <xsl:apply-templates select=".//photo[position() &gt;= ($curPage - 1)*$itemsPerPage + 1
                                        and position() &lt;= $curPage*$itemsPerPage]"
                       mode="pages"/>
</xsl:template>

<xsl:template match="photo" mode="pages">
  <xsl:variable name="fullFilename">
    <xsl:value-of select="$base"/>
    <xsl:value-of select="../name"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="image/name"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="image/type"/>
  </xsl:variable>

  <xsl:variable name="sizedFilename">
    <xsl:value-of select="$base"/>
    <xsl:value-of select="../name"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="image/resizedName"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="image/type"/>
  </xsl:variable>

  <xsl:variable name="thumbFilename">
    <xsl:value-of select="$base"/>
    <xsl:value-of select="../name"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="thumbnail/name"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="thumbnail/type"/>
  </xsl:variable>

  <xsl:value-of select="$dup"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$fullFilename"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$root"/>
  <xsl:value-of select="../name"/>
  <xsl:text>/</xsl:text>
    <xsl:value-of select="image/name"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="image/type"/>
  <xsl:text>&#10;</xsl:text>

  <xsl:value-of select="$dup"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$sizedFilename"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$root"/>
  <xsl:value-of select="../name"/>
  <xsl:text>/</xsl:text>
    <xsl:value-of select="image/resizedName"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="image/type"/>
  <xsl:text>&#10;</xsl:text>

  <xsl:value-of select="$dup"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$thumbFilename"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$root"/>
  <xsl:value-of select="../name"/>
  <xsl:text>/</xsl:text>
    <xsl:value-of select="thumbnail/name"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="thumbnail/type"/>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>
