#!/usr/bin/env perl

# This is valid Perl code but not yet .latexmkrc. 
# It is a template where the names of settings of this builder 
# must be replaced by their values. 
# As a result, a true .latexmkrc files arises which reflects the current settings of this builder. 

# The names to be replaced have the form $-{parameterName} or $-{functionName()} but without '-' 
# and both variants may only occur within double quoted literal Perl strings. 
# Although they look similarly, they are no variables to be substituted. 
# They are not allowed within single quoted strings 
# because if the java strings they represent contain a " this is always replaced by \" 
# before substituting. 
# This holds even if there is no variable substitution so " is superfluous and ' would do also from the point of view of Perl. 
# Perl interprets the sequence \" within double quoted strings as just " in the Perl string, 
# so that the Perl string is exactly what the original java string was. 

# A word on the difference between $-{parameterName} and $-{functionName()}: 
# Whereas the former is just the name of a setting, i.e. a parameter in class Settings, 
# the latter is the result of some computation based on the Settings. 
# As an example, $-patternLatexMainFilePerl()} is the parameter $-{patternLatexMainFile} 
# which is a regex pattern in the java world, which is useless in this context, 
# whereas $-{patternLatexMainFilePerl()} is the translation into an equivalent Perl pattern. 


# The following is to check whether the pom really overwrites the settings in defaultSettingsAs.properties
# ${chkTexOptions}<chkTexOptions>-q -b0 -L -H1</chkTexOptions>

# to create pdf via lualatex 
#$pdflatex = 'lualatex -file-line-error %O %S';

# PDF-generating modes are:
# 0: do NOT generate a pdf version of the document. (default)
# 1: pdflatex, as specified by $pdflatex variable (still largely in use)
# 2: via postscript conversion, as specified by the $ps2pdf variable (useless)
# 3: via dvi conversion, as specified by the $dvipdf variable (useless)
# 4: lualatex, as specified by the $lualatex variable (best)
#    Note that we abuse that by replacing lualatex by a variable
# 5: xelatex, as specified by the $xelatex variable (second best)
# This is abused here, just to invoke $lualatex which leads to run_latex
$pdf_mode = 4;# specifies creation of pdf via lualatex 

# If additional parameters must be passed to lualatex 
#'svg' package.
# It converts raw SVG files to the PDF+PDF_TEX combo using InkScape.
# $lualatex = "lualatex --shell-escape";

# note that -recorder is implicitly added by latexmk, 
# so may be duplicated, but no disadvantage 
# %O is the options (additional options passed by latexmk)
# %S source file (maybe %A and %B more appropriate: without ending)
#$lualatex = "${getLatex2pdfCommand()} ${latex2pdfOptions} %O %S";
$lualatex = "internal run_latex %A %O";

# superfluous for perl >=5.36 according to documentation, but does not work for me (perl 5.38?)
use v5.36;
use feature 'signatures';
# for treatment of sub functionName($val = undef)
no warnings 'experimental::signatures';
#no warnings 'experimental::args_array_with_signatures';
# the following desirable but currently not possible 
#use strict;
#use warnings;

__PACKAGE__ eq 'main' or die "Not in main package!";


package LatexHelpers;
# TBD: distinguish between DocumentMetadata and SetKeys 

# indicates whether a compilation has been run. 
# It is initialized in 
my $hasRunComp;

# written as side effect of parseTexFile 
# may be undef or empty which must be distinguished. 
my %docMeta;


my %metaPdfFlat;
my %metaPdfXmp;


sub reset() {
  $hasRunComp = 0;
  undef %docMeta;
  undef %metaPdfFlat;
  undef %metaPdfXmp;
}


sub hasRunComp($val = undef) {
    return $hasRunComp unless defined($val);
    $hasRunComp = $val;
}

# sub docMeta ($key = undef) {
#   return $docMeta{$key} if defined $key;
#   return \%docMeta; # reference on hash 
# }

sub docMeta($key = undef) {
  # Wenn der Hash leer ist, verhalte dich wie Java: return null (undef)
  return undef unless %docMeta; 

  # Ansonsten gib die Referenz (das Objekt) zurück
  return $docMeta{$key} if defined $key;
  return \%docMeta; 
}

