1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package installer;
23 
24 import java.net.URLDecoder;
25 import java.io.*;
26 import java.util.*;
27 import java.util.zip.*;
28 import java.awt.*;
29 import java.awt.event.*;
30 import javax.swing.*;
31 import java.net.*;
32 
33 public class InstUtil {
34 
buildSversionLocation()35     public static File buildSversionLocation() throws IOException {
36 		File theFile = null;
37         StringBuffer str = new StringBuffer();
38         str.append(System.getProperty("user.home"));
39         str.append(File.separator);
40 		StringBuffer thePath = new StringBuffer(str.toString());
41 
42         String os = System.getProperty("os.name");
43 
44 		if (os.indexOf("Windows") != -1) {
45 			boolean bSVersionInHomeDir = new File(thePath.toString() + "sversion.ini").exists();
46 
47 			if (!bSVersionInHomeDir) {
48 				thePath.append("Application Data");
49 				thePath.append(File.separator);
50 			}
51 			theFile = findVersionFile(new File(thePath.toString()));
52         } else if (os.indexOf("SunOS") != -1) {
53             thePath.append(".sversionrc");
54 			theFile = new File(thePath.toString());
55         } else if (os.indexOf("Linux") != -1) {
56             thePath.append(".sversionrc");
57 			theFile = new File(thePath.toString());
58 		}
59 
60 		if (theFile == null)
61 		{
62 			throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?");
63 		}
64 		if  (!theFile.exists())
65 		{
66 			throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?");
67 		}
68         return theFile;
69     }
70 
71 
72 
hasNetbeansInstallation()73     public static boolean hasNetbeansInstallation() {
74         boolean result = false;
75         try
76         {
77             result = checkForSupportedVersion( getNetbeansLocation(), versions );
78 
79             if (result == false)
80                 System.out.println("No supported version of NetBeans found.");
81         }
82         catch ( IOException ioe )
83         {
84             System.err.println("Exception caught trying to determine netbeans installation: " + ioe );
85             ioe.printStackTrace();
86             result = false;
87         }
88         return result;
89     }
90 
checkForSupportedVersion( Properties installs, String[] supportedVersions )91     private static boolean checkForSupportedVersion( Properties installs, String[] supportedVersions )
92     {
93         if ( installs != null )
94         {
95             for ( int index = 0; index < supportedVersions.length; index++ )
96             {
97                 String key = supportedVersions[ index ];
98                 String path = null;
99                 if ( ( path = installs.getProperty(key) ) != null )
100                 {
101                     // at least one supported version for netbeans present, so return;
102                     return true;
103                 }
104 
105             }
106         }
107         return false;
108     }
109 
110 
hasJeditInstallation()111     public static boolean hasJeditInstallation() {
112         boolean result = false;
113         try
114         {
115             result = checkForSupportedVersion( getJeditLocation(), versions );
116             if ( !result )
117             {
118                 System.out.println("No supported version for JEdit found.");
119             }
120         }
121         catch ( IOException ioe )
122         {
123             System.err.println("Exception caught trying to determine jedit installation: " + ioe );
124             ioe.printStackTrace();
125             result = false;
126         }
127         return result;
128     }
129 
130 
131 
getNetbeansLocation()132     public static Properties getNetbeansLocation() throws IOException {
133 	File theFile = null;
134 	Properties results = new Properties();
135 
136 	StringBuffer str = new StringBuffer();
137         str.append(System.getProperty("user.home"));
138         str.append(File.separator);
139 	StringBuffer thePath = new StringBuffer(str.toString());
140 
141         String os = System.getProperty("os.name");
142 
143 	if (os.indexOf("Windows") != -1) {
144 		//theFile = findVersionFile(new File(str.toString()));
145 		thePath.append(".netbeans");
146 		//theFile = new File(thePath.toString());
147         } else if (os.indexOf("SunOS") != -1) {
148 		thePath.append(".netbeans");
149 		//theFile = new File(thePath.toString());
150         } else if (os.indexOf("Linux") != -1) {
151 		thePath.append(".netbeans");
152 		//theFile = new File(thePath.toString());
153 	}
154 
155 	if ( thePath.toString().indexOf( ".netbeans" ) == -1 )
156 		return null;
157 	else if ( new File( thePath.append( File.separator+"3.4"+File.separator ).toString() ).isDirectory() ) {
158 
159 		System.out.println( "Found NetBeans 3.4 user directory: " + thePath );
160 		File netbeansLogFile = new File( thePath.toString() + File.separator + "system" + File.separator + "ide.log" );
161 		if( netbeansLogFile.exists() ) {
162 			String installPath = getNetbeansInstallation( netbeansLogFile );
163 			File f = new File(installPath);
164 			results.put("NetBeans 3.4", f.getPath()+File.separator);
165 			System.out.println( "NetBeans Installation directory: " + f.getPath());
166 		}
167 		else {
168 			System.out.println( "No NetBeans log file found" );
169                         return null;
170 		}
171 	}
172         else
173         {
174 	    System.out.println( "No NetBeans user directory found" );
175 	    return null;
176         }
177 
178 
179 	return results;
180     }
181 
182 
183 
getJeditLocation()184     public static Properties getJeditLocation() throws IOException {
185 
186 	/*if( !hasJeditInstallation() ) {
187 		System.out.println( "No Jedit found (line195 InstUtil");
188 		return null;
189 	}*/
190 
191 	File theFile = null;
192 	Properties results = new Properties();
193 
194 	StringBuffer str = new StringBuffer();
195         str.append(System.getProperty("user.home"));
196         str.append(File.separator);
197 	StringBuffer thePath = new StringBuffer(str.toString());
198 
199         String os = System.getProperty("os.name");
200         thePath.append(".jedit");
201 	//System.out.println( ".jedit path " + thePath );
202 
203 	File jeditLogFile = new File( thePath.toString() + File.separator + "activity.log" );
204 	if( jeditLogFile.exists() ) {
205 		String[] jeditDetails = getJeditInstallation( jeditLogFile );
206 		System.out.println( "getJeditLocation ) " + jeditDetails[0] );
207 		File f = new File(jeditDetails[0]);
208 		results.put("jEdit "+jeditDetails[1], jeditDetails[0]);
209 		System.out.println( "jeditDetails[0] is " + jeditDetails[0]);
210 	}
211 	else {
212 		System.out.println( "Prompt user for Jedit installation path" );
213 	}
214 
215 
216 	return results;
217     }
218 
219 
220 
221 
222 
getNetbeansInstallation( File logFile )223     private static String getNetbeansInstallation( File logFile ) {
224 	    String installPath = "";
225 	    try {
226 	    BufferedReader reader = new BufferedReader(new FileReader(logFile));
227 
228 	    for (String s = reader.readLine(); s != null; s = reader.readLine()) {
229 		s.trim();
230 		if( s.indexOf( "IDE Install" ) != -1 ) {
231 			int pathStart = s.indexOf( "=" ) + 2;
232 			//System.out.println( "pathStart " + pathStart );
233 			installPath = s.substring( pathStart, s.length() );
234 			//System.out.println( "installPath 1" + installPath );
235 			int pathEnd = installPath.indexOf( ";");
236 			//System.out.println( "pathEnd " + pathEnd );
237 			installPath = installPath.substring( 0, pathEnd ) +File.separator;
238 			//System.out.println( "pathStart " + pathStart );
239 			//int pathEnd = s.indexOf( ";");
240 			//System.out.println( "pathEnd " + pathEnd );
241 			//System.out.println( "s is " + s + " and " + s.length() + " long" );
242 			//installPath = s.substring( pathStart, pathEnd - 1 );
243 			installPath.trim();
244 			break;
245 		}
246 	    }
247 	    }
248 	    catch( IOException ioe ) {
249 		System.out.println( "Error reading Netbeans location information" );
250             }
251 	    //catch( FileNotFoundException fnfe ) {
252 		//System.out.println( "NetBeans ide.log FileNotFoundException" );
253 	    //}
254 
255 	    return installPath;
256     }
257 
258 
getJeditInstallation( File logFile )259     private static String[] getJeditInstallation( File logFile ) {
260 	    String[] jeditDetails = new String[2];
261 	    try {
262 	    BufferedReader reader = new BufferedReader(new FileReader(logFile));
263 	    String installPath = "";
264 	    String version = "";
265 
266 	    for (String s = reader.readLine(); s != null; s = reader.readLine()) {
267 		s.trim();
268 		if( s.indexOf( "jEdit home directory is" ) != -1 ) {
269 			int pathStart = new String( "[message] jEdit: jEdit home directory is " ).length();
270 			//System.out.println( "pathStart " + pathStart );
271 			installPath = s.substring( pathStart, s.length() ) +File.separator;
272 			System.out.println( "installPath 1" + installPath );
273 			//int pathEnd = installPath.indexOf( ";");
274 			//System.out.println( "pathEnd " + pathEnd );
275 			//installPath = installPath.substring( 0, pathEnd ) +File.separator;
276 			//System.out.println( "pathStart " + pathStart );
277 			//int pathEnd = s.indexOf( ";");
278 			//System.out.println( "pathEnd " + pathEnd );
279 			//System.out.println( "s is " + s + " and " + s.length() + " long" );
280 			//installPath = s.substring( pathStart, pathEnd - 1 );
281 			installPath.trim();
282 			//System.out.println( "installPath 2 " + installPath );
283 			//break;
284 			jeditDetails[0] = installPath;
285 		}
286 		if( s.indexOf( "jEdit: jEdit version" ) != -1 ) {
287 			int versionStart = s.indexOf( "version" ) + 8;
288 			System.out.println( "versionStart is: " + versionStart );
289 			version = s.substring( versionStart, s.length() );
290 			version.trim();
291 			System.out.println( "jEdit version is: " + version );
292 			jeditDetails[1] = version;
293 		}
294 	    }
295 	    }
296 	    catch( IOException ioe ) {
297 		System.out.println( "Error reading Jedit location information" );
298             }
299 	    //catch( FileNotFoundException fnfe ) {
300 		//System.out.println( "Jedit activity.log FileNotFoundException" );
301 	    //}
302 
303 	    return jeditDetails;
304     }
305 
306 
307 
findVersionFile(File start)308 	public static File findVersionFile(File start)
309 	{
310 		File versionFile = null;
311 
312 		File files[] = start.listFiles(new VersionFilter());
313 		if (files.length == 0)
314 		{
315 			File dirs[] = start.listFiles(new DirFilter());
316 			for (int i=0; i< dirs.length; i++)
317 			{
318 				versionFile = findVersionFile(dirs[i]);
319 				if (versionFile != null)
320 				{
321 					break;
322 				}
323 			}
324 		}
325 		else
326 		{
327 			versionFile = files[0];
328 		}
329 
330 		return versionFile;
331 	}
332 
verifySversionExists(File sversionFile)333     public static boolean verifySversionExists(File sversionFile) {
334         if (!sversionFile.exists())
335             return false;
336         return true;
337     }
338 
getOfficeVersions(File sversionFile)339     public static Properties getOfficeVersions(File sversionFile) throws IOException {
340         BufferedReader reader = new BufferedReader(new FileReader(sversionFile));
341         Vector values;
342         String sectionName = null;
343         Properties results = new Properties();
344 
345         for (String s = reader.readLine(); s != null; s = reader.readLine()) {
346             s.trim();
347             //System.out.println(s);
348             if (s.length() == 0)
349                 continue;
350             if (s.charAt(0) == '[') {
351                 sectionName = s.substring(1, s.length() - 1);
352                 //System.out.println(sectionName);
353                 continue;
354             }
355             if ((sectionName != null) && sectionName.equalsIgnoreCase("Versions")) {
356                 int equals = s.indexOf( "=" );
357 		String officeName = s.substring(0, equals );
358 
359 		String instPath = s.substring(equals + 8, s.length());
360 		String [] parts = new String[2];
361 		parts[0] = officeName;
362 		parts[1] = instPath + File.separator;
363 		//System.out.println( "InstUtil officeName " + officeName );
364 		//System.out.println( "InstUtil instPath " + instPath );
365 
366 		//String [] parts = s.split("=");
367                 if (parts.length == 2) {
368                     //ver.version = parts[0].trim();
369                     //File f = new File(parts[1].trim());
370 		    //results.put(parts[0].trim(), f.getPath());
371 		    try {
372                         URL url = new URL("file://" + parts[1].trim());
373 			String opSys =System.getProperty("os.name");
374 			if (opSys.indexOf("Windows")!=-1){
375 				String windowsPath = URLDecoder.decode( url.getPath() );
376 				boolean firstSlash = true;
377 				while( windowsPath.indexOf("/") != -1 ) {
378 					int forwardSlashPos = windowsPath.indexOf("/");
379 					String firstPart = windowsPath.substring( 0, forwardSlashPos );
380 					String lastPart = windowsPath.substring( forwardSlashPos + 1, windowsPath.length() );
381 					if( firstSlash ) {
382 						windowsPath = lastPart;
383 						firstSlash = false;
384 					}
385 					else {
386 						windowsPath = firstPart + "\\" + lastPart;
387 					}
388 				}
389 				int lastSlash = windowsPath.lastIndexOf("\\");
390 				windowsPath = windowsPath.substring( 0, lastSlash );
391 				results.put( parts[0].trim(), windowsPath );
392 			}
393 			else {
394 				//System.err.println( " InstUtil URLDecoder " + URLDecoder.decode(url.getPath()) );
395 				results.put(parts[0].trim(), URLDecoder.decode(url.getPath()));
396 			}
397                         //File f = new File(url);
398 
399 			//.sversion: OpenOffice.org 643=file:///scriptdev/neil/ScriptFrameOpenoffice1.0.1
400 			// parts = Installation name. f.getPath = Installation path
401                         //results.put(parts[0].trim(), f.getPath());
402 
403                         //results.put(parts[0].trim(), URLDecoder.decode(url.getPath()));
404 			//results.put( parts[0].trim(), windowsPath );
405 
406                     }
407                     catch (MalformedURLException eSyntax) {
408                         //throw new IOException("Error while reading version information");
409 			results.put(parts[0].trim(), parts[1].trim());
410 			//System.out.println(parts[0].trim() + " : " + parts[1].trim());
411 			System.err.println("GotHereException");
412                     }
413                 }
414                 else {
415                     System.out.println("not splitting on equals");
416                 }
417             }
418         }
419 
420         return results;
421     }
422 
getJavaVersion()423     public static String getJavaVersion() {
424         return System.getProperty("java.version");
425     }
426 
isCorrectJavaVersion()427     public static boolean isCorrectJavaVersion() {
428         if (System.getProperty("java.version").startsWith("1.4"))
429             return true;
430         return false;
431     }
432 
main(String args[])433     public static void main(String args[]) {
434         InstUtil inst = new InstUtil();
435 		File f = null;
436         try
437 		{
438 			f = inst.buildSversionLocation();
439 		}
440 		catch (IOException e)
441 		{
442 			e.printStackTrace();
443 			System.out.println(e.getMessage());
444 		}
445         if (!inst.verifySversionExists(f)) {
446             System.err.println("Problem with sversion.ini");
447         }
448         try {
449             Properties vers = inst.getOfficeVersions(f);
450         } catch (IOException e) {
451             e.printStackTrace();
452             System.err.println(e);
453         }
454         System.out.println(inst.getJavaVersion());
455         if (!inst.isCorrectJavaVersion()) {
456             System.err.println("Not correct Java Version");
457         }
458     }
459 
460     public static final String [] versions = {"NetBeans 3.4", "jEdit 4.0.3", "jEdit 4.1pre5" };
461     private static File tmpDir = null;
462 }
463 
464 
465 
466 class DirFilter implements java.io.FileFilter
467 {
accept(File aFile)468 	public boolean accept(File aFile)
469 	{
470 		return aFile.isDirectory();
471 	}
472 }
473 class VersionFilter implements java.io.FileFilter
474 {
accept(File aFile)475 	public boolean accept(File aFile)
476 	{
477 		if (aFile.getName().compareToIgnoreCase("sversion.ini") == 0)
478 		{
479 			return true;
480 		}
481 
482 		return false;
483 	}
484 }
485