1eval 'exec perl -wS $0 ${1+\"$@\"}'
2    if 0;
3
4#*************************************************************************
5#
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
14# OpenOffice.org is free software: you can redistribute it and/or modify
15# it under the terms of the GNU Lesser General Public License version 3
16# only, as published by the Free Software Foundation.
17#
18# OpenOffice.org is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU Lesser General Public License version 3 for more details
22# (a copy is included in the LICENSE file that accompanied this code).
23#
24# You should have received a copy of the GNU Lesser General Public License
25# version 3 along with OpenOffice.org.  If not, see
26# <http://www.openoffice.org/license.html>
27# for a copy of the LGPLv3 License.
28#
29#*************************************************************************
30
31# This is a pre check, which checks if some extra software exists
32
33BEGIN
34{
35    #       Adding the path of this script file to the include path in the hope
36    #       that all used modules can be found in it.
37    $0 =~ /^(.*)[\/\\]/;
38    push @INC, $1;
39}
40
41use strict;
42use English;                  # $OSNAME, ...
43use Getopt::Long;
44use Cwd;
45use Cwd 'chdir';
46my $cwd = getcwd();
47
48our $help;                    # Help option flag
49our $version;                 # Version option flag
50
51# flush STDOUT
52# my $old_handle = select (STDOUT); # "select" STDOUT and save # previously selected handle
53# $| = 1; # perform flush after each write to STDOUT
54# select ($old_handle); # restore previously selected handle
55
56$OUTPUT_AUTOFLUSH=1; # works only if use English is used.
57
58our $sGlobalIniFile;
59our $verbose = 0;
60our $ghostscript;
61our $imagemagick;
62our $java6;
63our $printerdriver;
64
65our $version_info = 'compare.pl';
66
67GetOptions(
68           "ghostscript"   => \$ghostscript,
69           "imagemagick"   => \$imagemagick,
70           "java6"         => \$java6,
71           "printerdriver" => \$printerdriver,
72           "verbose"       => \$verbose,
73
74           "help"          => \$help,
75           "version"       => \$version
76           );
77
78if ($help)
79{
80    print_usage(*STDOUT);
81    exit(0);
82}
83# Check for version option
84if ($version)
85{
86    print STDERR "$version_info\n";
87    exit(0);
88}
89
90# prepare the GlobalIniFile
91
92sub prepare()
93{
94    my $sEnv = "$ENV{PRJ}";
95    if (! $sEnv)
96    {
97        print "Warning: Seems you are not in a makefile.mk environment.\n";
98        $sEnv = "..";
99    }
100    my $sPath = getcwd();
101    $sPath .= "/" . $sEnv;
102    chdir ($sPath);
103    cwd();
104    $sPath = getcwd();
105    my $sInpath = $ENV{INPATH};
106    $sPath .= "/" . $sInpath . "/misc";
107    $sGlobalIniFile = "$sPath/pathes.ini";
108    print "Global Path ini file is: $sGlobalIniFile\n" if ($verbose);
109}
110
111sub unixpath($)
112{
113    my $path = shift;
114    $path =~ s/\\/\//g; # make out of '\' a '/'
115    return $path;
116}
117
118# search for file in a given path list.
119# the path list should be separated as the path variable in the corresponding OS
120sub searchForFileInPath($$)
121{
122    my $sFile = shift;
123    my $sPathList = shift;
124
125    my $sep = ':';
126    if ($OSNAME eq "MSWin32")
127    {
128        $sep = ';';
129    }
130    my @path = split($sep, $sPathList);
131
132    my $sPath;
133    my $startdir;
134    my $bFound = 0;
135    my $olddir = getcwd();
136
137    my $sWindowsHomeDir = unixpath(lc($ENV{WINDIR}));
138
139    foreach $startdir (@path)
140    {
141        my $nCount = 0;
142        #
143        # IMPORTANT: leave out windir path.
144        #
145        if ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
146        {
147            my $sPath = unixpath(lc(convertCygwinPath($startdir)));
148            if ($sPath =~ /^$sWindowsHomeDir/ )
149            {
150                print "path: $startdir is windows path leave out.\n" if ($verbose);
151                next;
152            }
153        }
154
155        local *DIR;
156        if (opendir (DIR, $startdir))           # open directory
157        {
158            print "path: $startdir" if ($verbose);
159            chdir ($startdir);
160            cwd();
161            my $myfile;
162            while ($myfile = readdir(DIR))      # get filename
163            {
164                if (-f $myfile )                # is it a file?
165                {
166                    $nCount ++;
167                    if ($myfile eq $sFile)      # is it the real file?
168                    {
169                        $sPath = $startdir;
170                        $bFound = 1;
171                        last;
172                    }
173                }
174            }
175            closedir(DIR);
176            print " ($nCount)\n" if ($verbose);
177        }
178        if ($bFound == 1)
179        {
180            last;
181        }
182    }
183    chdir ($olddir);
184    cwd();
185
186    return $sPath;
187}
188
189
190prepare();
191# don't remove the inifile, only build clean should do this.
192# if ( -e "$sGlobalIniFile")
193# {
194#     unlink($sGlobalIniFile);
195# }
196
197
198# small helper, which replaces the return code
199sub errorAdaption($)
200{
201    my $error = shift;
202    if ($error != 0)
203    {
204        $error = $error / 256;
205    }
206    if ($error > 127)
207    {
208        $error = $error - 256;
209    }
210    return $error;
211}
212
213# for every error we increment this variable by 1
214our $nGlobalErrors = 0;
215
216sub handleError($$)
217{
218    my $error = shift;
219    my $sText = shift;
220    if ($error != 0)
221    {
222        print "ERROR: search for $sText has failed with Errornumber: $error\n";
223        $nGlobalErrors ++;
224    }
225}
226
227sub convertCygwinPath($)
228{
229    my $sPath = shift;
230
231    if ($OSNAME eq "cygwin")
232    {
233        # print "Cygwin Path Patch.\n" if ($verbose);
234        if ($sPath =~ /\/cygdrive\/(.)/)
235        {
236            my $Letter = $1;
237            $sPath =~ s/\/cygdrive\/${Letter}/${Letter}\:/;
238            # print "Cygwin Path Patch: '$sPath'\n" if ($verbose);
239        }
240    }
241    return $sPath;
242}
243
244# append key=value to GlobalIniFile
245sub insertPath($$)
246{
247    my $sKey = shift;
248    my $sValue = shift;
249
250    $sValue = convertCygwinPath($sValue);
251    my $sIniFile = convertCygwinPath($sGlobalIniFile);
252    local *INIFILE;
253    if (open(INIFILE, ">>" . $sIniFile ))
254    {
255        print INIFILE "$sKey=$sValue\n";
256    }
257    close(INIFILE);
258}
259
260sub getFastPath($)
261{
262    my $sKey = shift;
263    my $sValue;
264    local *INIFILE;
265    my $sIniFile = convertCygwinPath($sGlobalIniFile);
266    if (open(INIFILE, $sIniFile))
267    {
268        my $line;
269        while ($line = <INIFILE>)
270        {
271            chomp($line);
272            if ( $line =~ /^$sKey=(.*)$/ )
273            {
274                $sValue = $1;
275                # print INIFILE "$sKey=$sValue\n";
276            }
277        }
278        close(INIFILE);
279    }
280    return $sValue;
281}
282
283sub checkForGhostscript()
284{
285    print "Search for Ghostscript\n" if ($verbose);
286    if ($OSNAME eq "linux" ||
287        $OSNAME eq "solaris")
288    {
289        # search for ghostscript
290        local *GHOSTSCRIPT;
291        if (open(GHOSTSCRIPT, "which gs 2>&1 |"))
292        {
293            my $line;
294            while ($line = <GHOSTSCRIPT>)
295            {
296                chomp($line);
297                print "- $line\n" if ($verbose);
298            }
299            close(GHOSTSCRIPT);
300        }
301        my $error = errorAdaption($?);
302        handleError($error, "Ghostscript");
303    }
304    elsif ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
305    {
306        my $sGSExe = "gswin32c.exe";
307        # my $sGSPath = "C:/gs/gs8.64/bin";
308        my $sGSPath = getFastPath("gs.path");
309        if (! $sGSPath)
310        {
311            $sGSPath = searchForFileInPath($sGSExe, $ENV{PATH});
312
313            if ( ! -e "$sGSPath/$sGSExe")
314            {
315                $nGlobalErrors ++;
316                print "ERROR: search for $sGSPath/$sGSExe failed.\n";
317                print "Please install ghostscript from www.adobe.com to and make it available in \$PATH variable \n";
318            }
319            else
320            {
321                insertPath("gs.path", $sGSPath);
322                insertPath("gs.exe", $sGSExe);
323            }
324        }
325        if ( -e "$sGSPath/$sGSExe" )
326        {
327            print "Found Ghostscript: '$sGSPath'\n" if ($verbose);
328        }
329    }
330    else
331    {
332        print "ERROR: Check for Ghostscript failed, due to unsupported '$OSNAME' environment.\n";
333        $nGlobalErrors ++;
334    }
335}
336
337
338sub checkForPSDriver()
339{
340    # we don't need to check for unix here, due to the fact, unix is per default be able to print in postscript
341    if ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
342    {
343        print "Check for postscript driver.\n" if ($verbose);
344        my $sWindowsRoot = $ENV{windir};
345        if (! $sWindowsRoot)
346        {
347            $sWindowsRoot = $ENV{WINDIR};
348        }
349        my $sCrossOfficeDriver = "${sWindowsRoot}/system32/crossoffice.ppd";
350        if ( ! -e "$sCrossOfficeDriver")
351        {
352            print "ERROR: Don't found Postscript driver $sCrossOfficeDriver file\n";
353            $nGlobalErrors ++;
354            print "Take a look on: http://so-gfxcmp.germany.sun.com/docs/further/convwatch/convwatch.html.\n";
355        }
356    }
357}
358
359sub checkForImageMagick()
360{
361    print "Search for Imagemagick\n" if ($verbose);
362    if ($OSNAME eq "linux" ||
363        $OSNAME eq "solaris")
364    {
365        # search for imagemagick
366        local *IMAGEMAGICK;
367        if (open(IMAGEMAGICK, "which convert 2>&1 |"))
368        {
369            my $line;
370            while ($line = <IMAGEMAGICK>)
371            {
372                chomp($line);
373                print "- $line\n" if ($verbose);
374            }
375            close(IMAGEMAGICK);
376        }
377        my $error = errorAdaption($?);
378        handleError($error, "Imagemagick");
379    }
380    elsif ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
381    {
382        my $sImageMagickExe = "convert.exe";
383        # my $sImageMagickPath = "C:/gs/gs8.64/bin";
384        my $sImageMagickPath = getFastPath("imagemagick.path");
385        if (! $sImageMagickPath)
386        {
387            $sImageMagickPath = searchForFileInPath($sImageMagickExe, $ENV{PATH});
388            if ($sImageMagickPath)
389            {
390                if ( ! -e "$sImageMagickPath/$sImageMagickExe")
391                {
392                    $nGlobalErrors ++;
393                    print "ERROR: search for $sImageMagickPath/$sImageMagickExe failed.\n";
394                    print "Please install ImageMagick from www.imagemagick.org to and make it available in \$PATH variable \n";
395                }
396                else
397                {
398                    insertPath("imagemagick.path", $sImageMagickPath);
399                    # insertPath("gs.exe", $sImageMagickExe);
400                }
401            }
402            else
403            {
404                # next try, search image magick in $PROGRAMFILES
405                my $sPrograms = unixpath($ENV{PROGRAMFILES});
406
407                if (! $sPrograms)
408                {
409                    print "There exist no \$PROGRAMFILES path, wrong Windows version?\n";
410                    $nGlobalErrors++;
411                }
412                else
413                {
414                    local *DIR;
415                    if (opendir (DIR, $sPrograms))           # open program directory
416                    {
417                        my $myfile;
418                        while ($myfile = readdir(DIR))       # get a filename
419                        {
420                            if ($myfile =~ /ImageMagick/)
421                            {
422                                $sImageMagickPath = $sPrograms . "/" . $myfile;
423                                last;
424                            }
425                        }
426                        closedir(DIR);
427                    }
428                    if (! -e $sImageMagickPath)
429                    {
430                        print "ImageMagick not found.\n";
431                        $nGlobalErrors ++;
432                    }
433                    else
434                    {
435                        insertPath("imagemagick.path", $sImageMagickPath);
436                    }
437                }
438            }
439
440        }
441        if ( -e "$sImageMagickPath/$sImageMagickExe" )
442        {
443            print "Found ImageMagick: '$sImageMagickPath'\n" if ($verbose);
444        }
445    }
446    else
447    {
448        print "ERROR: not supported environment\n";
449    }
450}
451
452sub checkForJava6()
453{
454    print "Search for Java6\n" if ($verbose);
455    my $javaexe = "java";
456    if ( $ENV{JAVA6} )
457    {
458        $javaexe = $ENV{JAVA6};
459    }
460
461    if ($OSNAME eq "linux" || $OSNAME eq "cygwin")
462    {
463        # search for imagemagick
464        local *JAVA;
465        if (open(JAVA, "$javaexe -version 2>&1 |"))
466        {
467            my $line;
468            while ($line = <JAVA>)
469            {
470                chomp($line);
471                print "- $line\n" if ($verbose);
472                if ( $line =~ /java version "(.*)"/ )
473                {
474                    my $javaversion = $1;
475                    my @version = split('\.', $javaversion);
476                    print "Found Java version: $version[1]  the complete version: $javaversion\n" if ($verbose);
477                    if ( $version[1] < 6)
478                    {
479                        print "Wrong Java version, at least Java version 6 is need but found $javaversion.\n";
480                        $nGlobalErrors++;
481                        print "It is possible to overwrite the java exe with environment variable JAVA6='path'.\n";
482                    }
483                    else
484                    {
485                        insertPath("java.exe", $javaexe);
486                    }
487                    last;
488                }
489            }
490            close(JAVA);
491        }
492        my $error = errorAdaption($?);
493        handleError($error, "Java");
494    }
495    elsif ($OSNAME eq "MSWin32")
496    {
497         my $javaexe = "java";
498         if ( $ENV{JAVA6} )
499         {
500             $javaexe = $ENV{JAVA6};
501         }
502
503         if (! -e $javaexe)
504         {
505             print "Java not found.\n";
506             $nGlobalErrors ++;
507         }
508         else
509         {
510             print "Found Java: '$javaexe'\n" if ($verbose);
511             insertPath("java.exe", $javaexe);
512         }
513    }
514    else
515    {
516        print "ERROR: Java not found.\n";
517    }
518}
519
520# different checks
521print "Environment '$OSNAME'\n" if ($verbose);
522
523if ($printerdriver)
524{
525    checkForPSDriver();
526}
527if ($ghostscript)
528{
529    checkForGhostscript();
530}
531if ($imagemagick)
532{
533    checkForImageMagick();
534}
535if ($java6)
536{
537    checkForJava6();
538}
539
540# return with found errors
541exit($nGlobalErrors);
542
543# ------------------------------------------------------------------------------
544sub print_usage(*)
545{
546    local *HANDLE = $_[0];
547    my $tool_name = basename($0);
548
549    print(HANDLE <<END_OF_USAGE);
550
551Usage: $tool_name [OPTIONS]
552
553    -ghostscript             Try to find ghostscript in your environment
554    -imagemagick             Try to find imagemagick
555    -java6                   Checks for java 1.6.x
556    -printerdriver           Try to find printer driver, windows only
557    -verbose                 be verbose
558
559    -h, --help               Print this help, then exit
560    -v, --version            Print version number, then exit
561
562END_OF_USAGE
563;
564}
565