use Data::Dumper; #for printing hashes 

sub parseTexFile($fileName) {
  # The pattern is used to read magic comments. 
  # Double quotes because the pattern contains single quotes; 
  # no interpolation 
  my ($programMagic, $chkDiffMagic, $chkDiffMagicVal, $docMetadata, $docClass);
  my $patternLatexMainFile = "${patternLatexMainFilePerl()}";
  print("patternLatexMainFile: \n$patternLatexMainFile\n");
  open my $info, $fileName or die "Could not open $fileName: $!";
  # the lines read so far (each line with newline)
  my $lines = "";
  while (my $line = <$info>) {
    print "$line\n";
    $lines = "$lines$line";
    if ($line =~ /$patternLatexMainFile/) {
      #print("line matches |$+{programMagic}|\n");
      #print("line matches |$+{docClass}|\n");
      #print("lines: \n$lines\n");
      close $info;
      if ($lines !~ /$patternLatexMainFile/) {
        die("$fileName is no latex main file: preamble does not match\n");
      }
      print("preamble matches: \n");
      $programMagic    = $+{programMagic}    if $+{programMagic};
      $chkDiffMagic    = $+{chkDiffMagic}    if $+{chkDiffMagic};
      $chkDiffMagicVal = $+{chkDiffMagicVal} if $+{chkDiffMagicVal};
      $docMetadata     = $+{docMetadata}     if $+{docMetadata};
      $docClass        = $+{docClass}        if $+{docClass};
    }
    # Here, the line does not match: go on 
    last if $docClass;
  } # while 



  print("docClass: $docClass\n");
  print("docMetas: $docMetadata\n");

  if (defined $docMetadata) {
    my $patternMetadata = "${patternMetadataPerl()}";

    while ($docMetadata =~ /$patternMetadata/g) {
      my $key   = $+{key};
      my $value = $+{value};   # kann undef sein

      # ggf. {} entfernen
      #$value =~ s/^\{|\}$//g if defined $value;
      if ($key eq 'pdfstandard') {

        my $oldVal = $docMeta{$key};
        if (defined $oldVal) {
          # Wenn schon was da war, hängen wir das neue mit Komma an
          $docMeta{$key} = $oldVal . ", " . $value;
        } else {
        # Erster Fund: Einfach speichern
        $docMeta{$key} = $value;
    }
      }

      $docMeta{$key} = $value // 1;  # 1 if $value is undef (uncompress)
    }
  } else {
    undef %docMeta;
  }
 
  print ("docMeta: %docMeta");

  # Make default value explicit 
  $chkDiffMagic = ($chkDiffMagic and not $chkDiffMagicVal)
    ? 'true' : $+{chkDiffMagicVal};
  # return the magic comments relevant in this context 
  return ($+{programMagic}, $chkDiffMagic);
}


#parseTexFile($ARGV[0]);

use Cwd qw(getcwd);
use File::Spec::Functions qw(catfile);
use Capture::Tiny qw(capture_stdout);

#use DateTime;
use DateTime::Format::ISO8601; # for ->parse_datetime


sub initPdfMetadataHash($pdfFile) {
    my %metaHash;

    # 1. Basis-Infos (CreationDate, Author, Pages, etc.)
    my ($stdout, $res) = capture_stdout {
      system("${getPdfMetainfoCommand()} ${pdfMetainfoXmpOptions} \"$pdfFile\"");
    };
    
    $res == 0 or die "FATAL: ${getPdfMetainfoCommand()} faied with return value $res.";
    my @lines = split /\r?\n/, $stdout;

    foreach my $line (@lines) {
      $line =~ /^(?<key>[^:]+):\s+(?<value>.*)$/ 
        or die "FATAL: Unexpected pdfinfo output format or empty line: ['$line']";
      $metaPdfFlat{$+{key}} = $+{value};
    }


    # 2. XMP-Metadaten (Die präzise Welt)
    ($stdout, $res) = capture_stdout {
        system("${getPdfMetainfoCommand()} ${pdfMetainfoOptions} \"$pdfFile\"");
    };

    $res == 0 or die "FATAL: ${getPdfMetainfoCommand()} failed with return value $res.";

    # TBD: read completely all 
    while ($stdout =~ m|<(?<key>[\w:]+)>(?<value>[^<>]+)</\k<key>>|sg) {
        die "FATAL: Duplicate XMP key found: ['$+{key}']" if exists $PdfXmp{$+{key}};
        $metaPdfXmp{$+{key}} = $+{value};
    }
}

