<xsl:stylesheet 
    xmlns:xsl  ="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:h    ="http://www.w3.org/1999/xhtml"
    xmlns:stm  ="http://www.w3.org/2001/sw/schedScrape.xsl?"
    xmlns:usps ="http://www.w3.org/2000/10/swap/pim/usps#"
    xmlns:con  ="http://www.w3.org/2000/10/swap/pim/contact#"
    xmlns:cal  ="http://www.w3.org/2002/12/cal/icaltzd#"
    xmlns:map  ="http://www.w3.org/2000/10/swap/pim/earthMap#"
    xmlns:k    ="http://opencyc.sourceforge.net/daml/cyc.daml#"
    xmlns:dc   ="http://purl.org/dc/elements/1.1/" 
    xmlns:r    ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:s    ="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:dt   ="http://www.w3.org/2001/XMLSchema#"
    xmlns:ss   ="http://www.w3.org/2001/sw/schedScrape?"
    >
<!-- oops... need new cal schema -->

<!--
  $Id: schedScrape.xsl,v 1.18 2005/04/14 17:16:21 connolly Exp $
  based on http://www.w3.org/People/Connolly/smart-home.xsl
	Id: smart-home.xsl,v 1.12 2002/03/14 05:14:25 connolly Exp 
  -->

<xsl:output method="xml" indent="yes"/>

<xsl:variable name="RDF_ns"
	      select='"http://www.w3.org/1999/02/22-rdf-syntax-ns#"'/>
<xsl:variable name="xsdt"
              select ='"http://www.w3.org/2001/XMLSchema#"' />

<xsl:param name="SchedFor" />

<xsl:template match="h:html">
  <r:RDF>

    <!-- title/date scraping lifted from
         group-scrape.xsl,v 1.4 2001/12/03 16:18:51
      -->
    <xsl:variable name="RCSDate"
         select = 'normalize-space(substring-before(substring-after(
					h:body/h:address,
					concat("$", "Date:")), "$"))'/>
    <!-- convert yyyy/mm/dd hh:mm:ss to yyyy-mm-ddThh:mm:ssZ -->
    <xsl:variable name="ISODate"
         select = 'concat(translate($RCSDate, "/ ", "-T"), "Z")'/>

    <cal:Vcalendar r:about="">
      <dc:source>
        <r:Description>
          <dc:date><xsl:value-of select="$ISODate"/></dc:date>
          <dc:title><xsl:value-of select="normalize-space(h:head/h:title)"/></dc:title>
	</r:Description>
      </dc:source>


      <xsl:for-each select='.//h:dl[@class="stm:schedule"]/h:dd'>
	<cal:component>
	  <xsl:call-template name="event"/>
	</cal:component>
      </xsl:for-each>
    </cal:Vcalendar>

  </r:RDF>
</xsl:template>



<xsl:template name="event">
  <xsl:variable name="id" select="preceding-sibling::h:dt[1]/@id"/>
  <xsl:variable name="when"  select="normalize-space(preceding-sibling::h:dt[1]//text())"/>
  <xsl:variable name="where" select="normalize-space(preceding-sibling::h:dt[1]//h:strong)"/>

  <xsl:variable name="eventLabel" select="h:strong | h:b"/>

  <cal:Vevent r:ID='{$id}'>

	<!-- WHAT -->
	<r:value><xsl:value-of select='normalize-space($eventLabel)'/></r:value>
	<cal:summary><xsl:value-of select='normalize-space($eventLabel)'/></cal:summary>

	<!-- WHO -->
	<xsl:if test="$SchedFor">
          <k:socialParticipants r:resource="{$SchedFor}" />
	</xsl:if>
    <xsl:for-each select="h:em">
      <xsl:variable name="aka" select='normalize-space()'/>

      <xsl:variable name="mbox"
	select='//*[@class="stm:people"]/*[h:em = $aka]/h:code/text()'/>

      <xsl:choose>
        <xsl:when test='$mbox'>
	  <cal:attendee r:parseType="Resource">
	    <cal:calAddress r:resource='{$mbox}'/>
	  </cal:attendee>

          <k:socialParticipants>
            <r:Description r:about='{concat("#who_", $mbox)}'>
              <r:value><xsl:value-of select='.'/></r:value>
	      <con:mailbox r:resource='{concat("mailto:", $mbox, "@w3.org")}'/>
            </r:Description>
          </k:socialParticipants>
        </xsl:when>

        <xsl:otherwise>
          <xsl:message>WHO??? cannot find mailbox for: <xsl:value-of select='$aka'/>
