1#! /usr/local/bin/perl
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7
8require('coreconf.pl');
9
10#######-- read in variables on command line into %var
11
12$use_jar = 1;
13$ZIP     = "$ENV{JAVA_HOME}/bin/jar";
14
15if ( $ENV{JAVA_HOME} eq "" ) {
16    $ZIP      = "zip";
17    $use_jar  = 0;
18}
19
20
21&parse_argv;
22
23
24######-- Do the packaging of jars.
25
26foreach $jarfile (split(/ /,$var{FILES}) ) {
27    print STDERR "---------------------------------------------\n";
28    print STDERR "Packaging jar file $jarfile....\n";
29
30    $jarinfo = $var{$jarfile};
31
32    ($jardir,$jaropts) = split(/\|/,$jarinfo);
33
34    if ( $use_jar ) {
35        $zipoptions = "-cvf";
36    } else {
37        $zipoptions = "-T -r";
38        if ($jaropts =~ /a/) {
39            if ($var{OS_ARCH} eq 'WINNT') {
40                $zipoptions .= ' -ll';
41            }
42        }
43    }
44
45# just in case the directory ends in a /, remove it
46    if ($jardir =~ /\/$/) {
47	chop $jardir;
48    }
49
50    $dirdepth --;
51
52    print STDERR "jardir = $jardir\n";
53    system("ls $jardir");
54
55    if (-d $jardir) {
56
57
58# count the number of slashes
59
60	$slashes =0;
61
62	foreach $i (split(//,$jardir)) {
63	    if ($i =~ /\//) {
64		$slashes++;
65	    }
66	}
67
68	$dotdots =0;
69
70	foreach $i (split(m|/|,$jardir)) {
71	    if ($i eq '..') {
72		$dotdots ++;
73	    }
74	}
75
76	$dirdepth = ($slashes +1) - (2*$dotdots);
77
78	print STDERR "changing dir $jardir\n";
79	chdir($jardir);
80	print STDERR "making dir META-INF\n";
81	mkdir("META-INF",0755);
82
83	$filelist = "";
84	opendir(DIR,".");
85	while ($_ = readdir(DIR)) {
86	    if (! ( ($_ eq '.') || ($_ eq '..'))) {
87		if ( $jaropts =~ /i/) {
88		    if (! /^include$/) {
89			$filelist .= "$_ ";
90		    }
91		}
92		else {
93		    $filelist .= "$_ ";
94		}
95	    }
96	}
97	closedir(DIR);
98
99	print STDERR "$ZIP $zipoptions $jarfile $filelist\n";
100	system("$ZIP $zipoptions $jarfile $filelist");
101	rmdir("META-INF");
102	    for $i (1 .. $dirdepth) {
103	    chdir("..");
104	    print STDERR "chdir ..\n";
105	}
106    }
107    else {
108        print STDERR "Directory $jardir doesn't exist\n";
109    }
110
111}
112
113