sub getCreationTimeMetaEpoch($pdfFile) {
  initPdfMetadataHash($pdfFile);
  my $creationDate = $metaPdfXmp{'xmp:CreateDate'} // $metaPdfFlat{'CreationDate'};
  defined $creationDate or die "Found no creation date";
  #$creationDate =~ s/^D://;
  #print("metainfo ok: $res\n");
  #print("metainfos: \n$stdout\n");
  #print("CreationDate: $creationDate}n");
  my $dt = DateTime::Format::ISO8601->parse_datetime($creationDate);
  my $creationDateEpoch = $dt->epoch();
  #print("internal epoch time: $creationDateEpoch\n");
  return $creationDateEpoch;
}

sub getTimestampDiff($fileName) {
  # The following is to determing PDF file to diff if chkDiff is set 
  my $pdfFileOrg = catfile(getcwd(), $fileName);

  my $baseDirectory   = "${baseDirectory}/"; # trailing '/' for concatenation 
  my $texSrcDirectory = "${texSrcDirectory}/";
  my $diffDirectory   = "${diffDirectory}/";

  $pdfFileOrg =~ s/\Q$baseDirectory$texSrcDirectory//;
  my $pdfFileDiff = "$baseDirectory$diffDirectory$pdfFileOrg";
  if (!-e $pdfFileDiff) {
    # Here, $epoch_timestamp is not defined 
    return undef;
  }
  #die("File $pdfFileDiff to diff does not exist ") unless ;
  # epoch time of last modification # TBD: avoid magic number 9 
  my $epoch_timestamp = int((stat($pdfFileDiff))[9]);
  my $creationDateEpoch = LatexHelpers::getCreationTimeMetaEpoch($pdfFileDiff);
  # my ($stdout, $res) = capture_stdout { system("${getPdfMetainfoCommand()} ${pdfMetainfoOptions} $pdfFileDiff") };
  # print ("metainfo ok: $res\n");
  # print ("metainfos: \n$stdout\n");
  # $stdout =~ /CreationDate:\s*(?<creationDate>.*)\R/ or die("${getPdfMetainfoCommand()} did not get CreationDate. ");
  # print ("CreationDate: $+{creationDate}");
  # my $dt = DateTime::Format::ISO8601->parse_datetime($+{creationDate});
  print("+++meta epoch time: $creationDateEpoch\n");
  print("+++file epoch time: $epoch_timestamp\n");
  return $creationDateEpoch;
}

1;

package main;


