# agolrss.php - hack to work around dynamic rss problems
#
# Some news aggregators barf on dynamic rss (livejournal, bloglines). So
# you can advertise yourblog/foo.xml as the rss url of your blog and hit
# this script whenever you update your blog. Not pretty, but temporary.

# begin user vars ##########################################################
#
# match to what you set in agolblog.php
#

# Specify where you want the rss file to be written
$rssfile = "./blogonmytoe.xml";

# Specify where you will store your posts.  This directory
# must be in the document root because a subdirectory of it
# will contain the permalinked archive copies of posts.
$datadir = "./agolbloga.data";

# Set the title and subtitle of your blog
$title = "Example Blog";
$subtitle = "The Agolbloga example blog...";

# What's the base url of this Agolbloga installation?
$baseurl = "http://127.0.0.1/example/agolbloga";

$scriptname = "index.php";

#
# end user vars ##########################################################


print "Generating rss ($rssfile)...";

$dater = date("r");

$rssstring = "




$title
$subtitle
$baseurl/$scriptname
$dater
en-us
Agolbloga v0.1

";


#
# get array of posts
# (use scandir in php5)
#
$dir = "$datadir";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $modtime = filemtime("$datadir/$filename");
    $combo = "$modtime" . ":" . "$filename";
    $files[] = $combo;
}
# sort them, reverse chronologically (standard blog order)
rsort($files);


#
# go through all the files and directories in the 
# datadir and print anything ending in .txt
#
foreach ( $files as $i) {
    if ( preg_match("/\.txt$/", $i) ) {
	$rssstring = $rssstring . "\n\n";
        list($date, $name) = split(":", $i);
        if ( ! $file = fopen ("$datadir/$name", "r") ) {
            print "error: could not open blurb!\n";
            exit;
        } else {
            $count = 0;
            while (!feof($file)) {
                $count++;
                $string = fgets($file, 4096);
                $trimmed = rtrim($string);
                if ( $count ==1 ) {
                    $rssstring = $rssstring .  "$trimmed\n";
                    $rssstring = $rssstring .  "\n";
            $rssstring = $rssstring .  "$baseurl/$scriptname?arch=$name\n";
        }
        $rssstring = $rssstring .  "\n\n";
    }
}

$rssstring = $rssstring . "


";

if ( ! $file = fopen ("$rssfile", "w") ) {
    print "error: could not open rssfile!\n";
    exit;
} else {
    if ( fwrite ($file, $rssstring) === FALSE ) {
        print "Cannot write to file!\n";
    }
}
fclose($file);

print "

Done!";