Adding new systempreference allowing for the library to add borrowers to institutions...
[koha_fer] / misc / translator / tmpl_process3.pl
index b8436b9..5a26be2 100755 (executable)
@@ -20,7 +20,7 @@ use VerboseWarnings qw( :warn :die );
 
 ###############################################################################
 
-use vars qw( @in_files $in_dir $str_file $out_dir );
+use vars qw( @in_files $in_dir $str_file $out_dir $quiet );
 use vars qw( @excludes $exclude_regex );
 use vars qw( $recursive_p );
 use vars qw( $pedantic_p );
@@ -97,14 +97,24 @@ sub text_replace (**) {
                    text_replace_tag($t, $attr): $t });
        } elsif ($kind eq TmplTokenType::TAG && %$attr) {
            print $output text_replace_tag($t, $attr);
+       } elsif ($s->has_js_data) {
+           for my $t (@{$s->js_data}) {
+               # FIXME for this whole block
+               if ($t->[0]) {
+                   printf $output "%s%s%s", $t->[2], find_translation $t->[3],
+                           $t->[2];
+               } else {
+                   print $output $t->[1];
+               }
+           }
        } elsif (defined $t) {
            print $output $t;
        }
     }
 }
 
-sub listfiles ($$) {
-    my($dir, $type) = @_;
+sub listfiles ($$$) {
+    my($dir, $type, $action) = @_;
     my @it = ();
     if (opendir(DIR, $dir)) {
        my @dirent = readdir DIR;       # because DIR is shared when recursing
@@ -115,9 +125,9 @@ sub listfiles ($$) {
            || (defined $exclude_regex && $dirent =~ /^(?:$exclude_regex)$/)) {
                ;
            } elsif (-f $path) {
-               push @it, $path if !defined $type || $dirent =~ /\.(?:$type)$/;
+               push @it, $path if (!defined $type || $dirent =~ /\.(?:$type)$/) || $action eq 'install';
            } elsif (-d $path && $recursive_p) {
-               push @it, listfiles($path, $type);
+               push @it, listfiles($path, $type, $action);
            }
        }
     } else {
@@ -128,6 +138,21 @@ sub listfiles ($$) {
 
 ###############################################################################
 
+sub mkdir_recursive ($) {
+    my($dir) = @_;
+    local($`, $&, $', $1);
+    $dir = $` if $dir ne /^\/+$/ && $dir =~ /\/+$/;
+    my ($prefix, $basename) = ($dir =~ /\/([^\/]+)$/s)? ($`, $1): ('.', $dir);
+    mkdir_recursive($prefix) if $prefix ne '.' && !-d $prefix;
+    if (!-d $dir) {
+       print STDERR "Making directory $dir..." unless $quiet;
+       # creates with rwxrwxr-x permissions
+       mkdir($dir, 0775) || warn_normal "$dir: $!", undef;
+    }
+}
+
+###############################################################################
+
 sub usage ($) {
     my($exitcode) = @_;
     my $h = $exitcode? *STDERR: *STDOUT;
@@ -148,12 +173,13 @@ Create or update PO files from templates, or install translated templates.
                               for input (install) or output (create, update)
   -x, --exclude=REGEXP        Exclude files matching the given REGEXP
       --help                  Display this help and exit
+  -q, --quiet                 no output to screen (except for errors)
 
 The -o option is ignored for the "create" and "update" actions.
-Try `perldoc $0' for perhaps more information.
+Try `perldoc $0 for perhaps more information.
 EOF
     exit($exitcode);
-}
+}#`
 
 ###############################################################################
 
@@ -161,7 +187,7 @@ sub usage_error (;$) {
     for my $msg (split(/\n/, $_[0])) {
        print STDERR "$msg\n";
     }
-    print STDERR "Try `$0 --help' for more information.\n";
+    print STDERR "Try `$0 --help for more information.\n";
     exit(-1);
 }
 
@@ -173,6 +199,7 @@ GetOptions(
     'recursive|r'                      => \$recursive_p,
     'str-file|s=s'                     => \$str_file,
     'exclude|x=s'                      => \@excludes,
+       'quiet|q'                               => \$quiet,
     'pedantic-warnings|pedantic'       => sub { $pedantic_p = 1 },
     'help'                             => \&usage,
 ) || usage_error;
@@ -211,7 +238,7 @@ if (-d $in_files[0]) {
     # input is a directory, generates list of files to process
     $in_dir = $in_files[0];
     $in_dir =~ s/\/$//; # strips the trailing / if any
-    @in_files = listfiles($in_dir, $type);
+    @in_files = listfiles($in_dir, $type, $action);
 } else {
     for my $input (@in_files) {
        die "You cannot specify input files and directories at the same time.\n"
@@ -252,16 +279,41 @@ if ($action eq 'create')  {
        unlink $str_file || die "$str_file: $!\n";
     }
     die "$str_file: Output file already exists\n" if -f $str_file;
-    my($tmph, $tmpfile) = tmpnam();
+    my($tmph1, $tmpfile1) = tmpnam();
+    my($tmph2, $tmpfile2) = tmpnam();
+    close $tmph2; # We just want a name
     # Generate the temporary file that acts as <MODULE>/POTFILES.in
     for my $input (@in_files) {
-       print $tmph "$input\n";
+       print $tmph1 "$input\n";
     }
-    close $tmph;
+    close $tmph1;
     # Generate the specified po file ($str_file)
-    $st = system ($xgettext, '-s', '-f', $tmpfile, '-o', $str_file);
-    warn_normal "Text extraction failed: $xgettext: $!\n", undef if $st != 0;
-#   unlink $tmpfile || warn_normal "$tmpfile: unlink failed: $!\n", undef;
+    $st = system ($xgettext, '-s', '-f', $tmpfile1, '-o', $tmpfile2);
+    # Run msgmerge so that the pot file looks like a real pot file
+    # We need to help msgmerge a bit by pre-creating a dummy po file that has
+    # the headers and the "" msgid & msgstr. It will fill in the rest.
+    if ($st == 0) {
+       # Merge the temporary "pot file" with the specified po file ($str_file)
+       # FIXME: msgmerge(1) is a Unix dependency
+       # FIXME: need to check the return value
+       unless (-f $str_file) {
+           local(*INPUT, *OUTPUT);
+           open(INPUT, "<$tmpfile2");
+           open(OUTPUT, ">$str_file");
+           while (<INPUT>) {
+               print OUTPUT;
+           last if /^\n/s;
+           }
+           close INPUT;
+           close OUTPUT;
+       }
+       $st = system('msgmerge', '-U', '-s', $str_file, $tmpfile2);
+    } else {
+       error_normal "Text extraction failed: $xgettext: $!\n", undef;
+       error_additional "Will not run msgmerge\n", undef;
+    }
+#   unlink $tmpfile1 || warn_normal "$tmpfile1: unlink failed: $!\n", undef;
+#   unlink $tmpfile2 || warn_normal "$tmpfile2: unlink failed: $!\n", undef;
 
 } elsif ($action eq 'update') {
     my($tmph1, $tmpfile1) = tmpnam();
@@ -309,34 +361,39 @@ if ($action eq 'create')  {
 
     # creates the new tmpl file using the new translation
     for my $input (@in_files) {
-       die "Assertion failed"
-               unless substr($input, 0, length($in_dir) + 1) eq "$in_dir/";
-
-       my $h = TmplTokenizer->new( $input );
-       $h->set_allow_cformat( 1 );
-       VerboseWarnings::set_input_file_name $input;
-
-       my $target = $out_dir . substr($input, length($in_dir));
-       my $targetdir = $` if $target =~ /[^\/]+$/s;
-       if (!-d $targetdir) {
-           print STDERR "Making directory $targetdir...";
-           # creates with rwxrwxr-x permissions
-           mkdir($targetdir, 0775) || warn_normal "$targetdir: $!", undef;
+               die "Assertion failed"
+                       unless substr($input, 0, length($in_dir) + 1) eq "$in_dir/";
+#              print "$input / $type\n";
+               if (!defined $type || $input =~ /\.(?:$type)$/) {
+                       my $h = TmplTokenizer->new( $input );
+                       $h->set_allow_cformat( 1 );
+                       VerboseWarnings::set_input_file_name $input;
+               
+                       my $target = $out_dir . substr($input, length($in_dir));
+                       my $targetdir = $` if $target =~ /[^\/]+$/s;
+                       mkdir_recursive($targetdir) unless -d $targetdir;
+                       print STDERR "Creating $target...\n" unless $quiet;
+                       open( OUTPUT, ">$target" ) || die "$target: $!\n";
+                       text_replace( $h, *OUTPUT );
+                       close OUTPUT;
+               } else {
+               # just copying the file
+                       my $target = $out_dir . substr($input, length($in_dir));
+                       my $targetdir = $` if $target =~ /[^\/]+$/s;
+                       mkdir_recursive($targetdir) unless -d $targetdir;
+                       system("cp -f $input $target");
+                       print STDERR "Copying $input...\n" unless $quiet;
+               }
        }
-       print STDERR "Creating $target...\n";
-       open( OUTPUT, ">$target" ) || die "$target: $!\n";
-       text_replace( $h, *OUTPUT );
-       close OUTPUT;
-    }
 
 } else {
     usage_error('Unknown action specified.');
 }
 
 if ($st == 0) {
-    printf "The %s seems to be successful.\n", $action;
+    printf "The %s seems to be successful.\n", $action unless $quiet;
 } else {
-    printf "%s FAILED.\n", "\u$action";
+    printf "%s FAILED.\n", "\u$action" unless $quiet;
 }
 exit 0;