/* ***** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/LGPL 2.1/GPL 2.0

The contents of this file are subject to the Mozilla Public License Version 
1.1 (the "License"); you may not use this file except in compliance with
...
for the specific language governing rights and limitations under the
License.

The Original Code is for Luminous Forts.

The Initial Developer of the Original Code is Hekar Khani.
Portions created by the Hekar Khani are Copyright (C) 2010
Hekar Khani. All Rights Reserved.

Contributor(s):
Hekar Khani <hekark@gmail.com>

Alternatively, the contents of this file may be used under the terms of
either of the GNU General Public License Version 2 or later (the "GPL"),
...
the terms of any one of the MPL, the GPL or the LGPL.

***** END LICENSE BLOCK ***** */

#include "cbase.h"
#ifdef MOD_LUA
#include "Lua_Main.h"

static CLuaManager g_LuaManager;
CLuaManager *LuaManager()
{
	return &g_LuaManager;
}

void CC_ExecuteLuaCode( const CCommand& args )
{
	if ( !UTIL_IsCommandIssuedByServerAdmin() )
		return;

	if ( args.ArgC() >= 1 )
	{
		LuaManager()->ExecuteString( args.Arg( 1 ) );
	}
}

static ConCommand lf_lua_string( "lf_lua_string", CC_ExecuteLuaCode, "Execute lua code (Argument must be in quotes)" );

void CC_ExecuteLuaFile( const CCommand& args )
{
	if ( !UTIL_IsCommandIssuedByServerAdmin() )
		return;

	if ( args.ArgC() >= 1 )
	{
		LuaManager()->ExecuteFile( args.Arg( 1 ) );
	}
}

static ConCommand lf_lua_file( "lf_lua_file", CC_ExecuteLuaFile, "Execute lua file in 'lua/' folder" );

#if 0
int add_file_and_line(lua_State* L)
{
   lua_Debug d;
   lua_getstack(L, 1, &d);
   lua_getinfo(L, "Sln", &d);
   std::string err = lua_tostring(L, -1);
   lua_pop(L, 1);
   Msg ( "%d:%d", d.short_src, d.currentline );

   if (d.name != 0)
   {
      Msg ( "(%s %s)\n", d.namewhat, d.name );
   }
   msg << " " << err;
   lua_pushstring(L, msg.str().c_str());
   return 1;
}
#endif // 0
CLuaManager::CLuaManager()
{
	// Create a new lua state
	m_State = lua_open();
	
	// Connect LuaBind to this lua state
	luabind::open( m_State );
}

CLuaManager::~CLuaManager()
{
	lua_close( m_State );
}

void CLuaManager::ExecuteString( const char *code )
{
	luaL_dostring( m_State, code );
	Msg( lua_tostring( m_State, -1 ) );
}

void CLuaManager::ExecuteFile( const char *filename )
{
}
#endif // MOD_LUA