# Implements use of bib2gls with glossaries-extra.
# Version of 5 May 2025.
# Thanks to Marcel Ilg for a suggestion.

push @generated_exts, 'glg', '%R*.glstex';

# For case that \GlsXtrLoadResources is used and so glstex file (first one)
# has same name as .aux file.
add_cus_dep( 'aux', 'glstex', 0, 'run_bib2gls' );


# The (commented-out) definition below ONLY WORKS if (a) aux_dir is
#   cwd, and (b) document uses only \glsxtrresourcefile, so that
#   glstex files have the same basenames as the corresponding .bib files.
#   and (c) does not use \GlsXtrLoadResources.
# It fails nastily if the document uses both of \glsxtrresourcefile and
#   \GlsXtrLoadResources.
# Note that the previous definition works fine if the document uses both of
#   \glsxtrresourcefile and  \GlsXtrLoadResources.  It only fails if the
#   document exclusively uses \glsxtrresourcefile.
#
# **Important:***
# ********* The use of \glsxtrresourcefile is now deprecated, as of  **********
# ********* v. 1.55 of the glossaries-extra package, which writes a  **********
# ********* warning message to the .log file if \glsxtrresourcefile  **********
# ********* is used.                                                 ********** 
# **** Therefore the following line is commented out, but the commented-out ****
# **** line is left in here, since it gives information that is relevant    ****
# **** for (certain) legacy documents.                                      ****
### add_cus_dep( 'bib', 'glstex', 0, 'run_bib2gls_alt' );

sub run_bib2gls {
    my $ret = 0;
    my ($base, $path) = fileparse( $_[0] );
    # Default encoding assumed by bib2gls is the system CS, which on
    # Windows is normally *not* CP65001 (aka UTF-8).  But the aux and log
    # files are UTF-8, at least with the default settings of TeX Live.
    # So we tell bib2gls about this.
    # With the options as given here, the .glg will be encoded in the system
    # CS, which will be suitable for latexmk's use, since that is the CS for
    # communicating with the file system.
    # (**Warning**, it is simpler to do this rather than using, the option
    #  --default-encoding to set everything to UTF-8, for then that would
    #  apply also to the .glg, which would entail having to convert filenames
    #  in the .glg file to the system CS.)
    my @bib2gls_cmd = (
         "--tex-encoding", "UTF-8",
         "--log-encoding", "UTF-8",
         "--group",
         "--dir",
         $path,
         $base
    );
    if ($silent) { unshift @bib2gls_cmd, "--silent"; }
    unshift @bib2gls_cmd, "bib2gls";
    print "Running '@bib2gls_cmd'...\n";
    $ret = system @bib2gls_cmd;
    
    if ($ret) {
        warn "Run_bib2gls: Error, so I won't analyze .glg file\n";
        return $ret;
    }
    # Analyze bib2gls's log file:
    my $glg = "$_[0].glg";
    if ( open( my $glg_fh, '<', $glg) ) {
        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;
    }
    else {
        warn "Run_bib2gls: Cannot read log file '$glg': $!\n";
    }
    return $ret;
}

sub run_bib2gls_alt {
    return Run_subst( 'internal run_bib2gls %Y%R' );
}

