#!/usr/bin/perl -w

# translate english F*EX files into locale languange

use File::Basename;
use File::Copy;

@trfiles = qw(
  lib/fex.pp
  cgi-bin/foc
  cgi-bin/fop
  cgi-bin/fuc
  cgi-bin/fup
  cgi-bin/fur
  cgi-bin/rup
  cgi-bin/pup
  htdocs/FAQ/FAQ.html
);

#  htdocs/FAQ/meta.faq
#  htdocs/FAQ/user.faq
#  htdocs/FAQ/admin.faq
#  htdocs/FAQ/misc.faq

@cpfiles = qw(
  lang.html
  htdocs/index.html
  htdocs/FAQ
  htdocs/FAQ.html
  lib/fup.pl
);

@FEX = getpwnam('fex') or die "$0: no user fex\n";
$> = $FEX[2];
$) = $FEX[3];

$FEXHOME = $ENV{FEXHOME} || $FEX[7];

# require "$FEXHOME/lib/fex.pp" 
#   or die "$0: cannot load $FEXHOME/lib/fex.pp - $!\n";

$tr = "locale/translations";

$lang = shift or &usage;
exit if $lang eq 'english';

open $tr,$tr or die "$0: cannot open $tr - $!\n";

while (<$tr>) {
  $n++;
  last if /^\s*$/;
  if (/^#\s*([\w-]+):/ and $lang eq $1) {
    $ll = $n;
  }
}

&usage unless $ll;

while (<$tr>) {
  next if /^#/;
  next if /^\s*$/;
  chomp;
  unless (/[a-z]/i) {
    die "$0: syntax error in $tr line $. : no text\n";
  }
  $e = $_;
  for my $l (2 .. $ll) { 
    $_ = <$tr>||'';
    chomp;
    unless (/[a-z]/i) {
      die "$0: syntax error in $tr line $. : no text\n";
    }
  }
  $T{$e} = $_;
  while (<$tr>) { last if /^\s*$/ }
}
  
close $tr;
@E = sort {length $b <=> length $a} keys %T;

mkdir "$FEXHOME/locale";
$_ = "$FEXHOME/locale/english";
symlink '..',$_ unless -e;

foreach $file (@trfiles) {
  local $/;
  mkdirp("$FEXHOME/locale/$lang/".dirname($file));
  $fe = "$FEXHOME/$file";
  $ft = "$FEXHOME/locale/$lang/$file";
  open $fe,"<$fe" or die "$0: cannot read $fe - $!\n";
  unlink $ft; # beware symlink!
  open $ft,">$ft" or die "$0: cannot write $ft - $!\n";
  $_ = <$fe>;
  close $fe;
  foreach $e (@E) {
    s/\Q$e/$T{$e}/g;
  }
  print {$ft} $_;
  close $ft;
  chmod((stat $fe)[2],$ft);
  print "$ft written\n";
}

foreach $file (@cpfiles) {
  my $fs = "locale/$lang/$file";
  if (-e $fs) {
    my $fd = "$FEXHOME/$fs";
    mkdirp(dirname($fd));
    if (-f $fs) {
      $fd .= '_new' if -e $fd;
      if (copy($fs,$fd)) {
        chmod((stat $fs)[2],$fd);
        print "$fd written\n";
      }
    } else {
      if (-f $fd) {
        my $fds = $fd.'_save';
        my $fdn = $fd.'_new';
        system "rm -rf $fds $fdn";
        rename $fd,$fds;
        system "tar cf - $fs | (cd $FEXHOME; tar xf -)";
        rename $fd,$fdn;
        rename $fds,$fd;
        print "$fdn written\n";
      } else {
        system "tar cf - $fs | (cd $FEXHOME; tar xf -)";
        print "$fd written\n";
      }
    }
  }
}

foreach $fs (qw(fex.ph dop)) {
  $fd = "$FEXHOME/locale/$lang/lib/$fs";
  symlink "../../../lib/$fs",$fd and print "$fd linked\n";
}

unless (-f "$FEXHOME/locale/$lang/htdocs/FAQ/meta.faq") {
  unlink "$FEXHOME/locale/$lang/htdocs/FAQ/FAQ.html";
  rmdir  "$FEXHOME/locale/$lang/htdocs/FAQ";
}

make_lf($lang);
make_lf('english'); # unless -f "$FEXHOME/lib/lf.pl";

exit;


sub make_lf {
  my $lang = shift;
  my $fexpp = "$FEXHOME/locale/$lang/lib/fex.pp";
  my $lf = "$FEXHOME/locale/$lang/lib/lf.pl";

  open $fexpp,$fexpp or die "$0: no $fexpp - $!\n";
  unlink $lf;
  open $lf,'>',$lf or die "$0: cannot write $lf - $!\n";
  print {$lf} "### auto-generated by install/translate - DO NOT EDIT! ###\n\n";
  local $/;
  $_ = <$fexpp>;
  s/.*\n(\#\#\#* locale functions)/$1/s;
  s/\nsub (\w+)/\n\$$1\{$lang\} = sub/gs;
  s/\n\}\n/\n\};\n/gs;
  print {$lf} $_;
  close $lf;
  close $fexpp;
}

  
  
# emulate mkdir -p
sub mkdirp {
  my $dir = shift;
  my $pdir;
  
  return if -d $dir;
  $dir =~ s:/+$::;
  die "$0: cannot mkdir /\n" unless $dir;
  $pdir = $dir;
  if ($pdir =~ s:/[^/]+$::) {
    mkdirp($pdir) unless -d $pdir;
  }
  unless (-d $dir) {
    mkdir $dir,0770 or die "$0: mkdir $dir - $!\n";
  }
}


sub usage {
  my @langs;

  open $tr,$tr or die "$0: cannot open $tr - $!\n";

  while (<$tr>) {
    last if /^\s*$/;
    push @langs,$1 if /^#\s*([\w-]+):/;
  }
  
  foreach my $lang (@langs) {
    if (not -l "locale/$lang" and -f "locale/$lang/lib/fup.pl") {
      print "usage: $0 $lang\n";
    }
  }
  
  exit 1;
}
