# Modifications 2015 Sep 9-10, John Collins
# Copyright 2014 Vincent Belaïche <vincent.b.1@hotmail.fr>

# With the settings here, latexmk can be used to process texinfo files
# (typical extension .texi) to pdf files, including the making of
# indices.  This version uses a newly documented internal routine
# of latexmk.

###!!!!!!!!!  NOTE THAT THE NAMES OF THE INDEX FILES ARE POSSIBLY
###           SUBJECT TO CHANGE IN FUTURE VERSIONS OF texinfo.
###           The configuration provided by this file was valid in
###           September 2015.  (The file texinfo.tex had version
###           2015-07-01.07.)

$quote_filenames = 1;
$pdflatex = 'internal mylatex %R %Z pdftex %O %S';
$latex = 'internal mylatex %R %Z etex %O %S';

sub mylatex {
    my $root = shift;
    my $dir_string = shift;
    my $ret = system @_;
    for my $ext (split " ",$texinfo_indices){
        my $idx = $dir_string.$root.'.'.$ext;
        my $ind = $idx.'s';
        if ( (-e $idx) && (-s $idx) ) {
            # Only make dependency on the ind-like file
            # if the idx-like file both exists and is of
            # non-zero length.  The test on the length is
            # needed because current versions of texindex
            # produce no output file if the input file is
            # of zero length.
            rdb_ensure_file( $rule, $ind );
	  }
    }
    return $ret;
}

# Please add needed other extensions if there are other user defined indices
$texinfo_indices = 'ky fn cp vr tp pg';

$clean_ext .= ' ' . $texinfo_indices;
$clean_full_ext .= ' pdf toc aux';

for my $ext (split " ",$texinfo_indices){
    my $index_ext = $ext . 's';
    add_cus_dep( $ext, $index_ext, 1, $ext . '_texindex');
    add_input_ext('pdflatex', $index_ext);
    add_input_ext('latex', $index_ext);
    $clean_ext .= " $index_ext";
}

sub ky_texindex 
{
    system( "texindex \"$_[0].ky\"" );
}
sub fn_texindex 
{
    system( "texindex \"$_[0].fn\"" );
}
sub cp_texindex
{
    system( "texindex \"$_[0].cp\"" );
}
sub vr_texindex
{
    system( "texindex \"$_[0].vr\"" );
}
sub tp_texindex
{
    system( "texindex \"$_[0].tp\"" );
}
sub pg_texindex
{
    system( "texindex \"$_[0].pg\"" );
}
