<?xml version="1.0"?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rss="http://purl.org/rss/1.0/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	version="1.0"
>
	<xsl:output method="text" />

	<xsl:template match="/" xml:space="preserve">
# ESF spec:
#  http://www.aquarionics.com/article/name/esf
# How this is done:
#  http://agresticism.org/furrow/2003/6/18/rdf-rss_to_esf/
title	<xsl:value-of select="//rss:channel/rss:title|//channel/title" />
link	<xsl:value-of select="//rss:channel/rss:link|//channel/link" />
<xsl:apply-templates select="//rss:item|//item" /></xsl:template>

	<xsl:template match="//rss:item" xml:space="preserve">
<xsl:apply-templates select="./dc:date" />	<xsl:value-of select="./rss:title" />	<xsl:value-of select="./rss:link" /></xsl:template>

	<xsl:template match="//item" xml:space="preserve">
<xsl:call-template name="stupid_dates" />	<xsl:value-of select="./title" />	<xsl:call-template name="link_or_guid" /></xsl:template>

	<xsl:template name="stupid_dates">
		<xsl:choose>
			<xsl:when test="./dc:date != ''">
				<xsl:apply-templates select="./dc:date" />
			</xsl:when>

			<xsl:otherwise>
				<xsl:apply-templates select="./pubDate" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="link_or_guid">
		<xsl:choose>
			<xsl:when test="./guid != '' and ./guid[@isPermalink] != 'false'">
				<xsl:value-of select="./guid" />
			</xsl:when>

			<xsl:otherwise>
				<xsl:value-of select="./link" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="//dc:date">
		<xsl:variable name="date" select="." />
		<xsl:variable name="year" select="number(substring($date, '1', '4'))" />
		<xsl:variable name="month" select="number(substring($date, '6', '2'))" />
		<xsl:variable name="day" select="number(substring($date, '9', '2'))" />
		<xsl:variable name="hours" select="number(substring($date, '12', '2'))" />
		<xsl:variable name="minutes" select="number(substring($date, '15', '2'))" />
		<xsl:variable name="seconds" select="number(substring($date, '18', '2'))" />

		<xsl:variable name="output">
			<xsl:call-template name="epoch_time">
				<xsl:with-param name="year" select="$year - 1970" />
				<xsl:with-param name="month" select="$month" />
				<xsl:with-param name="day" select="$day" />
				<xsl:with-param name="hours" select="$hours" />
				<xsl:with-param name="minutes" select="$minutes" />
				<xsl:with-param name="seconds" select="$seconds" />
			</xsl:call-template>
		</xsl:variable>

		<xsl:value-of select="number($output)" />
	</xsl:template>

	<xsl:template match="//pubDate">
		<xsl:variable name="date" select="." />
		<xsl:variable name="day" select="number(substring($date, '6', '2'))" />
		<xsl:variable name="month_name" select="substring($date, '9', '3')" />
		<xsl:variable name="year" select="number(substring($date, '13', '4'))" />
		<xsl:variable name="hours" select="number(substring($date, '18', '2'))" />
		<xsl:variable name="minutes" select="number(substring($date, '21', '2'))" />
		<xsl:variable name="seconds" select="number(substring($date, '24', '2'))" />

		<xsl:variable name="month">
			<xsl:choose>
				<xsl:when test="$month_name='Jan'">1</xsl:when>
				<xsl:when test="$month_name='Feb'">2</xsl:when>
				<xsl:when test="$month_name='Mar'">3</xsl:when>
				<xsl:when test="$month_name='Apr'">4</xsl:when>
				<xsl:when test="$month_name='May'">5</xsl:when>
				<xsl:when test="$month_name='Jun'">6</xsl:when>
				<xsl:when test="$month_name='Jul'">7</xsl:when>
				<xsl:when test="$month_name='Aug'">8</xsl:when>
				<xsl:when test="$month_name='Sep'">9</xsl:when>
				<xsl:when test="$month_name='Oct'">10</xsl:when>
				<xsl:when test="$month_name='Nov'">11</xsl:when>
				<xsl:when test="$month_name='Dec'">12</xsl:when>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="output">
			<xsl:call-template name="epoch_time">
				<xsl:with-param name="year" select="$year - 1970" />
				<xsl:with-param name="month" select="$month" />
				<xsl:with-param name="day" select="$day" />
				<xsl:with-param name="hours" select="$hours" />
				<xsl:with-param name="minutes" select="$minutes" />
				<xsl:with-param name="seconds" select="$seconds" />
			</xsl:call-template>
		</xsl:variable>

		<xsl:value-of select="number($output)" />
	</xsl:template>

	<xsl:template name="epoch_time">
		<xsl:param name="year" select="0" />
		<xsl:param name="month" select="1" />
		<xsl:param name="day" select="1" />
		<xsl:param name="hours" select="0" />
		<xsl:param name="minutes" select="0" />
		<xsl:param name="seconds" select="1" />

		<xsl:variable name="leap_year_days">
			<xsl:call-template name="epoch_time_leap_years">
				<xsl:with-param name="years" select="$year" />
				<xsl:with-param name="months" select="$month" />
				<xsl:with-param name="days" select="$day" />
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="month_days">
			<xsl:call-template name="epoch_time_days_gone">
				<xsl:with-param name="months" select="$month" />
			</xsl:call-template>
		</xsl:variable>

		<!-- I've screwed up somewhere. Take 1 off the day. -->
		<xsl:variable name="total_days" select="($year * 365) + $leap_year_days + $month_days + $day - 1" />

		<xsl:value-of select="((((($total_days * 24) + $hours) * 60) + $minutes) * 60) + $seconds" />
	</xsl:template>

	<xsl:template name="epoch_time_leap_years">
		<xsl:param name="years" select="0" />
		<xsl:param name="months" select="1" />
		<xsl:param name="days" select="1" />

		<xsl:variable name="years_adjusted">
			<xsl:choose>
				<xsl:when test="$months &#62; 2">
					<xsl:value-of select="$years" />
				</xsl:when>

				<!-- If it IS 29 February then that day will be added in the days bit. If it's not, then it's either in February or January and this year shouldn't be counted. -->

				<xsl:otherwise>
					<xsl:value-of select="$years - 1" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:value-of select="floor($years_adjusted * 0.25)" />
	</xsl:template>

	<xsl:template name="epoch_time_days_gone">
		<xsl:param name="months" select="1" />

		<xsl:choose>
			<xsl:when test="$months = 1">0</xsl:when>
			<xsl:when test="$months = 2">31</xsl:when>
			<xsl:when test="$months = 3">59</xsl:when>
			<xsl:when test="$months = 4">90</xsl:when>
			<xsl:when test="$months = 5">120</xsl:when>
			<xsl:when test="$months = 6">151</xsl:when>
			<xsl:when test="$months = 7">181</xsl:when>
			<xsl:when test="$months = 8">212</xsl:when>
			<xsl:when test="$months = 9">243</xsl:when>
			<xsl:when test="$months = 10">273</xsl:when>
			<xsl:when test="$months = 11">304</xsl:when>
			<xsl:when test="$months = 12">334</xsl:when>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
