#!/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          => 'Suspense',
	link           => 'http://granicus.if.org/podcasts/suspense',
	language       => 'en',
        description    => 'Suspense 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 ($episode, $ext);

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

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

	# Suspense 620819 939 Pages from a Diary (64-32) 11963 24m09s.mp3
	if (($y, $m, $d, $name) = ($file =~ /(\d{2})(\d{2})(\d{2})\s+\d+\s+(.+)\(.+\.mp3$/)) {
		$y += 1900;
		#warn "found $name -> $title\n";
	}
	# Forecast 400722 The Lodger (audition) (128-44) 28390 29m37s.mp3
	elsif (($y, $m, $d, $name) = ($file =~ /(\d{2})(\d{2})(\d{2})\s+(.+)\(.+\.mp3$/)) {
		$y += 1900;
	}
       	else {
		warn "unable to parse $file\n";
		next;
	}

	$date = "$y-$m-$d";

	$name = $opt{N} if defined $opt{N};
	$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/suspense/$_";
	$rss->add_item(
		title => $name,
		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;
