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