summaryrefslogtreecommitdiff
path: root/testgraphical/prechecks/softwaretests.pl
blob: f591fa13987980d7c31fc098e8980ecdbfce7f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
eval 'exec perl -wS $0 ${1+\"$@\"}'
    if 0;

#*************************************************************************
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# Copyright 2000, 2010 Oracle and/or its affiliates.
#
# OpenOffice.org - a multi-platform office productivity suite
#
# This file is part of OpenOffice.org.
#
# OpenOffice.org is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# OpenOffice.org is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenOffice.org.  If not, see
# <http://www.openoffice.org/license.html>
# for a copy of the LGPLv3 License.
#
#*************************************************************************

# This is a pre check, which checks if some extra software exists

BEGIN
{
    #       Adding the path of this script file to the include path in the hope
    #       that all used modules can be found in it.
    $0 =~ /^(.*)[\/\\]/;
    push @INC, $1;
}

use strict;
use English;                  # $OSNAME, ...
use Getopt::Long;
use Cwd;
use Cwd 'chdir';
my $cwd = getcwd();

our $help;                    # Help option flag
our $version;                 # Version option flag

# flush STDOUT
# my $old_handle = select (STDOUT); # "select" STDOUT and save # previously selected handle
# $| = 1; # perform flush after each write to STDOUT
# select ($old_handle); # restore previously selected handle

$OUTPUT_AUTOFLUSH=1; # works only if use English is used.

our $sGlobalIniFile;
our $verbose = 0;
our $ghostscript;
our $imagemagick;
our $java6;
our $printerdriver;

our $version_info = 'compare.pl';

GetOptions(
           "ghostscript"   => \$ghostscript,
           "imagemagick"   => \$imagemagick,
           "java6"         => \$java6,
           "printerdriver" => \$printerdriver,
           "verbose"       => \$verbose,

           "help"          => \$help,
           "version"       => \$version
           );

if ($help)
{
    print_usage(*STDOUT);
    exit(0);
}
# Check for version option
if ($version)
{
    print STDERR "$version_info\n";
    exit(0);
}

# prepare the GlobalIniFile

sub prepare()
{
    my $sEnv = "$ENV{PRJ}";
    if (! $sEnv)
    {
        print "Warning: Seems you are not in a makefile.mk environment.\n";
        $sEnv = "..";
    }
    my $sPath = getcwd();
    $sPath .= "/" . $sEnv;
    chdir ($sPath);
    cwd();
    $sPath = getcwd();
    my $sInpath = $ENV{INPATH};
    $sPath .= "/" . $sInpath . "/misc";
    $sGlobalIniFile = "$sPath/pathes.ini";
    print "Global Path ini file is: $sGlobalIniFile\n" if ($verbose);
}

sub unixpath($)
{
    my $path = shift;
    $path =~ s/\\/\//g; # make out of '\' a '/'
    return $path;
}

# search for file in a given path list.
# the path list should be separated as the path variable in the corresponding OS
sub searchForFileInPath($$)
{
    my $sFile = shift;
    my $sPathList = shift;

    my $sep = ':';
    if ($OSNAME eq "MSWin32")
    {
        $sep = ';';
    }
    my @path = split($sep, $sPathList);

    my $sPath;
    my $startdir;
    my $bFound = 0;
    my $olddir = getcwd();

    my $sWindowsHomeDir = unixpath(lc($ENV{WINDIR}));

    foreach $startdir (@path)
    {
        my $nCount = 0;
        #
        # IMPORTANT: leave out windir path.
        #
        if ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
        {
            my $sPath = unixpath(lc(convertCygwinPath($startdir)));
            if ($sPath =~ /^$sWindowsHomeDir/ )
            {
                print "path: $startdir is windows path leave out.\n" if ($verbose);
                next;
            }
        }

        local *DIR;
        if (opendir (DIR, $startdir))           # open directory
        {
            print "path: $startdir" if ($verbose);
            chdir ($startdir);
            cwd();
            my $myfile;
            while ($myfile = readdir(DIR))      # get filename
            {
                if (-f $myfile )                # is it a file?
                {
                    $nCount ++;
                    if ($myfile eq $sFile)      # is it the real file?
                    {
                        $sPath = $startdir;
                        $bFound = 1;
                        last;
                    }
                }
            }
            closedir(DIR);
            print " ($nCount)\n" if ($verbose);
        }
        if ($bFound == 1)
        {
            last;
        }
    }
    chdir ($olddir);
    cwd();

    return $sPath;
}


prepare();
# don't remove the inifile, only build clean should do this.
# if ( -e "$sGlobalIniFile")
# {
#     unlink($sGlobalIniFile);
# }


# small helper, which replaces the return code
sub errorAdaption($)
{
    my $error = shift;
    if ($error != 0)
    {
        $error = $error / 256;
    }
    if ($error > 127)
    {
        $error = $error - 256;
    }
    return $error;
}