# TBD: not ideal foor pdfViaDvi=true: conversion dvi to pdf is needed only once at the end, 
# whereas this method does conversion dvi to pdf each time also tex to dvi is performed. 
# Thus in the long run only run dvi2pdf; the rest is done with rules. 
sub run_latex($fileName, @opts) {

  # transforms string representations from pom to perl specific representations 
  # maybe there are alternative: yes for true and no for false. Clarify. 
  my %boolStrToVal = (true => 1, false => 0);

  # This presupposes that latexmk is invoked with the filename without extension 
  ($programMagic, $chkDiffMagic) = LatexHelpers::parseTexFile("$fileName.tex");

  # override settings if magic comment is present 
  my $latexCommand = ($programMagic ? $programMagic : "${getLatex2pdfCommand()}");
  my $chkDiffB     = ($chkDiffMagic ? $chkDiffMagic : "${chkDiff}");
  $chkDiffB = $boolStrToVal{$chkDiffB};

  my $timeEnv = "";
  my $epoch_timestamp;
  if ($chkDiffB) {
    # # The following is to determing PDF file to diff if chkDiff is set 
    # my $pdfFileOrg=catfile(getcwd, "$fileName.pdf");

    # my $baseDirectory="${baseDirectory}/";# trailing '/' for concatenation 
    # my $texSrcDirectory="${texSrcDirectory}/";
    # my $diffDirectory="${diffDirectory}/";

    # $pdfFileOrg =~ s/\Q$baseDirectory$texSrcDirectory//;
    # my $pdfFileDiff = "$baseDirectory$diffDirectory$pdfFileOrg";
    # die("File $pdfFileDiff to diff does not exist ") unless (-e $pdfFileDiff);

    $epoch_timestamp = LatexHelpers::getTimestampDiff("$fileName.pdf");
    if (defined($epoch_timestamp)) {
      # Here, the reference file exists 
      # For lualatex setting TZ=UTC is needed but FORCE_SOURCE_DATE is ignored 
      # For pdflatex setting TZ=UTC is superfluous but FORCE_SOURCE_DATE is needed; 
      # the same for xelatex 
      $timeEnv = "TZ=UTC SOURCE_DATE_EPOCH=$epoch_timestamp FORCE_SOURCE_DATE=1 ";
    } else {
      # Here, the reference PDF file does not exist, so local time but with GMT timezone 
      $timeEnv = "TZ=UTC ";
    }
    # in both cases note the trailing blank 
    # The settings are required both for direct compilation into PDF and for compilation via DVI 
  }

  my $pdfViaDvi = $boolStrToVal{"${pdfViaDvi}"};
  # note that exactly one of the two options -no-pdf -output-format=dvi applies; 
  # the other is ignored. 
  # TBD: eliminate: xelatex emits a warning because -output-format is unknown 
  my $addArgs = $pdfViaDvi ? "-no-pdf -output-format=dvi " : "";
  # TBD: evaluate return values properly.  
  my $res = system("$timeEnv$latexCommand ${latex2pdfOptions} $addArgs @opts $fileName");
  if ($pdfViaDvi) {
    # Note that $timeEnv is first of all suitable for the latex compiler. 
    # strictly speaking FORCE_SOURCE_DATE is not needed; the other variables are needed 
    # to set up 
    # TBC: ignores options %O from latexmk, acceptable 
    $res = $res or
      system("$timeEnv${getDvi2pdfCommand()} ${dvi2pdfOptions} $fileName");
  }
  LatexHelpers::hasRunComp(1);
  return $res;
}

#$postscript_mode = $dvi_mode = 0;

# to configure bibtex 
# bbl files are never precious 
$bibtex_use = 2;
$bibtex = "${bibtexCommand} ${bibtexOptions} %O %S";# default: bibtex %O %S

# this cannot be done according to the according latex maven plugin, 
# because the according parameter maxNumReRunsLatex may be set to -1 
# which signifies an infinite number of runs. 
$max_repeat = 30;

# default are tex and eps, but could also be pdf and ptx and mps
# Currently, all those files are given with explicit endings, 
# so no extensions to be added. 
#add_input_ext('');

# It is what it seems to be: clean inludes what was generated by cus 
$cleanup_includes_cusdep_generated = 1;
$cleanup_includes_generated = 1;

# TBD: clarify: xdv and dvi seem to be internal. 
# maybe missing other extensions in conjunction with synctex 
# maybe better @generated_exts see below 
push @generated_exts, "%R.synctex.gz", "%R.synctex";;

# bbl does not work
#@generated_exts = (@generated_exts, 'lol', 'bbl', 'glo', 'ist')
#print "Hello!"
#foreach (@generated_exts) {
#print "Generated exts: $_\n";
#}
#print "clean_ext\n";
# Here @generated_exts is ('aux', 'fls', 'log', # generated by latex already
# 'toc', 'lof', 'lot', 'out', # generated by latex conditionally 
# 'idx', 'ind', 'blg', 'ilg', # concerning indices 
# # strange enough: nothing for bibtex 
# 'xdv', 'bcf'

