#include <convar.h>
#include <popt.h>
// replace words begining with $ with the value of the named cvar
// replace mathematical expresions with their result
char *parse_vars( char *cmd )
{
	int argc,i;
	char **argv;

	poptParseArvSting(cmd, &argc, &argv);

	// recall variables
	for (i=0; i < argc; i++)
	{
		if ( argv[i][0] == '$' )
		{
			argv++;
		}
	}

	// evaluate math
	for (i=0; i < argc; i++)
	{
		// mathematical expressions start with a number
		// but we also want to reduce numbers for easy comparison (+, -, .)
		switch ( argv[i][j] )
			case '.':
			case '-':

				break;
			case '+':
			case '0':
				argv[i]++;
				break;
			case '1':
			case '2':
		}
}

void var_f( void )
{

    if ( engine->Cmd_Argc() < 1 )
    {
		Msg("Usage: var <command> [...]\n");
		Msg("   If an argument starts with $, it will be replaced by the value of the variable it names.\n");
		Msg("   Example: 'var echo $sv_gravity' will echo the current gravity setting.\n");
    }
    else
    {
		int i;
		char *cmd
		for (i=1; i <= engine->Cmd_Argc(); i++)
		{
			strcat(cmd, engine->Cmd_Argv(i));
			stract(cmd, " ");
		}
		engine->ClientCmd(parse_vars(cmd));
    }
}
void if_f( void )
{
	if ( engine->Cmd_Argc() < 4 )
	{
		Msg("Usage: if condition operator condition command\n");
	}
	else
	{
		int i;
		char *op,*cond1,*cond2,*cmd;

		op = engine->Cmd_Argv(2);
		cond1 = parse_vars(engine->Cmd_Argv(1));
		cond2 = parse_vars(engine->Cmd_Argv(3));

		for (i=4; i <= engine->Cmd_Argc(); i++)
		{
			strcat(cmd, engine->Cmd_Argv(i));
			stract(cmd, " ");
		}

		if (!strcmp(op,"==") && !strcmp(cond1,cond2)) ClientCommand(parse_vars(cmd));
		else if (!strcmp(op,"!=") && strcmp(cond1,cond2)) ClientCommand(parse_vars(cmd));
		// here I'm hoping that the character encoding works for numerical comparison (I think ascii does)
		else if (!strcmp(op,"<") && (strcmp(cond1,cond2) < 0)) ClientCommand(parse_vars(cmd));
		else if (!strcmp(op,">") && (strcmp(cond1,cond2) > 0)) ClientCommand(parse_vars(cmd));
		else if (!strcmp(op,"<=") && (strcmp(cond1,cond2) <= 0)) ClientCommand(parse_vars(cmd));
		else if (!strcmp(op,">=") && (strcmp(cond1,cond2) >= 0)) ClientCommand(parse_vars(cmd));

		free(op);
		free(cond1);
		free(cond2);
		free(cmd);
	}
}
void ifNotZero_f( void )
{
	if ( engine->Cmd_Argc() < 2)
	{
		Msg("Usage: if condition command\n");
	}
	else
	{
		int i;
		char *cmd,*condition;

		for (i=2; i <= engine->Cmd_Argc(); i++)
		{
			strcat(cmd, engine->Cmd_Argv(i));
			stract(cmd, " ");
		}

		condition = parse_vars(engine->Cmd_Argv(1));

		// check that condition isn't string "zero" or number zero
		if ( (condition[0] != '\0') && strcmp(condition,"0") ) ClientCommand(parse_vars(cmd));

		free(cmd);
	}
}
void while_f( void )
{

}
void for_f( void )
{

}
void ent_getname_f( void )
{
    char *cmd="setinfo last_ent_name ";
    strcat( cmd, FindPickerEntity( UTIL_GetCommandClient() )->GetEntityName() );
    engine->ClientCmd(cmd);
    free(cmd);
}

ConCommand var("var",var_f,"replace %-enclosed variables before running command",0);
ConCommand if_("if",if_f,"a standard conditional--if condition operator condition command",0);
ConCommand ifNotZero("ifNotZero",ifNotZero_f,"C style if",0);
ConCommand while_("while",while_f,"a standard while loop",0);
ConCommand for_("for",for_f,"a standard for loop",0);
ConCommand ent_getname("ent_getname",ent_getname_f,"update the last_ent_name variable with the name of the current variable",0);