# for every error we increment this variable by 1
our $nGlobalErrors = 0;

sub handleError($$)
{
    my $error = shift;
    my $sText = shift;
    if ($error != 0)
    {
        print "ERROR: search for $sText has failed with Errornumber: $error\n";
        $nGlobalErrors ++;
    }
}

sub convertCygwinPath($)
{
    my $sPath = shift;

    if ($OSNAME eq "cygwin")
    {
        # print "Cygwin Path Patch.\n" if ($verbose);
        if ($sPath =~ /\/cygdrive\/(.)/)
        {
            my $Letter = $1;
            $sPath =~ s/\/cygdrive\/${Letter}/${Letter}\:/;
            # print "Cygwin Path Patch: '$sPath'\n" if ($verbose);
        }
    }
    return $sPath;
}

# append key=value to GlobalIniFile
sub insertPath($$)
{
    my $sKey = shift;
    my $sValue = shift;

    $sValue = convertCygwinPath($sValue);
    my $sIniFile = convertCygwinPath($sGlobalIniFile);
    local *INIFILE;
    if (open(INIFILE, ">>" . $sIniFile ))
    {
        print INIFILE "$sKey=$sValue\n";
    }
    close(INIFILE);
}

sub getFastPath($)
{
    my $sKey = shift;
    my $sValue;
    local *INIFILE;
    my $sIniFile = convertCygwinPath($sGlobalIniFile);
    if (open(INIFILE, $sIniFile))
    {
        my $line;
        while ($line = <INIFILE>)
        {
            chomp($line);
            if ( $line =~ /^$sKey=(.*)$/ )
            {
                $sValue = $1;
                # print INIFILE "$sKey=$sValue\n";
            }
        }
        close(INIFILE);
    }
    return $sValue;
}

sub checkForGhostscript()
{
    print "Search for Ghostscript\n" if ($verbose);
    if ($OSNAME eq "linux" ||
        $OSNAME eq "solaris")
    {
        # search for ghostscript
        local *GHOSTSCRIPT;
        if (open(GHOSTSCRIPT, "which gs 2>&1 |"))
        {
            my $line;
            while ($line = <GHOSTSCRIPT>)
            {
                chomp($line);
                print "- $line\n" if ($verbose);
            }
            close(GHOSTSCRIPT);
        }
        my $error = errorAdaption($?);
        handleError($error, "Ghostscript");
    }
    elsif ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
    {
        my $sGSExe = "gswin32c.exe";
        # my $sGSPath = "C:/gs/gs8.64/bin";
        my $sGSPath = getFastPath("gs.path");
        if (! $sGSPath)
        {
            $sGSPath = searchForFileInPath($sGSExe, $ENV{PATH});

            if ( ! -e "$sGSPath/$sGSExe")
            {
                $nGlobalErrors ++;
                print "ERROR: search for $sGSPath/$sGSExe failed.\n";
                print "Please install ghostscript from www.adobe.com to and make it available in \$PATH variable \n";
            }
            else
            {
                insertPath("gs.path", $sGSPath);
                insertPath("gs.exe", $sGSExe);
            }
        }
        if ( -e "$sGSPath/$sGSExe" )
        {
            print "Found Ghostscript: '$sGSPath'\n" if ($verbose);
        }
    }
    else
    {
        print "ERROR: Check for Ghostscript failed, due to unsupported '$OSNAME' environment.\n";
        $nGlobalErrors ++;
    }
}


sub checkForPSDriver()
{
    # we don't need to check for unix here, due to the fact, unix is per default be able to print in postscript
    if ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
    {
        print "Check for postscript driver.\n" if ($verbose);
        my $sWindowsRoot = $ENV{windir};
        if (! $sWindowsRoot)
        {
            $sWindowsRoot = $ENV{WINDIR};
        }
        my $sCrossOfficeDriver = "${sWindowsRoot}/system32/crossoffice.ppd";
        if ( ! -e "$sCrossOfficeDriver")
        {
            print "ERROR: Don't found Postscript driver $sCrossOfficeDriver file\n";
            $nGlobalErrors ++;
            print "Take a look on: http://so-gfxcmp.germany.sun.com/docs/further/convwatch/convwatch.html.\n";
        }
    }
}

