#! /usr/bin/perl
#
# This is a Perl script used to convert the Icarus Wing source to latex

my $speaker;
my $counter = 0;

sub insertTOC {

    print q|

\vfill\eject

\thispagestyle{empty}

{
\fontsize{16pt}{20pt}
\selectfont
\centerline{Requirements}
\centerline{for}
\centerline{The Great anti-American Novel}
}

\begin{verbatim}
|;

    open(ICARUSWING, "<IcarusWing");
    while (<ICARUSWING>) {
	if (/^T / or /^Launch Day/) {
	    s/^.*    //;
	    print unless (/^T / or /^Launch Day/);
	}
    }
    close(ICARUSWING);

    print "\\end{verbatim}\n";
}

sub ProcessLine {

    s/\$/\\\$/;				# quote $

    s/Christian1/Christian\$^1\$/;	# add superscripts to "Christian"
    s/Christians1/Christians\$^1\$/;
    s/Christian2/Christian\$^2\$/;
    s/Christians2/Christians\$^2\$/;

    s/^al-Nass:/alNass:/;		# handle the hyphen like this

    # The speaking character can be indicated by a notation at the
    # beginning of the line or paragraph like "Alister:".  Note
    # if this is the case, or undef $speaker if we're at a blank
    # line (in between paragraphs)

    $speaker=$1 if (/^((\w|-)+):/);
    undef $speaker if (/^$/);
    $counter=0 if (/^$/);

    # Remove speaker indication now that we've processed it.
    s/^(\w+)://;

    # Set text color for material in quotes according to speaker
    # Actual colors are defined above in the LaTeX preamble.

    while (/"/ and defined $speaker) {
	if ($counter % 2 == 0) {
	    my $speaker1 = $speaker;
	    if ($speaker =~ s/([^_]+)_(.*)/$2/) {
		$speaker1 = $1;
	    }
	    s/"/\\textcolor{$speaker1}{``/;
	} else {
	    s/"/''}/;
	}

	$counter ++;
    }

    while (/"/) {
	if ($counter % 2 == 0) {
	    s/"/``/;
	} else {
	    s/"/''/;
	}

	$counter ++;
    }

    s/_([-\w',!.?;]+)_/{\\it \1}/g;	# convert _ to italics
    s/_/ /g;				# convert interior _ to spaces

    # Change '    ' in chapter title lines to hskip
    s/    /\\hskip 0.5in/  if (/^T [+-] [0-9]+/);

    # Put chapter titles on new pages in typewriter font
    #   the centerline is there to make T + 1 day overfill
    s/^T [+-] [0-9]+.*$/\\vfill\\eject\\centerline{\\tt \\hfill $&}\\vskip 0.5in/;

    # Ditto for Launch Day
    s/    /\\hskip 0.5in/  if (/^Launch Day/);
    s/^Launch Day+.*$/\\vfill\\eject{\\tt \\hfill $&}\\vskip 0.5in/;

    # Ditto for Epilog
    s/    /\\hskip 0.5in/  if (/^Epilog/);
    s/^Epilog+.*$/\\vfill\\eject{\\tt \\hfill $&}\\vskip 0.5in/;

    # Format poetry at beginning - with italics {{ }}
    s/^{{([^}]*)}}$/\\centerline{\\it \1}/;

    # Format poetry at beginning - without italics {
    s/^{([^}]*)$/\\centerline{\1}/;

    if (/Requirements for The Great anti-American Novel/) {
	&insertTOC;
    } else {
	print;
    }
}

sub ProcessFile {
    my ($FILE) = @_;

    while (<$FILE>) {
	my $filename = $_;
	chomp $filename;

	if ($filename =~ m/^[A-Z0-9]*$/ and -r $filename) {
	    open(NESTED_FILE, "<$filename");
	    ProcessFile(\*NESTED_FILE);
	    close(NESTED_FILE);
	} else {
	    ProcessLine;
	}
    }
}

print q|

\documentclass{book}

\usepackage{vmargin}
\usepackage{times}
\usepackage[pdftex]{color}

\usepackage[koi8-ru]{inputenc}
\usepackage[russian,english]{babel}

\setpapersize{custom}{6 in}{9 in}
\setmargrb{20mm}{20mm}{20mm}{20mm}

\pdfinfo{
  /Title	(Icarus' Wing)
  /Author	(Brent Baccala)
}

\begin{document}

\pagestyle{empty}

\parindent 0pt
\parskip 12pt

% default seems to be 10/12
% \fontsize{12pt}{14pt}
% \selectfont

\title{Icarus' Wing}
\author{Brent Baccala}
\date{}

% No title page for e-reader version
% \maketitle

\definecolor{Bible}{rgb}{0.7,0,0}

\definecolor{Mercuriou}{rgb}{0.4,0.2,0}
\definecolor{Burns}{rgb}{0.3,0.3,0}
\definecolor{Vic}{rgb}{0.4,0,0.4}
\definecolor{Alister}{rgb}{0,0.4,0}
\definecolor{Andrea}{rgb}{0,0,0.4}

\definecolor{Kyle}{rgb}{0,0,0}
\definecolor{David}{rgb}{0,0,0}

\definecolor{Ecks}{rgb}{0,0,0}
\definecolor{Wye}{rgb}{0,0,0}
\definecolor{Zee}{rgb}{0,0,0}

\definecolor{Beth}{rgb}{0,0,0}
\definecolor{James}{rgb}{0,0,0}
\definecolor{Jill}{rgb}{0,0,0}
\definecolor{Jeff}{rgb}{0,0,0}

\definecolor{Reginard}{rgb}{0,0,0}
\definecolor{alNass}{rgb}{0,0,0}

\frontmatter

\quad
\vskip 1in
\begin{center}
{\Huge Icarus' Wing}

{\small The Great anti-American Novel}
\vskip 5in {\Large by Brent Baccala}
\end{center}

\mainmatter
\pagestyle{plain}
\setcounter{page}{2}

|;


ProcessFile(\*STDIN);

print q|
\end{document}
|;
