// Path: src/com/thoughtworks/calabash/android/CalabashLogger.java
// public static void error(Object message) {
//     if (shouldLog && isNotEmpty(message))
//         log.error(message);
// }
// 
// Path: src/com/thoughtworks/calabash/android/CalabashLogger.java
// public static void info(Object message) {
//     if (shouldLog && isNotEmpty(message)) {
//         log.info(message);
//     }
// }


import org.joda.time.DateTime;
import org.jruby.RubyArray;
import org.jruby.RubyHash;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;
import java.io.File;
import java.io.FileFilter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.thoughtworks.calabash.android.CalabashLogger.error;
import static com.thoughtworks.calabash.android.CalabashLogger.info;
import static java.io.File.separator;
import static java.lang.String.format;

    private File getCalabashGemDirectory() throws CalabashException {
        File[] calabashGemPath = gemsDir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.isDirectory() && pathname.getName().startsWith("calabash-android");
            }
        });

        if (calabashGemPath.length == 0)
            throw new CalabashException(format("Error finding 'calabash-android' in the gempath : %s", gemsDir.getAbsolutePath()));
        if (calabashGemPath.length > 1)
            throw new CalabashException(format("Multiple matches for 'calabash-android' in the gempath : %s", gemsDir.getAbsolutePath()));
        return new File(calabashGemPath[0], "bin");
    }

    public RubyArray query(String query, String... args) throws CalabashException {
        ensureNotDisposed();
        try {
            info("Executing query - %s", query);
            container.put(QUERY_STRING, query);
            container.put(QUERY_ARGS, args);

            RubyArray queryResults = null;
            if (args != null && args.length > 0)
                queryResults = (RubyArray) container.runScriptlet(String.format("query(%s, *%s)", QUERY_STRING, QUERY_ARGS));
            else
                queryResults = (RubyArray) container.runScriptlet(String.format("query(%s)", QUERY_STRING));

            return queryResults;
        } catch (Exception e) {
// Path: src/com/thoughtworks/calabash/android/CalabashLogger.java
// public static void error(Object message) {
//     if (shouldLog && isNotEmpty(message))
//         log.error(message);
// }
// 
// Path: src/com/thoughtworks/calabash/android/CalabashLogger.java
// public static void info(Object message) {
//     if (shouldLog && isNotEmpty(message)) {
//         log.info(message);
//     }
// }


// Path: src/com/thoughtworks/calabash/android/CalabashWrapper.java
import org.joda.time.DateTime;
import org.jruby.RubyArray;
import org.jruby.RubyHash;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;
import java.io.File;
import java.io.FileFilter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.thoughtworks.calabash.android.CalabashLogger.error;
import static com.thoughtworks.calabash.android.CalabashLogger.info;
import static java.io.File.separator;
import static java.lang.String.format;


    private File getCalabashGemDirectory() throws CalabashException {
        File[] calabashGemPath = gemsDir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.isDirectory() && pathname.getName().startsWith("calabash-android");
            }
        });

        if (calabashGemPath.length == 0)
            throw new CalabashException(format("Error finding 'calabash-android' in the gempath : %s", gemsDir.getAbsolutePath()));
        if (calabashGemPath.length > 1)
            throw new CalabashException(format("Multiple matches for 'calabash-android' in the gempath : %s", gemsDir.getAbsolutePath()));
        return new File(calabashGemPath[0], "bin");
    }

    public RubyArray query(String query, String... args) throws CalabashException {
        ensureNotDisposed();
        try {
            info("Executing query - %s", query);
            container.put(QUERY_STRING, query);
            container.put(QUERY_ARGS, args);

            RubyArray queryResults = null;
            if (args != null && args.length > 0)
                queryResults = (RubyArray) container.runScriptlet(String.format("query(%s, *%s)", QUERY_STRING, QUERY_ARGS));
            else
                queryResults = (RubyArray) container.runScriptlet(String.format("query(%s)", QUERY_STRING));

            return queryResults;
        } catch (Exception e) {
            error("Execution of query: %s, failed", e, query);