# extensions ext to be deleted by latexmk -c 
# Note that the file names are %R.ext. 
# this may cause problems with extensions containing a dot. 
# Also this is not general enough 
# if the generated file deviates from %R by more than an extension. 
# In this case, use the form with explicit '%R'. 
# list of listings, whereas lof and lot are already present. 
push @generated_exts, "lol";
push @generated_exts, "dvi", "xdv";
# for beamer class 
push @generated_exts, "nav", "snm", "vrb", 'run.xml';
push @generated_exts, "clg";# log file for chktex: specific for latex builder LMP 
push @generated_exts, "soc";# package changes, also adding toc if not present 
push @generated_exts, "sil";# package silence
push @generated_exts, "tdo";# package todonotes
push @generated_exts, "xmpdata";# written by filecontents for pdfx 

# why are 'ist' and 'xdy' not under generated_exts? 
# note that currently, either %R or what is present is the extension only! 
# this does not make sense very much. 
# $clean_ext .= " stateMachine.log"; does not work, because stateMachine.log is the extension! 

# should be under indexing 
push @generated_exts, '%ist', '%xdy', '%R-*.ind', '%R-*.idx', '%R-*.ilg', '%R-*.ind';

# many arguments shall be quoted 
# but in many cases it is immaterial; except in metapost 
sub quote($inString) {
  $outString = $inString;
  $outString =~ s/^ */'/; # add leading ' after eliminating leading blanks 
  $outString =~ s/ *$/'/; # add closing ' after eliminating trailing blanks 
  # so far: quote after trim 
  $outString =~ s/ +/' '/g; # replace a sequence of blanks by a single one 
  $outString =~ s/''//;     # unquote the empty string 
  
  #print "out: $outString\n";
  return $outString;
}


add_cus_dep('fig', 'ptx', 0, 'fig2dev');
sub fig2dev($file) {
  print("create 'ptx' from '$file.fig'\n");
  rdb_add_generated("$file.pdf", "$file.eps");
  my ($name, $path) = fileparse($file);
  pushd($path);
    #fig2dev -L pstex    <fig2devGenOptions> <fig2devPdfEpsOptions>        xxx.fig xxx.eps   
    #fig2dev -L pdftex   <fig2devGenOptions> <fig2devPdfEpsOptions>        xxx.fig xxx.pdf   
    #fig2dev -L pdftex_t <fig2devGenOptions> <fig2devPtxOptions>    -p xxx xxx.fig xxx.ptx
    # TBD: evaluate return values properly 
    my $ret1 = system(qq/${getFig2devCommand()} -L  pstex   ${fig2devGenOptions} ${fig2devPdfEpsOptions}       $name.fig $name.eps/);
    my $ret2 = system(qq/${getFig2devCommand()} -L pdftex   ${fig2devGenOptions} ${fig2devPdfEpsOptions}       $name.fig $name.pdf/);
    my $ret3 = system(qq/${getFig2devCommand()} -L pdftex_t ${fig2devGenOptions} ${fig2devPtxOptions} -p $name $name.fig $name.ptx/);
  popd();
  $res1 >>= 8;
  $res2 >>= 8;
  $res3 >>= 8;
 return ($ret1 or $ret2 or $ret3);
}

my $gnuplotOptions = "";
add_cus_dep('gp', 'ptx', 0, 'gnuplot');
sub gnuplot($file) {
  print("create 'ptx' from '$file.gp'\n");
  rdb_add_generated("$file.pdf", "$file.eps");
  my ($name, $path) = fileparse($file);
  pushd($path);
    # here in the java code no quoting occurs 
    #my $gnuplotOptionsQ = quote(qq/${gnuplotOptions}/);
    my $ret1 = system(qq/${getGnuplotCommand()} -e "set terminal cairolatex pdf ${gnuplotOptions};\
            set output '$name.ptx';\
            load '$name.gp'"/);
    # my $ret2 = system("gnuplot -e \"set terminal cairolatex eps ${gnuplotOptions};\
    #           set output '$name.ptx';\
    #           load '$name.gp'\"");
  popd();
  $res >>= 8; # reconstruct return value of the application 
  return $ret;
}

