#include "runner.h"
#include "tier0/platform.h"
#include "tier1/utlstring.h"
#include "tier1/utlvector.h"
#include "unistd.h"
#include "sys/wait.h"
#include "tier1/commandline.h"
int IRunner::Run(CUtlString szName, CUtlVector<CUtlString>& args)
{
	pid_t pid = fork();
	if (pid < 0)
		Plat_FatalErrorFunc("Failed to fork");
	/* child */
	if (pid == 0)
	{
		CUtlVector<const char*> execargs;
		execargs.AppendTail(szName);
		if (ICommandLine::CheckParam("-fpcdebug"))
			V_printf("%s",szName.GetString());
		for (auto &arg: args)
		{
			execargs.AppendTail(arg);
			if (ICommandLine::CheckParam("-fpcdebug"))
				V_printf(" %s",arg.GetString());
		}
		if (ICommandLine::CheckParam("-fpcdebug"))
			V_printf("\n");
		execargs.AppendTail(0);
		execvp(szName, (char *const*)execargs.GetData());
	}
	/* parent */
	wait(NULL);
	return 0;
}

int IRunner::Run(CUtlString szName, CUtlString szDirectory, CUtlVector<CUtlString>& args)
{
	pid_t pid = fork();
	if (pid < 0)
		Plat_FatalErrorFunc("Failed to fork");
	/* child */
	if (pid == 0)
	{
		CUtlVector<const char*> execargs;
		execargs.AppendTail(szName);
		for (auto &arg: args)
		{
			execargs.AppendTail(arg);
		}
		execargs.AppendTail(0);
		chdir(szDirectory.GetString());
		execvp(szName, (char *const*)execargs.GetData());
	}
	/* parent */
	wait(NULL);
	return 0;
}

int IRunner::Run(CUtlString szName, CUtlString szDirectory, CUtlVector<CUtlString>& args, CUtlVector<CUtlString>& environment)
{

}

