#!/usr/bin/perl

$txt="$ENV{'DOCUMENT_ROOT'}/cgi-bin/news.txt";

print "Content-type: text/xml\n\n";
$first = 1;

print q`<?xml version="1.0"?>
<rss version="2.0">
 <channel>
  <title>OrangeCD Suite news</title>
  <link>https://www.firetongue.com/</link>
  <description>OrangeCD Suite, the music collection management software for Windows.</description>
  <language>en-us</language>`;

#  <lastBuildDate>`;
#
#	@st = stat($txt);
#	@gm = gmtime($st[9]);
#	$wd = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$gm[6]];
#	$mn = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$gm[4]];
#	$y = 1900 + $gm[5];
#	$tt = sprintf("%d:%02d:%02d", $gm[2], $gm[1], $gm[0]);
#	print "$wd, $gm[3] $mn $y $tt GMT";
#
#print q`</lastBuildDate>

print q`  <managingEditor>orangecd@firetongue.com</managingEditor>
  <webMaster>orangecd@firetongue.com</webMaster>
`;

if (open(FIN, $txt))
{
	while(true) {
		$_ = <FIN>;
		if ($_ eq "" || $_ eq "\n") { last;	}

		s/\n$//;
		($m,$d,$y)=split(/\//, $_);
		$_ = <FIN>;
		s/\n$//;
		$title = $_;

		$_ = <FIN>;
		$text = '';
		while ($_ ne "" && $_ ne "\n") {
			s/\n$/ /;
			$text .= $_;
			$_ = <FIN>;
		}

		$text =~ s/"/&quot;/g;
		$text =~ s/</&lt;/g;
		$text =~ s/>/&gt;/g;
		
		$anchor = "https://www.firetongue.com/#$y" . "_$m" . "_$d";

		print "  <item>
  <title>$title</title> 
  <link>$anchor</link> 
  <description>$text</description>
  <guid>$anchor</guid>
  <pubDate>";

	$wd = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[zeller1($m,$d,$y)];
	$mn = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[$m-1];
		print "$wd, $d $mn $y 08:00:00 GMT";

print "</pubDate> 
  </item>
";
	}
	close(FIN);
}

print " </channel>
</rss>
";

sub zeller1 { 
   local($month, $day, $year) = @_; 
   local($century, $offset); 
   local($leap_part, $day_of_week); 


   if ($month < 3) { $month += 12; $year--; } 


   $century = int($year / 100); 
   $offset = int($year % 100); 
   $leap_part = $offset + int($offset/4) + int($century/4) - 2 * $century; 
   int(($day + int(($month+1)*26 / 10) + $leap_part + 6 ) % 7); 
}
