package com.opera.core.systems;

import java.io.File;

import org.openqa.selenium.Platform;

/**
 * @author stuartk
 */
public class OperaPaths {

  /**
   * This method will try and find Opera on any system. It takes the following
   * steps:
   *  1. Check the environment variable "OPERA_PATH". If it exists, and the
   *     file it points to exists, then return
   *  2. (Unix) Call `which opera` to find the location
   *  3. Check if Opera exists at the default location on the respective OS
   *  4. Give up and return null
   * @return The path to Opera, or null
   */
  public String operaPath() {
    String path;
    if ((path = System.getenv("OPERA_PATH")) != null) return path;
    
    if (Platform.getCurrent().is(Platform.LINUX)) {
      // TODO run which to see if we can get the Opera location
      
      // Check the default location
      path = "/usr/bin/opera";     
    }
    else if (Platform.getCurrent().is(Platform.WINDOWS)) {
      String progfiles = System.getenv("PROGRAMFILES");
      if (progfiles == null) progfiles = "\\Program Files";
      
      path = progfiles + "\\Opera\\opera.exe";  
    }
    
    if (new File(path).exists()) return path;    
    return null;
  }

  /**
   * This method will try and find Opera Launcher on any system.
   * @return The path to Opera Launcher, or null
   */
  public String launcherPath() {
    return "/home/stuart/bin/opera-launcher";
  }

  /**
   * Check that the given path is not null, and exists.
   * @param path The path to check.
   * @return null if the path is null, or does not exist. Otherwise it returns
   * the given path.
   */
  private String checkPath(String path) {
    if (path != null && new File(path).exists()) return path;
    else return null;
  }

}

@@ -612,24 +612,31 @@
  String path
+ = null
 ;%0A    if ((p
@@ -641,16 +641,26 @@
 (path = 
+checkPath(
 System.g
@@ -679,16 +679,17 @@
 _PATH%22))
+)
  != null
@@ -707,25 +707,36 @@
 th;%0A    
-%0A    if (
+Platform platform = 
 Platform
@@ -740,32 +740,51 @@
 orm.getCurrent()
+;%0A%0A    if (platform
 .is(Platform.LIN
@@ -784,12 +784,11 @@
 orm.
-LINU
+UNI
 X)) 
@@ -807,21 +807,23 @@
 ODO run 
+%60
 which
+%60
  to see 
@@ -863,60 +863,78 @@
     
-  %0A      // Check the default location
+%7D%0A%0A    if (platform.is(Platform.LINUX)) %7B
 %0A      
+if ((
 path = 
+checkPath(
 %22/us
@@ -949,24 +949,39 @@
 era%22
-;     
+)) != null) return path;%0A
 %0A    %7D
-%0A   
  els
@@ -986,25 +986,25 @@
 lse if (
-P
+p
 latform.
 getCurre
@@ -999,21 +999,8 @@
 orm.
-getCurrent().
 is(P
@@ -1139,28 +1139,37 @@
 s%22;%0A
-      
 %0A      
+if ((
 path = 
+checkPath(
 prog
@@ -1200,54 +1200,141 @@
 exe%22
-;  %0A    %7D%0A    %0A    if (new File(path).exists()
+)) != null) return path;%0A%0A    %7D else if (platform.is(Platform.MAC)) %7B%0A      if ((path = checkPath(%22/Applications/Opera.app%22)) != null
 ) re
@@ -1343,20 +1343,24 @@
 rn path;
+%0A%0A
     
+%7D%0A
 %0A    ret

Tidy up and add Mac default location