import sys.*;
import sys.io.*;
import haxe.io.*;

/**
	This is intended to be used by TestSys and io.TestProcess.
*/
class ExitCode {
	static public var bin:String =
	#if interp
		"bin/interp/ExitCode";
	#elseif neko
		"bin/neko/ExitCode.n";
	#elseif hl
		#if hlc
			"bin/hlc/exitCode/ExitCode.exe";
		#else
			"bin/hl/ExitCode.hl";
		#end
	#elseif cpp
		#if debug
			"bin/cpp/ExitCode-debug";
		#else
			"bin/cpp/ExitCode";
		#end
	#elseif jvm
		"bin/jvm/ExitCode.jar";
	#elseif python
		"bin/python/ExitCode.py";
	#elseif php
		"bin/php/ExitCode/index.php";
	#elseif lua
		"bin/lua/ExitCode.lua";
	#elseif js
		"bin/js/ExitCode.js";
	#else
		null;
	#end

	static public function getNative():String {
		// This is just a script that behaves like ExitCode.hx,
		// which exits with the code same as the first given argument.
		// var scriptContent = switch (Sys.systemName()) {
		// 	case "Windows":
		// 		'@echo off\nexit /b %1';
		// 	case "Mac", "Linux", _:
		// 		'#!/bin/sh\nexit $1';
		// }
		// var scriptExt = switch (Sys.systemName()) {
		// 	case "Windows":
		// 		".bat";
		// 	case "Mac", "Linux", _:
		// 		".sh";
		// }

		var binExt = switch (Sys.systemName()) {
			case "Windows":
				".exe";
			case "Mac", "Linux", _:
				"";
		}

		var binPath = Path.join(["bin", "ExitCode"]) + binExt;
		if (FileSystem.exists(binPath)) {
			return binPath;
		}

		if (!FileSystem.exists("bin"))
			FileSystem.createDirectory("bin");

		switch (Sys.systemName()) {
			case "Windows":
				// var gcc = Sys.command("cl", ["src/ExitCode.c", "/Fobin", "/link", "/out:bin/ExitCode.exe"]);
				// if (gcc != 0)
				// 	throw "cannot compile ExitCode";
				File.copy(Path.join(["bin", "neko", "ExitCode"]) + binExt, binPath);
			case "Mac", "Linux", _:
				var gcc = Sys.command("gcc", ["src/ExitCode.c", "-o", "bin/ExitCode"]);
				if (gcc != 0)
					throw "cannot compile ExitCode";
		}

		return binPath;
	}

	static function main():Void {
		Sys.exit(Std.parseInt(Sys.args()[0]));
	}
}
