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

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

# should use YAML (or better) for all this, it's insane writing so many mini-parsers!

my %title;
my $scene_title_h = IO::File->new("scene_title");
$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 $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;
}
$role_actor_h->close();

#	print Dump(\%role_actors);

my $heading = <<'End';
<div class=Section1>

<p class=MsoNormal style='margin-bottom:12.0pt;tab-stops:108.0pt'><b><span
style='font-size:24.0pt;mso-font-kerning:16.0pt'>Rehearsals:<o:p></o:p></span></b></p>

<p class=MsoNormal><span style='mso-font-kerning:16.0pt'>Blah blah blah<o:p></o:p></span></p>

<p class=MsoNormal><span style='mso-font-kerning:16.0pt'><span
style="mso-spacerun: yes"> </span><o:p></o:p></span></p>

</div>

<span style='font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:
"Times New Roman";mso-font-kerning:16.0pt;mso-ansi-language:EN-AU;mso-fareast-language:
EN-US;mso-bidi-language:AR-SA'><br clear=all style='page-break-before:auto;
mso-break-type:section-break'>
</span>
End

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";}
h1
	{mso-style-next:Normal;
	margin-top:12.0pt;
	margin-right:0cm;
	margin-bottom:3.0pt;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	page-break-after:avoid;
	mso-outline-level:1;
	font-size:16.0pt;
	font-family:Arial;
	mso-font-kerning:16.0pt;
	font-weight:bold;}
h2
	{mso-style-next:Normal;
	margin-top:12.0pt;
	margin-right:0cm;
	margin-bottom:3.0pt;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	page-break-after:avoid;
	mso-outline-level:2;
	font-size:14.0pt;
	font-family:Arial;
	font-weight:bold;
	font-style:italic;}
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-paper-source:0;}
div.Section1
	{page:Section1;}
@page Section2
	{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:2 even 35.45pt;
	mso-paper-source:0;}
div.Section2
	{page:Section2;}
-->
</style>
</head>
<body lang=EN-AU style='tab-interval:36.0pt; tab-stops:108.0pt'>
MSOCRAPEND

print <<End;
$heading

<div class=Section2>
End

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

$heading

<div class=Section2>
End

my $tab = "<span style='mso-tab-count:1'> </span>";

my $timetable_h = IO::File->new("timetable");
$timetable_h->getline();
while (defined(my $line = $timetable_h->getline())) {
	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 $today = `date -d $yyyymmdd +"%A %B %e"`; chomp $today; $today =~ s/  / /;

	if ($today =~ /Monday/) { print $pagebreak; }

	if ($scenes_lunch || @scenes_330 || @other) {
		print big($today);
	}

	if ($scenes_lunch) {
		print big("Lunch: 1.00 - 1.30");
		print_scene($scenes_lunch);
	}
	if (@scenes_330) {
		print big("After School: 3.30 - 5.00");
		for my $scene (@scenes_330) {
			print_scene($scene);
		}
	}
	if (@other) {
		for (@other) {
			print big($_);
		}
		print "<br>\n";
	}
}
$timetable_h->close();
print <<'MSOCRAPEND';
</div>
</body>
</html>
MSOCRAPEND

sub print_scene {
	my ($scene) = @_;
	my ($act, $sc) = split /\./, $scene;
	my $title = "ACT ".("I"x$act)." - Scene $sc &mdash; ".uc($title{$scene});
	print big($title, 14);
	for my $character (@{$scene_characters{$scene}}) {
		my $character_disp = $character;
		$character_disp =~ s,\bOrphan\b,<i>orphan</i>,;
		print "$character";
		for my $actor (@{$role_actors{$character}}) {
			print "$tab$actor<br>\n";
		}
		if (!@{$role_actors{$character}}) { print "<br>\n"; }
	}
	print "<br>\n";
}

sub big {
	my ($text, $size) = @_;
	$size ||= 20;
	return "<b><span style='font-size:${size}pt'>$text</span></b><br>\n";
}
