#!/usr/bin/perl -w
use strict;

use IO::File;
use HTML::Entities;
use YAML;

my %title;
my $scene_title_h = IO::File->new("scene_title_short");
$scene_title_h->getline();
while (defined(my $line = $scene_title_h->getline())) {
	chomp $line;
	my ($scene, $title) = split /\t/, $line;
	$title{$scene} = $title;
}
$scene_title_h->close();

#	print Dump(\%title);

my @scenes;
my %scene_characters;
my $list;
my $scene;
my $scene_characters_h = IO::File->new("scenes_and_characters");
while (defined(my $line = $scene_characters_h->getline())) {
	chomp $line;
	if ($line eq "") {
		$scene_characters{$scene} = $list; undef $list;
	} elsif ($line =~ /^[0-9]/) {
		$scene = $line;
		push @scenes, $line;
		$list = [];
	} else {
		push @$list, $line;
	}
}
$scene_characters_h->close();

#	print Dump(\%scene_characters, \@scenes);

my %role_actors;
my %actor_roles;
my %actors;
my $role_actor_h = IO::File->new("role_actor");
while (defined(my $line = $role_actor_h->getline())) {
	chomp $line;
	my ($role, $actor) = split /\t/, $line;
	push @{$role_actors{$role}}, $actor;
	push @{$actor_roles{$actor}}, $role;
	$actors{$actor} = 1;
}
$role_actor_h->close();

#	print Dump(\%role_actors);


print "<html><body>\n";
print <<'MSOCRAPEND';
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 9">
<meta name=Originator content="Microsoft Word 9">
<link rel=File-List href="./cal_files/filelist.xml">
<link rel=Edit-Time-Data href="./cal_files/editdata.mso">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Author>Marney</o:Author>
  <o:LastAuthor>Marney</o:LastAuthor>
  <o:Revision>4</o:Revision>
  <o:TotalTime>9</o:TotalTime>
  <o:Created>2004-07-11T09:52:00Z</o:Created>
  <o:LastSaved>2004-07-11T10:08:00Z</o:LastSaved>
  <o:Pages>1</o:Pages>
  <o:Words>501</o:Words>
  <o:Characters>2858</o:Characters>
  <o:Company>Marney's Computer</o:Company>
  <o:Lines>23</o:Lines>
  <o:Paragraphs>5</o:Paragraphs>
  <o:CharactersWithSpaces>3509</o:CharactersWithSpaces>
  <o:Version>9.2720</o:Version>
 </o:DocumentProperties>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:View>Print</w:View>
  <w:Zoom>BestFit</w:Zoom>
 </w:WordDocument>
</xml><![endif]-->
<style>
<!--
 /* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
p
	{margin-right:0cm;
	mso-margin-top-alt:auto;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
@page Section1
	{size:595.3pt 841.9pt;
	margin:26.95pt 19.3pt 26.95pt 27.0pt;
	mso-header-margin:35.4pt;
	mso-footer-margin:35.4pt;
	mso-columns:3 even 17.85pt;
	mso-column-separator:solid;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
-->
</style>
</head>

<body lang=EN-AU style='tab-interval:36.0pt'>

<div class=Section1 style='font-size:9.0pt;mso-bidi-font-size:12.0pt'>
MSOCRAPEND

my $pagebreak = <<End;
<br clear=all style='mso-special-character:line-break;page-break-before:always'>
End

my $actor_count = 0; # XXX DEBUG
for my $actor (sort keys %actors) {

	warn "$actor\n";

	print "<span style='font-size:14pt'><b>$actor</b></span><br>\n";
	for my $role (@{$actor_roles{$actor}}) {
		print "<span style='font-size:14pt'>&nbsp;&nbsp;<b>$role</b></span><br>\n";
	}
	print "<hr noshade>\n";

	my $count = 0;
	my $timetable_h = IO::File->new("timetable");
	$timetable_h->getline();
	while (defined(my $line = $timetable_h->getline())) {
		if ($count ++) { print "<hr noshade>\n"; }
		chomp $line;
		my ($date, $scenes_lunch, $scenes_330, $other) = split /\t/, $line;
		for ($scenes_lunch, $scenes_330, $other) { $_ ||= ''; }
		my @scenes_330 = split /; /, $scenes_330;
		my @other = split /; /, $other;
		my ($month, $day) = split / /, $date;
		my $yyyymmdd = sprintf("2004%02d%02d", $month, $day);
		my $dow_month = `date -d $yyyymmdd +"%a %b"`; chomp $dow_month;
		my ($dow, $month_name) = split / /, $dow_month;
		print "<b>$month_name $day $dow</b>:<br>\n";
		undef $scenes_lunch if !$scenes_lunch || !my_scene($actor, $scenes_lunch);
		if ($scenes_lunch) {
			my $title = encode_entities($title{$scenes_lunch});
			print "<b>Lunch</b> - <b><i>$scenes_lunch</i></b> $title<br>\n";
		}
		@scenes_330 = grep {my_scene($actor, $_)} @scenes_330;
		if (@scenes_330) {
			print "<b>3:30</b> - ";
			my $n=0;
			for my $scene (@scenes_330) {
				my $comment = "";
				if ($scene =~ /^(.*?)(\(.*?\))$/) {
					$scene = $1; $comment = " $2";
				}
				if ($n++) { print "&nbsp;"x2; }
				my $title = encode_entities($title{$scene});
				$comment = encode_entities($comment);
				print "<b><i>$scene</i></b> $title$comment<br>\n";
			}
		}
		for (@other) {
			print "$_<br>\n";
		}
	}
	$timetable_h->close();

	print $pagebreak;

#		if (++$actor_count >= 3) { last }; # XXX DEBUG
}

print <<'MSOCRAPEND';
</div>
</body>
</html>
MSOCRAPEND

sub my_scene {
	my ($actor, $scene) = @_;
	if ($scene_characters{$scene}[0] eq "WHOLE CAST") { warn "WC"; return 1; }
	for my $role (@{$actor_roles{$actor}}) {
		for my $char (@{$scene_characters{$scene}}) {
			if ($char eq $role) { return 1; }
		}
	}
	return 0;
}
