<?php 
if ( empty($feed) ) {
	$feed = 'atom';
	$withcomments = 1;
	$doing_rss = 1;
	require('wp-blog-header.php');
}
	header('Content-type: application/atom+xml;charset=' . get_settings('blog_charset'), true);
	echo '<?xml version="1.0" encoding="'.get_settings('blog_charset').'"?'.'>'; 
?>

<?php echo '<?xml-stylesheet href="'; ?><?php bloginfo('stylesheet_directory'); ?><?php echo '/atom.css" type="text/css"'.'?>'; ?>

<?php
$i = 0;
if (have_posts()) :
  while (have_posts()) : the_post();
	if ($i < 1) {
		$i++;
?>

<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom">
	<title>
		<?php if (is_single() || is_page()) {
			echo "Comments on: "; the_title_rss();
		} else {
			bloginfo_rss("name"); echo " Comments";
		} ?>
	</title>
	<?php if (bloginfo_rss("description")) { ?>
		<subtitle><?php bloginfo_rss("description") ?></subtitle>
	<?php } ?>
	<id><?php bloginfo('url') ?>/comments/</id>
	<link href="<?php (is_single()) ? permalink_single_rss() : bloginfo_rss("url") ?>" rel="alternate" type="<?php bloginfo('html_type'); ?>"/>
	<link href="<?php bloginfo_rss("comments_rss2_url") ?>" rel="self" type="application/atom+xml"/>
	<updated><?php echo gmdate('Y-m-d\TH:i:s\Z'); ?></updated>
	<rights>Copyright <?php echo mysql2date('Y', get_lastpostdate('blog')); ?></rights>
	<author>
		<name>Various authors</name>
	</author>
	<generator version="<?php echo $wp_version ?>" uri="http://wordpress.org/">Wordpress</generator>

	<?php 
		if (is_single() || is_page()) {
			$comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_author_email, 
			comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, 
			$wpdb->posts.ID, $wpdb->posts.post_password FROM $wpdb->comments 
			LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE comment_post_ID = '$id' 
			AND $wpdb->comments.comment_approved = '1' AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'page') 
			AND post_date < '".date("Y-m-d H:i:59")."' 
			ORDER BY comment_date LIMIT " . get_settings('posts_per_rss') );
		} else { // if no post id passed in, we'll just use the last 10 comments.
			$comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_author_email, 
			comment_author_url, comment_date, comment_date_gmt, comment_content, comment_post_ID, 
			$wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_name, $wpdb->posts.post_password FROM $wpdb->comments 
			LEFT JOIN $wpdb->posts ON comment_post_id = id WHERE $wpdb->posts.post_status = 'publish' 
			AND $wpdb->comments.comment_approved = '1' AND post_date < '".date("Y-m-d H:i:s")."'  
			ORDER BY comment_date DESC LIMIT " . get_settings('posts_per_rss') );
		}
		// this line is WordPress' motor, do not delete it.
		if ($comments) {
			foreach ($comments as $comment) {
		?>

	<entry>
		<title>Re: <?php echo $comment->post_title; ?></title>
		<link href="<?php comment_link() ?>" rel="alternate" type="text/html" />
		<author>
			<name><?php comment_author_rss() ?></name>
			<?php if (get_comment_author_url()) {
				echo '<uri>'.get_comment_author_url().'</uri>';
			} ?>
		</author>
		<id><?php echo get_permalink($comment->ID) . "#comment-" . get_comment_ID(); ?></id>
		<updated><?php echo get_comment_time('Y-m-d\TH:i:s\Z', true); ?></updated>
		<published><?php echo get_comment_time('Y-m-d\TH:i:s\Z', true); ?></published>

		<?php if (!empty($comment->post_password) && $_COOKIE['wp-postpass'] != $comment->post_password) { ?>
			<content
				type="html"
				xml:base="<?php echo get_permalink($comment->ID); ?>">
				<![CDATA[<?php echo get_the_password_form($comment->post_name) ?>]]>
			</content>
		<?php } else { ?>
			<content
				type="html"
				xml:base="<?php echo get_permalink($comment->ID); ?>">
				<![CDATA[<?php comment_text() ?>]]>
			</content>
		<?php } // close check for password ?>
	</entry>

		<?php } } } endwhile; endif; ?>

</feed>

