// Path: src/main/java/at/favre/tools/uberadb/util/CmdUtil.java
// public final class CmdUtil {
// 
//     private CmdUtil() {
//     }
// 
//     public static <T> T[] concat(T[] first, T[] second) {
//         T[] result = Arrays.copyOf(first, first.length + second.length);
//         System.arraycopy(second, 0, result, first.length, second.length);
//         return result;
//     }
// 
//     public static File checkAndGetFromPATHEnvVar(CmdProvider provider, final String matchesExecutable) {
//         String pathEnv = System.getenv("PATH");
//         if (pathEnv != null) {
//             String[] pathParts = pathEnv.split(File.pathSeparator);
//             for (String pathPart : pathParts) {
//                 File pathFile = new File(pathPart);
// 
//                 if (pathFile.isFile() && pathFile.getName().toLowerCase().contains(matchesExecutable)) {
//                     return pathFile;
//                 } else if (pathFile.isDirectory()) {
//                     File[] matchedFiles = pathFile.listFiles(new FileFilter() {
//                         @Override
//                         public boolean accept(File pathname) {
//                             return FileUtil.getFileNameWithoutExtension(pathname).toLowerCase().equals(matchesExecutable);
//                         }
//                     });
// 
//                     if (matchedFiles != null) {
//                         for (File matchedFile : matchedFiles) {
//                             if (provider.canRunCmd(new String[]{matchedFile.getAbsolutePath()})) {
//                                 return matchedFile;
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//         return null;
//     }
// 
//     public static OS getOsType() {
//         String osName = System.getProperty("os.name").toLowerCase();
// 
//         if (osName.contains("win")) {
//             return OS.WIN;
//         }
//         if (osName.contains("mac")) {
//             return OS.MAC;
//         }
// 
//         return OS._NIX;
//     }
// 
//     public enum OS {
//         WIN, MAC, _NIX
//     }
// 
//     public static String concat(String[] array, String separator) {
//         StringBuilder sb = new StringBuilder();
//         String sep = "";
//         for (String s : array) {
//             sb.append(sep).append(s);
//             sep = separator;
//         }
//         return sb.toString();
//     }
// 
//     public static String jarVersion() {
//         return AdbTool.class.getPackage().getImplementationVersion();
//     }
// }


import at.favre.tools.uberadb.util.CmdUtil;
import java.io.File;
/*
 *
 *  *  Copyright 2016 Patrick Favre-Bulle
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *
 */

package at.favre.tools.uberadb;



public class AdbLocationFinderImpl implements AdbLocationFinder {

    private static final String WIN_DEFAULT_SDK = "\\AppData\\Local\\Android\\sdk\\platform-tools\\adb.exe";
    private static final String WIN_DEFAULT_ANDROID_HOME = "\\platform-tools\\adb.exe";

    private static final String[] MAC_DEFAULT = new String[]{"/usr/local/opt/android-sdk/platform-tools/adb"};
    private static final String MAC_DEFAULT_HOME = "/Library/Android/sdk/platform-tools/adb";
    private static final String MAC_DEFAULT_ANDROID_HOME = "/platform-tools/adb";

    private static final String[] LINUX_DEFAULT = MAC_DEFAULT;
    private static final String LINUX_DEFAULT_ANDROID_HOME = MAC_DEFAULT_ANDROID_HOME;
    private static final String LINUX_DEFAULT_2 = "/Android/Sdk/platform-tools/adb";

