#!/usr/bin/env perl

=for Copyright
 .
 Copyright (c) 2008-2019 Bruce Ravel (L<http://bruceravel.github.io/home>).
 All rights reserved.
 .
 This file is free software; you can redistribute it and/or
 modify it under the same terms as Perl itself. See The Perl
 Artistic License.
 .
 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.

=cut

use Demeter;
use Getopt::Long;

use subs qw(BOLD RED RESET YELLOW GREEN);
my ($color, $nocolor, $latex, $html, $noframe, $version, $help, $rmax) = (1,0,0,0,0,0,0,0);
GetOptions (
	    "color"     => \$color,
	    "nocolor"   => \$nocolor,
	    "latex"     => \$latex,
	    "html"      => \$html,
	    "noframe"   => \$noframe,
	    "rmax|r=s"  => \$rmax,
	    "version|v" => \$version,
	    "help|h"    => \$help,
	   );

&version, exit if $version;
&help,    exit if $help;

my $ANSIColor_exists = (eval "require Term::ANSIColor");
if ($ANSIColor_exists) {
  import Term::ANSIColor qw(:constants);
} else {
  $nocolor = 1;
};
($noframe = 1) if not ($latex or $html);

my $feff = Demeter::Feff -> new(yaml => $ARGV[0]||"feff.yaml");
$feff -> save(1);
my $style = ($nocolor) ? q{}
          : ($latex)   ? "latex"
          : ($html)    ? "css"
          : ($color)   ? {comment => BOLD.RED,
			  close   => RESET,
			  1       => BOLD.YELLOW,
			  2       => BOLD.GREEN,
			  0       => q{},
			 }
          :             q{};
$feff->rank_paths;
print header($latex, $html) if not $noframe;
print body($feff->intrp($style, $rmax), $latex, $html);
print footer($latex, $html) if not $noframe;

## The end!

sub header {
  my ($latex, $html) = @_;
  return &latex_header if $latex;
  return &html_header  if $html;
  return q{};
};
sub body {
  my ($text, $latex, $html) = @_;
  return &latex_body($text) if $latex;
  return &html_body($text)  if $html;
  return $text;
};
sub footer {
  my ($latex, $html) = @_;
  return &latex_footer if $latex;
  return &html_footer  if $html;
  return q{};
};

sub version {
  print "intrp for Demeter $Demeter::VERSION, copyright (c) 2008-2009, Bruce Ravel, http://bruceravel.github.io/home\n";
};
sub help {
  &version;
  print <<'EOH'

usage : intrp [options] myfeffcalc.yaml

    option           effect
 ------------------------------------------------------------------
   --color         write color output to the screen (default)
   --nocolor       write colorless output to the screen
   --latex         write latex file output
   --html          write html file output
   --noframe       write latex or html without header and footer
   -v, --version   display version number and exit
   -h, --help      show this help and exit

EOH
    ;
};

## latex output ==========================================================

sub latex_header {
  return <<'EOH'
\documentclass{article}
\usepackage{alltt}
\pdfoutput=1\relax
\usepackage[pdftex]{color}
\usepackage[pdftex,colorlinks,breaklinks,backref,bookmarks=true]{hyperref}
\usepackage{graphicx}
\pdfcompresslevel=9
\definecolor{commentcolor}{rgb}{0.70,0.00,0.00}
\definecolor{majorcolor}{rgb}{0.00,0.70,0.00}
\definecolor{minorcolor}{rgb}{0.50,0.30,0.00}

\setlength{\hoffset}{0truecm} \setlength{\voffset}{0truecm}
\setlength{\topmargin}{0truecm} \setlength{\marginparsep}{0.25truecm}
\setlength{\marginparwidth}{2.5truecm}
\setlength{\textheight}{22truecm} \setlength{\textwidth}{17.5truecm}
\setlength{\topmargin}{-1truecm}
\setlength{\oddsidemargin}{-1truecm}
\setlength{\evensidemargin}{-1truecm}
\setlength{\headsep}{0.5truecm}
%\setlength{\parindent}{21pt}

\begin{document}
\begin{alltt}
EOH
    ;
};

sub latex_body {
  my ($text) = @_;
  return $text;
};

sub latex_footer {
  return<<'EOH'
\end{alltt}
\end{document}
EOH
    ;
};


## html output ==========================================================

sub html_header {
  return <<'EOH'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Feff calculation interpretation</title>
<style type="text/css">
 <!--
.comment {
    font: 1em monospace;
    color: #990000;
}
.major {
    font: 1em monospace;
    color: #009900;
}
.minor {
    font: 1em monospace;
    color: #775500;
}
.normal {
    font: 1em monospace;
    color: #777777;
}
--></style>
</head>
<body>
<h1>Feff calculation interpretation</h1>
EOH
    ;
};

sub html_body {
  my ($text) = @_;
  $text =~ s{ (?!class)}{&nbsp;}g; # this is a bit fragile, but that
  return $text;                    # should be the only non-nbsp in
};                                 # the body of the html file

sub html_footer {
  return "</body></html>\n";
};



=head1 NAME

intrp - Write a textual interpretation of a Feff calculation

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

  intrp [option] myfeffcalc.yaml

If no yaml file is specified at the command line, F<feff.yaml>
in the current working directory will be used, if available.

=head1 DESCRIPTION

This writes a concise summary of a Feff calculation to standard
output, providing useful information for interpreting the calculation
and begining to set up a fit.  The degeneracy and nominal path length
are reported as well as an summary of the scattering geometry of each
path.

=head1 COMMAND LINE SWITCHES

=over 4

=item C<--color>

Write output using terminal ANSI colors.  This is the default
behavior.

=item C<--nocolor>

Suppress color output.  This is the default behavior if the terminal
colors are not available.

=item C<--latex>

Write output as a latex file ready to be compiled.  Note that the
output is written to the screen, so you may need to direct the output
to a file.

  intrp --latex myfeffcalc.yaml > myfeffcalc.tex

=item C<--html>

Write output in the form of an html page.  Note that the
output is written to the screen, so you may need to direct the output
to a file.

  intrp --html myfeffcalc.yaml > myfeffcalc.html

=item C<--noframe>

With this flag, the html or latex output will be written without the
header and footer.  That is, the output will be valid html or latex,
but will not contain the lines at the beginning and end of the file
necessary to make them free-standing html or compilable latex.

=item C<--rmax> or C<-r>

Restrict output to a given value of R.  If R is larger than the
longest path, all paths will be displayed.

=back

=head1 CONFIGURATION AND ENVIRONMENT

See L<Demeter::Config> for a description of Demeter's
configuration system.  The C<feff> configuration group controls many
aspects of the interpretation.

=head1 DEPENDENCIES

This script uses L<Term::ANSIColor> to color the terminal output, but
it defaults gracefully to colorless output if this module is not
available.

This reads the L<YAML|http://www.yaml.org> output from Demeter's
reimplementation of Feff's pathfinder.  This version of C<intrp> does
B<not> do its interpretation using F<feff.inp>, F<files.dat>, and
F<paths.dat>, like earlier incarnations and will B<not> do an
interpretation of your non-Demeter Feff calculations.

The dependencies of the Demeter system are in the
F<Build.PL> file.

=head1 BUGS AND LIMITATIONS

See L<Demeter::Feff> and L<Demeter::ScatteringPath>
for bugs and limitations of the underlying libraries.

Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)

Patches are welcome.

=head1 AUTHOR

Bruce Ravel (L<http://bruceravel.github.io/home>)

http://bruceravel.github.io/demeter/

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2008-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.

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.

=cut
