<?php

//truncates a string at a reasonable spot.
	function iTrunc($string, $length) {
		if (strlen($string)<=$length) {
			return $string;
		}
		
		$pos = strrpos($string,".");
		if ($pos>=$length-4) {
			$string = substr($string,0,$length-4);
			$pos = strrpos($string,".");
		}
		if ($pos>=$length*0.4) {
			return substr($string,0,$pos+1)." ...";
		}
		
		$pos = strrpos($string," ");
		if ($pos>=$length-4) {
			$string = substr($string,0,$length-4);
			$pos = strrpos($string," ");
		}
		if ($pos>=$length*0.4) {
			return substr($string,0,$pos)." ...";
		}
		
		return substr($string,0,$length-4)." ...";
			
	}

echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
echo "<rss version=\"2.0\" \n";
echo "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n";
echo "  xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
echo "	<channel>\n";
echo "		<title>Fodi: A comic by Matt Buchwald</title>\n";
echo "		<link>http://www.baldninja.com/</link>\n";
echo "		<description>The latest comics and news from \"Fodi.\"</description>\n";
echo "		<language>en-us</language>\n";
echo "		<copyright>Copyright 2003-2005 Matt Buchwald and Steven Coad</copyright>\n";
echo "		<pubDate>" . date("D, d M Y") . " 00:05:00 -0500</pubDate>\n";
echo "		<lastBuildDate>" . date("r") . "</lastBuildDate>\n";
echo "\n";
echo "		<ttl>60</ttl>\n";
echo "		<image>\n";
echo "			<url>http://www.baldninja.com/images/fodi.png</url>\n";
echo "			<title>Fodi: the comic</title>\n";
echo "			<link>http://www.baldninja.com.com/</link>\n";
echo "			<width>185</width>\n";
echo "			<height>45</height>\n";
echo "		</image>\n";
echo "\n";


//----------------- Code to fetch latest five comics-------------
$connection = @mysql_connect( "localhost", "baldnin_fcdsuser", "N4v1g4t3" )
	or die( "Unable to connect to the database" );

@mysql_select_db( "baldnin_fcds" );

// Getting the comic: if there is no argument specified, then the comic
// is the greatest id that is less than or equal to the current date

$current_date = date("Y")."-".date("m")."-".date("d");

$sql = "select date from comic where date <= '$current_date' ORDER BY date ASC";
$rs = mysql_query( $sql, $connection );

while( $row = mysql_fetch_array($rs) ) {
  $latest_comic_date =  $row[0];
}

$sql = "select comic_id from comic where date = '$latest_comic_date'";
$rs = mysql_query( $sql, $connection );
while( $row = mysql_fetch_array($rs) ) {
  $first_comic_to_display_id =  $row[0];
}
if ($first_comic_to_display_id < 5 ) {$last_comic_to_display_id = 1;}
else { $last_comic_to_display_id = $first_comic_to_display_id - 4; }

//loop to grab latest 5 comics
    for ($j = $first_comic_to_display_id; $j >= $last_comic_to_display_id; $j--) {

  $comic_comment_matt = null;
  $comic_comment_steve = null;

// Gets the comic information from the database

// First, get basic comic information
$sql = "select title, category, category_sequence_num, date from comic where comic_id = '$j'";
$rs = mysql_query( $sql, $connection );

while( $row = mysql_fetch_array($rs) ) {
  $comic_title = $row["title"];
  $series_id = $row['category'];
  $comic_series_num = $row["category_sequence_num"];
  $comic_date = $row["date"];
}

// Then, get series title
$sql = "select name from category where category_id = '$series_id'";
$rs = mysql_query( $sql, $connection );

while( $row = mysql_fetch_array($rs) ) {
  $comic_series = $row["name"];
}

// Finally, get comments - from old database
$connection = @mysql_connect( "localhost", "baldnin_comics", "navigate" )
	or die( "Unable to connect to the database" );

@mysql_select_db( "baldnin_comics" );

$sql = "select comment_matt, comment_steve from comments where comic_num = '$j'";
$rs = mysql_query( $sql, $connection );

while( $row = mysql_fetch_array($rs) ) {
  $comic_comment_matt = $row["comment_matt"];
  $comic_comment_steve = $row["comment_steve"];
}

//Go back to new database
$connection = @mysql_connect( "localhost", "baldnin_fcdsuser", "N4v1g4t3" )
	or die( "Unable to connect to the database" );

@mysql_select_db( "baldnin_fcds" );

// Set up the series info (x of y)
$sql = "select count(*) from comic where category = '$series_id' and date <= '$current_date'";
$rs = mysql_query( $sql, $connection ); 
while( $row = mysql_fetch_array($rs) ) {
  $comic_series_length = $row[0];
}

// Finds the extension of the comic graphic file
$comic_filename = "http://www.baldninja.com/images/comic/".$comic_date{2}.$comic_date{3}.$comic_date{5}.$comic_date{6}.$comic_date{8}.$comic_date{9};
$comic_path = "images/comic/".$comic_date{2}.$comic_date{3}.$comic_date{5}.$comic_date{6}.$comic_date{8}.$comic_date{9};

if( is_file($comic_path.".jpg") ) {
  $extension = ".jpg";
}
else {
  $extension = ".png";
}


echo "		<item>\n";
echo "			<title>Strip for " . $comic_date . ": " . $comic_title . "</title>\n";
echo "			<link>http://www.baldninja.com/archive.php?comic=" . $comic_date . "</link>\n";
echo "			<description>&lt;img src=&quot;" . $comic_filename.$extension . "&quot; alt=&quot;Comic Graphic: " . $comic_date . ": " . $comic_title . "&quot;&gt;</description>\n";
echo "			<guid isPermaLink=\"true\">http://www.baldninja.com/archive.php?comic=" . $comic_date . "</guid>\n";
echo "			<pubDate>" . date("r",strtotime($comic_date)) . "</pubDate>\n";
echo "		</item>\n";

} //end comic find for loop

//make the news feeds
include('http://www.baldninja.com/forum/SSI.php?ssi_function=boardNewsXML;board=1;limit=5;length=32767;'); 

echo "\n</channel>\n";
echo "</rss>\n";

//saveFeed("fodi.xml",$feed); 

?>