#!/usr/bin/perl -w

package pere_confupd;

use vars qw($VERSION);
$VERSION = "0.2.0";

use warnings 'all';
use locale;
use strict;
use POSIX;
use Lingua::Pere::Config;

my %HF = (
	conf => 1,
	seqb => 1,
	seqf => 1,
	tran => 1,
	wdsb => 1,
	wdsf => 1,
);

#**************************************************************************
sub dircon {
	my ($dir) = @_;

	opendir(DIR, $dir) or die("Can not open directory '$dir': $!");
	my @con = sort grep { $_ !~ /^[.]{1,2}$/ } readdir DIR;
	closedir(DIR);

	return @con;	
}

#**************************************************************************
sub chekfiles {
	my ($dir) = @_;
	
	my @fla = dircon($dir);
	my $cnt = keys %HF;
	foreach my $fil (@fla) {
		if (exists($HF{$fil}) && (-f "$dir/$fil")) {
			--$cnt;
		}
	}

	return $cnt;
}

#**************************************************************************
sub findpairs {
	my ($rut, $res) = @_;
	$res ||= {};

	my @ila = dircon($rut);
	foreach my $iln (@ila) {
		my $ild = "$rut/$iln";
		if (($iln =~ m/^[a-z]{2}-[a-z]{2}$/) && (-d $ild)) {
			my @ola = dircon($ild);
			foreach my $oln (@ola) {
				my $old = "$ild/$oln";
				if (($oln =~ m/^[a-z]{2}-[a-z]{2}$/) && (-d $old)) {
					my @tha = dircon($old);
					foreach my $thm (@tha) {
						my $thd = "$old/$thm";
						if (($thm =~ m/^[a-z][-a-z]*$/) && (-d $thd)) {
							my $cnt = chekfiles($thd);
							if ($cnt == 0) {
								$res->{$iln}->{$oln}->{$thm}= $thd;
							}
						}
					}
				}
			}
		} elsif (($iln =~ m/^([a-z]{2}-[a-z]{2})_([a-z]{2}-[a-z]{2})_([a-z][-a-z]*)$/) && (-d $ild)) {
			my $iln = $1;
			my $oln = $2;
			my $thm = $3;
			my $cnt = chekfiles($ild);
			if ($cnt == 0) {
				$res->{$iln}->{$oln}->{$thm}= $ild;
			}
		}
	}

	return $res;
}

#**************************************************************************
sub doit {
	$| = 1;

	my $cfg = Lingua::Pere::Config->new('pere-trans.config');

	$cfg->enc;
	$cfg->mem;
	my $rut = $cfg->rut;
	my $opt = $cfg->opt;
	my $fil = (exists($opt->{cfg})) ? $opt->{cfg} : $cfg->configpath;
	delete($opt->{cfg});
	delete($opt->{rut});
	$opt->{pairs} = findpairs($rut);

	$cfg->save($fil);
}

#**************************************************************************
&doit;
exit(0);
#**************************************************************************
__END__

=head1 NAME

pere-confupd - create or update configuration file

=head1 SYNOPSIS

pere-confupd [options ...]

	Options:
		-help     brief help message
		-man      full documentation
		-rut      translator databases location (required)
		-cfg      configuration file
		-enc      charset encoding
		-cas      case sensitive
		-mem      cache mem size

=head1 OPTIONS

=over 2

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=item B<-rut>

Directory containing translator databases

=item B<-cfg>

Enables to specify other file of a configuration

=item B<-cas>

Use case sensitive mode

=item B<-lin>

Means paragraph as single line

=item B<-enc>

Optional charset encoding for input document.
By default used C<UTF-8>

=item B<-mem>

Set the size of memory cache in megabytes, otherwise are used system defaults.

=back

=head1 DESCRIPTION

B<This script> used for create or update configuration file.

Default name for configuration file is C<pere-trans.config>.

It can be located at:

=over 2

=item 'B</etc>' - System configuration

=item 'B<~>'    - User's home

=item 'B<.>'    - Current working directory

=back

=cut