# metapost rule from http://tex.stackexchange.com/questions/37134
#add_cus_dep('mp', 'mps', 0, 'mpost');
add_cus_dep('mp', 'mps', 0, 'mpost');
sub mpost($file) {
  print("create 'mps' from '$file.mp'\n");
  rdb_add_generated("$file.mpx", "$file.fls", "$file.log");
  # TBD: mpx files are written only if the mp file contains latex parts. 
  # It would be cleaner to add the mpx file after invoking mpost 
  # with the condition that an mpx file is present. 
  # Likewise, 
  my ($name, $path) = fileparse($file);
  pushd($path);
    my $metapostOptionsQ = quote(qq/${metapostOptions}/);
    #print "quoted: $metapostOptionsQ\n";
    my $return = system(qq/${getMetapostCommand()} $metapostOptionsQ $name/);
  popd();
  $res >>= 8; # reconstruct return value of the application 
  return $return;
}


add_cus_dep('svg', 'ptx', 0, 'inkscape');
sub inkscape($file) {
  print("create  'ptx'from '$file.svg'\n");
  rdb_add_generated("$file.pdf", "$file.eps");
  my ($name, $path) = fileparse($file);
  pushd($path);
    my $ret1 = system(qq/${getSvg2devCommand()} --export-filename=$name.pdf ${svg2devOptions} $name.svg/);
    #my $ret2 = system("inkscape --export-filename=$name.eps -D --export-latex $name.svg ");
    #use File::Copy;
    # This works only for pdf, not for eps. 
    #unlink($name.pdf_tex) or die "cannot unlink $name.pdf_tex";
    rename("$name.pdf_tex", "$name.ptx");# or die "cannot move $name.pdf_tex";
  popd();
  $res >>= 8; # reconstruct return value of the application 
  return $ret1;# or $ret2;
}


# graphics for xfig (not appropriate for mixed tex/pdf)

# add_cus_dep('fig', 'pdf', 0, 'fig2pdf');

# sub fig2pdf {
# system( "fig2dev -Lpdf \"$_[0].fig\" \"$_[0].pdf\"" );
# }

$makeindex = 'internal run_makeSplitindex %A %O';

# if used \sindex[idx]{} and no other index name, this is misleading: is a single multi-index
sub parseIdxFileForMultiIdx($fileName) {
  $fileName = "$fileName.idx";
  # since the patter is likely to end in $, we use single quotes here 
  my $patternMultiIndex = '${patternMultiIndex}';
  open my $info, $fileName or die "Could not open $fileName: $!";
  my $foundDefaultEntry = 0;
  my %res = ();
  while (my $line = <$info>) {
    if ($line =~ /$patternMultiIndex/) {
      # Here, it is clear that we have a multi-index 
      $res{$2} = 1;
    } else {
      $foundDefaultEntry = 1;
    }
  }

  if ($foundDefaultEntry && %res) {
    # Here, both \index and \sindex are used 
    $res{"idx"} = 1;
  }
  close $info;
  # Here, it is clear that we have no multi-index 
  return keys %res;
}

sub run_makeSplitindex($fileName, @opts) {
  # $fileName is without ending 

  my @indexLabels = parseIdxFileForMultiIdx($fileName);
  if (@indexLabels) {
    # create dummy ind file 
    my $ind_fh = 'This is a dummy file. ';
    open($ind_fh, '>>', "$fileName.ind");
    close $ind_fh;

    foreach (@indexLabels) {
      # splitindex splits up the idx file into many 
      # and then generates ind file each 
      rdb_add_generated("$fileName-$_.idx", "$fileName-$_.ind");
    }

    $res = system("${splitIndexCommand} --makeindex ${makeIndexCommand} ${splitIndexOptions} $fileName -- ${makeIndexOptions} @opts");
  } else {
    $res = system("${makeIndexCommand} ${makeIndexOptions} @opts $fileName");
  }
  $res >>= 8; # reconstruct return value of the application 
  return $res
}

