1cdf0e10cSrcweir#!/usr/bin/perl
27e90fac2SAndrew Rist#**************************************************************
37e90fac2SAndrew Rist#
47e90fac2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
57e90fac2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
67e90fac2SAndrew Rist#  distributed with this work for additional information
77e90fac2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
87e90fac2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
97e90fac2SAndrew Rist#  "License"); you may not use this file except in compliance
107e90fac2SAndrew Rist#  with the License.  You may obtain a copy of the License at
117e90fac2SAndrew Rist#
127e90fac2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
137e90fac2SAndrew Rist#
147e90fac2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
157e90fac2SAndrew Rist#  software distributed under the License is distributed on an
167e90fac2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
177e90fac2SAndrew Rist#  KIND, either express or implied.  See the License for the
187e90fac2SAndrew Rist#  specific language governing permissions and limitations
197e90fac2SAndrew Rist#  under the License.
207e90fac2SAndrew Rist#
217e90fac2SAndrew Rist#**************************************************************
227e90fac2SAndrew Rist
237e90fac2SAndrew 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";
129*86e1cf34SPedro Giffuni    print STDOUT "    replacing every occurrence 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