!config
# Arara, the cool TeX automation tool
# Copyright (c) 2023, Island of TeX
# All rights reserved.
#
# This rule is part of arara.
identifier: bib2gls
name: Bib2Gls
authors:
- Island of TeX
commands:
- name: The Bib2Gls software
  command: >
    @{
       if (!clean.isEmpty())
       {
           prefix = [];
           if (isUnix()) {
               prefix = [ 'rm', '-f', '-v' ];
           }
           else {
               prefix = [ 'cmd', '/c', 'del' ];
           }

           base = getBasename(reference);
           removals = [];

           lines = readFromFile(base.concat('.').concat('aux'));

           java.util.regex.Pattern resourcePattern =
                java.util.regex.Pattern.compile(
                 "\\\\glsxtr@resource\\{.*\\}\\{([^\\}]+)\\}");

           if (!trans.isEmpty())
           {
              if (trans[0] == getOriginalFile())
              {
                 throwError('I cannot remove the main file reference.');
              }

              removals.add(getCommand(prefix, trans));
           }
           else
           {
              removals.add(getCommand(prefix, base.concat('.glg')));
           }

           foreach (line: lines)
           {
              matcher = resourcePattern.matcher(line);

              if (matcher.matches())
              {
                 removals.add(getCommand(prefix, matcher.group(1).concat('.glstex')));
              }
           }

           return removals;
       }
       else
       {
          return getCommand('bib2gls', dir, trans, group, interpret,
                 breakspace, trimfields, recordcount, recordcountunit,
                 cite, verbose, merge, locale, uniscript, packages,
                 ignore, custom, mapformats, options,
                 getBasename(reference.fileName));
       }
    }
arguments:
- identifier: dir
  flag: >
   @{
       return ['--dir', parameters.dir]
    }
- identifier: trans
  flag: >
   @{
       if ([ 'tex', 'ltx', 'cls', 'sty', 'bib',
             'dtx', 'ins', 'def', 'ldf' ].contains(parameters.trans)) {
          throwError('Forbidden transcript extension');
       }

       return ['--log-file', getBasename(reference.fileName)+"."+parameters.trans]
    }
- identifier: locale
  flag: >
   @{
       return ['--locale', parameters.locale]
    }
- identifier: group
  flag: >
   @{
       isTrue(parameters.group, '--group', '--no-group')
    }
- identifier: interpret
  flag: >
   @{
       isTrue(parameters.interpret, '--interpret', '--no-interpret')
    }
- identifier: breakspace
  flag: >
   @{
       isTrue(parameters.breakspace, '--break-space', '--no-break-space')
    }
- identifier: trimfields
  flag: >
   @{
       isTrue(parameters.trimfields, '--trim-fields', '--no-trim-fields')
    }
- identifier: recordcount
  flag: >
   @{
       isTrue(parameters.recordcount, '--record-count', '--no-record-count')
    }
- identifier: recordcountunit
  flag: >
   @{
       isTrue(parameters.recordcountunit, '--record-count-unit',
              '--no-record-count-unit')
    }
- identifier: cite
  flag: >
   @{
       isTrue(parameters.cite, '--cite-as-record', '--no-cite-as-record')
    }
- identifier: verbose
  flag: >
   @{
       isTrue(parameters.verbose, '--verbose', '--no-verbose')
    }
- identifier: merge
  flag: >
   @{
       isTrue(parameters.merge, '--merge-wrglossary-records',
              '--no-merge-wrglossary-records')
    }
- identifier: uniscript
  flag: >
   @{
       isTrue(parameters.uniscript, '--support-unicode-script',
              '--no-support-unicode-script')
    }
- identifier: packages
  flag: >
    @{
        if (isList(parameters.packages)) {
           elements = [];
           foreach (element : parameters.packages) {
              elements.add('--packages');
              elements.add(element);
           }
           return elements;
        }
        else {
            throwError('I was expecting a list of package names.');
        }
    }
- identifier: ignore
  flag: >
    @{
        if (isList(parameters.ignore)) {
           elements = [];
           foreach (element : parameters.ignore) {
              elements.add('--ignore-packages');
              elements.add(element);
           }
           return elements;
        }
        else {
            throwError('I was expecting a list of package names.');
        }
    }
- identifier: custom
  flag: >
    @{
        if (isList(parameters.custom)) {
           elements = [];
           foreach (element : parameters.custom) {
              elements.add('--custom-packages');
              elements.add(element);
           }
           return elements;
        }
        else {
            throwError('I was expecting a list of package names.');
        }
    }
- identifier: mapformats
  flag: >
    @{
        if (isList(parameters.mapformats)) {
          elements = [];
          foreach (element : parameters.mapformats) {
             if (isList(element) && element.size() == 2) {
                elements.add('--map-format');
                elements.add(element.get(0) + ':' + element.get(1));
             }
             else {
                throwError('I was expecting a map ' +
                           'format list [<key>, <value>] ');
             }
          }
          return elements;
        }
        else {
            throwError('I was expecting mapformats: [ [<key>, <value>], ' +
                       '..., [<key>, <value>] ].');
        }
    }
- identifier: options
  flag: >
    @{
        if (isList(parameters.options)) {
            return parameters.options;
        }
        else {
            throwError('I was expecting a list of options.');
        }
    }
- identifier: clean
  flag: >
    @{
       return isTrue(parameters.clean, 'true', '');
    }