sub checkForImageMagick()
{
    print "Search for Imagemagick\n" if ($verbose);
    if ($OSNAME eq "linux" ||
        $OSNAME eq "solaris")
    {
        # search for imagemagick
        local *IMAGEMAGICK;
        if (open(IMAGEMAGICK, "which convert 2>&1 |"))
        {
            my $line;
            while ($line = <IMAGEMAGICK>)
            {
                chomp($line);
                print "- $line\n" if ($verbose);
            }
            close(IMAGEMAGICK);
        }
        my $error = errorAdaption($?);
        handleError($error, "Imagemagick");
    }
    elsif ($OSNAME eq "MSWin32" || $OSNAME eq "cygwin")
    {
        my $sImageMagickExe = "convert.exe";
        # my $sImageMagickPath = "C:/gs/gs8.64/bin";
        my $sImageMagickPath = getFastPath("imagemagick.path");
        if (! $sImageMagickPath)
        {
            $sImageMagickPath = searchForFileInPath($sImageMagickExe, $ENV{PATH});
            if ($sImageMagickPath)
            {
                if ( ! -e "$sImageMagickPath/$sImageMagickExe")
                {
                    $nGlobalErrors ++;
                    print "ERROR: search for $sImageMagickPath/$sImageMagickExe failed.\n";
                    print "Please install ImageMagick from www.imagemagick.org to and make it available in \$PATH variable \n";
                }
                else
                {
                    insertPath("imagemagick.path", $sImageMagickPath);
                    # insertPath("gs.exe", $sImageMagickExe);
                }
            }
            else
            {
                # next try, search image magick in $PROGRAMFILES
                my $sPrograms = unixpath($ENV{PROGRAMFILES});

                if (! $sPrograms)
                {
                    print "There exist no \$PROGRAMFILES path, wrong Windows version?\n";
                    $nGlobalErrors++;
                }
                else
                {
                    local *DIR;
                    if (opendir (DIR, $sPrograms))           # open program directory
                    {
                        my $myfile;
                        while ($myfile = readdir(DIR))       # get a filename
                        {
                            if ($myfile =~ /ImageMagick/)
                            {
                                $sImageMagickPath = $sPrograms . "/" . $myfile;
                                last;
                            }
                        }
                        closedir(DIR);
                    }
                    if (! -e $sImageMagickPath)
                    {
                        print "ImageMagick not found.\n";
                        $nGlobalErrors ++;
                    }
                    else
                    {
                        insertPath("imagemagick.path", $sImageMagickPath);
                    }
                }
            }

        }
        if ( -e "$sImageMagickPath/$sImageMagickExe" )
        {
            print "Found ImageMagick: '$sImageMagickPath'\n" if ($verbose);
        }
    }
    else
    {
        print "ERROR: not supported environment\n";
    }
}

sub checkForJava6()
{
    print "Search for Java6\n" if ($verbose);
    my $javaexe = "java";
    if ( $ENV{JAVA6} )
    {
        $javaexe = $ENV{JAVA6};
    }

    if ($OSNAME eq "linux" || $OSNAME eq "cygwin")
    {
        # search for imagemagick
        local *JAVA;
        if (open(JAVA, "$javaexe -version 2>&1 |"))
        {
            my $line;
            while ($line = <JAVA>)
            {
                chomp($line);
                print "- $line\n" if ($verbose);
                if ( $line =~ /java version "(.*)"/ )
                {
                    my $javaversion = $1;
                    my @version = split('\.', $javaversion);
                    print "Found Java version: $version[1]  the complete version: $javaversion\n" if ($verbose);
                    if ( $version[1] < 6)
                    {
                        print "Wrong Java version, at least Java version 6 is need but found $javaversion.\n";
                        $nGlobalErrors++;
                        print "It is possible to overwrite the java exe with environment variable JAVA6='path'.\n";
                    }
                    else
                    {
                        insertPath("java.exe", $javaexe);
                    }
                    last;
                }
            }
            close(JAVA);
        }
        my $error = errorAdaption($?);
        handleError($error, "Java");
    }
    elsif ($OSNAME eq "MSWin32")
    {
         my $javaexe = "java";
         if ( $ENV{JAVA6} )
         {
             $javaexe = $ENV{JAVA6};
         }

         if (! -e $javaexe)
         {
             print "Java not found.\n";
             $nGlobalErrors ++;
         }
         else
         {
             print "Found Java: '$javaexe'\n" if ($verbose);
             insertPath("java.exe", $javaexe);
         }
    }
    else
    {
        print "ERROR: Java not found.\n";
    }
}

# different checks
print "Environment '$OSNAME'\n" if ($verbose);

if ($printerdriver)
{
    checkForPSDriver();
}
if ($ghostscript)
{
    checkForGhostscript();
}
if ($imagemagick)
{
    checkForImageMagick();
}
if ($java6)
{
    checkForJava6();
}

# return with found errors
exit($nGlobalErrors);

# ------------------------------------------------------------------------------
sub print_usage(*)
{
    local *HANDLE = $_[0];
    my $tool_name = basename($0);

    print(HANDLE <<END_OF_USAGE);

Usage: $tool_name [OPTIONS]

    -ghostscript             Try to find ghostscript in your environment
    -imagemagick             Try to find imagemagick
    -java6                   Checks for java 1.6.x
    -printerdriver           Try to find printer driver, windows only
    -verbose                 be verbose

    -h, --help               Print this help, then exit
    -v, --version            Print version number, then exit

END_OF_USAGE
;
}