#!/usr/bin/env perl

# Parse an e-mail alert from funeral-notices.co.uk to extract links

use strict;
use warnings;
use autodie qw(:all);

use MIME::QuotedPrint;
use Path::Tiny;
use URI::Find;

# Read from a file if given, otherwise from STDIN
my $content;
if(@ARGV == 1) {
	# Decode quoted-printable content
	$content = decode_qp(Path::Tiny::path($ARGV[0])->slurp());
} else {
	local $/;
	$content = decode_qp(<STDIN>);
}

my @entries;

# Create a URI::Find object to extract URLs
my $finder = URI::Find->new(sub {
	my $uri = shift;
	if($uri =~ m{funeral-notices\.co\.uk/notice/([^/]+)/([^/?#]+)}) {
		# print "Found URL: $uri\n";
		# print "'$1' => $2,\n";	# How it will appear in bin/create_DB.pl
		push @entries, { name => $1, id => $2 };
	}
});

# Find and process URLs in the content
$finder->find(\$content);

# Sort by name order and print the URLs
foreach my $entry (sort { $a->{'name'} cmp $b->{'name'} } @entries) {
	print "'", $entry->{'name'}, "' => ", $entry->{'id'}, ",\n";
}
