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