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