#!/usr/bin/env perl

use strict;
use warnings;

use XML::RSS;
use Lingua::EN::Titlecase;

use Getopt::Std;
use File::Path qw(make_path);
use String::Util qw(trim collapse);
use DateTime::Format::Mail;
my $mail = DateTime::Format::Mail->new();

my %opt;
my $rss = XML::RSS->new(version => '2.0');
$\ = "\n";

my %plot = (
	'1937-09-26' => q{A man who's looking for a job mistakenly becomes involved with a bank robbery. He is later sentenced to death for a cop killing he didn't commit. The Shadow investigates and locates a missed piece of evidence, which helps free the wrongly accused man.},
	'1937-10-17' => q{After the verdict is read at a criminal's trial, a judge and D. A. are threatened by the condemned man. The convict is then hanged.

Later, the judge is murdered, as is the district attorney. The stakes are high when the jury foreman, who is next on the killer's list, happens to be Margot Lane's father. How is the dead man following through with his threat? Can he be stopped? The Shadow knows.},
);

$rss->channel(
	title          => 'Burns and Allen',
	link           => 'http://granicus.if.org/podcasts/burnsallen',
	language       => 'en',
        description    => 'Burns and Allen Radio Show',
	pubDate	=> DateTime->now(formatter => $mail, time_zone => 'America/Chicago'),
	lastBuildDate  => DateTime->now(formatter => $mail, time_zone => 'America/Chicago'),
#              rating         => '(PICS-1.1 "http://www.classify.org/safesurf/" 1 r (SS~~000 1))',
	#      copyright      => 'unknown, probably public domain',
	#      pubDate        => 'Thu, 23 Aug 1999 07:00:00 GMT',
	#      lastBuildDate  => 'Thu, 23 Aug 1999 16:20:26 GMT',
	#      docs           => 'http://www.blahblah.org/fm.cdf',
	#      managingEditor => 'webmaster@granicus.if.org',
	#      webMaster      => 'webmaster@granicus.if.org',
);

#print $rss->as_string;
#$rss->image(title       => 'freshmeat.net',
#            url         => 'http://freshmeat.net/images/fm.mini.jpg',
#            link        => 'http://freshmeat.net',
#            width       => 88,
#            height      => 31,
#            description => 'This is the Freshmeat image stupid'
#            );

while (<>) {
	chomp;
	next if -d;

	my ($year, $episode, $ext);
	$year = 1938;

	#burnsallen/The Burns and Allen Show 1946-05-02 (33) The Folly of Molly O'Malley.mp3
	my $path = File::Spec->canonpath($_);
	my (undef, $dirs, $name) = File::Spec->splitpath($path);
	#my ($show, $season) = File::Spec->splitdir($dirs);
	my $show;

	my ($y, $m, $d, $date);

	my $title;
	if (($y, $m, $d, $title) = ($name =~ /(\d{4})-(\d{2})-(\d{2})\s*\(\d+\)\s*(.+)\.mp3$/)) {
		#warn "found $name -> $title\n";
		$date = "$y-$m-$d";
	} else {
		warn "unable to parse $name\n";
		next;
	}

	$name = $opt{N} if defined $opt{N};
	$year = $opt{y} if defined $opt{Y};
	$show = $opt{s} if defined $opt{S};

	if ($opt{t}) {
		$name = Lingua::EN::Titlecase->new($name);
	} elsif ($opt{T}) {
		$name = Lingua::EN::Titlecase->new(lc $name);
	}

	$name = collapse $name;

	my $url = "https://granicus.if.org/podcasts/burnsallen/$_";
	$rss->add_item(
		title => $title,
		link => $url,
		pubDate => DateTime->new(year => $y, month => $m, day => $d, hour => 18,
		       	formatter => $mail, time_zone => 'America/New_York'),
		#description => $plot{$date},
		# creates a guid field with permaLink=true
		#		permaLink  => "http://freshmeat.net/news/1999/06/21/930003829.html",
		# alternately creates a guid field with permaLink=false
		# guid     => "gtkeyboard-0.85"
		#		enclosure   => { url=>$url, type=>"application/x-bittorrent" },
		enclosure => { url => $url, type => "audio/mpeg" },
		#		description => 'blah blah'
	);
}

print $rss->as_string;
