Add KrillIndexer plugin

Change-Id: Iadce86ca87637c6fbd970f1fd7672ff946f31a98
diff --git a/Changes b/Changes
index 8970c17..8e24d05 100755
--- a/Changes
+++ b/Changes
@@ -4,7 +4,7 @@
         - Remove deprecated 'auth_support' (since 0.31)
           configuration parameter - use 'Auth' plugin instead. (diewald)
         - Support environment variable KALAMAR_CLIENT_FILE for
-          client information.
+          client information. (diewald)
         - Support KALAMAR_PLUGINS as a comma-separated environment
           variable to load plugins in addition to the 'plugins'
           parameter in the configuration file. (diewald)
@@ -13,6 +13,7 @@
           (diewald)
         - Add panel types. (diewald)
         - Add textSigle as a plugin receivable information. (diewald)
+        - Introduce krill-indexer command via plugin. (diewald)
 
 0.47 2022-11-22
         - Add command to generate super_client_info file. (diewald)
diff --git a/lib/Kalamar/Plugin/KrillIndexer.pm b/lib/Kalamar/Plugin/KrillIndexer.pm
new file mode 100644
index 0000000..adc4585
--- /dev/null
+++ b/lib/Kalamar/Plugin/KrillIndexer.pm
@@ -0,0 +1,20 @@
+package Kalamar::Plugin::KrillIndexer;
+use Mojo::Base 'Mojolicious::Plugin';
+use Mojo::Base -strict;
+sub register {
+  my ($plugin, $mojo) = @_;
+
+  my $java = `sh -c 'command -v java'`;
+  chomp $java;
+
+  if ($java eq '') {
+    warn('No java executable found in PATH. ' . __PACKAGE__ . ' requires a JVM.');
+    return 0;
+  };
+
+  # Add additional command path
+  push(@{$mojo->commands->namespaces}, __PACKAGE__);
+  return 1;
+};
+
+1;
diff --git a/lib/Kalamar/Plugin/KrillIndexer/krill_indexer.pm b/lib/Kalamar/Plugin/KrillIndexer/krill_indexer.pm
new file mode 100644
index 0000000..175cbb0
--- /dev/null
+++ b/lib/Kalamar/Plugin/KrillIndexer/krill_indexer.pm
@@ -0,0 +1,26 @@
+package Kalamar::Plugin::KrillIndexer::krill_indexer;
+use Mojo::Base 'Mojolicious::Command';
+
+has description => 'Index KorAP data using Krill';
+has usage       => sub {
+  my $indexer = $ENV{KRILL_INDEXER_PATH};
+  return "\n" . qx{java -jar $indexer} if $indexer;
+  return "\nThe jar file for the indexer can be set using\n" .
+    "the environment variable KRILL_INDEXER_PATH\n";
+};
+
+sub run {
+  shift;
+  my $indexer = $ENV{KRILL_INDEXER_PATH};
+  unless ($indexer) {
+    warn
+      'KRILL_INDEXER_PATH not set';
+    return 0;
+  };
+
+  print join(',', 'java', 'jar', $indexer, @_);
+  system('java', '-jar', $indexer, @_);
+  return 1;
+};
+
+1;
diff --git a/t/plugin/krill-indexer.t b/t/plugin/krill-indexer.t
new file mode 100644
index 0000000..4098e70
--- /dev/null
+++ b/t/plugin/krill-indexer.t
@@ -0,0 +1,39 @@
+use Test::More;
+use Test::Mojo;
+use Test::Output;
+
+my $java = `sh -c 'command -v java'`;
+chomp $java;
+
+if ($java eq '') {
+  plan skip_all => "Java is not installed";
+  return;
+};
+
+unless ($ENV{KRILL_INDEXER_PATH}) {
+  plan skip_all => "Krill-Indexer is not installed";
+  return;
+};
+
+my $t = Test::Mojo->new(Kalamar => {
+  Kalamar => {
+    plugins => ['KrillIndexer']
+  }
+});
+
+my $app = $t->app;
+
+my $cmds = $app->commands;
+
+ok(grep/::KrillIndexer/, @{$cmds->namespaces}, 'Namespace is set');
+
+stdout_like(
+  sub {
+    $cmds->run('krill-indexer');
+  },
+  qr{--inputDir}
+);
+
+done_testing;
+
+1;