#!/usr/bin/perl -w

use Data::Dumper;
use File::Spec;
use IO::File;
#use diagnostics;

my $cvs_info = '$Id: language.PL,v 1.1 2000/04/02 20:52:56 bruce Exp $ ';
my $cvs_version = (split(' ', $cvs_info))[2] || "pre_release";
my $thisdir = &identify_self;

print STDOUT
  "Xtal.pm language generation tool version $cvs_version for Xray::Xtal 0.30",
  $/;

##  the anon array contains the file extension and the index of the
##  language in the database files
my %lingos = ("english"	 => ["en", 1],
	      "spanish"	 => ["sp", 2],
	      "french"	 => ["fr", 3],
	      "german"	 => ["ge", 4],
	     );
my $sep = '\|'; # field separator in database files
#gaby# my $sep = ';'; # field separator in database files
my $nl = "\013"; #'';         # newline token (control-K, ascii 13)

my $header;			# read the tkatomsrc header from __DATA__
$header  = '# -*- mode: cperl -*-';
$header .= "$/# Xtal.pm language file generation tool, version $cvs_version$/";
while (<DATA>) {
  $header .= $_;
};

my %fh = ();
## open a file for each language and write out the header
foreach my $lang (keys %lingos) {
  my $ext = $lingos{$lang}->[0];
  my $file = "xtalrc." . $ext;
  $file = File::Spec -> catfile($thisdir, $file);
  $fh{$ext} = IO::File -> new();
  my $fh = $fh{$ext};
  open $fh, ">$file" or die $!;
  print $fh $header;
};
print STDOUT "  Reading language data for Xtal", $/;
my $file = "xtal.dat";
#gaby# my $file = "table.xtal";
$file = File::Spec -> catfile($thisdir, $file);
open DAT, "$file" or die $!;
while (<DAT>) {
  next if /^\s*$/;
  my @line = split($sep, $_);
  #gaby# shift @line;
  foreach my $i (0 .. $#line) { # clean up white space at ends
    $line[$i] =~ s/^\s+//;	  # of words and convert the ^K
    $line[$i] =~ s|$nl|$/|g;	  # characters to $/
    $line[$i] =~ s/\s+$//;
    $line[$i] =~ s/\"/\"/;
    #gaby# $line[$i] =~ s/\n$//;
  };
  foreach my $lang (keys %lingos) {
    my $index = $lingos{$lang}->[1];
    eval "\$$lang\{\'$line[0]\'\} = \"$line[$index]\";";
  };
};
close DAT;

%written = ();
foreach my $lang (keys %lingos) {
  my $ext = $lingos{$lang}->[0];
  unless ($written{$lang}) {
    ++$written{$lang};
    printf STDOUT "    Writing %-7s file : %15s$/", $lang, "xtalrc.$ext";
  };
  my $ref;
  eval "\$ref = \\\%$lang;";
  my $hash_dump = Data::Dumper->Dump([$ref], [qw/Xray::Xtal::strings/]);
  eval "undef \%$lang";
  undef $ref;
  my $fh = $fh{$ext};
  print $fh $hash_dump, $/;
};

foreach my $lang (keys %lingos) {
  my $ext = $lingos{$lang}->[0];
  my $fh = $fh{$ext};
  print $fh $/, "1;", $/;
  close $fh;
};



sub identify_self {
  my @caller = caller;
  use File::Basename qw(dirname);
  return dirname($caller[1]);
};


__DATA__

######################################################################
## Xtal.pm language configuration file
##                                     copyright (c) 2000 Bruce Ravel
##                                          ravel@phys.washington.edu
##                            http://feff.phys.washington.edu/~ravel/
##
##	  The latest version of Atoms can always be found at
##	    http://feff.phys.washington.edu/~ravel/software/atoms/
##
## -------------------------------------------------------------------
##     All rights reserved. This program is free software; you can
##     redistribute it and/or modify it under the same terms as Perl
##     itself.
##
##     This program is distributed in the hope that it will be useful,
##     but WITHOUT ANY WARRANTY; without even the implied warranty of
##     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##     Artistic License for more details.
## -------------------------------------------------------------------
######################################################################
##
## This is a language configuration file for Xtal.pm.  This file is
## read after atoms starts running.  The contents of this file are
## used is messages generated by Xtal.pm.
##
## To make another language file, copy this file to "xtalrc.??"  where
## ?? is a two letter symbol for your language.  Translate all of the
## hash values (but NOT the keys) and add an entry to the %languages
## hash in the language file for the new language.  And please send
## your translation to Bruce Ravel <ravel@phys.washingon.edu> so he
## can include it with future distributions of Atoms.
##
## Some languages:
##   en = English
##   fr = French
##   sp = Spanish
## and so on...
##
## Here is an example.  Pardon my crappy language skills.  Note that
## the key doesn't get translated, only the value -- that is very
## important!
##
## English:
## 'not_a_group' =>
##  'You have specified an unknown space group symbol.',
##
## Spanish:
## 'not_a_group' =>
##  'Ud ha specificado un grupo de simetria inconecido.'
##
## French:
## 'not_a_group' =>
##  'Vous avez specifie une groupe de symmetrie inconnu.'
####################################################################
## code:
