package org.jerrymouse.gaes.mavenPlugin.base;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Path;

import com.google.appengine.repackaged.com.google.protobuf.ByteString.Output;

public class MvnCommond extends Thread {
	List<String> args;
	File baseDir;
	PrintStream outputStream;
	String L = "/";
	String W = "\\";

	public MvnCommond(List<String> args, File baseDir, PrintStream outputStream) {
		super();
		this.args = args;
		this.baseDir = baseDir;
		this.outputStream = outputStream;
	}

	@Override
	public void run() {
		exec();
	}

	public void exec() {
		if (getM2HOME() == null) {
			execCmd();
		} else {
			execAnt();
		}
	}

	private void execCmd() {
		args.add(0, getMvnCommond());
		String[] argArray = new String[args.size()];
		args.toArray(argArray);
		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(argArray, null, baseDir);
			InputStream stderr = proc.getInputStream();
			BufferedReader br = new BufferedReader(
					new InputStreamReader(stderr));
			String line = null;
			while ((line = br.readLine()) != null)
				System.out.println(line);
			int exitVal = proc.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void execAnt() {
		Java java = new Java();
		Project project = new Project();
		DefaultLogger consoleLogger = new DefaultLogger();
		consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
		consoleLogger.setOutputPrintStream(outputStream);
		project.addBuildListener(consoleLogger);
		java.setProject(project);
		java.setTaskName("common cloud");
		File home = new File(getM2HOME() + "/boot");
		File pathJar = home.listFiles()[0];
		Path path = new Path(project,pathJar.getAbsolutePath());
		java.setClasspath(path);
		java.setFork(true);
		java.setFailonerror(true);
		java.createJvmarg().setLine(
				"-Dclassworlds.conf=" + getM2HOME() + getS() + "bin" + getS()
						+ "m2.conf");
		java.createJvmarg().setLine("-Dmaven.home=" + getM2HOME());
		java.setClassname("org.codehaus.classworlds.Launcher");
		for (String arg : args) {
			java.createArg().setLine(arg);
		}
		java.setDir(baseDir);
		System.out.println(java.getCommandLine());
		java.execute();
	}

	private static String getMvnCommond() {
		if (whichOS().startsWith(Windows))
			return "mvn.bat";
		else if (whichOS().equals(LINUX))
			return "mvn";
		return "mvn";
	}

	public static String whichOS() {
		String osName = System.getProperty("os.name");
		return osName;
	}

	public static String LINUX = "Linux";
	public static String Windows = "Windows";

	private static String getM2HOME() {
		return System.getenv("M2_HOME");
	}

	private String getS() {
		if (whichOS().startsWith(LINUX))
			return L;
		else if (whichOS().startsWith(Windows))
			return W;
		return L;
	}
}
