xref: /trunk/main/solenv/bin/licinserter.pl (revision cdf0e10c)
1*cdf0e10cSrcweir:
2*cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}'
3*cdf0e10cSrcweir    if 0;
4*cdf0e10cSrcweir#*************************************************************************
5*cdf0e10cSrcweir#
6*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7*cdf0e10cSrcweir#
8*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
9*cdf0e10cSrcweir#
10*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
11*cdf0e10cSrcweir#
12*cdf0e10cSrcweir# This file is part of OpenOffice.org.
13*cdf0e10cSrcweir#
14*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir#
18*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir#
24*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir#
29*cdf0e10cSrcweir#*************************************************************************
30*cdf0e10cSrcweir
31*cdf0e10cSrcweir#
32*cdf0e10cSrcweir# licinserter.pl - create license entries in extension description.xml
33*cdf0e10cSrcweir#
34*cdf0e10cSrcweir
35*cdf0e10cSrcweiruse File::Basename;
36*cdf0e10cSrcweir
37*cdf0e10cSrcweirmy $langswitch;
38*cdf0e10cSrcweir
39*cdf0e10cSrcweirsub usage()
40*cdf0e10cSrcweir{
41*cdf0e10cSrcweir    print STDERR "\nCreate extension descriptions with license-text entries\n";
42*cdf0e10cSrcweir    print STDERR "matching the language activated.\n";
43*cdf0e10cSrcweir    print STDERR "\nUsage:\n";
44*cdf0e10cSrcweir    print STDERR "\t$0 [--langsplit] infile \"naming pattern\" destination\n\n";
45*cdf0e10cSrcweir    print STDERR "\nExample:\n\n";
46*cdf0e10cSrcweir    print STDERR "$0 description.xml dir/license_xxx.txt outdir/description.xml\n\n";
47*cdf0e10cSrcweir    print STDERR "Creates \"someoutdir/description.xml\" with the license file entries like\n\"dir/license_en.US.txt\" ";
48*cdf0e10cSrcweir    print STDERR "for all languages found in the WITH_LANG environment\nvariable\n\n\n";
49*cdf0e10cSrcweir    print STDERR "Example2:\n\n";
50*cdf0e10cSrcweir    print STDERR "$0 --langsplit description.xml dir/license_xxx.txt someoutdir\n\n";
51*cdf0e10cSrcweir    print STDERR "Creates \"someoutdir/<language>/description.xml\" with one license file entry\n\"somedir/license_<language>.txt\" ";
52*cdf0e10cSrcweir    print STDERR "for all languages found in the WITH_LANG\nenvironment variable.\n\nNOTE: when using --langsplit \"destination\" needs to be a directory\n";
53*cdf0e10cSrcweir}
54*cdf0e10cSrcweir
55*cdf0e10cSrcweirif ( $ARGV[0] =~ /^-/ ) {
56*cdf0e10cSrcweir    $langswitch = shift @ARGV;
57*cdf0e10cSrcweir    if ( $langswitch ne "--langsplit" ) {
58*cdf0e10cSrcweir        usage();
59*cdf0e10cSrcweir        exit 1;
60*cdf0e10cSrcweir    }
61*cdf0e10cSrcweir    if ( ! -d $ARGV[2] ) {
62*cdf0e10cSrcweir        print STDERR "\nERROR - $ARGV[2] is not directory\n";
63*cdf0e10cSrcweir        usage();
64*cdf0e10cSrcweir        exit 2;
65*cdf0e10cSrcweir    }
66*cdf0e10cSrcweir}
67*cdf0e10cSrcweir
68*cdf0e10cSrcweirif ( $#ARGV != 2 ) {
69*cdf0e10cSrcweir    print "zzz\n";
70*cdf0e10cSrcweir    usage();
71*cdf0e10cSrcweir    exit 1;
72*cdf0e10cSrcweir}
73*cdf0e10cSrcweir
74*cdf0e10cSrcweiropen INFILE,$ARGV[0] or die "oops - no such file $ARGV[0]!\n";
75*cdf0e10cSrcweir
76*cdf0e10cSrcweirmy @inlines = <INFILE>;
77*cdf0e10cSrcweirclose INFILE;
78*cdf0e10cSrcweir
79*cdf0e10cSrcweirchomp @inlines;
80*cdf0e10cSrcweir
81*cdf0e10cSrcweir# Empty or unset WITH_LANG environment variable is set to default en-US.
82*cdf0e10cSrcweir# When WITH_LANG is set but does not contain en-US then that is prepended.
83*cdf0e10cSrcweirmy $WithLang = $ENV{WITH_LANG};
84*cdf0e10cSrcweirif ( ! defined $WithLang || $WithLang eq "")
85*cdf0e10cSrcweir{
86*cdf0e10cSrcweir    $WithLang = "en-US";
87*cdf0e10cSrcweir}
88*cdf0e10cSrcweirelsif ($WithLang !~ /\ben-US\b/)
89*cdf0e10cSrcweir{
90*cdf0e10cSrcweir    $WithLang = "en-US " . $WithLang;
91*cdf0e10cSrcweir}
92*cdf0e10cSrcweir
93*cdf0e10cSrcweir
94*cdf0e10cSrcweirif ( $langswitch eq "" ) {
95*cdf0e10cSrcweir    my @outlines;
96*cdf0e10cSrcweir	foreach my $i (@inlines) {
97*cdf0e10cSrcweir	    if ( $i =~ /license-text/ ) {
98*cdf0e10cSrcweir	        my $ii;
99*cdf0e10cSrcweir	        my $name;
100*cdf0e10cSrcweir	        foreach my $code ( split(/\s+/,$WithLang) ) {
101*cdf0e10cSrcweir	            $ii = $i;
102*cdf0e10cSrcweir	            $name = $ARGV[1];
103*cdf0e10cSrcweir	            $name =~ s/xxx/$code/;
104*cdf0e10cSrcweir	            $ii =~ s/isocode/$code/g;
105*cdf0e10cSrcweir	            $ii =~ s?licensefile?$name?g;
106*cdf0e10cSrcweir                push @outlines, "$ii\n";
107*cdf0e10cSrcweir	        }
108*cdf0e10cSrcweir	    } else {
109*cdf0e10cSrcweir            push @outlines, "$i\n";
110*cdf0e10cSrcweir	    }
111*cdf0e10cSrcweir	}
112*cdf0e10cSrcweir    open OUTFILE, ">$ARGV[2]" or die "ooops - can't open $ARGV[2] for writing\n";
113*cdf0e10cSrcweir    print OUTFILE @outlines;
114*cdf0e10cSrcweir    close OUTFILE or die "ooops - can't write to $ARGV[2]\n";
115*cdf0e10cSrcweir} else {
116*cdf0e10cSrcweir    my @outlines;
117*cdf0e10cSrcweir    my $outname = basename($ARGV[0],());
118*cdf0e10cSrcweir    foreach my $code ( split(/\s+/,$ENV{WITH_LANG}) ) {
119*cdf0e10cSrcweir        @outlines=();
120*cdf0e10cSrcweir		foreach my $i (@inlines) {
121*cdf0e10cSrcweir		    if ( $i =~ /license-text/ ) {
122*cdf0e10cSrcweir		        my $name;
123*cdf0e10cSrcweir                my $ii = $i;
124*cdf0e10cSrcweir		        $name = $ARGV[1];
125*cdf0e10cSrcweir		        $name =~ s/xxx/$code/;
126*cdf0e10cSrcweir		        $ii =~ s/isocode/$code/g;
127*cdf0e10cSrcweir		        $ii =~ s?licensefile?$name?g;
128*cdf0e10cSrcweir	            push @outlines, "$ii\n";
129*cdf0e10cSrcweir		    } else {
130*cdf0e10cSrcweir	            push @outlines, "$i\n";
131*cdf0e10cSrcweir		    }
132*cdf0e10cSrcweir		}
133*cdf0e10cSrcweir        mkdir "$ARGV[2]/$code";
134*cdf0e10cSrcweir        open OUTFILE, ">$ARGV[2]/$code/$outname" or die "ooops - can't open $ARGV[2]/$code/$outname for writing\n";
135*cdf0e10cSrcweir        print OUTFILE @outlines;
136*cdf0e10cSrcweir        close OUTFILE or die "ooops - can't write to $ARGV[2]/$code/$outname\n";
137*cdf0e10cSrcweir    }
138*cdf0e10cSrcweir}
139