xref: /aoo4110/main/solenv/bin/licinserter.pl (revision b1cdbd2c)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#**************************************************************
5#
6#  Licensed to the Apache Software Foundation (ASF) under one
7#  or more contributor license agreements.  See the NOTICE file
8#  distributed with this work for additional information
9#  regarding copyright ownership.  The ASF licenses this file
10#  to you under the Apache License, Version 2.0 (the
11#  "License"); you may not use this file except in compliance
12#  with the License.  You may obtain a copy of the License at
13#
14#    http://www.apache.org/licenses/LICENSE-2.0
15#
16#  Unless required by applicable law or agreed to in writing,
17#  software distributed under the License is distributed on an
18#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19#  KIND, either express or implied.  See the License for the
20#  specific language governing permissions and limitations
21#  under the License.
22#
23#**************************************************************
24
25
26
27#
28# licinserter.pl - create license entries in extension description.xml
29#
30
31use File::Basename;
32
33my $langswitch;
34
35sub usage()
36{
37    print STDERR "\nCreate extension descriptions with license-text entries\n";
38    print STDERR "matching the language activated.\n";
39    print STDERR "\nUsage:\n";
40    print STDERR "\t$0 [--langsplit] infile \"naming pattern\" destination\n\n";
41    print STDERR "\nExample:\n\n";
42    print STDERR "$0 description.xml dir/license_xxx.txt outdir/description.xml\n\n";
43    print STDERR "Creates \"someoutdir/description.xml\" with the license file entries like\n\"dir/license_en.US.txt\" ";
44    print STDERR "for all languages found in the WITH_LANG environment\nvariable\n\n\n";
45    print STDERR "Example2:\n\n";
46    print STDERR "$0 --langsplit description.xml dir/license_xxx.txt someoutdir\n\n";
47    print STDERR "Creates \"someoutdir/<language>/description.xml\" with one license file entry\n\"somedir/license_<language>.txt\" ";
48    print STDERR "for all languages found in the WITH_LANG\nenvironment variable.\n\nNOTE: when using --langsplit \"destination\" needs to be a directory\n";
49}
50
51if ( $ARGV[0] =~ /^-/ ) {
52    $langswitch = shift @ARGV;
53    if ( $langswitch ne "--langsplit" ) {
54        usage();
55        exit 1;
56    }
57    if ( ! -d $ARGV[2] ) {
58        print STDERR "\nERROR - $ARGV[2] is not directory\n";
59        usage();
60        exit 2;
61    }
62}
63
64if ( $#ARGV != 2 ) {
65    print "zzz\n";
66    usage();
67    exit 1;
68}
69
70open INFILE,$ARGV[0] or die "oops - no such file $ARGV[0]!\n";
71
72my @inlines = <INFILE>;
73close INFILE;
74
75chomp @inlines;
76
77# Empty or unset WITH_LANG environment variable is set to default en-US.
78# When WITH_LANG is set but does not contain en-US then that is prepended.
79my $WithLang = $ENV{WITH_LANG};
80if ( ! defined $WithLang || $WithLang eq "")
81{
82    $WithLang = "en-US";
83}
84elsif ($WithLang !~ /\ben-US\b/)
85{
86    $WithLang = "en-US " . $WithLang;
87}
88
89
90if ( $langswitch eq "" ) {
91    my @outlines;
92	foreach my $i (@inlines) {
93	    if ( $i =~ /license-text/ ) {
94	        my $ii;
95	        my $name;
96	        foreach my $code ( split(/\s+/,$WithLang) ) {
97	            $ii = $i;
98	            $name = $ARGV[1];
99	            $name =~ s/xxx/$code/;
100	            $ii =~ s/isocode/$code/g;
101	            $ii =~ s?licensefile?$name?g;
102                push @outlines, "$ii\n";
103	        }
104	    } else {
105            push @outlines, "$i\n";
106	    }
107	}
108    open OUTFILE, ">$ARGV[2]" or die "ooops - can't open $ARGV[2] for writing\n";
109    print OUTFILE @outlines;
110    close OUTFILE or die "ooops - can't write to $ARGV[2]\n";
111} else {
112    my @outlines;
113    my $outname = basename($ARGV[0],());
114    foreach my $code ( split(/\s+/,$ENV{WITH_LANG}) ) {
115        @outlines=();
116		foreach my $i (@inlines) {
117		    if ( $i =~ /license-text/ ) {
118		        my $name;
119                my $ii = $i;
120		        $name = $ARGV[1];
121		        $name =~ s/xxx/$code/;
122		        $ii =~ s/isocode/$code/g;
123		        $ii =~ s?licensefile?$name?g;
124	            push @outlines, "$ii\n";
125		    } else {
126	            push @outlines, "$i\n";
127		    }
128		}
129        mkdir "$ARGV[2]/$code";
130        open OUTFILE, ">$ARGV[2]/$code/$outname" or die "ooops - can't open $ARGV[2]/$code/$outname for writing\n";
131        print OUTFILE @outlines;
132        close OUTFILE or die "ooops - can't write to $ARGV[2]/$code/$outname\n";
133    }
134}
135