1*cdf0e10cSrcweir#!/usr/bin/perl
2*cdf0e10cSrcweir#*************************************************************************
3*cdf0e10cSrcweir#
4*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5*cdf0e10cSrcweir#
6*cdf0e10cSrcweir# Copyright 2000, 2011 Oracle and/or its affiliates.
7*cdf0e10cSrcweir#
8*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
9*cdf0e10cSrcweir#
10*cdf0e10cSrcweir# This file is part of OpenOffice.org.
11*cdf0e10cSrcweir#
12*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
13*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
14*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
15*cdf0e10cSrcweir#
16*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
17*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
18*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
20*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
21*cdf0e10cSrcweir#
22*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
23*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
24*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
25*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
26*cdf0e10cSrcweir#
27*cdf0e10cSrcweir#*************************************************************************
28*cdf0e10cSrcweir
29*cdf0e10cSrcweirparse_args();
30*cdf0e10cSrcweirexecute_args();
31*cdf0e10cSrcweirexit(0);
32*cdf0e10cSrcweir
33*cdf0e10cSrcweirmy $source = undef;
34*cdf0e10cSrcweirmy $dest = undef;
35*cdf0e10cSrcweirmy @languages = undef;
36*cdf0e10cSrcweir
37*cdf0e10cSrcweirsub parse_args
38*cdf0e10cSrcweir{
39*cdf0e10cSrcweir    # at most two arguments
40*cdf0e10cSrcweir    explain(), exit(100) if ( $#ARGV > 1 );
41*cdf0e10cSrcweir
42*cdf0e10cSrcweir    # destination file is the second argument, if present
43*cdf0e10cSrcweir    $dest = $ARGV[1] if ( $#ARGV > 0 );
44*cdf0e10cSrcweir
45*cdf0e10cSrcweir    # source file is the first argument if present
46*cdf0e10cSrcweir    if ( $#ARGV > -1 )
47*cdf0e10cSrcweir    {
48*cdf0e10cSrcweir        $source = $ARGV[0];
49*cdf0e10cSrcweir        if ( ! -f $source )
50*cdf0e10cSrcweir        {
51*cdf0e10cSrcweir            print STDERR "$source is not a valid file, aborting";
52*cdf0e10cSrcweir            exit(101);
53*cdf0e10cSrcweir        }
54*cdf0e10cSrcweir    }
55*cdf0e10cSrcweir
56*cdf0e10cSrcweir    # check which languages to use
57*cdf0e10cSrcweir    my $languages = $ENV{WITH_LANG};
58*cdf0e10cSrcweir    if ( ( ! defined $languages ) || ( "$languages" eq "" ) )
59*cdf0e10cSrcweir    {
60*cdf0e10cSrcweir        print STDERR "$0: WITH_LANG not set or empty, defaulting to 'en-US'\n";
61*cdf0e10cSrcweir        $languages = "en-US";
62*cdf0e10cSrcweir    }
63*cdf0e10cSrcweir    @languages = split ( ' ', $languages );
64*cdf0e10cSrcweir}
65*cdf0e10cSrcweir
66*cdf0e10cSrcweirsub execute_args
67*cdf0e10cSrcweir{
68*cdf0e10cSrcweir    my @description = ();
69*cdf0e10cSrcweir    if ( defined $source )
70*cdf0e10cSrcweir    {
71*cdf0e10cSrcweir        open SOURCE, "$source" || die "could not open $source: $?\n";
72*cdf0e10cSrcweir        @description = <SOURCE>;
73*cdf0e10cSrcweir        close SOURCE;
74*cdf0e10cSrcweir    }
75*cdf0e10cSrcweir    else
76*cdf0e10cSrcweir    {
77*cdf0e10cSrcweir        @description = <STDIN>;
78*cdf0e10cSrcweir    }
79*cdf0e10cSrcweir
80*cdf0e10cSrcweir    if ( defined $dest )
81*cdf0e10cSrcweir    {
82*cdf0e10cSrcweir        open DEST, ">$dest" || die "could not open $dest for writing: $?\n";
83*cdf0e10cSrcweir    }
84*cdf0e10cSrcweir
85*cdf0e10cSrcweir    foreach (@description)
86*cdf0e10cSrcweir    {
87*cdf0e10cSrcweir        chomp; s/\r//;
88*cdf0e10cSrcweir
89*cdf0e10cSrcweir        if ( /\#LANG\#/ )
90*cdf0e10cSrcweir        {
91*cdf0e10cSrcweir            foreach $lang ( @languages )
92*cdf0e10cSrcweir            {
93*cdf0e10cSrcweir                my $transformed = $_;
94*cdf0e10cSrcweir                $transformed =~ s/\#LANG#/$lang/g;
95*cdf0e10cSrcweir                if ( defined $dest )
96*cdf0e10cSrcweir                {
97*cdf0e10cSrcweir                    print DEST "$transformed\n";
98*cdf0e10cSrcweir                }
99*cdf0e10cSrcweir                else
100*cdf0e10cSrcweir                {
101*cdf0e10cSrcweir                    print STDOUT "$transformed\n";
102*cdf0e10cSrcweir                }
103*cdf0e10cSrcweir            }
104*cdf0e10cSrcweir        }
105*cdf0e10cSrcweir        else
106*cdf0e10cSrcweir        {
107*cdf0e10cSrcweir            if ( defined $dest )
108*cdf0e10cSrcweir            {
109*cdf0e10cSrcweir                print DEST "$_\n";
110*cdf0e10cSrcweir            }
111*cdf0e10cSrcweir            else
112*cdf0e10cSrcweir            {
113*cdf0e10cSrcweir                print STDOUT "$_\n";
114*cdf0e10cSrcweir            }
115*cdf0e10cSrcweir        }
116*cdf0e10cSrcweir    }
117*cdf0e10cSrcweir
118*cdf0e10cSrcweir    close DEST if ( defined $dest );
119*cdf0e10cSrcweir}
120*cdf0e10cSrcweir
121*cdf0e10cSrcweir# explains the program's usage
122*cdf0e10cSrcweirsub explain
123*cdf0e10cSrcweir{
124*cdf0e10cSrcweir    print STDOUT "usage:\n";
125*cdf0e10cSrcweir    print STDOUT "  $0 [<description_file> [<output_file>]]\n";
126*cdf0e10cSrcweir    print STDOUT "  transforms the given extension description file\n";
127*cdf0e10cSrcweir    print STDOUT "\n";
128*cdf0e10cSrcweir    print STDOUT "  If <output_file> is not given, STDOUT is used.\n";
129*cdf0e10cSrcweir    print STDOUT "  If <description_file> is not given, STDIN is used.\n";
130*cdf0e10cSrcweir    print STDOUT "\n";
131*cdf0e10cSrcweir    print STDOUT "  The following transformations are done at the moment:\n";
132*cdf0e10cSrcweir    print STDOUT "  - duplicate all lines containing #LANG#, for ever token of \$WITH_LANG\n";
133*cdf0e10cSrcweir    print STDOUT "    replacing every occurance of \$LANG with a token\n";
134*cdf0e10cSrcweir    print STDOUT "\n";
135*cdf0e10cSrcweir    print STDOUT "  And yes, the functionality of this script should be\n";
136*cdf0e10cSrcweir    print STDOUT "  - moved to solenv/inc/tg_ext.mk\n";
137*cdf0e10cSrcweir    print STDOUT "  - implemented as XSLT, to be much less error-prone\n";
138*cdf0e10cSrcweir}
139