    @Override
    public LocationResult find(CmdProvider cmdProvider, String customPath) {
        String osName = System.getProperty("os.name").toLowerCase();

        if (customPath != null && new File(customPath).exists() && cmdProvider.canRunCmd(new String[]{customPath})) {
            return new LocationResult(Location.CUSTOM, new String[]{customPath});
        }

// Path: src/main/java/at/favre/tools/uberadb/util/CmdUtil.java
// public final class CmdUtil {
// 
//     private CmdUtil() {
//     }
// 
//     public static <T> T[] concat(T[] first, T[] second) {
//         T[] result = Arrays.copyOf(first, first.length + second.length);
//         System.arraycopy(second, 0, result, first.length, second.length);
//         return result;
//     }
// 
//     public static File checkAndGetFromPATHEnvVar(CmdProvider provider, final String matchesExecutable) {
//         String pathEnv = System.getenv("PATH");
//         if (pathEnv != null) {
//             String[] pathParts = pathEnv.split(File.pathSeparator);
//             for (String pathPart : pathParts) {
//                 File pathFile = new File(pathPart);
// 
//                 if (pathFile.isFile() && pathFile.getName().toLowerCase().contains(matchesExecutable)) {
//                     return pathFile;
//                 } else if (pathFile.isDirectory()) {
//                     File[] matchedFiles = pathFile.listFiles(new FileFilter() {
//                         @Override
//                         public boolean accept(File pathname) {
//                             return FileUtil.getFileNameWithoutExtension(pathname).toLowerCase().equals(matchesExecutable);
//                         }
//                     });
// 
//                     if (matchedFiles != null) {
//                         for (File matchedFile : matchedFiles) {
//                             if (provider.canRunCmd(new String[]{matchedFile.getAbsolutePath()})) {
//                                 return matchedFile;
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//         return null;
//     }
// 
//     public static OS getOsType() {
//         String osName = System.getProperty("os.name").toLowerCase();
// 
//         if (osName.contains("win")) {
//             return OS.WIN;
//         }
//         if (osName.contains("mac")) {
//             return OS.MAC;
//         }
// 
//         return OS._NIX;
//     }
// 
//     public enum OS {
//         WIN, MAC, _NIX
//     }
// 
//     public static String concat(String[] array, String separator) {
//         StringBuilder sb = new StringBuilder();
//         String sep = "";
//         for (String s : array) {
//             sb.append(sep).append(s);
//             sep = separator;
//         }
//         return sb.toString();
//     }
// 
//     public static String jarVersion() {
//         return AdbTool.class.getPackage().getImplementationVersion();
//     }
// }


// Path: src/main/java/at/favre/tools/uberadb/AdbLocationFinderImpl.java
import at.favre.tools.uberadb.util.CmdUtil;
import java.io.File;

/*
 *
 *  *  Copyright 2016 Patrick Favre-Bulle
 *  *
 *  *  Licensed under the Apache License, Version 2.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS,
 *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *  See the License for the specific language governing permissions and
 *  *  limitations under the License.
 *
 */

package at.favre.tools.uberadb;



public class AdbLocationFinderImpl implements AdbLocationFinder {

    private static final String WIN_DEFAULT_SDK = "\\AppData\\Local\\Android\\sdk\\platform-tools\\adb.exe";
    private static final String WIN_DEFAULT_ANDROID_HOME = "\\platform-tools\\adb.exe";

    private static final String[] MAC_DEFAULT = new String[]{"/usr/local/opt/android-sdk/platform-tools/adb"};
    private static final String MAC_DEFAULT_HOME = "/Library/Android/sdk/platform-tools/adb";
    private static final String MAC_DEFAULT_ANDROID_HOME = "/platform-tools/adb";

    private static final String[] LINUX_DEFAULT = MAC_DEFAULT;
    private static final String LINUX_DEFAULT_ANDROID_HOME = MAC_DEFAULT_ANDROID_HOME;
    private static final String LINUX_DEFAULT_2 = "/Android/Sdk/platform-tools/adb";

    @Override
    public LocationResult find(CmdProvider cmdProvider, String customPath) {
        String osName = System.getProperty("os.name").toLowerCase();

        if (customPath != null && new File(customPath).exists() && cmdProvider.canRunCmd(new String[]{customPath})) {
            return new LocationResult(Location.CUSTOM, new String[]{customPath});
        }

        File pathAdbExe = CmdUtil.checkAndGetFromPATHEnvVar(cmdProvider, "adb");