#!/usr/bin/perl -w
#
# generate-online: this file is part of the prosper-online package.
#
# Copyright (C) 2003  Alain Bannay <albannay@club-internet.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

use strict;
use Getopt::Long;
use Data::Dumper;

my $prog = "generate-online";
my $version = 0.1;
my $templ_dir = "/usr/local/share/generate-online";

my ($want_version, $want_help, $input);
my $output   = 'html';
my $prefix   = 'slide';
my $style    = 'w3c';
my $scale    = 60;
my $format   = 'png';
my $caption  = '\author (\email) (C) \year';
my $overlays = 0;

GetOptions(
	"version"   => \$want_version,
	"help"      => \$want_help,
	"input=s"   => \$input,
	"output=s"  => \$output,
	"prefix=s"  => \$prefix,
	"style=s"   => \$style,
	"format=s"  => \$format,
	"caption=s" => \$caption,
	"scale=i"   => \$scale,
	"overlays"  => \$overlays
);

# print version and exit
if ($want_version)
{
	print "$prog (Version: $version)\n";
	exit 0;
}

# print help and exit
if ($want_help)
{
	display_usage();
	exit 0;
}

unless ($input)
{
	display_usage();
	exit 1;
}

# check files really exist
my $tex_file  = "$input.tex";
my $ps_file   = "$input.ps";
my $html_file = "$input.html";
die("No such tex file $tex_file, aborting\n") unless (-f $tex_file);
die("No such ps file $ps_file, aborting\n") unless (-f $ps_file);

create_directory($output, $style);
generate_slides($output, $tex_file, $format, $style, $prefix, $caption, $overlays);
generate_images($output, $ps_file, $format, $scale);
create_symlink($output, $html_file, $style);

sub display_usage
{
	print <<EOF;
Usage: $0 [options] --input <input>
options:
--version:           print version and exit
--help:              print help and exit
--output <output>:   output directory (default: html)
--prefix <prefix>:   slide files prefix (default: slide)
--format <format>:   image files format (default: png)
--scale <scale>:     image files scale (default: 60)
--style <style>:     slide style (default: w3c)
--caption <caption>: slide caption (default: \author (\email) (C) 2003)
--overlays:         overlays support
EOF
}

# create output directory structure
sub create_directory
{
	my ($output_dir, $style) = @_;

	system("cp -r $templ_dir/$style $output_dir");
	system("rm -f $output_dir/$style/*.txt");
}

# create final symlink
sub create_symlink
{
	my ($output_dir, $html_file, $style) = @_;

	my $link_file;
	if ($style eq "frames")
	{
		$link_file = "$output_dir/$style/frames.html";
	}
	else
	{
		$link_file = "$output_dir/$style/index.html";
	}

	unlink $html_file if (-f $html_file);
	symlink $link_file, $html_file;
}


# generate images
sub generate_images
{
	my ($output_dir, $ps_file, $format, $scale) = @_;
	system("convert -scale $scale% -rotate 90 $ps_file $output_dir/graphics/slides/$prefix%03d.$format");
}

# generate slides
sub generate_slides
{
	my ($output_dir, $tex_file, $format, $style, $prefix, $caption, $overlays) =  @_;

	# $presentation holds a reference to a structure (hashtable) wich contains
	# all the informations about the presentation.
	# $presentation->{TITLE} (title of the presentation)
	# $presentation->{SLIDES}{foil02} (foil02 slide)
	# $presentation->{SLIDES}{foil02}{TITLE} (title of the foil2 slide)
	# $presentation->{SLIDES}{$presentation->{SLIDES}{foilgrp01}{NEXT}}{TITLE}
	# 	(title of the next slide of the foilgrp01 slide)
	my $presentation = create_presentation ($tex_file, $caption, $prefix, $format, $overlays);

	#print Dumper($presentation);

	SWITCH: {
		if ($style eq "frames") { generate_slides_frames($output_dir, $presentation); last SWITCH; }
		if ($style eq "tables") { generate_slides_tables($output_dir, $presentation); last SWITCH; }
		if ($style eq "gtree")  { generate_slides_gtree($output_dir, $presentation);  last SWITCH; }
		if ($style eq "w3c")    { generate_slides_w3c($output_dir, $presentation);    last SWITCH; }
		die("$style : Unknown style. Must be frames, w3c, tables or gtree\n")
	}
}

sub generate_slides_frames 
{
	generate_frames_frames (@_);
	generate_frames_toc (@_);
	generate_frames_titlepg (@_);
	generate_frames_foils_and_foilgrps (@_);
}

sub generate_slides_w3c
{
	generate_w3c_toc (@_);
	generate_w3c_index (@_);
	generate_w3c_foils_and_foilgrps (@_);
}

sub generate_slides_tables
{
	generate_tables_toc (@_);
	generate_tables_index (@_);
	generate_tables_foils_and_foilgrps (@_);
}

sub generate_slides_gtree
{
	generate_gtree_toc (@_);
	generate_gtree_foils_and_foilgrps (@_);
}

