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