#!/usr/bin/perl

use strict;
use warnings;

use Carp::Assert;
use Date::Format;
use File::Basename;
use File::Copy;
use Getopt::Std;
#use File::stat;
use XML::LibXML;
use XML::LibXSLT;

use Ernad::Common;
use Ernad::Constant;
use Ernad::Erimp;
use Ernad::Files;
use Krichel::File;


my %o;
&Getopt::Std::getopts('vfo:', \%o);
my $do_verbose = $o{'v'} // 0;
my $do_force = $o{'f'} // 0;
my $do_only= $o{'o'} // 0;

my $type='static_page';

my $impna=$ARGV[0];
if(not defined($impna)) {
  die "I need the impna.";
}
my $e=Ernad::Erimp->new({'impna'=>$impna});
if(not $e) {
  die "You geve me an invalid impna $impna\n";
}

my $www_dir=$e->{'dir'}->{'www'};
my $web_dir=$e->{'dir'}->{'web'};
my $web_opt_dir=$e->{'dir'}->{'web_opt'};
my $style_file=$e->{'dir'}->{'style'}.'/'.$type.$e->{'const'}->{'xsl_ext'};

## Testing creates XML pages before the transformation. This is done on the
## test machine only. We don't need testing for static pages. FixMe, the
## testing parameter should be renamed.
$e->{'testing'}='';

## FixMe: we should do this with apach configs
if($impna ne 'bims') {
  &Ernad::Files::check_links_in_dir($web_opt_dir);
  &Ernad::Files::check_links_in_dir($www_dir);
  &Ernad::Files::check_links_in_dir($web_dir);
}

foreach my $fufi (`find $www_dir/`) {
  chomp $fufi;
  if($do_only and not $fufi=~m|\Q$do_only\E|) {
    next;
  }
  if(-d $fufi) {
    if($do_verbose) {
      print "$fufi is a directory\n";
    }
    next;
  }
  my $file=$fufi;
  $file=~s|$www_dir||;
  chomp $fufi;
  if($do_verbose) {
    print "found $fufi\n";
  }
  if(-l $fufi) {
    if($impna ne 'bims') {
      &Ernad::Files::check_link($fufi);
    }
    if(substr($fufi,-4) eq '.xml') {
      $e->{'t'}->transform_file($fufi,$type);
      if($do_verbose) {
        print "skipping link $fufi\n";
      }
    }
  }
  if(&Ernad::Files::is_emacs_file($fufi)) {
    next;
  }
  my $out_file=$fufi;
  $out_file=~s|$www_dir/|$web_opt_dir/| or die;
  my $out_file_html=$out_file;
  $out_file_html=~s|\.xml|.html|;
  ## if the out file does not exist, remove it's dangling opt link too
  ##
  &Ernad::Files::remove_dangling_opt_link_to_file($out_file);
  &Ernad::Files::remove_dangling_opt_link_to_file($out_file_html);
  if(not -f $out_file_html) {
    my $no_opt_out_file=Ernad::Files::remove_opt($out_file_html);
    if(-l $no_opt_out_file) {
      &Ernad::Files::check_link($no_opt_out_file);
      if($do_verbose) {
        print "I delete the link $no_opt_out_file\n";
      }
      system("rm $no_opt_out_file");
    }
  }
  if($fufi=~m|obsolete|) {
    if($do_verbose) {
      print "I skip $fufi\n";
    }
    next;
  }
  if($fufi=~m|~$|) {
    if($do_verbose) {
      print "I skip $fufi\n";
    }
    next;
  }
  if($fufi=~m|\.xml$| and not -l "$fufi.txt") {
    if($do_verbose) {
      print "$fufi need transforming\n";
    }
    my $update_time=&Ernad::Files::does_file_need_renewal($out_file_html,$fufi,$style_file);
    if(not $update_time) {
      if(not $do_force) {
        if($do_verbose) {
          print "out file $out_file_html does not need renewing\n";
          ##
        }
        next;
      }
      elsif((-f $out_file_html or -f readlink($out_file_html))) {
        $update_time=Ernad::Dates::mtime($fufi);
      }
      else {
        $update_time=time;
      }
    }
    if($do_verbose) {
      print "$fufi is to be transformed\n";
      print "update_time is $update_time\n";
    }
    my $update_date=time2str("%Y\x{2013}%m\x{2013}%d",$update_time,"UTC");
    if($do_verbose) {
      print "I transform ... $fufi";
    }
    my $doc=$e->{'t'}->transform_file($fufi,$type, {update => "'$update_date'"});
    if($do_verbose) {
      print "done.\n";
    }
    &Krichel::File::prepare($out_file_html);
    $doc->toFile($out_file_html);
    if($do_verbose) {
      print "wrote $out_file_html\n";
    }
    next;
  }
  if($do_verbose) {
    print "special $fufi\n";
  }
  if(-f $fufi and not -l $fufi) {
    my $update_time=&Ernad::Files::does_file_need_renewal($out_file,$fufi);
    if($update_time) {
      if($do_verbose) {
        print "I copy $fufi to $out_file\n";
      }
      copy($fufi,$out_file) or die;
    }
  }
  &Ernad::Files::make_opt_link($out_file);
  &Ernad::Files::check_text_link($fufi,$web_opt_dir);
}


&Ernad::Files::check_opt_links($web_opt_dir);
