#!/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          => 'The Shadow Radio',
	link           => 'http://granicus.if.org/podcasts/shadow',
	language       => 'en',
        description    => 'The shadow radio programs',
	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',
);

#$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;
	next unless /\.mp3$/;

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

	#shadow/Season 3 (1939-40)/1940.03.24 Plot That Failed.afpk
	#shadow/Season 3 (1939-40)/1939.09.24 Dead Men Talk.png
	#shadow/Season 3 (1939-40)/1940.03.24 Plot That Failed.mp3
	#shadow/Season 3 (1939-40)/1940.03.24 Plot That Failed.png


	my $path = File::Spec->canonpath($_);
	my (undef, $dirs, $name) = File::Spec->splitpath($path);
	my ($show, $season) = File::Spec->splitdir($dirs);

	next if $name =~ /_spectrogram.png/;

	$name =~ s/(\d{4})\.(\d{2})\.(\d{2})/$1-$2-$3/;
	my ($y, $m, $d) = ($1, $2, $3);
	my $date = "$y-$m-$d";

	my $title;
	if (($title) = ($name =~ /\d{4}-\d{2}-\d{2}\s*(.+)\.[^.]+$/)) {
		#warn "found $name -> $title\n";
	} else {
		warn "unable to parse $name\n";
		exit 1;
	}

	$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/shadow/$_";
	$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;
