#!/usr/bin/perl

use strict;
use warnings;

use Carp qw(confess);
use File::Slurper;
use Getopt::Std;

use Ernad::Css;
use Ernad::Dates;
use Ernad::Erimp;
use Ernad::Test;
use Krichel::File;

binmode(STDOUT,'utf8');


if(not -d '//etc/wpa_supplicant') {
  print "You can't run this script here.\n";
  exit;
}


## options into %o;
my %o;
Getopt::Std::getopts('prst', \%o);
my $do_publish=$o{'p'} // '';
my $do_random=$o{'r'} // '';
my $do_sausage=$o{'s'} // '';
my $do_text_only=$o{'t'} // '';
if($do_sausage) {
  use Ernad::Common;
  use Ernad::Sausage;
}

my $impna=$ARGV[0] // die "I need an impna";
our $e=Ernad::Erimp->new({'impna'=> $impna});

## renew the product_css if necessary
my $xslt_dir=$e->{'dir'}->{'xslt'};
my $product_css='product_css';
my $product_css_file="$xslt_dir/$product_css.xml";
my $product_xslt_file="$xslt_dir/$product_css.xslt.xml";
my $product_css_time=&Ernad::Dates::mtime($product_css_file);
my $product_xslt_time=&Ernad::Dates::mtime($product_xslt_file);
if($product_css_time > $product_xslt_time) {
  my $s="xsltproc make_$product_css.xslt.xml $product_css.xml > $product_css.xslt.xml";
  print "$s\n";
  system("cd $xslt_dir ; xsltproc make_$product_css.xslt.xml $product_css.xml > $product_css.xslt.xml");
}


my $in_file;
if($do_random) {
  $in_file=&Ernad::Test::find_recent_random_sent();
  if(not $in_file) {
    confess "I don't find a recent random sent file for you.";
  }
}
else {
  $in_file=$ARGV[1] // '';
  if(not $in_file) {
    print "Use -r or give me an in_file\n";
  }
  if(not -f $in_file) {
    print "I don't see your in_file to test.\n";
    exit;
  }
  my $e=shift // confess "I need an erimp here.";
}
if($in_file=~m|^\.gz$|) {
  my $tmp_file="/tmp/test_$impna.amf.xml";
  system("zcat $in_file > $tmp_file");
  $in_file=$tmp_file;
}

## fixme, this should be ernad dependsx
my $home_dir=$ENV{'HOME'};
my $out_dir="$home_dir/$impna/public_html/opt/bench";
my $style_dir="$home_dir/ernad/style/$impna";



## basic screens
my $v;
$v->{'pickup'}='papers_screen';
$v->{'filter'}='papers_screen';
$v->{'sortis'}='sorting_screen';
$v->{'vieweb'}='final_html_screen';
$v->{'vitext'}='final_text_screen';
$v->{'maiweb'}='email_html_issue';
$v->{'matext'}='email_text_issue';
$v->{'pubweb'}='make_issue_html';

## which screens to change css for on testing?
my $css_change;
$css_change->{'pickup'}=1;
$css_change->{'filter'}=1;
$css_change->{'pubweb'}=1;
$css_change->{'sortis'}=1;

## parameters
my $p;
$p->{'pickup'}->{'stage'}='created';
$p->{'filter'}->{'stage'}='selected';

if($do_publish) {
  ## work on the CSS file
  my $css_file;
  if(not $do_text_only) {
    $css_file=&Ernad::Css::file('its_a_test');
  }
  ## the output file to write to
  if($css_file) {
    print "I upload $css_file\n";
    system("upload_file $css_file > /tmp/upload_css &");
  }
}

foreach my $view (keys %$v) {
  if($do_text_only) {
    if($view ne 'matext') {
      #print "I skip view $view\n";
      next;
    }
    #print "I do $view\n";
  }
  my $err_file='/tmp/bench_'.$impna.'_'."$view.err";
  unlink $err_file;
  my $out_file;
  if($view eq 'matext') {
    $out_file="$out_dir/$view.txt";
  }
  else {
    $out_file="$out_dir/$view.html";
  }
  my $style_file=$style_dir.'/'.$v->{$view}.'.xslt.xml';
  if(not $style_file) {
    print "I don't see your $style_file\n";
    exit;
  }
  my $params='';
  if($p->{$view}) {
    my @names=keys %{$p->{$view}};
    foreach my $name (@names) {
      $params.=" --param $name ";
      $params.="\"'".$p->{$view}->{$name}."'\"";
    }
  }
  if($do_sausage) {
    my $doc=&Krichel::File::load($in_file);
    &Ernad::Sausage::inject($doc);
    ## new tmp file name
    $in_file="/tmp/$view.xml";
    $doc->toFile($in_file);
  }
  my $s="xsltproc $params $style_file $in_file";
  $s.=" > $out_file 2> $err_file";
  unlink $err_file;
  system($s);
  ## aiplane short cut
  if($do_text_only) {
    system("cat $out_file");
    exit;
  }
  if($css_change->{$view}) {
    &Ernad::Test::change_furl_in_file($out_file,$e);
  }
  if($do_publish) {
    system("upload_file $out_file > /tmp/view.copy &");
  }
  my $err=`cat $err_file`;
  chomp $err;
  if($err) {
    $err="while processing $style_file ...\n$err";
    &File::Slurper::write_text($err_file,$err);
    print "$style_file\n\n";
    print `cat $err_file`;
    if($err=~m|file (\S+) line (\d+)|) {
      system("emacs $err_file +$2 $1");
    }
    exit;
  }
}
