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