summaryrefslogtreecommitdiff
path: root/odk/config/configure.pl
blob: f3d0e08b3440f733aef9f7d11862f6810e833bf5 (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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
# configure.pl - a perl script to set a minimal environment for the SDK.
#
# Copyright 2000, 2010 Oracle and/or its affiliates.
#

use IO::File;
use File::Basename;

$main::hostname= $ARGV[0];
$main::sdkpath= $ARGV[1];
$main::OO_SDK_NAME= $ARGV[2];

$main::OO_MAJORVERSION=$main::OO_SDK_NAME;
$main::OO_MINORVERSION=$main::OO_SDK_NAME;
$main::OO_MAJORVERSION =~ s#[^\d]+(\d).(\d).+#$1#go;
$main::OO_MINORVERSION =~ s#[^\d]+(\d).(\d).+#$2#go;

$main::OO_SDK_CONFIG_HOME= "$ENV{HOME}/$main::OO_SDK_NAME";

$main::operatingSystem = `/bin/sh $main::sdkpath/config.guess | cut -d"-" -f3,4`;
chomp ($main::operatingSystem);

$main::OO_SDK_HOME = $main::sdkpath;
#$main::OO_SDK_HOME = "";
$main::OO_SDK_HOME_SUGGESTION = $main::sdkpath;

$main::OFFICE_HOME = "";

$main::OO_SDK_MAKE_HOME = "";
$main::makeName = "make";
if ( $main::operatingSystem =~ m/solaris/ ||
     $main::operatingSystem =~ m/freebsd/ )
{
    $main::makeName = "gmake";
}
$main::OO_SDK_MAKE_HOME_SUGGESTION = searchprog($main::makeName);
$main::makeVersion = "3.79.1";
$main::correctVersion = 0;

$main::OO_SDK_ZIP_HOME = "";
$main::OO_SDK_ZIP_HOME_SUGGESTION = searchprog("zip");
$main::zipVersion = "2.3";

$main::OO_SDK_CAT_HOME = "";
$main::OO_SDK_CAT_HOME_SUGGESTION = searchprog("cat");
# TODO cat version
# $main::catVersion = "";

$main::OO_SDK_SED_HOME = "";
$main::OO_SDK_SED_HOME_SUGGESTION = searchprog("sed");
# TODO sed version
# $main::sedVersion = "";

$main::OO_SDK_CPP_HOME = "";
$main::cppName = "gcc";
$main::cppVersion = "4.0.1";
$main::OO_SDK_CPP_HOME_SUGGESTION = searchprog($main::cppName);

$main::OO_SDK_JAVA_HOME = "";
$main::OO_SDK_JAVA_HOME_SUGGESTION = searchprog("javac");
$main::javaVersion = "1.6";

$main::SDK_AUTO_DEPLOYMENT = "";
$main::SDK_AUTO_DEPLOYMENT_SUGGESTION = "YES";

$main::OO_SDK_OUTPUT_DIR_SUGGESTION = "$ENV{HOME}";
$main::OO_SDK_OUTPUT_DIR = "";
$main::skipOutputDir = 0;

$main::return = 0;

if ( $main::operatingSystem =~ m/darwin/ )
{
    print " Used SDK = $main::OO_SDK_HOME\n\n";

    $main::OFFICE_HOME_SUGGESTION = searchMacOffice();
    while ( (! -d "$main::OFFICE_HOME" ) ||
            ((-d "$main::OFFICE_HOME") && (! -d "$main::OFFICE_HOME/Contents/MacOS")) )
    {
        print " Enter the Office installation directory [$main::OFFICE_HOME_SUGGESTION]: ";
        $main::OFFICE_HOME = readStdIn();
        chop($main::OFFICE_HOME);
        if ( $main::OFFICE_HOME eq "" )
        {
            $main::OFFICE_HOME = $main::OFFICE_HOME_SUGGESTION;
        }

        if ( ! -d "$main::OFFICE_HOME" )
        {
            $main::OFFICE_HOME = "";
            print " Error: An office installation is required, please specify the path to a valid installation.\n";
        }

    }
} else
{
    $main::OFFICE_HOME_SUGGESTION = searchoffice();

    if ( $main::OFFICE_HOME_SUGGESTION eq "" ) {
        # prepare Office path
        $main::OFFICE_HOME_SUGGESTION = searchprog("soffice");
    }

    if ( ! $main::OFFICE_HOME_SUGGESTION eq "" )
    {
        my $tmpOffice = readlink "$main::OFFICE_HOME_SUGGESTION/soffice";

        if ( $tmpOffice eq "" )
        {
            $tmpOffice = "$main::OFFICE_HOME_SUGGESTION/soffice";
        }

        my $offset = rindex($tmpOffice, "/program/soffice");
        if ( $offset != -1 )
        {
            $main::OFFICE_HOME_SUGGESTION = substr($tmpOffice, 0, $offset);
        } else
        {
            $offset = rindex($tmpOffice, "/soffice");
            if ( $offset != -1 )
            {
                $main::OFFICE_HOME_SUGGESTION = substr($tmpOffice, 0, $offset);
            } else
            {
                $main::OFFICE_HOME_SUGGESTION = "";
            }
        }
    }

    while ( (! -d "$main::OFFICE_HOME" ) ||
            ((-d "$main::OFFICE_HOME") && (! -d "$main::OFFICE_HOME/program")) )
    {
        print " Enter the Office installation directory [$main::OFFICE_HOME_SUGGESTION]: ";
        $main::OFFICE_HOME = readStdIn();
        chop($main::OFFICE_HOME);
        if ( $main::OFFICE_HOME eq "" )
        {
            $main::OFFICE_HOME = $main::OFFICE_HOME_SUGGESTION;
        }

        if ( ! -d "$main::OFFICE_HOME" )
        {
            $main::OFFICE_HOME = "";
            print " Error: An office installation is required, please specify the path to a valid installation.\n";
        } else
        {
            # special work for a network installation, no program directory but a link to the soffice binary
            if ( (! -d "$main::OFFICE_HOME/program") && (-e "$main::OFFICE_HOME/soffice") )
            {
                my $soserver = `ls -l $OFFICE_HOME_SUGGESTION/soffice | sed -n 's/.* -> //p'`;
                $soserver= substr($soserver, 0, rindex($soserver, "program") - 1);

                if ( ! -d $soserver )
                {
                    $main::OFFICE_HOME = "";
                    print " Error: An office installation is required, please specify the path to a valid installation.\n";
                } else
                {
                    $main::OFFICE_HOME = $soserver;
                }
            }
        }
    }
}

# prepare GNU make path
while ( (!$main::correctVersion) &&
        ((! -d "$main::OO_SDK_MAKE_HOME" ) ||
         ((-d "$main::OO_SDK_MAKE_HOME") && (! -e "$main::OO_SDK_MAKE_HOME/$main::makeName"))) )
{
    print " Enter GNU make ($main::makeVersion or higher) tools directory [$main::OO_SDK_MAKE_HOME_SUGGESTION]: ";
    $main::OO_SDK_MAKE_HOME = readStdIn();
    chop($main::OO_SDK_MAKE_HOME);
    if ( $main::OO_SDK_MAKE_HOME eq "" )
    {
        $main::OO_SDK_MAKE_HOME = $main::OO_SDK_MAKE_HOME_SUGGESTION;
    }
    if ( (! -d "$main::OO_SDK_MAKE_HOME") ||
         ((-d "$main::OO_SDK_MAKE_HOME") && (! -e "$main::OO_SDK_MAKE_HOME/$main::makeName")) )
    {
        $main::OO_SDK_MAKE_HOME = "";
        print " Error: GNU make is required, please specify a GNU make tools directory.\n";
    } else
    {
        #check version
        my $testVersion = `$OO_SDK_MAKE_HOME/$main::makeName --version`;
        if ( $testVersion eq "")
        {
            print " Set the environment variable OO_SDK_MAKE_HOME to your GNU build tools directory.\n";
            print " GNU make version $main::makeVersion can be obtained at ftp://ftp.gnu.org/gnu/make/\n";
        } else
        {
            if ($testVersion =~ m#((\d+\.)+\d+)# )
            {
                $testVersion = $1;
            }
            $main::correctVersion = testVersion($main::makeVersion, $testVersion, "$main::OO_SDK_MAKE_HOME/$main::makeName", 1);
            if ( !$main::correctVersion )
            {
                print " The '$main::makeName' command found at '$main::OO_SDK_MAKE_HOME' has a wrong version\n";
                $main::OO_SDK_MAKE_HOME = "";
            }
        }
    }
}

# prepare zip path
$main::correctVersion = 0;
while ( (!$main::correctVersion) &&
        ((! -d "$main::OO_SDK_ZIP_HOME" ) ||
         ((-d "$main::OO_SDK_ZIP_HOME") && (! -e "$main::OO_SDK_ZIP_HOME/zip"))) )
{
    print " Enter zip ($main::zipVersion or higher) tool directory [$main::OO_SDK_ZIP_HOME_SUGGESTION]: ";
    $main::OO_SDK_ZIP_HOME = readStdIn();
    chop($main::OO_SDK_ZIP_HOME);
    if ( $main::OO_SDK_ZIP_HOME eq "" )
    {
        $main::OO_SDK_ZIP_HOME = $main::OO_SDK_ZIP_HOME_SUGGESTION;
    }
    if ( (! -d "$main::OO_SDK_ZIP_HOME") ||
         ((-d "$main::OO_SDK_ZIP_HOME") && (! -e "$main::OO_SDK_ZIP_HOME/zip")) )
    {
        $main::OO_SDK_ZIP_HOME = "";
        print " Error: zip tool is required, please specify a zip tool directory.\n";
    } else
    {
        #check version
        my $testVersion = `$OO_SDK_ZIP_HOME/zip -h 2>&1 | egrep Zip | head -n 1`;
        $testVersion =~ s#Zip ([\d.]+) .*#$1#go;
        if ( $testVersion eq "")
        {
            print " Set the environment variable OO_SDK_ZIP_HOME to your zip tool directory.\n";
            print " zip version $main::zipVersion can be obtained at ftp://www.info-zip.org/\n";
        } else
        {
            if ($testVersion =~ m#((\d+\.)+\d+)# )
            {
                $testVersion = $1;
            }
            $main::correctVersion = testVersion($main::zipVersion, $testVersion, "$main::OO_SDK_MAKE_HOME/zip", 1);
            if ( !$main::correctVersion )
            {
                print " The 'zip' command found at '$main::OO_SDK_ZIP_HOME' has a wrong version\n";
                $main::OO_SDK_ZIP_HOME = "";
            }
        }
    }
}

# prepare cat path
$main::correctVersion = 0;
while ( (!$main::correctVersion) &&
        ((! -d "$main::OO_SDK_CAT_HOME" ) ||
         ((-d "$main::OO_SDK_CAT_HOME") && (! -e "$main::OO_SDK_CAT_HOME/cat"))) )
{
    print " Enter cat tool directory [$main::OO_SDK_CAT_HOME_SUGGESTION]: ";
    $main::OO_SDK_CAT_HOME = readStdIn();
    chop($main::OO_SDK_CAT_HOME);
    if ( $main::OO_SDK_CAT_HOME eq "" )
    {
        $main::OO_SDK_CAT_HOME = $main::OO_SDK_CAT_HOME_SUGGESTION;
    }
    if ( (! -d "$main::OO_SDK_CAT_HOME") ||
         ((-d "$main::OO_SDK_CAT_HOME") && (! -e "$main::OO_SDK_CAT_HOME/cat")) )
    {
        $main::OO_SDK_CAT_HOME = "";
        print " Error: cat tool is required, please specify a cat tool directory.\n";
    }
    # else ...
    # TODO check version
    # NOTE: only Linux cat understands --version
}

# prepare sed path
$main::correctVersion = 0;
while ( (!$main::correctVersion) &&
        ((! -d "$main::OO_SDK_SED_HOME" ) ||
         ((-d "$main::OO_SDK_SED_HOME") && (! -e "$main::OO_SDK_SED_HOME/sed"))) )
{
    print " Enter sed tool directory [$main::OO_SDK_SED_HOME_SUGGESTION]: ";
    $main::OO_SDK_SED_HOME = readStdIn();
    chop($main::OO_SDK_SED_HOME);
    if ( $main::OO_SDK_SED_HOME eq "" )
    {
        $main::OO_SDK_SED_HOME = $main::OO_SDK_SED_HOME_SUGGESTION;
    }
    if ( (! -d "$main::OO_SDK_SED_HOME") ||
         ((-d "$main::OO_SDK_SED_HOME") && (! -e "$main::OO_SDK_SED_HOME/sed")) )
    {
        $main::OO_SDK_SED_HOME = "";
        print " Error: sed tool is required, please specify a sed tool directory.\n";
    }
    # else ...
    # TODO check version
    # NOTE: only Linux sed understands --version
}

# prepare C++ compiler path
$main::correctVersion = 0;
while ( (!$main::correctVersion) &&
        ((! -d "$main::OO_SDK_CPP_HOME" ) ||
         ((-d "$main::OO_SDK_CPP_HOME") && (! -e "$main::OO_SDK_CPP_HOME/$main::cpp"))) )
{
    print " C++ compilers where for example a language binding exist:\n";
    print "  - Solaris, Sun WorkShop 6 update 1 C++ 5.2 2000/09/11 or higher\n";
    print "  - Linux, GNU C++ compiler, gcc version 4.0.1 or higher\n";
    print "  - MacOS, GNU C++ compiler, gcc version 4.0.1 or higher\n";
    print " Enter the directory of the C++ compiler, the directory\n";
    print " where the compiler is located (optional) [$main::OO_SDK_CPP_HOME_SUGGESTION]: ";

    $main::OO_SDK_CPP_HOME = readStdIn();
    chop($main::OO_SDK_CPP_HOME);
    if ( $main::OO_SDK_CPP_HOME eq "" )
    {
        $main::OO_SDK_CPP_HOME = $main::OO_SDK_CPP_HOME_SUGGESTION;
    }

    if ( ! $main::OO_SDK_CPP_HOME eq "" )
    {
        if ( (! -d "$main::OO_SDK_CPP_HOME") ||
             ((-d "$main::OO_SDK_CPP_HOME") && (! -e "$main::OO_SDK_CPP_HOME/$main::cppName")) )
        {
            print " Error: Could not find directory '$main::OO_SDK_CPP_HOME'.\n";
            if ( skipChoice("C++ compiler") == 1 )
            {
                $main::correctVersion = 1;
            }
            $main::OO_SDK_CPP_HOME = "";
        } else
        {
            #check version
            if ( $main::cppName eq "gcc" )
            {
                my $testVersion = `$OO_SDK_CPP_HOME/$main::cppName -dumpversion`;
                if ( $testVersion eq "")
                {
                    print " The '$main::cppName' command found at $main::OO_SDK_CPP_HOME/$main::cppName is not a ";
                    print " GNU compiler.\nSet the environment variable OO_SDK_CPP_HOME to your GNU build tools ";
                    print " directory.\nA GNU compiler version $main::cppVersion can be obtained at ";
                    print " ftp://ftp.gnu.org/gnu/gcc/\n";
                } else
                {
                    $main::correctVersion = testVersion($main::cppVersion, $testVersion, "$main::OO_SDK_CPP_HOME/$main::cppName", 1);
                    if ( !$main::correctVersion )
                    {
                        print " The '$main::cppName' command found at '$main::OO_SDK_CPP_HOME' has a wrong version\n";
                        if ( skipChoice("C++ compiler") == 1 )
                        {
                            $main::correctVersion = 1;
                        }

                        $main::OO_SDK_CPP_HOME = "";
                    }
                }
            }
        }
    } else
    {
        # the C++ compiler is optional
        $main::correctVersion = 1;
    }
}


# prepare Java path
$main::correctVersion = 0;

# prepare Java suggestion (cut bin directory to be in the root of the Java SDK)
$main::offset = rindex($main::OO_SDK_JAVA_HOME_SUGGESTION, "/bin");
if ( $main::offset != -1 )
{
    $main::OO_SDK_JAVA_HOME_SUGGESTION = substr($main::OO_SDK_JAVA_HOME_SUGGESTION, 0, $main::offset);
}

while ( (!$main::correctVersion) &&
        ((! -d "$main::OO_SDK_JAVA_HOME" ) ||
         ((-d "$main::OO_SDK_JAVA_HOME") && (! -e "$main::OO_SDK_JAVA_HOME/bin/javac"))) )
{
    print " Enter Java SDK (1.6 or higher) installation directory (optional) [$main::OO_SDK_JAVA_HOME_SUGGESTION]: ";
    $main::OO_SDK_JAVA_HOME = readStdIn();
    chop($main::OO_SDK_JAVA_HOME);
    if ( $main::OO_SDK_JAVA_HOME eq "" )
    {
        $main::OO_SDK_JAVA_HOME = $main::OO_SDK_JAVA_HOME_SUGGESTION;
    }
    if ( ! $main::OO_SDK_JAVA_HOME eq "" )
    {
        if ( (! -d "$main::OO_SDK_JAVA_HOME") ||
             ((-d "$main::OO_SDK_JAVA_HOME") && (! -e "$main::OO_SDK_JAVA_HOME/bin/javac")) )
        {
            print " Error: Could not find directory '$main::OO_SDK_JAVA_HOME' or '$main::OO_SDK_JAVA_HOME/bin/javac'.\n";
            if ( skipChoice("JAVA SDK") == 1 )
            {
                $main::correctVersion = 1;
            }
            $main::OO_SDK_JAVA_HOME = "";
        } else
        {
            #check version
            my $testVersion = `$main::OO_SDK_JAVA_HOME/bin/java -version 2>&1 | egrep "java version" | head -n 1 | sed -e 's#.*version "##' | sed -e 's#".*##'`;
            $testVersion =~ s#([^\n]+)\n#$1#go;

            $main::correctVersion = testVersion($main::javaVersion, $testVersion, "$main::OO_SDK_JAVA_HOME/bin/java", 1);
            if ( !$main::correctVersion )
            {
                if ( skipChoice("JAVA SDK") == 1 )
                {
                    $main::correctVersion = 1;
                }
                $main::OO_SDK_JAVA_HOME = "";
            }
        }
    }else
    {
        # the Java SDK is optional
        $main::correctVersion = 1;
    }
}


# prepare output directory (optional)
while ( (!$main::skipOutputDir) &&
        (! -d "$main::OO_SDK_OUTPUT_DIR") )
{
    print " Default output directory is in your HOME directory.\n";
    print " Enter an existent directory if you prefer a different output directory (optional) [$main::OO_SDK_OUTPUT_DIR_SUGGESTION]: ";

    $main::OO_SDK_OUTPUT_DIR = readStdIn();

    chop($main::OO_SDK_OUTPUT_DIR);
    if ( $main::OO_SDK_OUTPUT_DIR eq "" )
    {
        $main::OO_SDK_OUTPUT_DIR = $main::OO_SDK_OUTPUT_DIR_SUGGESTION;
    }
    if ( ! $main::OO_SDK_OUTPUT_DIR eq "" )
    {
        if ( ! -d "$main::OO_SDK_OUTPUT_DIR" )
        {
            print " Error: Could not find directory '$main::OO_SDK_OUTPUT_DIR'.\n";
            if ( skipChoice("optional output directory") == 1 )
            {
                $main::skipOutputDir = 1;
            }
            $main::OO_SDK_OUTPUT_DIR = "";
        }
    } else
    {
        # the output directory is optional
        $main::skipOutputDir = 1;
    }
}

# prepare auto deployment
while ( $main::SDK_AUTO_DEPLOYMENT eq "" ||
     ((! $main::SDK_AUTO_DEPLOYMENT eq "YES") &&
      (! $main::SDK_AUTO_DEPLOYMENT eq "NO")) )
{
    print " Automatic deployment of UNO components (YES/NO) [$main::SDK_AUTO_DEPLOYMENT_SUGGESTION]: ";
    $main::SDK_AUTO_DEPLOYMENT = uc <STDIN>;
    chop($main::SDK_AUTO_DEPLOYMENT);
    if ( $main::SDK_AUTO_DEPLOYMENT eq "" )
    {
        $main::SDK_AUTO_DEPLOYMENT = "YES";
    }
}

prepareScriptFile("setsdkenv_unix.sh.in", "setsdkenv_unix.sh");
chmod 0644, "$main::OO_SDK_CONFIG_HOME/$main::hostname/setsdkenv_unix.sh";

print "\n";
print " ************************************************************************\n";
print " * ... your SDK environment has been prepared.\n";
print " * For each time you want to use this configured SDK environment, you\n";
print " * have to run the \"setsdkenv_unix\" script file!\n";
print " * Alternatively can you source one of the scripts\n";
print " *   \"$main::OO_SDK_CONFIG_HOME/$main::hostname/setsdkenv_unix.sh\"\n";
print " * to get an environment without starting a new shell.\n";
print " ************************************************************************\n\n";

exit $return;

sub skipChoice
{
    my $msg = shift;
    my $skip = "";
    while ( !( $skip eq "yes" || $skip eq "no") )
    {
        print " Do you want to skip the choice of the '$msg' (YES/NO): [YES] ";
        $skip = lc <STDIN>;
        chop($skip);
        if ( $skip eq "" ) { $skip = "yes"; } # default
        if ( $skip eq "yes" )
        {
            return 1;
        }
    }
    return 0;
}

sub resolveLink
{
    my $base= shift;
    my $link= shift;

    my $resolvedpath = "$base/$link";
    my $linktarget =  readlink "$resolvedpath";
    my $resolvedlink = "";

    while ( $linktarget ne "") {

    if ( $linktarget =~ m/^\/.*/ )
    {
        $resolvedpath = "$linktarget";
    } else {
        $resolvedpath = `cd $base/$linktarget; pwd`;
        chop $resolvedpath;
    }
    $base = dirname("$resolvedpath");

    $linktarget = readlink "$resolvedpath";
    }

    $resolvedlink = `cd $resolvedpath; pwd`;
    chop $resolvedlink;
    return $resolvedlink;
}

sub searchprog
{
    my $_search= shift;
    my $tmpPath = `echo "\$PATH"`;
    my @pathList = split(":" , $tmpPath);
    my $progDir = "";

    if ( $_search eq "javac" )
    {
        if ( $main::operatingSystem =~ m/darwin/ ) {
            $progDir = resolveLink("/System/Library/Frameworks/JavaVM.Framework/Versions", "CurrentJDK");

            if ( -e "$progDir/Home/bin/javac" )
            {
                return "$progDir/Home/bin";
            }
        }

        if ( $main::operatingSystem =~ m/solaris/ ) {
            $progDir = resolveLink("/usr/jdk", "latest");
            if ( -e "$progDir/bin/javac" )
            {
                return "$progDir/bin";
            }
        }
    }

    if ( $_search eq "gmake" && $main::operatingSystem =~ m/solaris/ ) {
        if ( -e "/usr/sfw/bin/gmake" )
        {
            return "/usr/sfw/bin";
        }
    }

    foreach $i (@pathList)
    {
        chomp ($i);

        if ( -e "$i/$_search" )
        {

            if ( index($i, "/") == 0 )
            {
                # # absolute path; leave unchanged
                $progDir = $i;
            } else
            {
                $progDir = `cd "$i"; pwd`;
            }
            return $progDir
        }
    }
    return $progDir
}

sub searchMacOffice
{
    if (-d "/Applications/LibreOffice.app" ) {
        return "/Applications/LibreOffice.app"
    }
    if (-d "/Applications/OpenOffice.org.app" ) {
        return "/Applications/OpenOffice.org.app"
    }
    if (-d "/Applications/Oracle Open Office.app" ) {
        return "/Applications/Oracle Open Office.app";
    }
    if (-d "/Applications/StarOffice.app" ) {
        return "/Applications/StarOffice.app";
    }
    if (-d "/Applications/StarSuite.app" ) {
        return "/Applications/StarSuite.app";
    }

    return "";
}

sub searchoffice
{
    my $offset = rindex($main::sdkpath, "/libreoffice");
    my $tmpOffice = substr($main::sdkpath, 0, $offset);
    my $officepath = "$tmpOffice/libreoffice";

#   if ( $main::OO_MINORVERSION > 0) {
#       $officepath = "$officepath$main::OO_MINORVERSION";
#   }

    # search corresponding office for this SDK
    if (-d $officepath && -e "$officepath/program/soffice") {
        return $officepath;
    }
    # fallback
    my $tmpversion = $main::OO_MAJORVERSION;
#   if ( $main::OO_MINORVERSION > 0) {
#       $tmpversion = "$tmpversion.$main::OO_MINORVERSION";
#   }

    $officepath = "$tmpOffice/oracle_open_office$tmpversion";
    if (-d $officepath && -e "$officepath/program/soffice") {
        return $officepath;
    }

    # Before trying versioned directories, check if the sdk is directly under
    # the office path.
    $officepath = $main::sdkpath . "/..";
    if (-d $officepath && -e "$officepath/program/soffice") {
        return $officepath;
    }

    my $tmpversion = $main::OO_MAJORVERSION + 6;
    if ( $main::OO_MINORVERSION > 0) {
        $tmpversion = "$tmpversion.$main::OO_MINORVERSION";
    }

    $officepath = "$tmpOffice/staroffice$tmpversion";
    if (-d $officepath && -e "$officepath/program/soffice") {
        return $officepath;
    }
    $officepath = "$tmpOffice/StarOffice$tmpversion";
    if (-d $officepath && -e "$officepath/program/soffice") {
        return $officepath;
    }
    $officepath = "$tmpOffice/starsuite$tmpversion";
    if (-d $officepath && -e "$officepath/program/soffice") {
        return $officepath;
    }
    $officepath = "$tmpOffice/StarSuite$tmpversion";
    if (-d $officepath && -e "$officepath/program/soffice") {
        return $officepath;
    }
    $officepath = "";

    # search other potential matching office versions
    my $path = "/opt/";
    my $entry = "";
    my $version = "0";
    for $entry (glob($path.'*')) {
        ## if the file is a directory
        if( -d $entry) {

            if ($entry =~ m#(.+(o|O)ffice(\.org){0,1}(\d([\d\.]){0,2}))# ||
                $entry =~ m#(.+(s|S)uite(.*)(\d([\d\.]){0,2}))# )
            {
                if ($4 > $version) {
                    $version = $4;
                    $officepath = $entry;
                }
            }
        }
    }
    return $officepath;
}



sub testVersion
{
    my $tmpMustBeVersion = shift;
    my $tmpTestVersion = shift;
    my $toolName = shift;
    # 1=check + message 2=check only
    my $checkOnly = shift;
    my @mustBeVersion = split(/\.|_|-/,$tmpMustBeVersion);
    my @testVersion = split(/\.|_|-/,$tmpTestVersion);
    my $length = $#mustBeVersion;

    if ($#testVersion < $#mustBeVersion) {
        $length = $#testVersion;
    }

    for ($i=0; $i <= $length; $i++ )
    {
        if ( $testVersion[$i] > $mustBeVersion[$i] )
        {
            return 1; # 1 indicates a correct version
        }

        if ( $testVersion[$i] < $mustBeVersion[$i] )
        {
            if ( $#checkOnly == 1 ) {
                print " The command '$toolName' has the version $tmpTestVersion.\n";
                print " The SDK requires at least the version $tmpMustBeVersion.\n";
            }
            return 0;
        }
    }

    return 1; # 1 indicates a correct version
}

sub readStdIn
{
    my $tmpstdin = <STDIN>;
    if ( index($tmpstdin, "\$") != -1)
    {
        return `echo $tmpstdin`;
    }

    return $tmpstdin;
}

sub prepareScriptFile()
{
    my $inputFile = shift;
    my $outputFile = shift;

    if ( ! -d "$main::OO_SDK_CONFIG_HOME/$main::hostname" )
    {
        system("mkdir -p $main::OO_SDK_CONFIG_HOME/$main::hostname");
    }

    open ( FILEIN, "$main::sdkpath/$inputFile" ) || die "\nERROR: could not open '$main::sdkpath/$inputFile' for reading";
    open ( FILEOUT, ">$main::OO_SDK_CONFIG_HOME/$main::hostname/$outputFile" ) || die "\nERROR: could not open '$main::OO_SDK_CONFIG_HOME/$main::hostname/$outputFile' for writing";

    while ( <FILEIN> )
    {
        $_ =~ s#\@OO_SDK_NAME\@#$main::OO_SDK_NAME#go;
        $_ =~ s#\@OO_SDK_HOME\@#$main::OO_SDK_HOME#go;
        $_ =~ s#\@OFFICE_HOME\@#$main::OFFICE_HOME#go;
        $_ =~ s#\@OO_SDK_MAKE_HOME\@#$main::OO_SDK_MAKE_HOME#go;
        $_ =~ s#\@OO_SDK_ZIP_HOME\@#$main::OO_SDK_ZIP_HOME#go;
        $_ =~ s#\@OO_SDK_CAT_HOME\@#$main::OO_SDK_CAT_HOME#go;
        $_ =~ s#\@OO_SDK_SED_HOME\@#$main::OO_SDK_SED_HOME#go;
        $_ =~ s#\@OO_SDK_CPP_HOME\@#$main::OO_SDK_CPP_HOME#go;
        $_ =~ s#\@OO_SDK_JAVA_HOME\@#$main::OO_SDK_JAVA_HOME#go;
        $_ =~ s#\@SDK_AUTO_DEPLOYMENT\@#$main::SDK_AUTO_DEPLOYMENT#go;
        $_ =~ s#\@OO_SDK_OUTPUT_DIR\@#$main::OO_SDK_OUTPUT_DIR#go;

        print FILEOUT $_;
    }

    close FILEIN;
    close FILEOUT;
}