# create_presentation {{{
# This function:
# - read the source file,
# - search for important informations about the presentation,
# - stock these informations in an structure,
# - returns a reference to this structure.
sub create_presentation
{
	# Five arguments : input file, caption, slide prefix, graphic format
	# and overlays.
	my ($source, $caption, $image_prefix, $format, $with_overlays) = @_;

	if (! $caption)
	{
		$caption = "";
	}
	
	my $pres;
	$pres = {};	
	my $slides;
	$slides = {};	# $slides holds a reference to a hashtable of slides
			# keyed by name.
			# name can be of the following forms:
			# - cover: for \maketitle slide
			# - foilgrp001: for \part slide
			# - foil001: for begin/end{slide} without overlays or
			# 		with overlays of 1
			# - foil001-O1: for begin/end{slide} with overlays 
			# 		(and if the overlays mode is enabled)
	$pres->{SLIDES} = $slides;
	
	my $order;
	$order = [];	# $order holds a reference to an array wich contains
			# slide name in appearance order.
			# Usefull to generate toc or ttoc.
	$pres->{ORDER} = $order;
	
	# Initialization with empty string
	$pres->{TITLE} = "";
	$pres->{SUBTITLE} = "";
	$pres->{AUTHOR} = "";
	$pres->{EMAIL} = "";
	$pres->{INSTITUTION} = "";
	$pres->{CAPTION} = "";

	my $image_num = 0;	# current indice for image
	my $slide_num = 0;	# current indice for slide (each overlays of a same
				# slide have the same slide_num (INDICE).
	my $in_overlays = 0;	# indicates the overlays mode.
	my $overlays_total = 0;	# total number of overlays.
	my $overlays_num = 1;	# current indice of the overlay.
	my $foil_num = 1;	# counter to create files foil001 foil002...
	my $foilgrp_num = 1;	# counter to create files foilgrp001 foilgrp002...
	my $image_ext = ".$format";	# extension of the image.
	my $current_grp = "cover";	# the cover slide is the current group
					# when none group is defined.
	my $key;		# holds the name of the current slide.
	my $prev_key;		# holds the name of the previous slide.

	# Open the input file in read-only mode.
	open(SOURCE, "<$source") or die("open: $!");
	
	# Remove the extension (normally .tex) form the source file.
	$source =~ s/(.*)\.(.*)/$1/;
	$pres->{SOURCE} = $source;

	# We read all lines of the input file.
	while( <SOURCE> )
	{
		my $slide;
		# Extraction of the title...
		if( $_ =~ m/^\s*\\title{(.*)}/)
		{
			$pres->{TITLE} = detex ($1);
		}
		# the subtitle...
		elsif( $_ =~ m/^\s*\\subtitle{(.*)}/)
		{
			$pres->{SUBTITLE} = detex ($1);
		}
		# the author...
		elsif( $_ =~ m/^\s*\\author{(.*)}/)
		{
			$pres->{AUTHOR} = detex ($1);
		}
		# the email...
		elsif( $_ =~ m/^\s*\\email{(.*)}/)
		{
			$pres->{EMAIL} = detex ($1);
		}
		# and the institution.
		elsif( $_ =~ m/^\s*\\institution{(.*)}/)
		{
			$pres->{INSTITUTION} = detex ($1);
		}
		elsif( $_ =~ m/^\s*\\maketitle/)
		{
			$slide = {};
			$slide->{TITLE} = $pres->{TITLE};
		
			$key = "cover";
		
			$slide->{IMAGE} = $image_prefix.three_digits_num($image_num).$image_ext;
			
			$slide->{INDICE} = 0;
			$slide->{PREV} = $prev_key;
			$slides->{$prev_key}{NEXT} = $key if defined ($prev_key);
			$slide->{UP} = "cover";
			
			# Insert the new slide (update the $pres->{SLIDES} hashtable)
			$slides->{$key} = $slide;
			# update the $pres->{ORDER} array
			push (@{$order}, $key);
			
			$prev_key = $key;
			$image_num++;
		}
		elsif( $_ =~ m/^\s*\\part(\[.*\])?{(.*)}/)
		{
			$slide = {};
			$slide->{TITLE} = detex ($2);
			
			$key = "foilgrp".three_digits_num($foilgrp_num);
			
			$slide->{IMAGE} = $image_prefix.three_digits_num($image_num).$image_ext;
			$image_num++;
			
			$slide_num++;
			$slide->{INDICE} = $slide_num;
			$slide->{PREV} = $prev_key;
			$slides->{$prev_key}{NEXT} = $key if defined ($prev_key);
			$prev_key = $key;
			$slide->{UP} = "cover";
			
			# Insert the new slide (update the $pres->{SLIDES} hashtable)
			$slides->{$key} = $slide;
			# update the $pres->{ORDER} array
			push (@{$order}, $key);
			
			$current_grp = $key;
			$foilgrp_num++;
		}
		elsif( $_ =~ m/^\s*\\begin{slide}(\[.*\])?{(.*)}/)
		{
			$slide = {};
			$slide->{TITLE} = detex ($2);
			
			if ($in_overlays)
			{
			# name for slide with overlays is like foil002-01
				$key = "foil".three_digits_num($foil_num)."-".two_digits_num($overlays_num);
				$slide->{OVERLAYS_IND} = $overlays_num;
				$slide->{OVERLAYS_LAST} = 0;
			}
			else
			{
				$key = "foil".three_digits_num($foil_num);
			}
			
			$slide->{IMAGE} = $image_prefix.three_digits_num($image_num).$image_ext;
			$image_num++;
			
			$slide_num++;
			$slide->{INDICE} = $slide_num;
			$slide->{PREV} = $prev_key;
			$slides->{$prev_key}{NEXT} = $key if defined ($prev_key);
			$prev_key = $key;
			$slide->{UP} = $current_grp;
			
			# Insert the new slide (update the $pres->{SLIDES} hashtable)
			$slides->{$key} = $slide;
			# update the $pres->{ORDER} array
			push (@{$order}, $key);
			
			# We add foils corresponding to the overlays
			if ($in_overlays)
			{
				my $slide_ov;
				$overlays_num++;
				# The overlay number 1 is already done.
				for (;$overlays_num <= $overlays_total ; $overlays_num++)
				{
					$slide_ov = {};
					$slide_ov->{TITLE} = $slide->{TITLE};
					$key = "foil".three_digits_num($foil_num)."-".two_digits_num($overlays_num);
					$slide_ov->{OVERLAYS_IND} = $overlays_num;
					$slide_ov->{OVERLAYS_LAST} = 0;
					$slide_ov->{IMAGE} = $image_prefix.three_digits_num($image_num).$image_ext;
					$image_num++;
					$slide_ov->{INDICE} = $slide_num;
					$slide_ov->{PREV} = $prev_key;
					$slides->{$prev_key}{NEXT} = $key if defined ($prev_key);
					$prev_key = $key;
					$slide_ov->{UP} = $current_grp;

					# Insert the new slide (update the $pres->{SLIDES} hashtable)
					$slides->{$key} = $slide_ov;
					# update the $pres->{ORDER} array
					push (@{$order}, $key);
				}
				$slide_ov->{OVERLAYS_LAST} = 1;
				# update the OVERLAYS_TOTAL value
				foreach my $ov (@{$order}) 
				{
					if ($ov =~ m/^foil(\d\d\d)-\d\d$/)
					{
						# Only for the slides recently added
						if ($1 == three_digits_num($foil_num))
						{
							$slides->{$ov}{OVERLAYS_TOTAL} = $overlays_total;
						}
					}
				}
			}
			$foil_num++;
		}
		elsif( $_ =~ m/^\s*\\overlays{(.*)}/)
		{
			$overlays_total = $1;
		# Conditions to pass in overlays mode ($in_overlays = 1): 
		# - $with_overlays is true (corresponding to the -o option,
		# - number total of overlays is greater than 1.
			if (($with_overlays) && ($overlays_total > 1))
			{
				$in_overlays = 1;
				$overlays_num = 1;
			}
		}
		elsif( $_ =~ m/^\s*\\end{slide}/)
		{
			# reset of the overlay mode.
			$in_overlays = 0;
		}
	}
	$slides->{$prev_key}{NEXT} = undef;
	$pres->{TOTAL} = $slide_num;
	$pres->{YEAR} = (localtime)[5] + 1900;

	# Replace keywords of the caption by their values.
	$caption =~ s/(.*)\\title(.*)/$1$pres->{TITLE}$2/;
	$caption =~ s/(.*)\\author(.*)/$1$pres->{AUTHOR}$2/;
	$caption =~ s/(.*)\\email(.*)/$1$pres->{EMAIL}$2/;
	$caption =~ s/(.*)\\year(.*)/$1$pres->{YEAR}$2/;
	$caption =~ s/(.*)\\institution(.*)/$1$pres->{INSTITUTION}$2/;
	$pres->{CAPTION} = $caption;
	
	return $pres;
}
#}}}

