#!/usr/bin/perl
# Time-stamp: <2007-08-12 11:57:25 phm>
BEGIN { $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin' };
use strict;
use POSIX qw(strftime);
use English -no_match_vars;
umask oct('002');
use utf8;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
use Encode qw(_utf8_on);
use IO::File;
use Data::Dumper;

sub usage {
print STDERR <<_EOHELP_
cjkstrs_xput
 1. Japanische Zeichenketten aus Datei in Datenbank zurückimportieren
_EOHELP_
}

use A2E::Opts;
my $opts = new A2E::Opts 'vdht:';

my $VERBOSE = $opts->{v} || $ENV{VERBOSE};
my $DEBUG = $opts->{d} || $ENV{DEBUG};
my $DBNOM = $opts->{D} || 'a2e';
my $TABLE = $opts->{t} || 'jaen';
my $HELPU = $opts->{h};

use A2E::db;
my $localhost = !@ARGV;
our $db = new A2E::db $DBNOM, { debug=>$DEBUG, verbose=>$VERBOSE, localhost=>$localhost };

my ($n_xput, $dok, $id, $lin, %tok) = ();
my %space = ( ja => '', en => ' ' );
sub xput {
    return unless $tok{ja};
    return unless $tok{en};
    $n_xput++;
    $db->put($TABLE, { ja=>$tok{ja} }, en=>$tok{en}, dok=>$tok{dok}, dok=>$tok{id}, line=>$tok{lin});
    %tok = ();
};

FILE: while (my $fi = shift) {
    my $fi_h = new IO::File $fi, 'r';
    $fi_h->binmode(':utf8');
    my $lng = '';
  LINE: while (<$fi_h>) {
      if (m(\A\s*\Z)) { xput;next LINE };
      if (m(\A\#\s\S)) { xput;next LINE };
      if (m(\A\s*\-\-\-\s*)) { xput;next LINE };
      if (m(\Adok\:\s+(\w+)\s*\Z)) { $tok{dok} = $1;next LINE };
      if (m(\Aid\:\s+(\w+)\s*\Z)) { $tok{id} = $1;next LINE };
      if (m(\Aja\:\s+(.*)\s*\Z)) {
	  xput;
	  $tok{ja} = $1;
	  $tok{en} = '';
	  $lng = 'ja';
	  next;
      };
      if (m(\Aen\:\s+(.*)\s*\Z)) {
	  $tok{en} = $1;
	  $lng = 'en';
	  next;
      };
      if (m(\A\s*(\S.*\S)\s*\Z)) { # add a line
	  $tok{$lng} .= $space{$lng} . $1;
      };
  };
};
xput;

$db->vecho("$n_xput records entered");

# Local Variables:
# coding: utf-8
# url: https://a2e.de/adv/prg/bin/cjkstrs_xput.pl
# srcfile:/adv/prg/bin/cjkstrs_xget.pl
# mode: perl
# End:


