xref: /aoo4110/main/scp2/source/templates/modules.pl (revision b1cdbd2c)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24my $completelangiso_var = $ENV{COMPLETELANGISO_VAR};
25my $lastcompletelangiso_var = "";
26my $outfile = "";
27my $infile = "";
28my @infile = ();
29my $globalcounter = 0;
30my $globallinecounter = 0;
31my $verbose = 0;
32
33if ( !defined $completelangiso_var) {
34	print STDERR "ERROR: No language defined!\n";
35	exit 1;
36}
37
38startup_check();
39
40# if ( "$completelangiso_var" eq "$lastcompletelangiso_var" ) {
41#    print STDERR "No new languages. Keeping old file\n";
42#    exit 0;
43# }
44
45my @completelangiso = split " +", $completelangiso_var;
46
47open OUTFILE, ">$outfile" or die "$0 ERROR: cannot open $outfile for writing!\n";
48print OUTFILE "// generated file, do not edit\n\n";
49print OUTFILE "// languages used for last time generation\n";
50print OUTFILE "// completelangiso: $completelangiso_var\n\n";
51write_ALL_MODULES();
52close OUTFILE;
53check_counter();
54
55sub check_counter
56{
57	print STDERR "Wrote modules for $globalcounter languages ($globallinecounter lines)!\n" if $verbose;
58	if ( $globalcounter == 0 )
59	{
60		print STDERR "ERROR: No languages found!\n";
61		exit 1;
62	}
63
64	if ( $globallinecounter == 0 )
65	{
66		print STDERR "ERROR: No lines written!\n";
67		exit 1;
68	}
69}
70
71
72sub write_ALL_MODULES
73{
74	my $counter = 0;
75	my $linecounter = 0;
76	my $linecount = $#infile + 1;
77	# print STDERR "Lines in inputfile: $linecount!\n";
78
79	foreach $lang (@completelangiso) {
80		$language = $lang;
81		$language_ = $lang;
82		$language_ =~ s/-/_/;
83		$languagebig_ = uc($lang);
84		$languagebig_ =~ s/-/_/;
85		$counter++;
86		my $sortkey = 100 * $counter;
87
88		for ( $i = 0; $i <= $#infile; $i++) {
89			my $line = $infile[$i];
90			if (( $line =~  /^\s*\*/ ) || ( $line =~  /^\s*\/\*/ )) { next; }
91			$line =~ s/\<LANGUAGE\>/$language/g;
92			$line =~ s/\<LANGUAGE_\>/$language_/g;
93			$line =~ s/\<LANGUAGEBIG_\>/$languagebig_/g;
94			$line =~ s/\<SORTKEY\>/$sortkey/g;
95			print OUTFILE $line;
96			$linecounter++;
97		}
98		print OUTFILE "\n";
99	}
100	print OUTFILE "\n";
101
102	$globalcounter = $counter;
103	$globallinecounter = $linecounter;
104}
105
106sub startup_check
107{
108	my $i;
109
110    if ( $#ARGV >= 0 )
111    {
112        if ( $ARGV[0] eq "-verbose" )
113        {
114            $verbose = 1;
115            shift @ARGV;
116        }
117        elsif ( $ARGV[0] eq "-quiet" )
118        {
119            # no special quiet flag/mode
120            shift @ARGV;
121        }
122    }
123
124    for ( $i=0; $i <= $#ARGV; $i++) {
125        if ( "$ARGV[$i]" eq "-o" ) {
126            if ( defined $ARGV[ $i + 1] ) {
127                $outfile = $ARGV[ $i + 1];
128                $i++;
129            }
130        } elsif ( "$ARGV[$i]" eq "-i" ) {
131            if ( defined $ARGV[ $i + 1] ) {
132                $infile = $ARGV[ $i + 1];
133                $i++;
134            }
135        } else {
136            usage();
137        }
138    }
139
140    usage() if $i < 3;
141    usage() if "$outfile" eq "";
142    usage() if "$infile" eq "";
143
144    if ( -f "$infile" ) {
145		open INFILE, "$infile" or die "$0 - ERROR: $infile exists but isn't readable.\n";
146		@infile = <INFILE>;
147		close( INFILE );
148		print STDERR "Reading template file: $infile\n" if $verbose;
149		my $num = $#infile + 1;
150		# print STDERR "Number of lines: $num\n";
151    } else {
152    	die "Template file \"$infile\" not found!\n";
153    	exit 1;
154    }
155
156    if ( -f "$outfile" ) {
157        # changed script - run always
158        return if (stat($0))[9] > (stat("$outfile"))[9] ;
159        # changed template file - run always
160        return if (stat($infile))[9] > (stat("$outfile"))[9] ;
161
162        open OLDFILE, "$outfile" or die "$0 - ERROR: $outfile exists but isn't readable.\n";
163        while ( $line = <OLDFILE> ) {
164            if ( $line =~ /^\/\/.*completelangiso:/ ) {
165                $lastcompletelangiso_var = $line;
166                chomp $lastcompletelangiso_var;
167                $lastcompletelangiso_var =~ s/^\/\/.*completelangiso:\s*//;
168                last;
169            }
170        }
171        close OLDFILE;
172    }
173
174}
175
176sub usage
177{
178    print STDERR "Generate language modules from language script particle template (*.sct file)\n";
179    print STDERR "perl $0 [-verbose] -o outputfile -i inputfile\n";
180    exit  1;
181}
182