# three_digits_num {{{
# This function puts a number with the format %03d.
sub three_digits_num
{
	# One argument: a number.
	my ($n) = @_;
	
	$n =~ s/^(\d{1})$/0$1/;
	$n =~ s/^(\d{2})$/0$1/;

	return $n;
}
#}}}
# two_digits_num {{{
# This function puts a number with the format %02d.
sub two_digits_num
{
	# One argument: a number.
	my ($n) = @_;
	
	$n =~ s/^(\d{1})$/0$1/;
	return $n;
}
#}}}
# detex {{{
# This function remove LaTeX constructs from a string.
sub detex
{
	# One argument: a string.
	my ($str) = @_;
	
	# Delete commands like \cmd{...}
	$str =~ s/\\\w*\s*{(.*?)}/$1/g;

	# Delete command like {\cmd ...}
	$str =~ s/{\\\w+\s*(.*?)}/$1/g;

	# Delete carriage returns
	$str =~ s/\\\\//g;

	return $str;
}
#}}}

# generate_frames_frames {{{
# generate the frames.html file needed by the frames output style.
sub generate_frames_frames
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $style_dir  = "$out_dir/frames";
	my $templfname = "$templ_dir/frames/frames/frames.txt";
	my $outfname   = "$style_dir/frames.html";
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;

		print OUTPUT $_; 
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_frames_toc {{{
# generate the toc.html file needed by the frames output style.
sub generate_frames_toc
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;
	
	my $style_dir  = "$out_dir/frames";
	my $templfname = "$templ_dir/frames/frames/toc.txt";
	my $outfname   = "$style_dir/toc.html";
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		# We update title
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;

		if ($_ =~ m/INSERT_TOC_HERE/)
		{
			my $tab = "\t\t\t";
			my $infoilgrp= 0;
			my $first_overlays;
			foreach my $foil_name (@{$pres->{ORDER}}) {
				if ($foil_name =~ m/^foil\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t";
					}
					else
					{
						$tab = "\t\t\t\t";
					}
					print OUTPUT "$tab<DIV class=\"toc-foil\">\n";
					print OUTPUT "$tab\t<img alt=\"-\" src=\"../graphics/toc/bullet.png\"/>\n";
					print OUTPUT "$tab\t<A href=\"$foil_name.html\" target=\"foil\">$pres->{SLIDES}{$foil_name}{TITLE}</A>\n";
					print OUTPUT "$tab</DIV>\n";
				}
				elsif ($foil_name =~ m/^foilgrp\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t";
						$infoilgrp= 1;
					}
					else
					{
						print OUTPUT "\t\t\t</DIV>\n";
						$tab = "\t\t\t";
					}
					print OUTPUT "$tab<DIV class=\"toc-foilgroup\">\n";
					print OUTPUT "$tab\t<img alt=\"-\" src=\"../graphics/toc/open.png\"/>\n";
					print OUTPUT "$tab\t<A href=\"$foil_name.html\" target=\"foil\">$pres->{SLIDES}{$foil_name}{TITLE}</A>\n";
				}
				# foil with overlays
				elsif ($foil_name =~ m/^foil\d\d\d-(\d\d)$/)
				{
					if ($1 eq "01")
					{
						$first_overlays = $foil_name;
					}
					if ($pres->{SLIDES}{$foil_name}{OVERLAYS_LAST})
					{
						my $ov_tot;
						my $ov_str;
						if ($infoilgrp == 0)
						{
							$tab = "\t\t\t";
						}
						else
						{
							$tab = "\t\t\t\t";
						}
						print OUTPUT "$tab<DIV class=\"toc-foil\">\n";
						print OUTPUT "$tab\t<img alt=\"-\" src=\"../graphics/toc/bullet.png\"/>\n";
						$ov_tot = $pres->{SLIDES}{$foil_name}{OVERLAYS_TOTAL};
						$ov_str = "<font size=2>  [$ov_tot]</font>";
						print OUTPUT "$tab\t<A href=\"$first_overlays.html\" target=\"foil\">$pres->{SLIDES}{$foil_name}{TITLE} $ov_str</A>\n";
						print OUTPUT "$tab</DIV>\n";
					}
				}
			}
			if ($infoilgrp)
			{
				print OUTPUT "\t\t\t</DIV>\n";
			}
		}
		else
		{
			print OUTPUT $_;
		}
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_frames_titlepg {{{
# generate the titlepg.html file needed by the frames output style.
sub generate_frames_titlepg
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $style_dir  = "$out_dir/frames";
	my $templfname = "$templ_dir/frames/frames/titlepg.txt";
	my $outfname   = "$style_dir/titlepg.html";
	my $image      = $pres->{SLIDES}{cover}{IMAGE};

	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{cover}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{cover}{NEXT}}{TITLE}/;
		$_ =~ s/SLIDE_IMAGE/<img src=..\/graphics\/slides\/$image alt=\"$image\"\/>\n/;

		print OUTPUT $_; 
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_frames_foils_and_foilgrps {{{
# generate the foilnn.html and the foilgrpnn.html files needed by the frames
# output style.
sub generate_frames_foils_and_foilgrps
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;
	
	my $last;

	foreach my $foil_name (@{$pres->{ORDER}}) {
		if ($pres->{TOTAL} == $pres->{SLIDES}{$foil_name}{INDICE})
		{
			$last = 1;
		}
		else
		{
			$last = 0;
		}
		generate_frames_foil ($out_dir, $pres, $foil_name, $last);
	}
}
#}}}
# generate_frames_foil {{{
# generate a particular foil[grp]nn.html.
sub generate_frames_foil
{
	# Three arguments: reference to the presentation, foil name,
	# boolean at true if it's the last.
	my ($out_dir, $pres, $foil_name, $last) = @_;
	
	my $style_dir = "$out_dir/frames";
	my $outfname  = "$style_dir/$foil_name.html";
	my $templfname;
	
	my $image = $pres->{SLIDES}{$foil_name}{IMAGE};
	my $ov_str = "";
	my $ov_ind;
	my $ov_tot;
	
	if ($foil_name =~ m/cover/)
	{
		return;
	}
	
	# foils and foilgrps are treated in the same way.
	if ($last == 0)
	{
		$templfname = "$templ_dir/frames/frames/foil.txt";
	}
	else
	{
		$templfname = "$templ_dir/frames/frames/lastfoil.txt";
	}
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");

	if ($foil_name =~ m/^foil\d\d\d-\d\d$/)
	{
		$ov_ind = $pres->{SLIDES}{$foil_name}{OVERLAYS_IND};
		$ov_tot = $pres->{SLIDES}{$foil_name}{OVERLAYS_TOTAL};
		$ov_str = "<font size=2> [$ov_ind/$ov_tot]</font>";
	}
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/TITLE/$pres->{SLIDES}{$foil_name}{TITLE}/;
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/PREV/$pres->{SLIDES}{$foil_name}{PREV}/;
		$_ =~ s/PREV_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{$foil_name}{PREV}}{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{$foil_name}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{$foil_name}{NEXT}}{TITLE}/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;
		$_ =~ s/OVERLAYS/$ov_str/;
		$_ =~ s/SLIDE_IMAGE/<img src=..\/graphics\/slides\/$image alt=\"$image\"\/>\n/;
		$_ =~ s/cover.html/titlepg.html/;
		print OUTPUT $_; 
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}

# generate_w3c_toc {{{
# generate the toc.html file needed by the w3c output style.
sub generate_w3c_toc
{
	
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $style_dir  = "$out_dir/w3c";
	my $templfname = "$templ_dir/w3c/w3c/toc.txt";
	my $outfname   = "$style_dir/toc.html";
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{cover}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{cover}{NEXT}}{TITLE}/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;

		if ($_ =~ m/INSERT_TOC_HERE/)
		{
			my $tab = "\t\t\t\t\t";
			my $infoilgrp= 0;
			my $first_overlays;
			foreach my $foil_name (@{$pres->{ORDER}}) {
				if ($foil_name =~ m/^foil\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t\t\t";
					}
					else
					{
						$tab = "\t\t\t\t\t\t\t\t";
					}
					print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$foil_name.html\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
				}
				elsif ($foil_name =~ m/^foilgrp\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t\t\t";
						$infoilgrp= 1;
					}
					else
					{
						$tab = "\t\t\t\t\t";
						print OUTPUT "$tab\t\t</dl>\n";
						print OUTPUT "$tab\t</dd>\n";
					}
					print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$foil_name.html\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
					print OUTPUT "$tab\t<dd>\n";
					print OUTPUT "$tab\t\t<dl>\n";
				}
				# foil with overlays
				elsif ($foil_name =~ m/^foil\d\d\d-(\d\d)$/)
				{
					if ($1 eq "01")
					{
						$first_overlays = $foil_name;
					}
					if ($pres->{SLIDES}{$foil_name}{OVERLAYS_LAST})
					{
						if ($infoilgrp == 0)
						{
							$tab = "\t\t\t\t\t";
						}
						else
						{
							$tab = "\t\t\t\t\t\t\t\t";
						}
						print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$first_overlays.html\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
					}
				}
			}
			if ($infoilgrp)
			{
				$tab = "\t\t\t\t\t";
				print OUTPUT "$tab\t\t</dl>\n";
				print OUTPUT "$tab\t</dd>\n";
			}
		}
		else
		{
			print OUTPUT $_;
		}
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_w3c_index {{{
# generate the index.html file needed by the w3c output style.
sub generate_w3c_index
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $style_dir  = "$out_dir/w3c";
	my $templfname = "$templ_dir/w3c/w3c/index.txt";
	my $outfname   = "$style_dir/index.html";
	my $image      = $pres->{SLIDES}{cover}{IMAGE};

	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{cover}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{cover}{NEXT}}{TITLE}/;
		$_ =~ s/SLIDE_IMAGE/<img src=..\/graphics\/slides\/$image alt=\"$image\"\/>\n/;

		print OUTPUT $_; 
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_w3c_foils_and_foilgrps {{{
# generate the foilnn.html and the foilgrpnn.html files needed by the w3c
# output style.
sub generate_w3c_foils_and_foilgrps
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $last;
	
	foreach my $foil_name (@{$pres->{ORDER}}) {
		if ($pres->{TOTAL} == $pres->{SLIDES}{$foil_name}{INDICE})
		{
			$last = 1;
		}
		else
		{
			$last = 0;
		}
		generate_w3c_foil ($out_dir, $pres, $foil_name, $last);
	}
}
#}}}
# generate_w3c_foil {{{
# generate a particular foil[grp]nn.html.
sub generate_w3c_foil
{
	# Three arguments: reference to the presentation, foil name,
	# boolean at true if it's the last.
	my ($out_dir, $pres, $foil_name, $last) = @_;
	
	my $style_dir = "$out_dir/w3c";
	my $outfname  = "$style_dir/$foil_name.html";
	my $templfname;
	
	my $image = $pres->{SLIDES}{$foil_name}{IMAGE};
	my $ov_str = "";
	my $ov_ind;
	my $ov_tot;
	
	if ($foil_name =~ m/cover/)
	{
		return;
	}
	
	# foils and foilgrps are treated in the same way.
	if ($last == 0)
	{
		$templfname = "$templ_dir/w3c/w3c/foil.txt";
	}
	else
	{
		$templfname = "$templ_dir/w3c/w3c/lastfoil.txt";
	}
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	if ($foil_name =~ m/^foil\d\d\d-\d\d$/)
	{
		$ov_ind = $pres->{SLIDES}{$foil_name}{OVERLAYS_IND};
		$ov_tot = $pres->{SLIDES}{$foil_name}{OVERLAYS_TOTAL};
		$ov_str = "<font size=2> [$ov_ind/$ov_tot]</font>";
	}

	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/TITLE/$pres->{SLIDES}{$foil_name}{TITLE}/;
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/PREV/$pres->{SLIDES}{$foil_name}{PREV}/;
		$_ =~ s/PREV_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{$foil_name}{PREV}}{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{$foil_name}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{$foil_name}{NEXT}}{TITLE}/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;
		$_ =~ s/SLIDE_IMAGE/<img src=..\/graphics\/slides\/$image alt=\"$image\"\/>\n/;
		$_ =~ s/INDICE/$pres->{SLIDES}{$foil_name}{INDICE}/;
		$_ =~ s/OVERLAYS/$ov_str/;
		$_ =~ s/TOTAL/$pres->{TOTAL}/;
		$_ =~ s/cover.html/index.html/;
		print OUTPUT $_; 
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}

# generate_tables_toc {{{
# generate the toc.html file needed by the tables output style.
sub generate_tables_toc
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;
	
	my $style_dir  = "$out_dir/tables";
	my $templfname = "$templ_dir/tables/tables/toc.txt";
	my $outfname   = "$style_dir/toc.html";
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{cover}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{cover}{NEXT}}{TITLE}/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;

		if ($_ =~ m/INSERT_TOC_HERE/)
		{
			my $tab = "\t\t\t\t\t";
			my $infoilgrp= 0;
			my $first_overlays;
			foreach my $foil_name (@{$pres->{ORDER}}) {
				if ($foil_name =~ m/^foil\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t\t\t";
					}
					else
					{
						$tab = "\t\t\t\t\t\t\t\t";
					}
					print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$foil_name.html\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
				}
				elsif ($foil_name =~ m/^foilgrp\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t\t\t";
						$infoilgrp= 1;
					}
					else
					{
						$tab = "\t\t\t\t\t";
						print OUTPUT "$tab\t\t</dl>\n";
						print OUTPUT "$tab\t</dd>\n";
					}
					print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$foil_name.html\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
					print OUTPUT "$tab\t<dd>\n";
					print OUTPUT "$tab\t\t<dl>\n";
				}
				# foil with overlays
				elsif ($foil_name =~ m/^foil\d\d\d-(\d\d)$/)
				{
					if ($1 eq "01")
					{
						$first_overlays = $foil_name;
					}
					if ($pres->{SLIDES}{$foil_name}{OVERLAYS_LAST})
					{
						if ($infoilgrp == 0)
						{
							$tab = "\t\t\t\t\t";
						}
						else
						{
							$tab = "\t\t\t\t\t\t\t\t";
						}
						print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$first_overlays.html\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
					}
				}
			}
			if ($infoilgrp)
			{
				$tab = "\t\t\t\t\t";
				print OUTPUT "$tab\t\t</dl>\n";
				print OUTPUT "$tab\t</dd>\n";
			}
		}
		else
		{
			print OUTPUT $_;
		}
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_tables_index {{{
# generate the index.html file needed by the tables output style.
sub generate_tables_index
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $style_dir  = "$out_dir/tables";
	my $templfname = "$templ_dir/tables/tables/index.txt";
	my $outfname   = "$style_dir/index.html";
	my $image      = $pres->{SLIDES}{cover}{IMAGE};

	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{cover}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{cover}{NEXT}}{TITLE}/;
		$_ =~ s/SLIDE_IMAGE/<img src=..\/graphics\/slides\/$image alt=\"$image\"\/>\n/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;

		print OUTPUT $_; 
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_tables_foils_and_foilgrps {{{
# generate the foilnn.html and the foilgrpnn.html files needed by the tables
# output style.
sub generate_tables_foils_and_foilgrps
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $last;
	my $grp = "";
	
	foreach my $foil_name (@{$pres->{ORDER}}) {
		if ($pres->{TOTAL} == $pres->{SLIDES}{$foil_name}{INDICE})
		{
			$last = 1;
		}
		else
		{
			$last = 0;
		}
		if ($foil_name =~ m/^foilgrp\d\d\d$/)
		{
			$grp = $foil_name;
		}
		generate_tables_foil ($out_dir, $pres, $foil_name, $last, $grp);
	}
}
#}}}
# generate_tables_foil {{{
# generate a particular foil[grp]nn.html.
sub generate_tables_foil
{
	# Four arguments: reference to the presentation, foil name,
	# boolean at true if it's the last and current foil group.
	my ($out_dir, $pres, $foil_name, $last, $current_grp) = @_;
	
	my $style_dir = "$out_dir/tables";
	my $outfname  = "$style_dir/$foil_name.html";
	my $templfname;
	
	my $image = $pres->{SLIDES}{$foil_name}{IMAGE};
	
	if ($foil_name =~ m/cover/)
	{
			  return;
	}
	
	# foils and foilgrps are treated in the same way.
	if ($last == 0)
	{
		$templfname = "$templ_dir/tables/tables/foil.txt";
	}
	else
	{
		$templfname = "$templ_dir/tables/tables/lastfoil.txt";
	}
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/TITLE/$pres->{SLIDES}{$foil_name}{TITLE}/;
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/PREV/$pres->{SLIDES}{$foil_name}{PREV}/;
		$_ =~ s/PREV_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{$foil_name}{PREV}}{TITLE}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{$foil_name}{NEXT}/;
		$_ =~ s/NEXT_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{$foil_name}{NEXT}}{TITLE}/;
		$_ =~ s/UP/$pres->{SLIDES}{$foil_name}{UP}/;
		$_ =~ s/UP_TITLE/$pres->{SLIDES}{$pres->{SLIDES}{$foil_name}{UP}}{TITLE}/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;
		$_ =~ s/SLIDE_IMAGE/<img src=..\/graphics\/slides\/$image alt=\"$image\"\/>/;
		$_ =~ s/cover.html/index.html/;
		if ($_ =~ m/INSERT_TTOC_HERE/)
		{
			my $grp= "";
			foreach my $foil (@{$pres->{ORDER}}) {
				if ($foil =~ m/^foil\d\d\d$/)
				{
					if ($grp eq "")
					{
						if ($foil eq $foil_name)
						{
							tables_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "pointer", 0);
						}
						else
						{
							tables_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "blank", 0);
						}
					}
				}
				elsif ($foil =~ m/^foilgrp\d\d\d$/)
				{
					$grp = $foil;
					if ($grp eq $current_grp)
					{
						tables_insert_ttoc_foilgrp_open (\*OUTPUT, $pres, $foil_name, $current_grp);
					}
					else
					{
						tables_insert_ttoc_foilgrp (\*OUTPUT, $pres, $foil, "blank");
					}
				}
				elsif ($foil =~ m/^(foil\d\d\d)-(\d\d)$/)
				{
					if ($grp eq "")
					{
						if ($foil eq $foil_name)
						{
							tables_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "pointer", 0);
						}
						else
						{
							# For overlays num 1 and for slides other than current slide.
							# Ex: if current slide ($foil_name) = foil002-02, we must
							# treat foil001-01, foil003-01... but not foil002-01.
							if (("$2" eq "01") && ($foil_name !~ m/$1/ ))
							{
								tables_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "blank", 0);
							}
						}
					}
				}
			}
		}
		else
		{
			print OUTPUT $_;
		}
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# tables_insert_ttoc_foil {{{
sub tables_insert_ttoc_foil
{
	# Five arguments: Output file handler, reference to the presentation
	# and foil name, image file and level of indentation.
	my ($out, $pres, $foil_name, $file, $level) = @_;

	my $tab = "\t\t\t\t\t\t\t\t";
	my $title = $pres->{SLIDES}{$foil_name}{TITLE};
	my $ov_str = "";
	my $ov_ind;
	my $ov_tot;
	
	if ($level == 1)
	{
		print $out "$tab<img alt=\" \" src=\"..\/graphics\/blank.png\"/>\n";
	}
	if ($file eq "pointer")
	{
		print $out "$tab<img alt=\"+\" src=\"..\/graphics\/pointer.png\"/>\n";
		print $out "$tab<span class=\"ttoc-foil\">\n";
		if ($foil_name =~ m/^foil\d\d\d-\d\d$/)
		{
			$ov_ind = $pres->{SLIDES}{$foil_name}{OVERLAYS_IND};
			$ov_tot = $pres->{SLIDES}{$foil_name}{OVERLAYS_TOTAL};
			$ov_str = "<font size=2> [$ov_ind/$ov_tot]</font>";
			print $out "$tab\t<a href=\"$foil_name.html\">$title $ov_str</a>\n";
		}
		else
		{
			print $out "$tab\t<a href=\"$foil_name.html\">$title</a>\n";
		}
	}
	elsif ($file eq "blank")
	{
		print $out "$tab<img alt=\" \" src=\"..\/graphics\/blank.png\"/>\n";
		print $out "$tab<span class=\"ttoc-foil\">\n";
		if ($foil_name =~ m/^foil\d\d\d-\d\d$/)
		{
			$ov_tot = $pres->{SLIDES}{$foil_name}{OVERLAYS_TOTAL};
			$ov_str = "<font size=2> [$ov_tot]</font>";
			print $out "$tab\t<a href=\"$foil_name.html\">$title $ov_str</a>\n";
		}
		else
		{
			print $out "$tab\t<a href=\"$foil_name.html\">$title</a>\n";
		}
	}
	print $out "$tab</span>\n";
	print $out "$tab<br/>\n";

}
#}}}
# tables_insert_ttoc_foilgrp_open {{{
sub tables_insert_ttoc_foilgrp_open
{
	# Four arguments: Output file handler, reference to the presentation,
	# foil name and current group of the foil.
	my ($out, $pres, $foil_name, $current_grp) = @_;

	my $tab = "\t\t\t\t\t\t\t\t";
	my $title = $pres->{SLIDES}{$foil_name}{TITLE};
	my $grp = "";

	foreach my $foil (@{$pres->{ORDER}}) {
		if ($foil =~ m/^foil\d\d\d$/)
		{
			if ($grp eq $current_grp)
			{
				if ($foil eq $foil_name)
				{
					tables_insert_ttoc_foil ($out, $pres, $foil, "pointer", 1);
				}
				else
				{
					tables_insert_ttoc_foil ($out, $pres, $foil, "blank", 1);
				}
			}
		}
		elsif ($foil =~ m/^foilgrp\d\d\d$/)
		{
			$grp = $foil;
			if ($grp eq $current_grp)
			{
				if ($foil eq $foil_name)
				{
					tables_insert_ttoc_foilgrp ($out, $pres, $foil, "pointer");
				}
				else
				{
					tables_insert_ttoc_foilgrp ($out, $pres, $foil, "blank");
				}
			}
		}
		elsif ($foil =~ m/^(foil\d\d\d)-(\d\d)$/)
		{
			if ($grp eq $current_grp)
			{
				if ($foil eq $foil_name)
				{
					tables_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "pointer", 1);
				}
				else
				{
					# For overlays num 1 and for slides other than current slide.
					# Ex: if current slide ($foil_name) = foil002-02, we must treat
					# foil001-01, foil003-01... but not foil002-01.
					if (("$2" eq "01") && ($foil_name !~ m/$1/ ))
					{
						tables_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "blank", 1);
					}
				}
			}
		}
	}
}
#}}}
# tables_insert_ttoc_foilgrp {{{
sub tables_insert_ttoc_foilgrp
{
	# Four arguments: Output file handler, reference to the presentation
	# and foil name and image file.
	my ($out, $pres, $foil_name, $file) = @_;

	my $tab = "\t\t\t\t\t\t\t\t";
	my $title = $pres->{SLIDES}{$foil_name}{TITLE};

	print $out "$tab<img alt=\" \" src=\"..\/graphics\/$file.png\"/>\n";
	print $out "$tab<span class=\"ttoc-foilgroup\">\n";
	print $out "$tab\t<a href=\"$foil_name.html\">$title</a>\n";
	print $out "$tab</span>\n";
	print $out "$tab<br/>\n";

}
#}}}