# This set of dependencies is only complete 
# if we restrict ourselves to types defined by options 
# without using \newglossary explicitly 
add_cus_dep( 'acn', 'acr', 0, 'run_makeglossaries' );
add_cus_dep( 'slo', 'sls', 0, 'run_makeglossaries' );
add_cus_dep( 'nlo', 'nls', 0, 'run_makeglossaries' );
#add_cus_dep( 'idx', 'ind', 0, 'run_makeglossaries' ); # would collide with indexing 
add_cus_dep( 'glo', 'gls', 0, 'run_makeglossaries' );
# TBD: add file endings for symbols, 
# not only here but also in the java code. 
push @generated_exts, 'glo', 'gls', 'glg';
push @generated_exts, 'acn', 'acr', 'alg';
push @generated_exts, 'slo', 'sls', 'slg';
push @generated_exts, 'nlo', 'nls', 'nlg';
# push @generated_exts, 'idx', 'ind', 'ilg'; # would collide with indexing 
push @generated_exts, "ist", "xdy"; # index stylefile created by the glossaries package 
# TBD: add xdy also in java code 

#$clean_ext .= " acr acn alg glo gls glg";# TBD: clarify: better in @generated_exts? 

sub run_makeglossaries($file) {
  my $options = "${makeGlossariesOptions}";
  if ($silent) {
    $options = "$options -q";
  }
  $res = system("${makeGlossariesCommand} $options $file");
  $res >>= 8; # reconstruct return value of the application 
  return $res;
}

# !!! ONLY WORKS WITH VERSION 4.54 or higher of latexmk
#TBD: take into account: modified:
#  '$_[0]'->

# #############
# # makeindex #
# #############
# @ist = glob("*.ist");
# if (scalar(@ist) > 0) {
#         $makeindex = "makeindex -s $ist[0] %O -o %D %S";
# }

# Implementing glossary with bib2gls and glossaries-extra, with the
#  log file (.glg) analyzed to get dependence on a .bib file.

# !!! ONLY WORKS WITH VERSION 4.54 or higher of latexmk

push @generated_exts, 'glg', '%R*.glstex';
# TBD: clarify 
#push @generated_exts, 'glg', '%R*.glstex';

# TBD: clarify treatment of encoding. 
# why we can avoid  
#        "--tex-encoding", "UTF-8",
#        "--log-encoding", "UTF-8",
# We need tex sources encoding in editor: utf8 
# in header 
# \iftutex%
#   \usepackage{fontspec}% sets utf8
# \else
#   % this seems to work with beamer also 
#   \usepackage[utf8]{inputenc}
#   \usepackage[T1]{fontenc}
# \fi
# in bib file: 1st line: 
# % Encoding: UTF-8


add_cus_dep('aux', 'glstex', 0, 'run_bib2gls');
# Explanation can be found in 
# https://tex.stackexchange.com/questions/400325/latexmkrc-for-bib2gls
sub run_bib2gls($file) {
  $options = "--group";
  if ($silent) {
    $options = "--silent $options";
  }
  my $ret = system("bib2gls $options $file");
  $ret >>= 8; # reconstruct return value of the application 
  if ($ret) {
    warn "Run_bib2gls: Error, running bib2gls; return value $ret.\n";
    return $ret;
  }

  # my ($base, $path) = fileparse($file);
  # if ($path && -e "$base.glstex") {
  #   rename "$base.glstex", "$path$base.glstex";
  # }

  # Analyze log file to find the bib-files.
  my $glg= "$file.glg";
  $isopen = open(my $glg_fh, '<', $glg);
  if (not $isopen) {
    warn "Run_bib2gls: Error opening log file '$glg'\n";
    return not @isopen;
  }
  rdb_add_generated($glg); 
 
  while (<$glg_fh>) {
    s/\s*$//;
    if (/^Reading\s+(.+)$/) {
      rdb_ensure_file($rule, $1);
    }
    if (/^Writing\s+(.+)$/) {
      rdb_add_generated($1);
    }
  }
  close $glg_fh;

  return $ret;
}

# The following code from John Collins is complementary to code in 
# changes/PythonTeXdep
# This code shall not be erased, it may only be deactivated. 
#$pythontex = 'pythontexW %R';#'pythontexW %O %R';
$pythontex = '${getPythontexCommand()} ${pythontexOptions} %R';#'pythontexW %O %R';

