#!/bin/bash

# generate the .h file

KEMI_MAX_SIZE=1536

cat > ../app_lua_kemi_export.h <<EOF
/**
 * Copyright (C) 2016-2020 Daniel-Constantin Mierla (asipto.com)
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/**
 * this file is generated - do not edit
 */

#ifndef __APP_LUA_KEMI_EXPORT_H__
#define __APP_LUA_KEMI_EXPORT_H__

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "../../core/kemi.h"

#define SR_KEMI_LUA_EXPORT_SIZE	${KEMI_MAX_SIZE}

typedef struct sr_kemi_lua_export {
	lua_CFunction pfunc;
	sr_kemi_t *ket;
} sr_kemi_lua_export_t;

sr_kemi_t *sr_kemi_lua_export_get(int idx);
lua_CFunction sr_kemi_lua_export_associate(sr_kemi_t *ket);

#endif
EOF

# generate the .c file

cat > ../app_lua_kemi_export.c <<EOF
/**
 * Copyright (C) 2016-2020 Daniel-Constantin Mierla (asipto.com)
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/**
 * this file is generated - do not edit
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "../../core/dprint.h"

#include "app_lua_api.h"
#include "app_lua_kemi_export.h"

EOF

CEND=${KEMI_MAX_SIZE}

for (( c=0; c<CEND; c++ )); do
	echo >>../app_lua_kemi_export.c
	echo "/**" >>../app_lua_kemi_export.c
	echo " *" >>../app_lua_kemi_export.c
	echo " */" >>../app_lua_kemi_export.c
	echo "static int sr_kemi_lua_exec_func_${c}(lua_State *L)" >>../app_lua_kemi_export.c
	echo "{" >>../app_lua_kemi_export.c
	echo "	return sr_kemi_lua_exec_func(L, ${c});" >>../app_lua_kemi_export.c
	echo "}" >>../app_lua_kemi_export.c
done

echo >>../app_lua_kemi_export.c
echo "/**" >>../app_lua_kemi_export.c
echo " *" >>../app_lua_kemi_export.c
echo " */" >>../app_lua_kemi_export.c

echo "static sr_kemi_lua_export_t _sr_kemi_lua_export_list[] = {" >>../app_lua_kemi_export.c
for (( c=0; c<CEND; c++ )); do
	echo "	{ sr_kemi_lua_exec_func_${c}, NULL}," >>../app_lua_kemi_export.c
done
echo "	{NULL, NULL}" >>../app_lua_kemi_export.c
echo "};" >>../app_lua_kemi_export.c

cat >> ../app_lua_kemi_export.c <<EOF

/**
 *
 */
sr_kemi_t *sr_kemi_lua_export_get(int idx)
{
	if(idx<0 || idx>=SR_KEMI_LUA_EXPORT_SIZE)
		return NULL;
	return _sr_kemi_lua_export_list[idx].ket;
}

/**
 *
 */
lua_CFunction sr_kemi_lua_export_associate(sr_kemi_t *ket)
{
	int i;
	for(i=0; i<SR_KEMI_LUA_EXPORT_SIZE; i++) {
		if(_sr_kemi_lua_export_list[i].ket==NULL) {
			_sr_kemi_lua_export_list[i].ket = ket;
			return _sr_kemi_lua_export_list[i].pfunc;
		}
		if(_sr_kemi_lua_export_list[i].ket==ket) {
			return _sr_kemi_lua_export_list[i].pfunc;
		}
	}
	LM_ERR("no more indexing slots\n");
	return NULL;
}
EOF
