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# packconfig.pl - pack xml configuration into archives
33*cdf0e10cSrcweir#
34*cdf0e10cSrcweir
35*cdf0e10cSrcweiruse strict;
36*cdf0e10cSrcweiruse Getopt::Long;
37*cdf0e10cSrcweiruse File::Find;
38*cdf0e10cSrcweiruse File::Basename;
39*cdf0e10cSrcweiruse Archive::Zip qw(:ERROR_CODES :CONSTANTS);
40*cdf0e10cSrcweir
41*cdf0e10cSrcweir#### globals ####
42*cdf0e10cSrcweir
43*cdf0e10cSrcweirmy $out_file;                # path to output archive
44*cdf0e10cSrcweirmy $tmp_out_file;            # path to temporary output file
45*cdf0e10cSrcweirmy $files_path;              # path to look for desired files
46*cdf0e10cSrcweirmy $verbose;                 # be verbose
47*cdf0e10cSrcweirmy $extra_verbose;           # be extra verbose
48*cdf0e10cSrcweirmy $do_rebuild = 0;          # is rebuilding zipfile required?
49*cdf0e10cSrcweir
50*cdf0e10cSrcweir#### script id #####
51*cdf0e10cSrcweir
52*cdf0e10cSrcweir( my $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/;
53*cdf0e10cSrcweir
54*cdf0e10cSrcweirmy $script_rev;
55*cdf0e10cSrcweirmy $id_str = ' $Revision: 1.3.24.2 $ ';
56*cdf0e10cSrcweir$id_str =~ /Revision:\s+(\S+)\s+\$/
57*cdf0e10cSrcweir  ? ($script_rev = $1) : ($script_rev = "-");
58*cdf0e10cSrcweir
59*cdf0e10cSrcweir#print "$script_name -- version: $script_rev\n";
60*cdf0e10cSrcweir
61*cdf0e10cSrcweir#### main #####
62*cdf0e10cSrcweir
63*cdf0e10cSrcweirparse_options();
64*cdf0e10cSrcweirmy %files_hash;
65*cdf0e10cSrcweirmy $file_ref = get_files();
66*cdf0e10cSrcweir
67*cdf0e10cSrcweir$do_rebuild = is_file_newer(\%files_hash) if $do_rebuild == 0;
68*cdf0e10cSrcweir
69*cdf0e10cSrcweirif ( $do_rebuild == 1 ) {
70*cdf0e10cSrcweir    create_zip_archive(\%files_hash);
71*cdf0e10cSrcweir    replace_file($tmp_out_file, $out_file);
72*cdf0e10cSrcweir    print_message("packing  $out_file finished.");
73*cdf0e10cSrcweir} else {
74*cdf0e10cSrcweir    print_message("$out_file up to date. nothing to do.");
75*cdf0e10cSrcweir}
76*cdf0e10cSrcweir
77*cdf0e10cSrcweirexit(0);
78*cdf0e10cSrcweir
79*cdf0e10cSrcweir#### subroutines ####
80*cdf0e10cSrcweir
81*cdf0e10cSrcweirsub parse_options
82*cdf0e10cSrcweir{
83*cdf0e10cSrcweir    my $opt_help;
84*cdf0e10cSrcweir    my $p = Getopt::Long::Parser->new();
85*cdf0e10cSrcweir    my $success =$p->getoptions(
86*cdf0e10cSrcweir                             '-h' => \$opt_help,
87*cdf0e10cSrcweir                             '-o=s' => \$out_file,
88*cdf0e10cSrcweir                             '-i=s' => \$files_path,
89*cdf0e10cSrcweir                             '-v'   => \$verbose,
90*cdf0e10cSrcweir                             '-vv'  => \$extra_verbose
91*cdf0e10cSrcweir                            );
92*cdf0e10cSrcweir
93*cdf0e10cSrcweir    if ( $opt_help || !$success || !$out_file || !$files_path )
94*cdf0e10cSrcweir    {
95*cdf0e10cSrcweir        usage();
96*cdf0e10cSrcweir        exit(1);
97*cdf0e10cSrcweir    }
98*cdf0e10cSrcweir
99*cdf0e10cSrcweir    #define intermediate output file
100*cdf0e10cSrcweir    $tmp_out_file="$out_file"."$$".$ENV{INPATH};
101*cdf0e10cSrcweir    # Sanity checks.
102*cdf0e10cSrcweir
103*cdf0e10cSrcweir    # Check if out_file can be written.
104*cdf0e10cSrcweir    my $out_dir = dirname($out_file);
105*cdf0e10cSrcweir    print_error("no such directory: '$out_dir'", 2) if ! -d $out_dir;
106*cdf0e10cSrcweir    print_error("can't search directory: '$out_dir'", 2) if ! -x $out_dir;
107*cdf0e10cSrcweir    print_error("directory is not writable: '$out_dir'", 2) if ! -w $out_dir;
108*cdf0e10cSrcweir
109*cdf0e10cSrcweir    # Check paths.
110*cdf0e10cSrcweir    foreach ($files_path) {
111*cdf0e10cSrcweir        print_error("no such directory: '$_'", 2) if ! -d $_;
112*cdf0e10cSrcweir        print_error("can't search directory: '$_'", 2) if ! -x $_;
113*cdf0e10cSrcweir    }
114*cdf0e10cSrcweir}
115*cdf0e10cSrcweir
116*cdf0e10cSrcweirsub get_files
117*cdf0e10cSrcweir{
118*cdf0e10cSrcweir    local @main::file_list;
119*cdf0e10cSrcweir
120*cdf0e10cSrcweir    find_files(\%files_hash);
121*cdf0e10cSrcweir
122*cdf0e10cSrcweir    if ( !keys %files_hash ) {
123*cdf0e10cSrcweir        print_error("can't find any image lists in '$files_path'", 3);
124*cdf0e10cSrcweir    }
125*cdf0e10cSrcweir
126*cdf0e10cSrcweir    return wantarray ? @main::file_list : \@main::file_list;
127*cdf0e10cSrcweir}
128*cdf0e10cSrcweir
129*cdf0e10cSrcweirsub find_files
130*cdf0e10cSrcweir{
131*cdf0e10cSrcweir    my $files_hash_ref = shift;
132*cdf0e10cSrcweir    find({ wanted => \&wanted, no_chdir => 0 }, "$files_path");
133*cdf0e10cSrcweir    foreach ( @main::file_list ) {
134*cdf0e10cSrcweir        /^\Q$files_path\E\/(.*)$/o;
135*cdf0e10cSrcweir        $files_hash_ref->{$1}++;
136*cdf0e10cSrcweir    }
137*cdf0e10cSrcweir}
138*cdf0e10cSrcweir
139*cdf0e10cSrcweirsub wanted
140*cdf0e10cSrcweir{
141*cdf0e10cSrcweir    my $file = $_;
142*cdf0e10cSrcweir
143*cdf0e10cSrcweir    if ( $file =~ /.*\.xml$/ && -f $file ) {
144*cdf0e10cSrcweir        push @main::file_list, $File::Find::name;
145*cdf0e10cSrcweir    }
146*cdf0e10cSrcweir}
147*cdf0e10cSrcweir
148*cdf0e10cSrcweirsub is_file_newer
149*cdf0e10cSrcweir{
150*cdf0e10cSrcweir    my $test_hash_ref = shift;
151*cdf0e10cSrcweir    my $reference_stamp = 0;
152*cdf0e10cSrcweir
153*cdf0e10cSrcweir    print_message("checking timestamps ...") if $verbose;
154*cdf0e10cSrcweir    if ( -e $out_file ) {
155*cdf0e10cSrcweir        $reference_stamp = (stat($out_file))[9];
156*cdf0e10cSrcweir        print_message("found $out_file with $reference_stamp ...") if $verbose;
157*cdf0e10cSrcweir    }
158*cdf0e10cSrcweir    return 1 if $reference_stamp == 0;
159*cdf0e10cSrcweir
160*cdf0e10cSrcweir    foreach ( sort keys %{$test_hash_ref} ) {
161*cdf0e10cSrcweir        my $path = $files_path;
162*cdf0e10cSrcweir        $path .= "/" if "$path" ne "";
163*cdf0e10cSrcweir        $path .= "$_";
164*cdf0e10cSrcweir        print_message("checking '$path' ...") if $extra_verbose;
165*cdf0e10cSrcweir        my $mtime = (stat($path))[9];
166*cdf0e10cSrcweir        return 1 if $reference_stamp < $mtime;
167*cdf0e10cSrcweir    }
168*cdf0e10cSrcweir    return 0;
169*cdf0e10cSrcweir}
170*cdf0e10cSrcweir
171*cdf0e10cSrcweirsub create_zip_archive
172*cdf0e10cSrcweir{
173*cdf0e10cSrcweir    my $zip_hash_ref = shift;
174*cdf0e10cSrcweir    print_message("creating config archive ...") if $verbose;
175*cdf0e10cSrcweir    my $zip = Archive::Zip->new();
176*cdf0e10cSrcweir
177*cdf0e10cSrcweir    # on Mac OS X Intel we have unxmacxi.pro, on Mac OS X PowerPC unxmacxp.pro .. and so on
178*cdf0e10cSrcweir    my $platform = $ENV{INPATH};
179*cdf0e10cSrcweir
180*cdf0e10cSrcweir    foreach ( sort keys %{$zip_hash_ref} ) {
181*cdf0e10cSrcweir        my $path = "$files_path/$_";
182*cdf0e10cSrcweir	# only Mac OS X Aqua is concerned here
183*cdf0e10cSrcweir	# but changes for other platforms can easely be added following the same principle
184*cdf0e10cSrcweir	if ( ( $platform =~ /^.*macx*/) && ($path =~ /^.*menubar.xml/ ) ) {
185*cdf0e10cSrcweir	    $path = modify_mac_menus($path);
186*cdf0e10cSrcweir	}
187*cdf0e10cSrcweir	print_message("zipping '$path' ...") if $extra_verbose;
188*cdf0e10cSrcweir	if ( !$zip->addFile($path, $_) ) {
189*cdf0e10cSrcweir	    print_error("can't add file '$path' to config zip archive: $!", 5);
190*cdf0e10cSrcweir	}
191*cdf0e10cSrcweir    }
192*cdf0e10cSrcweir    my $status = $zip->writeToFileNamed($tmp_out_file);
193*cdf0e10cSrcweir    if ( $status != AZ_OK ) {
194*cdf0e10cSrcweir        print_error("write image zip archive '$tmp_out_file' failed. Reason: $status", 6);
195*cdf0e10cSrcweir    }
196*cdf0e10cSrcweir    return;
197*cdf0e10cSrcweir}
198*cdf0e10cSrcweir
199*cdf0e10cSrcweirsub modify_mac_menus
200*cdf0e10cSrcweir{
201*cdf0e10cSrcweir    my $path_base = "$ENV{'SOLARENV'}";
202*cdf0e10cSrcweir    $path_base =~ s/solenv//;
203*cdf0e10cSrcweir
204*cdf0e10cSrcweir    my $new_file_name = "$path_base"."postprocess"."\/"."$ENV{INPATH}"."\/"."misc"."\/"."$_";
205*cdf0e10cSrcweir
206*cdf0e10cSrcweir    my $new_directory = $new_file_name;
207*cdf0e10cSrcweir    $new_directory =~ s/\/menubar.xml//;
208*cdf0e10cSrcweir    if ( ! -e $new_directory) {
209*cdf0e10cSrcweir	`mkdir -p "$new_directory"`;
210*cdf0e10cSrcweir    }
211*cdf0e10cSrcweir
212*cdf0e10cSrcweir    my $old_file_name = "$files_path/$_";
213*cdf0e10cSrcweir
214*cdf0e10cSrcweir    `cp $old_file_name $new_file_name`;
215*cdf0e10cSrcweir
216*cdf0e10cSrcweir    my $temp_file_name = "$new_file_name"."_tmp";
217*cdf0e10cSrcweir    my $xsl_file = "macosx/macosx_menubar_modification.xsl";
218*cdf0e10cSrcweir
219*cdf0e10cSrcweir    my $result = `xsltproc $xsl_file $new_file_name > $temp_file_name`;
220*cdf0e10cSrcweir
221*cdf0e10cSrcweir    if ( $result != 0) {
222*cdf0e10cSrcweir	print_error("xsltproc '$xsl_file' '$new_file_name'> '$temp_file_name' failed",1)
223*cdf0e10cSrcweir    }
224*cdf0e10cSrcweir
225*cdf0e10cSrcweir    replace_file( $temp_file_name, $new_file_name );
226*cdf0e10cSrcweir    return $new_file_name;
227*cdf0e10cSrcweir}
228*cdf0e10cSrcweir
229*cdf0e10cSrcweirsub replace_file
230*cdf0e10cSrcweir{
231*cdf0e10cSrcweir    my $source_file = shift;
232*cdf0e10cSrcweir    my $dest_file = shift;
233*cdf0e10cSrcweir    my $result = 0;
234*cdf0e10cSrcweir
235*cdf0e10cSrcweir    $result = unlink($dest_file) if -f $dest_file;
236*cdf0e10cSrcweir    if ( $result != 1 && -f $dest_file ) {
237*cdf0e10cSrcweir        unlink $source_file;
238*cdf0e10cSrcweir        print_error("couldn't remove '$dest_file'",1);
239*cdf0e10cSrcweir    }  else {
240*cdf0e10cSrcweir        if ( !rename($source_file, $dest_file)) {
241*cdf0e10cSrcweir            unlink $source_file;
242*cdf0e10cSrcweir            print_error("couldn't rename '$source_file'",1);
243*cdf0e10cSrcweir        }
244*cdf0e10cSrcweir    }
245*cdf0e10cSrcweir    return;
246*cdf0e10cSrcweir}
247*cdf0e10cSrcweir
248*cdf0e10cSrcweirsub usage
249*cdf0e10cSrcweir{
250*cdf0e10cSrcweir    print STDERR "Usage: packimages.pl [-h] -o out_file -i file_path\n";
251*cdf0e10cSrcweir    print STDERR "Creates archive of images\n";
252*cdf0e10cSrcweir    print STDERR "Options:\n";
253*cdf0e10cSrcweir    print STDERR "    -h                 print this help\n";
254*cdf0e10cSrcweir    print STDERR "    -o out_file        path to output archive\n";
255*cdf0e10cSrcweir    print STDERR "    -i file_path       path to directory containing the config files\n";
256*cdf0e10cSrcweir    print STDERR "    -v                 verbose\n";
257*cdf0e10cSrcweir    print STDERR "    -vv                very verbose\n";
258*cdf0e10cSrcweir}
259*cdf0e10cSrcweir
260*cdf0e10cSrcweirsub print_message
261*cdf0e10cSrcweir{
262*cdf0e10cSrcweir    my $message     = shift;
263*cdf0e10cSrcweir
264*cdf0e10cSrcweir    print "$script_name: ";
265*cdf0e10cSrcweir    print "$message\n";
266*cdf0e10cSrcweir    return;
267*cdf0e10cSrcweir}
268*cdf0e10cSrcweir
269*cdf0e10cSrcweirsub print_warning
270*cdf0e10cSrcweir{
271*cdf0e10cSrcweir    my $message     = shift;
272*cdf0e10cSrcweir
273*cdf0e10cSrcweir    print STDERR "$script_name: ";
274*cdf0e10cSrcweir    print STDERR "WARNING $message\n";
275*cdf0e10cSrcweir    return;
276*cdf0e10cSrcweir}
277*cdf0e10cSrcweir
278*cdf0e10cSrcweirsub print_error
279*cdf0e10cSrcweir{
280*cdf0e10cSrcweir    my $message     = shift;
281*cdf0e10cSrcweir    my $error_code  = shift;
282*cdf0e10cSrcweir
283*cdf0e10cSrcweir    print STDERR "$script_name: ";
284*cdf0e10cSrcweir    print STDERR "ERROR: $message\n";
285*cdf0e10cSrcweir
286*cdf0e10cSrcweir    if ( $error_code ) {
287*cdf0e10cSrcweir        print STDERR "\nFAILURE: $script_name aborted.\n";
288*cdf0e10cSrcweir        exit($error_code);
289*cdf0e10cSrcweir    }
290*cdf0e10cSrcweir    return;
291*cdf0e10cSrcweir}
292