<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:wn="http://xmlns.com/wordnet/1.6/"
    xmlns:review="http://www.purl.org/stuff/rev#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:book="http://purl.org/NET/schemas/book/">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="seeAlso"/>
<xsl:param name="mbox_sha1sum"/>

<xsl:template match="opt">
    <xsl:variable name="homepage">
        <xsl:choose>
            <xsl:when test="contains(header/@link, 'view')">
                <xsl:value-of select="substring-before(substring-after(header/@link, '?url='), '&amp;')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring-after(header/@link, '?url=')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <rdf:RDF>
        <rdf:Description rdf:about="">
            <dc:title><xsl:value-of select="header/@title"/></dc:title>
            <dc:description><xsl:value-of select="header/@description"/></dc:description>
            <foaf:maker rdf:nodeID="me"/>
        </rdf:Description>

        <foaf:Person rdf:nodeID="me">
            <xsl:if test="$seeAlso">
                <rdfs:seeAlso rdf:resource="{$seeAlso}"/>
            </xsl:if>
            <xsl:if test="$mbox_sha1sum">
                <foaf:mbox_sha1sum><xsl:value-of select="$mbox_sha1sum"/></foaf:mbox_sha1sum>
            </xsl:if>
            <foaf:weblog rdf:resource="{$homepage}"/>
            <foaf:holdsAccount>
                <foaf:OnlineAccount>
                  <foaf:accountServiceHomepage rdf:resource="http://www.allconsuming.net"/>
                  <foaf:accountName><xsl:value-of select="substring-before(substring-after(header/@description, ' for '), '.')"/></foaf:accountName>
                </foaf:OnlineAccount>
            </foaf:holdsAccount>

            <xsl:variable name="description" select="/opt/header/@description"/>
            <xsl:for-each select="asins">
                <xsl:choose>
                    <xsl:when test="starts-with($description, 'Currently')">
                        <book:reading>
                            <xsl:call-template name="doBook"/>
                        </book:reading>
                    </xsl:when>
                    <xsl:when test="starts-with($description, 'Completed')">
                        <book:read>
                            <xsl:call-template name="doBook"/>
                        </book:read>
                    </xsl:when>
                    <xsl:when test="starts-with($description, 'Favorite')">
                        <book:favourite>
                            <xsl:call-template name="doBook"/>
                        </book:favourite>
                    </xsl:when>
                    <xsl:when test="starts-with($description, 'Purchased')">
                        <book:toRead>
                            <xsl:call-template name="doBook"/>
                        </book:toRead>
                    </xsl:when>
                    <xsl:when test="starts-with($description, 'Re-Read')">
                        <book:reading>
                            <xsl:call-template name="doBook"/>
                        </book:reading>
                        <book:read rdf:nodeID="{generate-id(.)}"/>
                    </xsl:when>
                    <xsl:otherwise/>
                </xsl:choose>
            </xsl:for-each>

        </foaf:Person>

    </rdf:RDF>
</xsl:template>

<xsl:template match="*"/>

<xsl:template name="doBook">
    <xsl:variable name="comment">
        <xsl:choose>
            <xsl:when test="@comment">
                <xsl:value-of select="normalize-space(@comment)"/>
            </xsl:when>
            <xsl:when test="comments">
                <xsl:value-of select="normalize-space(comments/@tdesc)"/>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:variable>
    <book:Book rdf:nodeID="{generate-id(.)}">
        <dc:title><xsl:value-of select="@title"/></dc:title>
        <book:isbn><xsl:value-of select="@asin"/></book:isbn>
        <xsl:if test="$comment != ''">
            <review:hasReview>
                <review:Review>
                    <review:reviewer rdf:nodeID="me"/>
                        <review:text><xsl:value-of select="$comment"/></review:text>
                </review:Review>
            </review:hasReview>
        </xsl:if>
        <foaf:page>
            <foaf:Document rdf:about="{@amazon_url}">
                <dc:title>Amazon.com: Books: <xsl:value-of select="@title"/></dc:title>
                <dc:description>Amazon page about <xsl:value-of select="@title"/></dc:description>
            </foaf:Document>
        </foaf:page>
        <foaf:page>
            <foaf:Document rdf:about="{@allconsuming_url}">
                <dc:title>All Consuming: Book Info: <xsl:value-of select="@title"/></dc:title>
                <dc:description>All Consuming page about <xsl:value-of select="@title"/></dc:description>
            </foaf:Document>
        </foaf:page>
        <foaf:page>
            <foaf:Document rdf:about="http://isbn.nu/{@asin}">
                <dc:title><xsl:value-of select="@title"/></dc:title>
                <dc:description>isbn.nu price comparision page for <xsl:value-of select="@title"/></dc:description>
            </foaf:Document>
        </foaf:page>
        <foaf:page>
            <foaf:Document rdf:about="http://www.aaabooksearch.com/ISBN/{@asin}">
                <dc:title>Compare Prices: <xsl:value-of select="@title"/></dc:title>
                <dc:description>AAABookSearch price comparision page for <xsl:value-of select="@title"/></dc:description>
            </foaf:Document>
        </foaf:page>
        <foaf:page>
            <foaf:Document rdf:about="http://www.bookfinder4u.com/IsbnSearch.aspx?mode=direct&amp;isbn={@asin}">
                <dc:title>BookFinder4U: <xsl:value-of select="@title"/></dc:title>
                <dc:description>BookFinder4U price comparision page for <xsl:value-of select="@title"/></dc:description>
            </foaf:Document>
        </foaf:page>
    </book:Book>
</xsl:template>

</xsl:stylesheet>