what: [<xsl:value-of select="normalize-space($eventLabel)"/>]
when: [<xsl:value-of select="$when"/>]
where: [<xsl:value-of select="$where"/>]

          </xsl:message>
        </xsl:otherwise>
      </xsl:choose>

    </xsl:for-each>

    <xsl:call-template name="ss:whenProps">
      <xsl:with-param name="when" select="$when"/>
    </xsl:call-template>

    <xsl:if test="$where">
      <xsl:call-template name="ss:whereProps">
        <xsl:with-param name="where" select="$where"/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="$eventLabel/h:a">
        <con:homePage r:resource='{$eventLabel/h:a/@href}'/>
        <cal:url r:resource='{$eventLabel/h:a/@href}'/>
      <!-- hmm.. other sorts of links: for group meetings,
	   e.g. IETF meetings, link to the group home

           or: link to proceedings, rather that event home page

           maybe rather than con:homepage, I should just
	   say this is *some* page about the event
            -->

    </xsl:if>
    <xsl:for-each select='.//h:a[@rel="seeAlso"]'> <!-- MAGIC -->
      <s:seeAlso r:resource='{@href}'/>
    </xsl:for-each>

  </cal:Vevent>

  <!-- @@TODO: cite presentation matierals, event records, etc. -->

</xsl:template>



<xsl:template name="ss:whenProps">
  <!-- formats: dd mmm yyyy
                dd-dd mm yyyy
                mmm yyyy
                ~ mmm yyyy
       @@ test it! -->

  <xsl:param name="when"/>
  
	<!-- WHEN -->
    <xsl:variable name="days" select='substring-before($when, " ")'/>
    <xsl:variable name="sday" select='number(substring-before($days, "-"))'/>
    <xsl:variable name="eday" select='number(substring-after($days, "-"))'/>

    <xsl:variable name="month">
      <xsl:call-template name="monthIndex">
        <xsl:with-param name="name" select='substring-before(substring-after($when, " "), " ")'/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="year" select='number(substring-after(substring-after($when, " "), " "))'/>

    <xsl:choose>
      <xsl:when test='$eday > 0'>
	<xsl:variable name='start'
		      select='concat(
		      format-number($year, "0000"), "-",
		      format-number($month, "00"), "-",
		      format-number($sday, "00"))'/>
	<xsl:variable name='end'>
	  <xsl:call-template name="nextDay">
	    <xsl:with-param name="year" select="$year" />
	    <xsl:with-param name="m" select="$month" />
	    <xsl:with-param name="d" select="$eday" />
	  </xsl:call-template>
	</xsl:variable>

	<!-- @@bummer... sax2rdf doesn't support attributes on propElts -->
        <k:startingDate>
          <r:Description dt:date='{$start}'/>
	</k:startingDate>

        <k:endingDate>
          <r:Description dt:date='{$end}'/>
        </k:endingDate>


	<cal:dtstart r:datatype='{concat($xsdt, "date")}'>
	  <xsl:value-of select="$start" />
	</cal:dtstart>
	<cal:dtend r:datatype='{concat($xsdt, "date")}'>
	  <xsl:value-of select="$end" />
	</cal:dtend>
	
      </xsl:when>


      <xsl:when test='number($days) > 0'>
	<xsl:variable name='date'
		      select='concat(
		      format-number($year, "0000"), "-",
		      format-number($month, "00"), "-",
		      format-number(number($days), "00"))'/>
	<xsl:variable name='end'>
	  <xsl:call-template name="nextDay">
	    <xsl:with-param name="year" select="$year" />
	    <xsl:with-param name="m" select="$month" />
	    <xsl:with-param name="d" select="number($days)" />
	  </xsl:call-template>
	</xsl:variable>

        <k:startingDate>
          <r:Description dt:date='{$date}'/>
        </k:startingDate>

        <k:endingDate>
          <r:Description dt:date='{$date}'/>
        </k:endingDate>

	<cal:dtstart r:datatype='{concat($xsdt, "date")}'>
	  <xsl:value-of select="$date" />
	</cal:dtstart>
	<cal:dtend r:datatype='{concat($xsdt, "date")}'>
	  <xsl:value-of select="$end" />
	</cal:dtend>

      </xsl:when>

      <xsl:otherwise>
	<!-- hmm... is 2004-02 really an XSDT date? -->
        <k:startingDate r:datatype='{concat($xsdt, "date")}'>
          <xsl:value-of select='concat(
	      format-number($year, "0000"), "-",
	      format-number($month, "00")
	  )'/>
        </k:startingDate>
        <k:endingDate r:datatype='{concat($xsdt, "date")}'>
          <xsl:value-of select='concat(
	      format-number($year, "0000"), "-",
	      format-number($month, "00")
	  )'/>
        </k:endingDate>
      </xsl:otherwise>
    </xsl:choose>

