Check version of KorAP-Tokenizer when requested

Change-Id: Iea411e514ba3a3eb8eb93a252c9652f38095a98d
diff --git a/Changes b/Changes
index 4c8a75b..79d0bbc 100644
--- a/Changes
+++ b/Changes
@@ -1,7 +1,9 @@
-2.3.4 2022-11-04
+2.3.4 2022-11-09
         - Improve stability of XML entity replacement.
+        - Check version for script and KorAP-Tokenizer
+          library when requested.
 
-2.3.3 2022-03-28
+2.3.3 2022-03-30
         - Load KorAP-Tokenizer only on request.
 
 2.3.2 2022-03-23
diff --git a/lib/KorAP/XML/TEI/Tokenizer/KorAP.pm b/lib/KorAP/XML/TEI/Tokenizer/KorAP.pm
index b3c09d7..dfa23ff 100644
--- a/lib/KorAP/XML/TEI/Tokenizer/KorAP.pm
+++ b/lib/KorAP/XML/TEI/Tokenizer/KorAP.pm
@@ -4,6 +4,8 @@
 use warnings;
 use File::Share ':all';
 
+our $VERSION = '2.3.4';
+
 use constant {
   WAIT_SECS => 30
 };
diff --git a/script/tei2korapxml b/script/tei2korapxml
index 63ea525..5c74b91 100755
--- a/script/tei2korapxml
+++ b/script/tei2korapxml
@@ -59,6 +59,7 @@
   'header-file=s'         => \(my $header_file = 'header'),
   'tokens-file=s'         => \(my $tokens_file = 'tokens'),
   'log|l=s'               => \(my $log_level   = 'notice'),
+  'required-version|rv=s' => \(my $required_version),
   ''                      => \(my $stdio),
   'help|h' => sub {
     pod2usage(
@@ -84,6 +85,15 @@
 $log->notice('Debugging is activated') if DEBUG;
 
 
+if ($required_version) {
+  $required_version =~ /^\s*(\d+\.\d+\.\d+)\s*$/;
+  if (!$1 || $1 ne $VERSION) {
+    $log->error("Required version $required_version mismatches version $VERSION");
+    exit(1);
+  };
+};
+
+
 # tag (without attributes), which contains the primary text
 my $_TEXT_BODY = 'text';
 # optional
@@ -111,6 +121,13 @@
     require KorAP::XML::TEI::Tokenizer::KorAP;
     1;
   };
+
+  my $korap_tok_ver = $KorAP::XML::TEI::Tokenizer::KorAP::VERSION;
+  if ($korap_tok_ver ne $VERSION) {
+    $log->error("KorAP-Tokenizer version ($korap_tok_ver) differs from the expected version ($VERSION)");
+    exit(1);
+  };
+
   $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
 };
 
@@ -514,6 +531,11 @@
 
 Print version information.
 
+=item B<--required-version|-rv>
+
+Version required for conversion. If the script doesn't match
+the version passed, it will exit immediately.
+
 =item B<--tokenizer-call|-tc>
 
 Call an external tokenizer process, that will tokenize
diff --git a/t/script.t b/t/script.t
index f311db0..51f5751 100644
--- a/t/script.t
+++ b/t/script.t
@@ -747,5 +747,31 @@
   ->stderr_like(qr!Debugging is activated!);
 };
 
+subtest 'Required version testing' => sub {
+  test_tei2korapxml(
+    tmp => 'script_out',
+    file => $file,
+    param => '-rv=2.2.2'
+  )->stderr_like(qr!^Required version 2\.2\.2 mismatches version!);
+
+  test_tei2korapxml(
+    tmp => 'script_out',
+    file => $file,
+    param => '--required-version=2.2'
+  )->stderr_like(qr!^Required version 2\.2 mismatches version!);
+
+  test_tei2korapxml(
+    tmp => 'script_out',
+    file => $file,
+    param => '-rv=' . $KorAP::XML::TEI::Tokenizer::KorAP::VERSION
+  )->stderr_like(qr!GOE_AGA\.00000!);
+
+  test_tei2korapxml(
+    tmp => 'script_out',
+    file => $file,
+    param => '-rv=   "  ' . $KorAP::XML::TEI::Tokenizer::KorAP::VERSION . '  "'
+  )->stderr_like(qr!GOE_AGA\.00000!);
+};
+
 
 done_testing;