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
27use strict;
28use Class::Struct;
29use Getopt::Long;
30use File::Temp;
31use File::Path;
32
33my @files;
34my @file_names;
35my $module_name = '';
36my @current;
37my @buffer;
38my $last_file;
39my $last_path;
40my $last_localize_file;
41my $first_run = "1";
42my $sdf_filename;
43my $merge_dir;
44my $state = "none";
45
46$SIG{INT}  = 'inthandler';
47$SIG{QUIT} = 'quithandler';
48
49struct ( sdf_obj =>
50{
51    module      => '$',
52    file        => '$',
53    dir         => '$',
54    FILEHANDLE  => '$',
55    line        => '$',
56    endoffile   => '$'
57}
58);
59
60parse_options();
61my $lock_file   = $merge_dir."/lock.mk";
62acquire_lock();
63read_sdf_file_names();
64init();
65my $reference;
66my $path ;
67my $localize_file;
68while( hasLines() )
69{
70    @current = ();
71    foreach ( @files )
72    {
73        push @current , $_;
74    }
75
76    $reference = getNextIdentifier( );
77
78    @current = ();
79    foreach ( @files )
80    {
81        if( $_->module eq $reference->module && $_->dir eq $reference->dir )
82        {
83            push @current , $_ ;
84        }
85    }
86    write_lines();
87}
88if( $#current+1 ne 0 )
89{
90    ( $path , $localize_file ) = make_paths();
91    add_to_buffer();
92    write_buffer( $path , $localize_file );
93}
94release_lock();
95exit( 0 );
96
97##########################################################################################
98sub acquire_lock
99{
100    if( -e $lock_file ){
101        $state = "blocked";
102        print "WARNING: Lock file '$lock_file' 'found, waiting ....\n";
103        my $cnt = 0;
104        sleep 10 , while( -e $lock_file && $cnt++ < 180 );
105        exit( 0 );
106    }else
107    {
108        $state = "locked";
109        print "Writing lock file '$lock_file'\n";
110        open FILE, ">$lock_file" or die "Can't create lock file '$lock_file'";
111        print FILE "L10N_LOCK=YES" ;
112        close ( FILE );
113    }
114}
115sub release_lock
116{
117    print "Deleting lock file '$lock_file'\n";
118    unlink $lock_file, if( -e $lock_file );
119    $state = "none";
120}
121sub inthandler
122{
123    release_lock() , if( $state eq "locked" );
124    exit( -1 );
125}
126sub quithandler
127{
128    release_lock() , if( $state eq "locked" );
129    exit( 0 );
130}
131
132sub init
133{
134    foreach my $file ( @file_names )
135    {
136        my $obj = new sdf_obj;
137        open my $FILEHANDLE , "<$file" or die "Can't open file '$file'";
138        $obj->FILEHANDLE ( $FILEHANDLE ) ;
139        getNextSdfObj( $obj );
140        push @files, $obj ;
141        print "Open file '$file'\n";
142    }
143}
144
145# get the next module/file
146sub getNextIdentifier
147{
148    my @sorted = sort {
149        return $a->module.$a->dir cmp $b->module.$b->dir;
150    } @current ;
151    return shift @sorted;
152}
153
154# update the obj with the next line
155sub getNextSdfObj
156{
157    my $obj             = shift;
158    my $line = readline ( $obj->FILEHANDLE );
159    if ( $line eq undef )
160    {
161        $obj->endoffile( "true" );
162    }
163    else
164    {
165        $line =~ /^(([^\t]*)\t([^\t]*)[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t*)/o ;
166        if( defined $1 && defined $2 && defined $3 )
167        {
168            $obj->line  ( $1 );
169            $obj->module( $2 );
170            $obj->file  ( $3 );
171            $obj->dir   ( getDir( $3 ) );
172        }
173        else
174        {
175            $obj->line  ( "" );
176            $obj->module( "" );
177            $obj->file  ( "" );
178            $obj->dir   ( "" );
179        }
180    }
181    return $obj;
182}
183sub getNextSdfObjModule
184{
185    my $obj             = shift;
186    while( !$obj->endoffile )
187    {
188        my $line = readline ( $obj->FILEHANDLE );
189        if ( $line eq undef )
190        {
191            $obj->endoffile( "true" );
192        }
193        else
194        {
195            $line =~ /^(([^\t]*)\t([^\t]*).*)/o ;
196            if( defined $1 && defined $2 && defined $3 )
197            {
198                $obj->line  ( $1 );
199                $obj->module( $2 );
200                $obj->file  ( $3 );
201                $obj->dir   ( getDir( $3 ) );
202            }
203            else
204            {
205                $obj->line  ( "" );
206                $obj->module( "" );
207                $obj->file  ( "" );
208                $obj->dir   ( "" );
209            }
210            return $obj , if( $obj->module eq $module_name )
211        }
212    }
213    #return $obj;
214}
215sub getDir
216{
217    my $path     = shift ;
218    $path        =~ s/\//\\/g;
219    my @tmp_path = split /\\/ , $path;
220    pop @tmp_path;
221    $path        = join  '\\' , @tmp_path;
222    return $path;
223}
224
225sub hasLines
226{
227    my $hasLines  = "";
228    my @tmpfiles;
229    foreach ( @files )
230    {
231        push @tmpfiles , $_, if( !$_->endoffile );
232    }
233    @files = @tmpfiles;
234    return $#files+1;
235}
236
237sub make_paths
238{
239    my $localizeFile = $merge_dir."\\".$current[ 0 ]->module."\\".$current[ 0 ]->file;
240    my $path = getDir( $localizeFile );
241    $path =~ s/\\/\//g;
242
243    $localizeFile = $path."/localize.sdf";
244
245    return ( $path , $localizeFile );
246}
247sub write_lines
248{
249    if( $first_run ){
250        add_to_buffer();
251        my( $path , $localize_file ) = make_paths();
252        $last_path = $path;
253        $last_localize_file = $localize_file;
254        mkpath $path;
255        write_buffer( $path , $localize_file );
256        $first_run = '';
257    }
258    else
259    {
260        return , if ( $#current+1 eq 0 );
261        my( $path , $localize_file ) = make_paths();
262        if( $path eq $last_path )
263        {
264            add_to_buffer();
265        }
266        else
267        {
268            mkpath $path;
269            write_buffer( $last_path , $last_localize_file );
270            add_to_buffer();
271            $last_path = $path;
272            $last_localize_file = $localize_file;
273        }
274    }
275}
276sub add_to_buffer
277{
278	my $plainline;
279	my $afile;
280	my $amodule;
281    foreach my $elem ( @current )
282    {
283		do {
284        $amodule=$elem->module;
285        $afile=$elem->file;
286		$plainline=$elem->line;
287        push @buffer, $plainline;
288        getNextSdfObj( $elem );
289		} while ( !$elem->endoffile && $amodule eq $elem->module && $afile eq $elem->file );
290    }
291}
292sub write_buffer
293{
294    my $path            = shift;
295    my $localize_file   = shift;
296    my $cnt             = $#buffer+1;
297    print "Write to $path $cnt lines\n";
298    open FILE , ">>$localize_file" or die "Can't open file '$localize_file'\n";
299    foreach ( @buffer )
300    {
301        print FILE $_."\n";
302    }
303    @buffer = ();
304}
305sub parse_options
306{
307    my $success = GetOptions( 'sdf_files=s' => \$sdf_filename , 'merge_dir=s' => \$merge_dir ); #, 'module=s' => \$module_name );
308    if( ! ( $sdf_filename && $merge_dir && $success ) )
309    {
310        usage();
311        exit( -1 );
312    }
313}
314
315sub usage
316{
317    print "Usage: fast_merge -sdf_files <file containing sdf file names> -merge_dir <directory>\n" ;
318}
319
320sub read_sdf_file_names
321{
322    open FILE , "<$sdf_filename" or die "Can't open file '$sdf_filename'\n";
323    while (  <FILE> )
324    {
325        push @file_names , split " " , $_ ;
326    }
327    close ( FILE );
328}
329
330
331