</xsl:template>



<xsl:template name="monthIndex">
  <xsl:param name="name"/>
  <!-- this takes me back to COBOL days. -->

  <xsl:variable name="mmm">
    <xsl:value-of select='substring(translate($name, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1, 3)'/>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test='$mmm = "JAN"'>
      <xsl:value-of select='1'/>
    </xsl:when>

    <xsl:when test='$mmm = "FEB"'>
      <xsl:value-of select='2'/>
    </xsl:when>

    <xsl:when test='$mmm = "MAR"'>
      <xsl:value-of select='3'/>
    </xsl:when>

    <xsl:when test='$mmm = "APR"'>
      <xsl:value-of select='4'/>
    </xsl:when>

    <xsl:when test='$mmm = "MAY"'>
      <xsl:value-of select='5'/>
    </xsl:when>

    <xsl:when test='$mmm = "JUN"'>
      <xsl:value-of select='6'/>
    </xsl:when>

    <xsl:when test='$mmm = "JUL"'>
      <xsl:value-of select='7'/>
    </xsl:when>

    <xsl:when test='$mmm = "AUG"'>
      <xsl:value-of select='8'/>
    </xsl:when>

    <xsl:when test='$mmm = "SEP"'>
      <xsl:value-of select='9'/>
    </xsl:when>

    <xsl:when test='$mmm = "OCT"'>
      <xsl:value-of select='10'/>
    </xsl:when>

    <xsl:when test='$mmm = "NOV"'>
      <xsl:value-of select='11'/>
    </xsl:when>

    <xsl:when test='$mmm = "DEC"'>
      <xsl:value-of select='12'/>
    </xsl:when>

  </xsl:choose>

</xsl:template>