push @generated_exts, "pytxcode", "plg";
push @generated_exts, "depytx", "%R.depytx.tex", "dplg";

push @generated_exts, "${prefixPytexOutFolder}%R/*", "${prefixPytexOutFolder}%R";
#$extra_rule_spec{'pythontex'}  = [ 'internal', '', 'mypythontex', "%Y%R.pytxcode", "%Y${prefixPytexOutFolder}-%R/%R.pytxmcr", "%R", 1 ];
$extra_rule_spec{'pythontex'} = [
  'internal', '', 'mypythontex', 
  "%R.pytxcode", "${prefixPytexOutFolder}%R/%R.pytxmcr", "%R", 1
];

# Explanation for PythonTeX dependency 
# can be found in changes/PythonTeXdep 
# In fact, to make this work, 
# the code provided there must be included in package pythontex
# TBD: this function is invoked based on global variables. 
# For me, this is quite ugly... no parameters 
sub mypythontex {
  my $result_dir = $aux_dir1 . "${prefixPytexOutFolder}$$Pbase";
  my $ret        = Run_subst($pythontex, 2);
  rdb_add_generated(glob "$result_dir/*");

  my $fh = new FileHandle $$Pdest, "r";
  #open( my $fh, "<", $$Pdest );
  if ($fh) {
    #print "path: $ENV{PATH}";
    while (<$fh>) {
      if (/^%PythonTeX dependency:\s+'([^']+)';/) {
        print "Found pythontex dependency '$1'\n";
        rdb_ensure_file( $rule, $aux_dir1 . $1 );
      }
    }
    undef $fh;
  } else {
    warn "mypythontex: I could not read '$$Pdest'\n",
         "  to check dependencies\n";
  }
  return $ret;
}


# for htlatex 
push @generated_exts, "4tc", "4ct", "tmp", "xref", "css", "idv", "lg";
# TBD: for -C remove also html and xhtml
# TBD: check that this plugin also removes all these extensions.. think of lg. 


# biblatex
# push @generated_exts, "run.xml";# does run.xml work? 
# $clean_ext .= " %R-blx.bib";

sub run_preDecideCompilation($source, $target) {
  print("prior to decision whether compiling $source to $target needed\n");
  LatexHelpers::reset();
}

$compiling_cmd = "internal run_preDecideCompilation %T %D";
$compiling_cmd = $compiling_cmd;# calming down linter 

sub run_onSuccess($target) {
  print("Compilation succeeded without warning.\n");
  run_onSuccessWarn($target);
}

sub run_onWarn($target) {
  print("Compilation succeeded with warning(s).\n");
  run_onSuccessWarn($target);
}

sub run_onSuccessWarn($target) {
  # TBD: find out the cases where $target is empty 
  if ($target !~ m/.pdf$/) {
    print("created no pdf\n");
    return;
  }
  # currently, postprocessing occurs for pdf files only 

  if (LatexHelpers::hasRunComp()) {
    print("newly created pdf\n");
    my $ref = LatexHelpers::docMeta();

    if (!defined $ref) {
      print "no \\DocumentMetadata/SetKeys";
      return;
    }
    my %all = %$ref;
    print "All Metadata:\n";
    for my $k (keys %all) {
      print "  $k => $all{$k}\n";
    }
    return unless LatexHelpers::docMeta("pdfstandard");
  } else {
    # TBD: not perfect 
    print("old pdf check standard whether specified or not\n");
  }

  # Generalization needed for windows only 
  my ($bat, $cmd) = ("", "") ;
  if ($^O eq "Mswin32" ) {
    $bat=".bat";
    $cmd="cmd /c " ;
  } 
  my $res = system("$cmd" . "${verifyStdCommand}$bat ${verifyStdOptions} \"$target\"");
  $res >>= 8; # reconstruct return value of the application 
  print("${verifyStdCommand} return value: $res\n");
  if ($res == 0) {
    print("Conformance as requested. \n");
  } else {
    print("Conformance ***not*** as requested. \n");
  }
}

$success_cmd="internal run_onSuccess %D";

$warning_cmd="internal run_onWarn %D";

$failure_cmd="echo '...compilation with failure'";
