<?php

require 'lib.php';

// What kind of request?

if($_GET === array()) // do usage
{
    
readfile('feed-wizard.html');
    die();
}
else 
// prepare a feed
{
    if(
$debug)
        
header('Content-Type: text/plain; charset=UTF-8');
    else
        
header('Content-Type: application/rss+xml; charset=UTF-8');
}

// templates

$feedHead = <<<EOXML
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Today in %1\$s %2\$s</title>
        <link>http://irc.peeron.com/xkcd/map/map.html?lat=%1\$s&amp;long=%2\$s&amp;zoom=8</link>
        <description>Geohash reminder feed. A new entry appears every day (as soon as data for that day is available.)</description>

        <language>en</language>
        <pubDate>%3\$s</pubDate>
        <lastBuildDate>%3\$s</lastBuildDate>

        <atom:link href="%4\$s" rel="self" type="application/rss+xml" />

        <!--item>
            <title>Status: Still in development</title>
            <link>http://www.geohash.info/srv/feed.php?lat=%1\$s&amp;lon=%2\$s&amp;debug=true</link>
            <description>Still have some bugs to work out. Always verify the meetup with the linked Peeron map.</description>
            <pubDate>Wed, 04 Jun 2008 8:45:00 +0400</pubDate>
            <guid isPermaLink="true">http://www.geohash.info/srv/feed.php?lat=%1\$s&amp;lon=%2\$s&amp;debug=true</guid>
        </item-->


EOXML;

$feedBody = <<<EOXML
        <item>
            <title>Geohash %3\$s</title>
            <link>http://irc.peeron.com/xkcd/map/map.html?lat=%1\$s&amp;long=%2\$s&amp;date=%3\$s&amp;zoom=8</link>
            <comments>http://wiki.xkcd.com/geohashing/%3\$s_%1\$s_%2\$s</comments>
            <description>%5\$s</description>
            <pubDate>%4\$s</pubDate>
            <guid isPermaLink="false">geohash:%3\$s@%1\$s,%2\$s</guid>
        </item>
EOXML;

$feedFoot = <<<EOXML
    </channel>
</rss>
EOXML;

function 
writeFeedHead()
{
    global 
$feedHead$lat$lon$publishDate;
    
    
$selfURL sprintf("http://%s/srv/feed.php?lat=%s&lon=%s"$_SERVER['SERVER_NAME'], $lat['orig'], $lon['orig']);
    
    
printf($feedHeadhtmlspecialchars($lat['ID']), htmlspecialchars($lon['ID']), htmlspecialchars($publishDate), htmlspecialchars($selfURL));
}

function 
writeFeedBody()
{
    global 
$feedBody$dow$dowDate$localDate$lat$lon$publishDate;
    
    if(
$dow === false)
        return;
    
    
$meetup theAlgorithm($localDate['string'], $dow$lat$lon);
    
$staticMapURL createMeetupStaticMap($lat$lon$meetup);
    
$interactiveMapURL sprintf('http://irc.peeron.com/xkcd/map/map.html?lat=%s&long=%s&date=%s&zoom=8'$lat['ID'], $lon['ID'], $localDate['string']);
    
    
$text sprintf('<p>Meetup location:</p> <ul><li>%s %s</li><li>%s %s</li><li>%s %s</li></ul>  <p><a href="%s" title="Interactive map"><img src="%s" width="400" height="400" alt="Map of meetup" /></a></p> <p>Used Dow value of %s from %s</p>'$meetup['lat']['DDD'], $meetup['lon']['DDD'], $meetup['lat']['DMM'], $meetup['lon']['DMM'], $meetup['lat']['DMS'], $meetup['lon']['DMS'], $interactiveMapURL$staticMapURL$dow$dowDate['string']);
    
    
debugComment('Feed item text'$text);
    
    
printf($feedBodyhtmlspecialchars($lat['ID']), htmlspecialchars($lon['ID']), htmlspecialchars($localDate['string']), htmlspecialchars($publishDate), htmlspecialchars($text));
}

function 
writeFeedFoot()
{
    global 
$feedFoot;
    
    echo 
$feedFoot;
}

// gather requirements

$lat getCoord('lat'$LATITUDE);
$lon getCoord('lon'$LONGITUDE);

// initial calculations

$is30WRule = ($lon['full'] > -30);
$hoursOffset $lon['full'] * 24 360;
$secondsOffset $hoursOffset 3600;

debugComment('is30WRule'$is30WRule);
debugComment('timeOffset'$hoursOffset);

$genTime time();
$localTime $genTime $secondsOffset;

$localDate getDateFormatted($localTime);
$dowDate calcDowDate($localTime$is30WRule$localDate);

debugComment('Local date'$localDate);
debugComment('DOW date'$dowDate);

// retrieve data

$dow grabDOW($dowDate);

// compute publishing date

$dowUTC strtotime("{$dowDate['string']}T09:30-04:30");
$midnightUTC strtotime("{$localDate['string']}T00:00Z") - $secondsOffset;

if(
$debug)
{
    
$dowDateUTC date('r'$dowUTC);
    
$midnightDateUTC date('r'$midnightUTC);
    
debugComment('DOW UTC'$dowDateUTC);
    
debugComment('Midnight UTC'$midnightDateUTC);
}

$publishTimeUTC max($dowUTC$midnightUTC);
$publishDate date('r'$publishTimeUTC);

// output feed

writeFeedHead();
writeFeedBody();
writeFeedFoot();

?>