<xsl:template name="nextDay">
  <!-- inspired by the COBOL response to ncm's Coding Challenge
    http://www.advogato.org/article/804.html
    -->

  <xsl:param name="year" />
  <xsl:param name="m" />
  <xsl:param name="d" />

  <xsl:variable name="dout">
    <xsl:choose>
      <xsl:when test='$d &lt; 28'><xsl:value-of select='$d + 1'/></xsl:when>

      <xsl:otherwise>

	<xsl:variable name="monthMax">
	  <xsl:choose>
	    <xsl:when test='($m = 09) or
			    ($m = 04) or
			    ($m = 06) or
			    ($m = 11)'>
	      <xsl:value-of select='30' />
	    </xsl:when>
	    <xsl:when test='($m = 1) or
			    ($m = 3) or
			    ($m = 5) or
			    ($m = 7) or
			    ($m = 8) or
			    ($m = 10) or
			    ($m = 12)'>
	      <xsl:value-of select='31' />
	    </xsl:when>

	    <xsl:otherwise>
	      <xsl:variable name="yy" select='$year mod 100' />

	      <!-- @@ugh... leaving out some century logic -->
	      <xsl:choose>
		<xsl:when test='($yy = 04) or
				($yy = 08) or
				($yy = 12) or
				($yy = 16) or
				($yy = 20) or
				($yy = 24) or
				($yy = 28) or
				($yy = 32) or
				($yy = 36) or
				($yy = 40) or
				($yy = 44) or
				($yy = 48) or
				($yy = 52) or
				($yy = 56) or
				($yy = 60) or
				($yy = 64) or
				($yy = 68) or
				($yy = 72) or
				($yy = 76) or
				($yy = 80) or
				($yy = 84) or
				($yy = 88) or
				($yy = 92) or
				($yy = 96) '>
		  <xsl:value-of select='29' />
		</xsl:when>
		<xsl:otherwise>
		  <xsl:value-of select='28' />
		</xsl:otherwise>
	      </xsl:choose>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:variable>

	<xsl:choose>
	  <xsl:when test='$d + 1 &gt; $monthMax'>
	    <xsl:value-of select='1'/>
	</xsl:when>
	<xsl:otherwise><xsl:value-of select='$d + 1'/></xsl:otherwise>
	</xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name='mout'>
    <xsl:choose>
      <xsl:when test='$dout = 1'>
	<xsl:choose>
	  <xsl:when test='$m = 12'>
	    <xsl:value-of select='1'/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select='$m + 1' />
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='$m' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name='yout'>
    <xsl:choose>
      <xsl:when test='$mout = 1 and $dout = 1'>
	<xsl:value-of select='$year + 1' />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='$year' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of select='concat(
			format-number($yout, "0000"), "-",
			format-number($mout, "00"), "-",
			format-number($dout, "00"))'/>

</xsl:template>


<xsl:template name="ss:whereProps">
  <!-- formats: Cityname, Countryname
                Cityname, ST
       @@ test it!
       assume unique names for countries within a file
       depend on unique names for U.S. states
    -->

  <xsl:param name="where"/>

  <xsl:variable name="whereCity"
    select='normalize-space(substring-before($where, ","))'/>
  <xsl:variable name="whereRegion"
    select='normalize-space(substring-after($where, ","))'/>

  <cal:location><xsl:value-of select='$where'/></cal:location>

  <k:eventOccursAt>
    <k:City r:about="#place_{concat($whereRegion, '_', $whereCity)}">
      <xsl:choose>
        <xsl:when test='string-length($whereRegion) = 2'>
          <usps:cityName><xsl:value-of select='$whereCity'/></usps:cityName>
          <k:inRegion>
            <r:Description r:about="#us_state_{$whereRegion}">
              <usps:stateAbbr><xsl:value-of select='$whereRegion'/></usps:stateAbbr>
            </r:Description>
          </k:inRegion>
          
          <map:cityName><xsl:value-of select='$whereCity'/></map:cityName>
          <k:inRegion>
            <r:Description r:about="http://opencyc.sourceforge.net/daml/cyc.daml#UnitedStatesOfAmerica">
              <map:countryName>U.S.A.</map:countryName>
            </r:Description>
          </k:inRegion>
        </xsl:when>
        
        <xsl:otherwise>
          <map:cityName><xsl:value-of select='$whereCity'/></map:cityName>
          <k:inRegion>
            <r:Description r:about="#region_{$whereRegion}">
              <map:countryName><xsl:value-of select='$whereRegion'/></map:countryName>
            </r:Description>
          </k:inRegion>
        </xsl:otherwise>
      </xsl:choose>
    </k:City>
  </k:eventOccursAt>
</xsl:template>

<!-- don't pass text thru -->
<xsl:template match="text()|@*">
</xsl:template>


</xsl:stylesheet>