# generate_gtree_toc {{{
# generate the toc.html file needed by the gtree output style.
sub generate_gtree_toc
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;
	
	my $style_dir  = "$out_dir/gtree";
	my $templfname = "$templ_dir/gtree/gtree/toc.txt";
	my $outfname   = "$style_dir/toc.html";
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;

		if ($_ =~ m/INSERT_TOC_HERE/)
		{
			my $tab = "\t\t\t\t\t";
			my $infoilgrp= 0;
			my $first_overlays;
			foreach my $foil_name (@{$pres->{ORDER}}) {
				if ($foil_name =~ m/^foil\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t\t\t";
					}
					else
					{
						$tab = "\t\t\t\t\t\t\t\t";
					}
					print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$foil_name.html\" style=\"color: blue; text-decoration: none;\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
				}
				elsif ($foil_name =~ m/^foilgrp\d\d\d$/)
				{
					if ($infoilgrp == 0)
					{
						$tab = "\t\t\t\t\t";
						$infoilgrp= 1;
					}
					else
					{
						$tab = "\t\t\t\t\t";
						print OUTPUT "$tab\t\t</dl>\n";
						print OUTPUT "$tab\t</dd>\n";
					}
					print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$foil_name.html\" style=\"color: blue; text-decoration: none;\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
					print OUTPUT "$tab\t<dd>\n";
					print OUTPUT "$tab\t\t<dl>\n";
				}
				# foil with overlays
				elsif ($foil_name =~ m/^foil\d\d\d-(\d\d)$/)
				{
					if ($1 eq "01")
					{
						$first_overlays = $foil_name;
					}
					if ($pres->{SLIDES}{$foil_name}{OVERLAYS_LAST})
					{
						if ($infoilgrp == 0)
						{
							$tab = "\t\t\t\t\t";
						}
						else
						{
							$tab = "\t\t\t\t\t\t\t\t";
						}
						print OUTPUT "$tab<dt>$pres->{SLIDES}{$foil_name}{INDICE}. <a href=\"$first_overlays.html\" style=\"color: blue; text-decoration: none;\">$pres->{SLIDES}{$foil_name}{TITLE}</a></dt>\n";
					}
				}
			}
			if ($infoilgrp)
			{
				$tab = "\t\t\t\t\t";
				print OUTPUT "$tab\t\t</dl>\n";
				print OUTPUT "$tab\t</dd>\n";
			}
		}
		else
		{
			print OUTPUT $_;
		}
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# generate_gtree_foils_and_foilgrps {{{
# generate the foilnn.html and the foilgrpnn.html files needed by the gtree
# output style.
sub generate_gtree_foils_and_foilgrps
{
	# Two arguments: output directory and presentation.
	my ($out_dir, $pres) = @_;

	my $last;
	my $grp = "";
	
	foreach my $foil_name (@{$pres->{ORDER}}) {
		if ($pres->{TOTAL} == $pres->{SLIDES}{$foil_name}{INDICE})
		{
			$last = 1;
		}
		else
		{
			$last = 0;
		}
		if ($foil_name =~ m/^foilgrp\d\d\d$/)
		{
			$grp = $foil_name;
		}
		generate_gtree_foil ($out_dir, $pres, $foil_name, $last, $grp);
	}
}
#}}}
# generate_gtree_foil {{{
# generate a particular foil[grp]nn.html.
sub generate_gtree_foil
{
	# Four arguments: reference to the presentation, foil name,
	# boolean at true if it's the last and current foil group.
	my ($out_dir, $pres, $foil_name, $last, $current_grp) = @_;
	
	my $style_dir = "$out_dir/gtree";
	my $outfname  = "$style_dir/$foil_name.html";
	my $templfname;
	
	my $image = $pres->{SLIDES}{$foil_name}{IMAGE};
	
	if ($foil_name =~ m/cover/)
	{
		$templfname = "$templ_dir/gtree/gtree/index.txt";
		$outfname = "$style_dir/index.html";
		$image = $pres->{SLIDES}{cover}{IMAGE};
	}
	else
	{
		# foils and foilgrps are treated in the same way.
		if ($last == 0)
		{
			$templfname = "$templ_dir/gtree/gtree/foil.txt";
		}
		else
		{
			$templfname = "$templ_dir/gtree/gtree/lastfoil.txt";
		}
	}
	
	# Open the output file in write mode.
	open(OUTPUT, ">$outfname") or die("open: $!");
	
	# Open the model file in read-only.
	open(TEMPLATE, "<$templfname") or die("open: $!");
	
	# We read all lines of the this file.
	while( <TEMPLATE> )
	{
		$_ =~ s/TITLE/$pres->{SLIDES}{$foil_name}{TITLE}/;
		$_ =~ s/PRES_TITLE/$pres->{TITLE}/;
		$_ =~ s/PREV/$pres->{SLIDES}{$foil_name}{PREV}/;
		$_ =~ s/NEXT/$pres->{SLIDES}{$foil_name}{NEXT}/;
		$_ =~ s/CAPTION/$pres->{CAPTION}/;
		$_ =~ s/INDICE/$pres->{SLIDES}{$foil_name}{INDICE}/;
		$_ =~ s/TOTAL/$pres->{TOTAL}/;
		$_ =~ s/SLIDE_IMAGE/<img src=..\/graphics\/slides\/$image \/>/;
		$_ =~ s/cover.html/index.html/;
		if ($_ =~ m/INSERT_TTOC_HERE/)
		{
			my $grp= "";
			foreach my $foil (@{$pres->{ORDER}}) {
				if ($foil =~ m/^foil\d\d\d$/)
				{
					if ($grp eq "")
					{
						if ($foil eq $foil_name)
						{
							#ttoc-foil-selected
							gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foil-selected", $pres, $foil);
						}
						else
						{
							#ttoc-foil
							gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foil", $pres, $foil);
						}
					}
					else
					{
						if ($grp eq $current_grp)
						{
							if ($foil eq $foil_name)
							{
								#ttoc-foilingrp-selected
								gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foilingrp-selected", $pres, $foil);
							}
							else
							{
								#ttoc-foilingrp
								gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foilingrp", $pres, $foil);
							}
						}
					}
				}
				elsif ($foil =~ m/^foilgrp\d\d\d$/)
				{
					$grp = $foil;
					if ($grp eq $current_grp)
					{
						if ($foil eq $foil_name)
						{
							#ttoc-grp-open-selected
							gtree_insert_ttoc_item (\*OUTPUT, "ttoc-grp-open-selected", $pres, $foil);
						}
						else
						{
							#ttoc-grp-open
							gtree_insert_ttoc_item (\*OUTPUT, "ttoc-grp-open", $pres, $foil);
						}
					}
					else
					{
						#ttoc-grp-closed
						gtree_insert_ttoc_item (\*OUTPUT, "ttoc-grp-closed", $pres, $foil);
					}
				}
				elsif ($foil =~ m/^(foil\d\d\d)-(\d\d)$/)
				{
					if ($grp eq "")
					{
						if ($foil eq $foil_name)
						{
							#ttoc-foil-selected
							gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foil-selected", $pres, $foil);
							#gtree_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "pointer", 0);
						}
						else
						{
							# For overlays num 1 and for slides other than current slide.
							# Ex: if current slide ($foil_name) = foil002-02, we must
							# treat foil001-01, foil003-01... but not foil002-01.
							if (("$2" eq "01") && ($foil_name !~ m/$1/ ))
							{
								#ttoc-foil
								gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foil", $pres, $foil);
								#gtree_insert_ttoc_foil (\*OUTPUT, $pres, $foil, "blank", 0);
							}
						}
					}
					else
					{
						if ($grp eq $current_grp)
						{
							if ($foil eq $foil_name)
							{
								#ttoc-foilingrp-selected
								gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foilingrp-selected", $pres, $foil);
							}
							else
							{
								if (("$2" eq "01") && ($foil_name !~ m/$1/ ))
								{
									#ttoc-foilingrp
									gtree_insert_ttoc_item (\*OUTPUT, "ttoc-foilingrp", $pres, $foil);
								}
							}
						}
					}
				}
			}
		}
		else
		{
			print OUTPUT $_;
		}
	}

	close(TEMPLATE);
	close(OUTPUT);
}
#}}}
# gtree_insert_ttoc_item {{{
sub gtree_insert_ttoc_item
{
	# Four arguments: Output file handler, template file name, 
	# reference to the presentation and foil name.
	my ($out, $ttoc_file, $pres, $foil_name) = @_;
	
	my $ttoc_templ = "$templ_dir/gtree/gtree/$ttoc_file.txt";
	my $title = $pres->{SLIDES}{$foil_name}{TITLE};
	my $ov_str = "";
	my $ov_ind;
	my $ov_tot;
	
	# Open the model file in read-only.
	open(TTOC_TEMPLATE, "<$ttoc_templ") or die("open: $!");
	
	if ($foil_name =~ m/^foil\d\d\d-\d\d$/)
	{
		if ($ttoc_file =~ m/selected/)
		{
			$ov_ind = $pres->{SLIDES}{$foil_name}{OVERLAYS_IND};
			$ov_tot = $pres->{SLIDES}{$foil_name}{OVERLAYS_TOTAL};
			$ov_str = "<font size=2> [$ov_ind/$ov_tot]</font>";
		}
		else
		{
			$ov_tot = $pres->{SLIDES}{$foil_name}{OVERLAYS_TOTAL};
			$ov_str = "<font size=2> [$ov_tot]</font>";
		}
	}

	# We read all lines of the this file.
	while( <TTOC_TEMPLATE> )
	{
		$_ =~ s/TITLE/$title/;
		$_ =~ s/OVERLAYS/$ov_str/;
		$_ =~ s/FOIL_NAME/$foil_name/;
		print $out $_;
	}
}
#}}}

# vim:fdm=marker
