package com.dw.lib.test;

import com.dw.lib.EnvironmentHelper;
import com.dw.lib.FileUtils;
import java.util.*;

public class WhereCLI {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			if (args.length != 1){
				System.out.println("List all execute file within PATH environment");
				System.out.println("jwhere <execute file>");
				return;
			}
			
			String pathSplitter = "/";
			String envSplitter = ":";
			
			if (EnvironmentHelper.getOSType() == EnvironmentHelper.OSType.WIN) {
				pathSplitter = "\\";
				envSplitter = ";";
			}
			
			String path = EnvironmentHelper.getEnv("PATH");
			String[] paths = path.split(envSplitter);
			String executeFile = args[0];
			FileUtils utils = new FileUtils();
			
			List<String> result = new ArrayList<String>();
			
			for(String p : paths) {
				if(p.endsWith(pathSplitter)) {
					p = p.substring(0, p.length() - 2);
				}
				String tempFullPath = p + pathSplitter + executeFile;
				if(utils.isExisted(tempFullPath) && utils.isFile(tempFullPath)) {
					if(!result.contains(tempFullPath))
						result.add(tempFullPath);
				}
			}
			
			for (String s: result) {
				System.out.println(s);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

@@ -394,186 +394,295 @@
 %0A%09%09%09
-String pathSplitter = %22/%22;%0A%09%09%09String envSplitter = %22:%22;%0A%09%09%09%0A%09%09%09if (EnvironmentHelper.getOSType() == EnvironmentHelper.OSType.WIN) %7B%0A%09%09%09%09pathSplitter = %22%5C%5C%22;%0A%09%09%09%09envSplitter = %22;%22
+boolean isWindows = EnvironmentHelper.getOSType() == EnvironmentHelper.OSType.WIN;%0A%09%09%09String pathSplitter = %22/%22;%0A%09%09%09String envSplitter = %22:%22;%0A%09%09%09String pathExt = %22%22;%0A%09%09%09%0A%09%09%09if (isWindows) %7B%0A%09%09%09%09pathSplitter = %22%5C%5C%22;%0A%09%09%09%09envSplitter = %22;%22;%0A%09%09%09%09pathExt = EnvironmentHelper.getEnv(%22PATHEXT%22)
 ;%0A%09%09
@@ -1228,16 +1228,321 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%09%0A%09%09%09%09if(isWindows) %7B%0A%09%09%09%09%09for(String ext : pathExt.split(envSplitter)) %7B%0A%09%09%09%09%09%09tempFullPath = p + pathSplitter + executeFile + ext;%0A%09%09%09%09%09%09if(utils.isExisted(tempFullPath) && utils.isFile(tempFullPath)) %7B%0A%09%09%09%09%09%09%09if(!result.contains(tempFullPath))%0A%09%09%09%09%09%09%09%09result.add(tempFullPath);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09

add windows ext