package com.build;

import java.io.File;

import com.badlogic.gdx.jnigen.AntScriptGenerator;
import com.badlogic.gdx.jnigen.BuildConfig;
import com.badlogic.gdx.jnigen.BuildExecutor;
import com.badlogic.gdx.jnigen.BuildTarget;
import com.badlogic.gdx.jnigen.BuildTarget.TargetOs;
import com.badlogic.gdx.jnigen.parsing.RobustJavaMethodParser;
import com.badlogic.gdx.jnigen.NativeCodeGenerator;
import com.escplay.codegen.XpeCodeGen;

public class Build {
	
	static public void main (String[] args) throws Exception {

		boolean flag = true;
		if (flag) {
			// generate JS code
			XpeCodeGen.generate("../gdx-bullet/src", "../gdx-bullet-dragome/src", new DragomeWrapper());
			return;
		}

		// generate C/C++ code
		RobustJavaMethodParser.CustomIgnoreTag = "[";
		new NativeCodeGenerator().generate("../gdx-bullet/src", "../gdx-bullet/bin" + File.pathSeparator + "../../../../Projects/Libgdx/gdx/bin", "jni");

		String cppFlags = "";
		cppFlags += " -fno-strict-aliasing";
		cppFlags += " -fno-rtti";
		cppFlags += " -DBT_NO_PROFILE";

		String[] excludes = { "src/bullet/BulletMultiThreaded/GpuSoftBodySolvers/**", "emscripten/**"};
		String[] headers = { "src/bullet/", "src/custom/", "src/extras/Serialize/" };

		// generate build scripts
		BuildTarget win32home = BuildTarget.newDefaultTarget(TargetOs.Windows, false);
		win32home.compilerPrefix = "";
		win32home.buildFileName = "build-windows32home.xml";
		win32home.excludeFromMasterBuildFile = true;
		win32home.cExcludes = win32home.cppExcludes = excludes;
		win32home.headerDirs = headers;
		win32home.cppFlags += cppFlags;

		BuildTarget win32 = BuildTarget.newDefaultTarget(TargetOs.Windows, false);
		win32.cExcludes = win32.cppExcludes = excludes;
		win32.headerDirs = headers;
		win32.cppFlags += cppFlags;

		BuildTarget win64 = BuildTarget.newDefaultTarget(TargetOs.Windows, true);
		win64.cExcludes = win64.cppExcludes = excludes;
		win64.headerDirs = headers;
		win64.cppFlags += cppFlags;

		BuildTarget lin32 = BuildTarget.newDefaultTarget(TargetOs.Linux, false);
		lin32.cExcludes = lin32.cppExcludes = excludes;
		lin32.headerDirs = headers;
		lin32.cppFlags += cppFlags;

		BuildTarget lin64 = BuildTarget.newDefaultTarget(TargetOs.Linux, true);
		lin64.cExcludes = lin64.cppExcludes = excludes;
		lin64.headerDirs = headers;
		lin64.cppFlags += cppFlags;

		BuildTarget mac = BuildTarget.newDefaultTarget(TargetOs.MacOsX, false);
		mac.cExcludes = mac.cppExcludes = excludes;
		mac.headerDirs = headers;
		mac.cppFlags += cppFlags;

		BuildTarget mac64 = BuildTarget.newDefaultTarget(TargetOs.MacOsX, true);
		mac64.cExcludes = mac.cppExcludes = excludes;
		mac64.headerDirs = headers;
		mac64.cppFlags += cppFlags;

		BuildTarget android = BuildTarget.newDefaultTarget(TargetOs.Android, false);
		android.cExcludes = android.cppExcludes = excludes;
		android.headerDirs = headers;
		android.cppFlags += cppFlags + " -fexceptions";

		BuildTarget ios = BuildTarget.newDefaultTarget(TargetOs.IOS, false);
		ios.cExcludes = ios.cppExcludes = excludes;
		ios.headerDirs = headers;
		ios.cppFlags += cppFlags;

		new AntScriptGenerator().generate(new BuildConfig("gdx-bullet", "target", "natives", "jni"), win32home, win32, win64, lin32, lin64, mac, mac64, android, ios);
//		new FileHandle(new File("jni/Application.mk")).writeString("\nAPP_STL := stlport_static\n", true);
		
		boolean success = true;
		
//		if(success)
//			success = BuildExecutor.executeAnt("jni/build-windows64.xml", "-v -Dhas-compiler=true clean postcompile");
		if(success)
			success = BuildExecutor.executeAnt("jni/build-windows32.xml", "-v -Dhas-compiler=true postcompile");
		
		if (success) 
			BuildExecutor.executeAnt("jni/build.xml", "-v pack-natives");
	}
}
@@ -36,16 +36,45 @@
 o.File;%0D
+%0Aimport java.net.URLDecoder;%0D
 %0A%0D%0Aimpor
@@ -575,20 +575,179 @@
  flag = 
-true
+false;%0D%0A%0D%0A%09%09String path = Build.class.getProtectionDomain().getCodeSource().getLocation().getPath();%0D%0A%09%09path = URLDecoder.decode(path, %22UTF-8%22).replace(%22bin/%22, %22%22)
 ;%0D%0A%09%09if 
@@ -795,32 +795,39 @@
 odeGen.generate(
+path + 
 %22../gdx-bullet/s
@@ -1015,16 +1015,23 @@
 enerate(
+path + 
 %22../gdx-
@@ -1034,32 +1034,39 @@
 gdx-bullet/src%22,
+ path +
  %22../gdx-bullet/
@@ -1092,16 +1092,23 @@
 arator +
+ path +
  %22../../
@@ -1138,16 +1138,23 @@
 dx/bin%22,
+ path +
  %22jni%22);
@@ -3452,16 +3452,23 @@
 bullet%22,
+ path +
  %22target
@@ -3480,16 +3480,23 @@
 atives%22,
+ path +
  %22jni%22),
@@ -3877,32 +3877,39 @@
 utor.executeAnt(
+path + 
 %22jni/build-windo
@@ -3955,24 +3955,151 @@
 compile%22);%0D%0A
+%09%09if(success)%0D%0A%09%09%09success = BuildExecutor.executeAnt(path + %22jni/build-windows64.xml%22, %22-v -Dhas-compiler=true postcompile%22);%0D%0A
 %09%09%0D%0A%09%09if (su
@@ -4127,32 +4127,39 @@
 utor.executeAnt(
+path + 
 %22jni/build.xml%22,

Fix bullet build