summaryrefslogtreecommitdiff
path: root/scripting/workben/installer/InstUtil.java
blob: 0fb687c80f0009a0ee075ca3e154c51ec70c9dab (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
/*
 * 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 .
 */

package installer;

import java.net.URLDecoder;
import java.io.*;
import java.util.*;
import java.net.*;

public class InstUtil {

    public static File buildSversionLocation() throws IOException {
        File theFile = null;
        StringBuffer str = new StringBuffer();
        str.append(System.getProperty("user.home"));
        str.append(File.separator);
        StringBuffer thePath = new StringBuffer(str.toString());

        String os = System.getProperty("os.name");

        if (os.indexOf("Windows") != -1) {
            boolean bSVersionInHomeDir = new File(thePath.toString() + "sversion.ini").exists();

            if (!bSVersionInHomeDir) {
                thePath.append("Application Data");
                thePath.append(File.separator);
            }
            theFile = findVersionFile(new File(thePath.toString()));
        } else if (os.indexOf("SunOS") != -1) {
            thePath.append(".sversionrc");
            theFile = new File(thePath.toString());
        } else if (os.indexOf("Linux") != -1) {
            thePath.append(".sversionrc");
            theFile = new File(thePath.toString());
        }

        if (theFile == null)
        {
            throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?");
        }
        if  (!theFile.exists())
        {
            throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?");
        }
        return theFile;
    }



    public static boolean hasNetbeansInstallation() {
        boolean result = false;
        try
        {
            result = checkForSupportedVersion( getNetbeansLocation(), versions );

            if (result == false)
                System.out.println("No supported version of NetBeans found.");
        }
        catch ( IOException ioe )
        {
            System.err.println("Exception caught trying to determine netbeans installation: " + ioe );
            ioe.printStackTrace();
            result = false;
        }
        return result;
    }

    private static boolean checkForSupportedVersion( Properties installs, String[] supportedVersions )
    {
        if ( installs != null )
        {
            for ( int index = 0; index < supportedVersions.length; index++ )
            {
                String key = supportedVersions[ index ];
                String path = null;
                if ( ( path = installs.getProperty(key) ) != null )
                {
                    // at least one supported version for netbeans present, so return;
                    return true;
                }

            }
        }
        return false;
    }


    public static boolean hasJeditInstallation() {
        boolean result = false;
        try
        {
            result = checkForSupportedVersion( getJeditLocation(), versions );
            if ( !result )
            {
                System.out.println("No supported version for JEdit found.");
            }
        }
        catch ( IOException ioe )
        {
            System.err.println("Exception caught trying to determine jedit installation: " + ioe );
            ioe.printStackTrace();
            result = false;
        }
        return result;
    }



    public static Properties getNetbeansLocation() throws IOException {
    File theFile = null;
    Properties results = new Properties();

    StringBuffer str = new StringBuffer();
        str.append(System.getProperty("user.home"));
        str.append(File.separator);
    StringBuffer thePath = new StringBuffer(str.toString());

        String os = System.getProperty("os.name");

    if (os.indexOf("Windows") != -1) {
        //theFile = findVersionFile(new File(str.toString()));
        thePath.append(".netbeans");
        //theFile = new File(thePath.toString());
        } else if (os.indexOf("SunOS") != -1) {
        thePath.append(".netbeans");
        //theFile = new File(thePath.toString());
        } else if (os.indexOf("Linux") != -1) {
        thePath.append(".netbeans");
        //theFile = new File(thePath.toString());
    }

    if ( thePath.toString().indexOf( ".netbeans" ) == -1 )
        return null;
    else if ( new File( thePath.append( File.separator+"3.4"+File.separator ).toString() ).isDirectory() ) {

        System.out.println( "Found NetBeans 3.4 user directory: " + thePath );
        File netbeansLogFile = new File( thePath.toString() + File.separator + "system" + File.separator + "ide.log" );
        if( netbeansLogFile.exists() ) {
            String installPath = getNetbeansInstallation( netbeansLogFile );
            File f = new File(installPath);
            results.put("NetBeans 3.4", f.getPath()+File.separator);
            System.out.println( "NetBeans Installation directory: " + f.getPath());
        }
        else {
            System.out.println( "No NetBeans log file found" );
                        return null;
        }
    }
        else
        {
        System.out.println( "No NetBeans user directory found" );
        return null;
        }


    return results;
    }



    public static Properties getJeditLocation() throws IOException {

    /*if( !hasJeditInstallation() ) {
        System.out.println( "No Jedit found (line195 InstUtil");
        return null;
    }*/

    Properties results = new Properties();

    StringBuffer str = new StringBuffer();
        str.append(System.getProperty("user.home"));
        str.append(File.separator);
    StringBuffer thePath = new StringBuffer(str.toString());

        String os = System.getProperty("os.name");
        thePath.append(".jedit");
    //System.out.println( ".jedit path " + thePath );

    File jeditLogFile = new File( thePath.toString() + File.separator + "activity.log" );
    if( jeditLogFile.exists() ) {
        String[] jeditDetails = getJeditInstallation( jeditLogFile );
        System.out.println( "getJeditLocation ) " + jeditDetails[0] );
        results.put("jEdit "+jeditDetails[1], jeditDetails[0]);
        System.out.println( "jeditDetails[0] is " + jeditDetails[0]);
    }
    else {
        System.out.println( "Prompt user for Jedit installation path" );
    }


    return results;
    }





    private static String getNetbeansInstallation( File logFile ) {
        String installPath = "";
        try {
        BufferedReader reader = new BufferedReader(new FileReader(logFile));

        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
        s.trim();
        if( s.indexOf( "IDE Install" ) != -1 ) {
            int pathStart = s.indexOf( "=" ) + 2;
            //System.out.println( "pathStart " + pathStart );
            installPath = s.substring( pathStart, s.length() );
            //System.out.println( "installPath 1" + installPath );
            int pathEnd = installPath.indexOf( ";");
            //System.out.println( "pathEnd " + pathEnd );
            installPath = installPath.substring( 0, pathEnd ) +File.separator;
            //System.out.println( "pathStart " + pathStart );
            //int pathEnd = s.indexOf( ";");
            //System.out.println( "pathEnd " + pathEnd );
            //System.out.println( "s is " + s + " and " + s.length() + " long" );
            //installPath = s.substring( pathStart, pathEnd - 1 );
            installPath.trim();
            break;
        }
        }
        reader.close();
        }
        catch( IOException ioe ) {
        System.out.println( "Error reading Netbeans location information" );
            }
        //catch( FileNotFoundException fnfe ) {
        //System.out.println( "NetBeans ide.log FileNotFoundException" );
        //}

        return installPath;
    }


    private static String[] getJeditInstallation( File logFile ) {
        String[] jeditDetails = new String[2];
        try {
        BufferedReader reader = new BufferedReader(new FileReader(logFile));
        String installPath = "";
        String version = "";

        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
        s.trim();
        if( s.indexOf( "jEdit home directory is" ) != -1 ) {
            int pathStart = new String( "[message] jEdit: jEdit home directory is " ).length();
            //System.out.println( "pathStart " + pathStart );
            installPath = s.substring( pathStart, s.length() ) +File.separator;
            System.out.println( "installPath 1" + installPath );
            //int pathEnd = installPath.indexOf( ";");
            //System.out.println( "pathEnd " + pathEnd );
            //installPath = installPath.substring( 0, pathEnd ) +File.separator;
            //System.out.println( "pathStart " + pathStart );
            //int pathEnd = s.indexOf( ";");
            //System.out.println( "pathEnd " + pathEnd );
            //System.out.println( "s is " + s + " and " + s.length() + " long" );
            //installPath = s.substring( pathStart, pathEnd - 1 );
            installPath.trim();
            //System.out.println( "installPath 2 " + installPath );
            //break;
            jeditDetails[0] = installPath;
        }
        if( s.indexOf( "jEdit: jEdit version" ) != -1 ) {
            int versionStart = s.indexOf( "version" ) + 8;
            System.out.println( "versionStart is: " + versionStart );
            version = s.substring( versionStart, s.length() );
            version.trim();
            System.out.println( "jEdit version is: " + version );
            jeditDetails[1] = version;
        }
        }
        reader.close();
        }
        catch( IOException ioe ) {
        System.out.println( "Error reading Jedit location information" );
            }
        //catch( FileNotFoundException fnfe ) {
        //System.out.println( "Jedit activity.log FileNotFoundException" );
        //}
        return jeditDetails;
    }



    public static File findVersionFile(File start)
    {
        File versionFile = null;

        File files[] = start.listFiles(new VersionFilter());
        if (files.length == 0)
        {
            File dirs[] = start.listFiles(new DirFilter());
            for (int i=0; i< dirs.length; i++)
            {
                versionFile = findVersionFile(dirs[i]);
                if (versionFile != null)
                {
                    break;
                }
            }
        }
        else
        {
            versionFile = files[0];
        }

        return versionFile;
    }

    public static boolean verifySversionExists(File sversionFile) {
        if (!sversionFile.exists())
            return false;
        return true;
    }

    public static Properties getOfficeVersions(File sversionFile) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(sversionFile));
        String sectionName = null;
        Properties results = new Properties();

        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
            s.trim();
            //System.out.println(s);
            if (s.length() == 0)
                continue;
            if (s.charAt(0) == '[') {
                sectionName = s.substring(1, s.length() - 1);
                //System.out.println(sectionName);
                continue;
            }
            if ((sectionName != null) && sectionName.equalsIgnoreCase("Versions")) {
                int equals = s.indexOf( "=" );
        String officeName = s.substring(0, equals );

        String instPath = s.substring(equals + 8, s.length());
        String [] parts = new String[2];
        parts[0] = officeName;
        parts[1] = instPath + File.separator;
        //System.out.println( "InstUtil officeName " + officeName );
        //System.out.println( "InstUtil instPath " + instPath );

        //String [] parts = s.split("=");
                if (parts.length == 2) {
                    //ver.version = parts[0].trim();
                    //File f = new File(parts[1].trim());
            //results.put(parts[0].trim(), f.getPath());
            try {
                        URL url = new URL("file://" + parts[1].trim());
            String opSys =System.getProperty("os.name");
            if (opSys.indexOf("Windows")!=-1){
                String windowsPath = URLDecoder.decode( url.getPath() );
                boolean firstSlash = true;
                while( windowsPath.indexOf("/") != -1 ) {
                    int forwardSlashPos = windowsPath.indexOf("/");
                    String firstPart = windowsPath.substring( 0, forwardSlashPos );
                    String lastPart = windowsPath.substring( forwardSlashPos + 1, windowsPath.length() );
                    if( firstSlash ) {
                        windowsPath = lastPart;
                        firstSlash = false;
                    }
                    else {
                        windowsPath = firstPart + "\\" + lastPart;
                    }
                }
                int lastSlash = windowsPath.lastIndexOf("\\");
                windowsPath = windowsPath.substring( 0, lastSlash );
                results.put( parts[0].trim(), windowsPath );
            }
            else {
                //System.err.println( " InstUtil URLDecoder " + URLDecoder.decode(url.getPath()) );
                results.put(parts[0].trim(), URLDecoder.decode(url.getPath()));
            }
                        //File f = new File(url);

            //.sversion: OpenOffice.org 643=file:///scriptdev/neil/ScriptFrameOpenoffice1.0.1
            // parts = Installation name. f.getPath = Installation path
                        //results.put(parts[0].trim(), f.getPath());

                        //results.put(parts[0].trim(), URLDecoder.decode(url.getPath()));
            //results.put( parts[0].trim(), windowsPath );

                    }
                    catch (MalformedURLException eSyntax) {
                        //throw new IOException("Error while reading version information");
            results.put(parts[0].trim(), parts[1].trim());
            //System.out.println(parts[0].trim() + " : " + parts[1].trim());
            System.err.println("GotHereException");
                    }
                }
                else {
                    System.out.println("not splitting on equals");
                }
            }
        }
        reader.close();
        return results;
    }

    public static String getJavaVersion() {
        return System.getProperty("java.version");
    }

    public static boolean isCorrectJavaVersion() {
        if (System.getProperty("java.version").startsWith("1.4"))
            return true;
        return false;
    }

    public static void main(String args[]) {
        InstUtil inst = new InstUtil();
        File f = null;
        try
        {
            f = InstUtil.buildSversionLocation();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        if (!InstUtil.verifySversionExists(f)) {
            System.err.println("Problem with sversion.ini");
        }
        try {
            InstUtil.getOfficeVersions(f);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println(e);
        }
        System.out.println(InstUtil.getJavaVersion());
        if (!InstUtil.isCorrectJavaVersion()) {
            System.err.println("Not correct Java Version");
        }
    }

    public static final String [] versions = {"NetBeans 3.4", "jEdit 4.0.3", "jEdit 4.1pre5" };
    private static File tmpDir = null;
}



class DirFilter implements java.io.FileFilter
{
    public boolean accept(File aFile)
    {
        return aFile.isDirectory();
    }
}
class VersionFilter implements java.io.FileFilter
{
    public boolean accept(File aFile)
    {
        if (aFile.getName().compareToIgnoreCase("sversion.ini") == 0)
        {
            return true;
        }

        return false;
    }
}