extern "C" 
{
  #include "lua.h"
  #include "lauxlib.h"
}

#include "luacvaux.h"

#include "lua_core.h"
#include "lua_imgproc.h"
#include "lua_calib3d.h"
#include "lua_highgui.h"
#include "lua_legacy.h"
#include "lua_objdetect.h"
#include "lua_features2d.h"
#include "lua_video.h"

static const luaL_Reg luacv[]=
{
  {NULL,NULL}
};


static const luaL_Reg loadedlibs[]={
  {CORE_NAME,luaopen_luacv_core}, 
  {CALIB3D_NAME,luaopen_luacv_calib3d},
  {FEATURES2D_NAME,luaopen_luacv_features2d},
  {HIGHGUI_NAME,luaopen_luacv_highgui},
  {IMGPROC_NAME,luaopen_luacv_imgproc},
  //{LEGACY_NAME,luaopen_luacv_legacy},
  {OBJDETECT_NAME,luaopen_luacv_objdetect},
  {VIDEO_NAME,luaopen_luacv_video},
  {NULL,NULL}
};

extern "C" {
#if defined(WIN32) || defined(WIN64)
__declspec(dllexport) 
#endif
int luaopen_luacv(lua_State *L)
{

  objectReg(LIBNAME,luacv_m);

  luaL_newlib(L,luacv);

  //**************** luacv methods registration ****************//
  luaL_getmetatable(L,LIBNAME);
  lua_setmetatable(L,-2);

  lua_pushglobaltable(L);
  lua_pushvalue(L, -2);
  lua_setfield(L, -2,LIBNAME);
  lua_pop(L, 1);

  //**************** module functions registration ****************//
  for (luaL_Reg *lib = (luaL_Reg*)loadedlibs; lib->func; lib++) 
  {
    luaL_requiref(L, lib->name, lib->func, 1);
    lua_getglobal(L,LIBNAME);
    lua_pushnil(L);
    while(lua_next(L,-3) !=0)
    {
      lua_pushvalue(L, -2);
      lua_insert(L, -2);
      lua_settable(L, -4);
    }
  }

  return 1;
}
}
