#!/usr/bin/perl
#
# Call    demo.pl [-v] folder [directory]
#        -v        = verbose
#        folder    = any kind of folder
#        directory = where the output will be

# example:
#     demo.pl -v $MAIL

sub usage() { die "Usage: $0 [-v] folder\n" }

use strict;
use warnings;

use File::Spec::Functions;

# Select the browser you use, to display the result
my $display = sub {
   "galeon -x $_[0]"
#  "mozilla -remote 'openFile($_[0])'"
#  "netscape -remote 'openFile($_[0])'"
};

my $verbose = @ARGV && $ARGV[0] eq '-v' ? shift @ARGV : 0;
@ARGV or usage;

#my $template_system;     # use default = OODoc
#my $template_system  = 'HTML::FromMail::Format::Magic';
my $template_system = 'HTML::FromMail::Format::OODoc';

# Relative directory with template files.  The template used is
# examples/magic1/message/index.html
my $templates = "templ_demo";

# Only for my own home test environment
use lib qw(lib ../lib);
use lib '/home/markov/shared/perl/MailBox3/lib';   # Mail::Box
use lib '/home/markov/shared/perl/Template/lib';   # OODoc::Template

# Here the real work starts
use Mail::Box::Manager;
use HTML::FromMail;
use Log::Report;

use File::Temp 'tempdir';

#
# Get the folder
#

@ARGV >= 1
	or error "no email folder name specified.";

my $filename = shift;

my $temp = @ARGV ? shift : tempdir;
if(@ARGV)
{	warning "too many command-line arguments.";
	usage;
}

#
# Open the folder to take first message
#

my $mgr      = Mail::Box::Manager->new;
my $folder   = $mgr->open($filename)
	or fault "cannot open folder $filename";

my $msg      = $folder->message(0)
	or error "The folder is empty... one message is required";

$msg->printStructure if $verbose;

#
# Look for the templates to be used.  Usually this is simple, but in
# the case of this example script, I cannot hard-code the path.
#

$templates = catdir 'examples', $templates
	unless -d $templates;

-d $templates
	or error "cannot find templates in $templates for the example. In which directory are you?";

print "Taking templates from $templates\n" if $verbose;

#
# Start the formatter
#

print "Producing output in $temp.\n" if $verbose;

# Translate a message into its location
sub message_directory($)
{   my $message = shift;
}

# Compose settings, in this case, for each page the same so only one
# formatter object is needed.

my %message_settings = (
	message_directory => \&message_directory,
);

my %field_settings = (
	address => 'MAILTO',
);

my %settings = (
	message => \%message_settings,
	field   => \%field_settings,
);

my @template_system = defined $template_system ? (formatter => $template_system) : ();

my $fmt   = HTML::FromMail->new(
	@template_system,
	templates => $templates,
	settings  => \%settings,
);

$fmt->export(
	$msg,
	use      => [ 'message/index.html', 'message/details.html' ],
	output   => $temp,
);

my $reply = $msg->reply(
	include     => 'INLINE',
	group_reply => 1,
);

$fmt->export(
	$reply,
	use      => 'message/reply.html',
	output   => $temp,
);

#
# Force open browser to load the produced page
#

my $start = catfile $temp, 'index.html';
print "Displaying $start\n" if $verbose;

system($display->($start))==0
	or fault "Couldn't send instruction to the browser";

print "Ready!\n" if $verbose;
