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 = null;
    if ((path = checkPath(System.getenv("OPERA_PATH"))) != null) return path;
    Platform platform = Platform.getCurrent();

    if (platform.is(Platform.UNIX)) {
      // TODO run `which` to see if we can get the Opera location
    }

    if (platform.is(Platform.LINUX)) {
      if ((path = checkPath("/usr/bin/opera")) != null) return path;

    } else if (platform.is(Platform.WINDOWS)) {
      String progfiles = System.getenv("PROGRAMFILES");
      if (progfiles == null) progfiles = "\\Program Files";

      if ((path = checkPath(progfiles + "\\Opera\\opera.exe")) != null) return path;

    } else if (platform.is(Platform.MAC)) {
      if ((path = checkPath("/Applications/Opera.app")) != null) 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;
  }

}

@@ -45,75 +45,220 @@
 .io.
-File;%0A%0Aimport org.openqa.selenium.Platform;%0A%0A/**%0A * @author stuartk
+BufferedReader;%0Aimport java.io.File;%0Aimport java.io.InputStreamReader;%0A%0Aimport org.openqa.selenium.Platform;%0A%0A/**%0A * @author stuartk%0A * This class tries to find the paths to Opera and Opera Launcher on any system
 %0A */
@@ -944,67 +944,77 @@
     
-// TODO run %60which%60 to see if we can get the Opera location
+if ((path = checkPath(execProg(%22which opera%22))) != null) return path;
 %0A   
@@ -2002,40 +2002,103 @@
 ath 
-!
+=
 = null
- && new File(path).exists
+) return null;%0A%0A    File file = new File(path);%0A    if (file.exists() && file.canExecute
 ()) 
@@ -2114,17 +2114,13 @@
 th;%0A
+%0A
     
-else 
 retu
@@ -2122,21 +2122,470 @@
  return null
+;%0A  %7D%0A%0A  private String execProg(String cmd) %7B%0A    String output = %22%22;%0A    try %7B%0A      Process process = Runtime.getRuntime().exec(cmd);%0A%0A      BufferedReader out= new BufferedReader(new InputStreamReader(process%0A          .getInputStream()));%0A%0A      String line = null;%0A%0A      while ((line = out.readLine()) != null) %7B%0A        output += line;%0A      %7D%0A%0A      process.waitFor();%0A    %7D catch (Exception e) %7B%0A      return null;%0A    %7D%0A%0A    return output
 ;%0A  %7D%0A%0A%7D%0A

Add code to execute /usr/bin/opera