#!/bin/bash

# generate the .h file

KEMI_MAX_SIZE=1536

cat > ../app_ruby_kemi_export.h <<EOF
/**
 * Copyright (C) 2018 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_RUBY_KEMI_EXPORT_H__
#define __APP_RUBY_KEMI_EXPORT_H__

#include <ruby.h>

#include "../../core/kemi.h"

#define SR_KEMI_RUBY_EXPORT_SIZE	${KEMI_MAX_SIZE}

typedef struct sr_kemi_ruby_export {
	app_ruby_function pfunc;
	sr_kemi_t *ket;
} sr_kemi_ruby_export_t;

sr_kemi_t *sr_kemi_ruby_export_get(int idx);
app_ruby_function sr_kemi_ruby_export_associate(sr_kemi_t *ket);

#endif
EOF

# generate the .c file

cat > ../app_ruby_kemi_export.c <<EOF
/**
 * Copyright (C) 2018 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 "../../core/dprint.h"

#include "app_ruby_api.h"
#include "app_ruby_kemi_export.h"

EOF

CEND=${KEMI_MAX_SIZE}

for (( c=0; c<CEND; c++ )); do
	echo >>../app_ruby_kemi_export.c
	echo "/**" >>../app_ruby_kemi_export.c
	echo " *" >>../app_ruby_kemi_export.c
	echo " */" >>../app_ruby_kemi_export.c
	echo "static VALUE sr_kemi_ruby_exec_func_${c}(int argc, VALUE* argv, VALUE self)" >>../app_ruby_kemi_export.c
	echo "{" >>../app_ruby_kemi_export.c
	echo "	return sr_kemi_ruby_exec_func(NULL, ${c}, argc, argv, self);" >>../app_ruby_kemi_export.c
	echo "}" >>../app_ruby_kemi_export.c
done

echo >>../app_ruby_kemi_export.c
echo "/**" >>../app_ruby_kemi_export.c
echo " *" >>../app_ruby_kemi_export.c
echo " */" >>../app_ruby_kemi_export.c

echo "static sr_kemi_ruby_export_t _sr_kemi_ruby_export_list[] = {" >>../app_ruby_kemi_export.c
for (( c=0; c<CEND; c++ )); do
	echo "	{ sr_kemi_ruby_exec_func_${c}, NULL}," >>../app_ruby_kemi_export.c
done
echo "	{NULL, NULL}" >>../app_ruby_kemi_export.c
echo "};" >>../app_ruby_kemi_export.c

cat >> ../app_ruby_kemi_export.c <<EOF

/**
 *
 */
sr_kemi_t *sr_kemi_ruby_export_get(int idx)
{
	if(idx<0 || idx>=SR_KEMI_RUBY_EXPORT_SIZE)
		return NULL;
	return _sr_kemi_ruby_export_list[idx].ket;
}

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