1#!/usr/bin/perl
2#**************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21#**************************************************************
22
23
24
25parse_args();
26execute_args();
27exit(0);
28
29my $source = undef;
30my $dest = undef;
31my @languages = undef;
32
33sub parse_args
34{
35    # at most two arguments
36    explain(), exit(100) if ( $#ARGV > 1 );
37
38    # destination file is the second argument, if present
39    $dest = $ARGV[1] if ( $#ARGV > 0 );
40
41    # source file is the first argument if present
42    if ( $#ARGV > -1 )
43    {
44        $source = $ARGV[0];
45        if ( ! -f $source )
46        {
47            print STDERR "$source is not a valid file, aborting";
48            exit(101);
49        }
50    }
51
52    # check which languages to use
53    my $languages = $ENV{WITH_LANG};
54    if ( ( ! defined $languages ) || ( "$languages" eq "" ) )
55    {
56        print STDERR "$0: WITH_LANG not set or empty, defaulting to 'en-US'\n";
57        $languages = "en-US";
58    }
59    @languages = split ( ' ', $languages );
60}
61
62sub execute_args
63{
64    my @description = ();
65    if ( defined $source )
66    {
67        open SOURCE, "$source" || die "could not open $source: $?\n";
68        @description = <SOURCE>;
69        close SOURCE;
70    }
71    else
72    {
73        @description = <STDIN>;
74    }
75
76    if ( defined $dest )
77    {
78        open DEST, ">$dest" || die "could not open $dest for writing: $?\n";
79    }
80
81    foreach (@description)
82    {
83        chomp; s/\r//;
84
85        if ( /\#LANG\#/ )
86        {
87            foreach $lang ( @languages )
88            {
89                my $transformed = $_;
90                $transformed =~ s/\#LANG#/$lang/g;
91                if ( defined $dest )
92                {
93                    print DEST "$transformed\n";
94                }
95                else
96                {
97                    print STDOUT "$transformed\n";
98                }
99            }
100        }
101        else
102        {
103            if ( defined $dest )
104            {
105                print DEST "$_\n";
106            }
107            else
108            {
109                print STDOUT "$_\n";
110            }
111        }
112    }
113
114    close DEST if ( defined $dest );
115}
116
117# explains the program's usage
118sub explain
119{
120    print STDOUT "usage:\n";
121    print STDOUT "  $0 [<description_file> [<output_file>]]\n";
122    print STDOUT "  transforms the given extension description file\n";
123    print STDOUT "\n";
124    print STDOUT "  If <output_file> is not given, STDOUT is used.\n";
125    print STDOUT "  If <description_file> is not given, STDIN is used.\n";
126    print STDOUT "\n";
127    print STDOUT "  The following transformations are done at the moment:\n";
128    print STDOUT "  - duplicate all lines containing #LANG#, for ever token of \$WITH_LANG\n";
129    print STDOUT "    replacing every occurance of \$LANG with a token\n";
130    print STDOUT "\n";
131    print STDOUT "  And yes, the functionality of this script should be\n";
132    print STDOUT "  - moved to solenv/inc/tg_ext.mk\n";
133    print STDOUT "  - implemented as XSLT, to be much less error-prone\n";
134}
135