xref: /aoo41x/main/scp2/source/templates/modules.pl (revision cdf0e10c)
1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28my $completelangiso_var = $ENV{COMPLETELANGISO_VAR};
29my $lastcompletelangiso_var = "";
30my $outfile = "";
31my $infile = "";
32my @infile = ();
33my $globalcounter = 0;
34my $globallinecounter = 0;
35my $verbose = 0;
36
37if ( !defined $completelangiso_var) {
38	print STDERR "ERROR: No language defined!\n";
39	exit 1;
40}
41
42startup_check();
43
44# if ( "$completelangiso_var" eq "$lastcompletelangiso_var" ) {
45#    print STDERR "No new languages. Keeping old file\n";
46#    exit 0;
47# }
48
49my @completelangiso = split " +", $completelangiso_var;
50
51open OUTFILE, ">$outfile" or die "$0 ERROR: cannot open $outfile for writing!\n";
52print OUTFILE "// generated file, do not edit\n\n";
53print OUTFILE "// languages used for last time generation\n";
54print OUTFILE "// completelangiso: $completelangiso_var\n\n";
55write_ALL_MODULES();
56close OUTFILE;
57check_counter();
58
59sub check_counter
60{
61	print STDERR "Wrote modules for $globalcounter languages ($globallinecounter lines)!\n" if $verbose;
62	if ( $globalcounter == 0 )
63	{
64		print STDERR "ERROR: No languages found!\n";
65		exit 1;
66	}
67
68	if ( $globallinecounter == 0 )
69	{
70		print STDERR "ERROR: No lines written!\n";
71		exit 1;
72	}
73}
74
75
76sub write_ALL_MODULES
77{
78	my $counter = 0;
79	my $linecounter = 0;
80	my $linecount = $#infile + 1;
81	# print STDERR "Lines in inputfile: $linecount!\n";
82
83	foreach $lang (@completelangiso) {
84		$language = $lang;
85		$language_ = $lang;
86		$language_ =~ s/-/_/;
87		$languagebig_ = uc($lang);
88		$languagebig_ =~ s/-/_/;
89		$counter++;
90		my $sortkey = 100 * $counter;
91
92		for ( $i = 0; $i <= $#infile; $i++) {
93			my $line = $infile[$i];
94			if (( $line =~  /^\s*\*/ ) || ( $line =~  /^\s*\/\*/ )) { next; }
95			$line =~ s/\<LANGUAGE\>/$language/g;
96			$line =~ s/\<LANGUAGE_\>/$language_/g;
97			$line =~ s/\<LANGUAGEBIG_\>/$languagebig_/g;
98			$line =~ s/\<SORTKEY\>/$sortkey/g;
99			print OUTFILE $line;
100			$linecounter++;
101		}
102		print OUTFILE "\n";
103	}
104	print OUTFILE "\n";
105
106	$globalcounter = $counter;
107	$globallinecounter = $linecounter;
108}
109
110sub startup_check
111{
112	my $i;
113
114    if ( $#ARGV >= 0 )
115    {
116        if ( $ARGV[0] eq "-verbose" )
117        {
118            $verbose = 1;
119            shift @ARGV;
120        }
121        elsif ( $ARGV[0] eq "-quiet" )
122        {
123            # no special quiet flag/mode
124            shift @ARGV;
125        }
126    }
127
128    for ( $i=0; $i <= $#ARGV; $i++) {
129        if ( "$ARGV[$i]" eq "-o" ) {
130            if ( defined $ARGV[ $i + 1] ) {
131                $outfile = $ARGV[ $i + 1];
132                $i++;
133            }
134        } elsif ( "$ARGV[$i]" eq "-i" ) {
135            if ( defined $ARGV[ $i + 1] ) {
136                $infile = $ARGV[ $i + 1];
137                $i++;
138            }
139        } else {
140            usage();
141        }
142    }
143
144    usage() if $i < 3;
145    usage() if "$outfile" eq "";
146    usage() if "$infile" eq "";
147
148    if ( -f "$infile" ) {
149		open INFILE, "$infile" or die "$0 - ERROR: $infile exists but isn't readable.\n";
150		@infile = <INFILE>;
151		close( INFILE );
152		print STDERR "Reading template file: $infile\n" if $verbose;
153		my $num = $#infile + 1;
154		# print STDERR "Number of lines: $num\n";
155    } else {
156    	die "Template file \"$infile\" not found!\n";
157    	exit 1;
158    }
159
160    if ( -f "$outfile" ) {
161        # changed script - run always
162        return if (stat($0))[9] > (stat("$outfile"))[9] ;
163        # changed template file - run always
164        return if (stat($infile))[9] > (stat("$outfile"))[9] ;
165
166        open OLDFILE, "$outfile" or die "$0 - ERROR: $outfile exists but isn't readable.\n";
167        while ( $line = <OLDFILE> ) {
168            if ( $line =~ /^\/\/.*completelangiso:/ ) {
169                $lastcompletelangiso_var = $line;
170                chomp $lastcompletelangiso_var;
171                $lastcompletelangiso_var =~ s/^\/\/.*completelangiso:\s*//;
172                last;
173            }
174        }
175        close OLDFILE;
176    }
177
178}
179
180sub usage
181{
182    print STDERR "Generate language modules from language script particle template (*.sct file)\n";
183    print STDERR "perl $0 [-verbose] -o outputfile -i inputfile\n";
184    exit  1;
185}
186