+my $out = '';
+
+# atom/rss feed header and footer
+if ($index and ($format eq 'atom' or $format eq 'rss')) {
+ if ($header) {
+ my $now_iso8601 = `date -Iseconds -u | tr -d \\\\n`;
+ my $now_rfc5322 = `date -uR | tr -d \\\\n`;
+ my $atom_rel = $format eq 'atom' ? 'self' : 'alternate';
+ my $rss_rel = $format eq 'rss' ? 'self' : 'alternate';
+ my $link = $format eq 'atom' ? 'link' : 'atom:link';
+ my $links = '';
+ if ($lang eq 'en') {
+ $links = qq(
+<$link hreflang="fa" href="https://bndl.org/fa/notes.atom" rel="alternate" type="application/atom+xml" />
+<$link hreflang="fa" href="https://bndl.org/fa/notes.rss" rel="alternate" type="application/rss+xml" />
+<$link hreflang="fa" href="https://bndl.org/fa/bandali.fa.txt" rel="alternate" type="text/plain" />
+<$link hreflang="fa" href="https://bndl.org/fa/" rel="alternate" type="text/html" />
+<$link href="https://bndl.org/notes.atom" rel="$atom_rel" type="application/atom+xml" />
+<$link href="https://bndl.org/notes.rss" rel="$rss_rel" type="application/rss+xml" />
+<$link href="https://bndl.org/bandali.txt" rel="alternate" type="text/plain" />
+<$link href="https://bndl.org" rel="alternate" type="text/html" />);
+ } elsif ($lang eq 'fa') {
+ $links = qq(
+<link hreflang="en" href="https://bndl.org/notes.atom" rel="alternate" type="application/atom+xml" />
+<link hreflang="en" href="https://bndl.org/notes.rss" rel="alternate" type="application/rss+xml" />
+<link hreflang="en" href="https://bndl.org/bandali.txt" rel="alternate" type="text/plain" />
+<link hreflang="en" href="https://bndl.org" rel="alternate" type="text/html" />
+<link href="https://bndl.org/fa/notes.atom" rel="$atom_rel" type="application/atom+xml" />
+<link href="https://bndl.org/fa/notes.rss" rel="$rss_rel" type="application/rss+xml" />
+<link href="https://bndl.org/fa/bandali.fa.txt" rel="alternate" type="text/plain" />
+<link href="https://bndl.org/fa/" rel="alternate" type="text/html" />);
+ }
+ $links =~ s/^\n//;
+
+ $out .= '<?xml version="1.0" encoding="UTF-8" ?>';
+ $out .= ($format eq 'atom') ? qq(
+<feed xml:lang="$lang" xmlns="http://www.w3.org/2005/Atom">
+<title>$site_title</title>
+<subtitle>$site_desc</subtitle>
+<id>$feed_id</id>
+$links
+<updated>$now_iso8601</updated>)
+ : ($format eq 'rss') ? qq(
+<rss version="2.0"
+ xmlns:atom="http://www.w3.org/2005/Atom"
+ xmlns:content="http://purl.org/rss/1.0/modules/content/">
+<channel>
+<title>$site_title</title>
+<description>$site_desc</description>
+<link>$site_url</link>
+<language>$lang</language>
+<lastBuildDate>$now_rfc5322</lastBuildDate>
+<pubDate>$now_rfc5322</pubDate>
+<ttl>1800</ttl>
+$links)
+ : '';
+ } elsif ($footer) {
+ $out .= ($format eq 'atom') ? '</feed>'
+ : ($format eq 'rss') ? '</channel></rss>'
+ : '';
+ }
+
+ # we're done
+ goto PRINT;
+}
+
+