/**
 * Copyright (C) 2007 iptego GmbH
 *
 * This file is part of ser, a free SIP server.
 *
 * ser 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
 *
 * ser 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "sr_module.h"
#include "dprint.h"
#include "mem/mem.h"

#include "binds.h"


xl_parse_format_f *xl_parse;
xl_print_log_f *xl_print;

struct tm_binds tmb;

#define PARSE_BIND_NAME	"xparse"
#define PRINT_BIND_NAME	"xprint"

#define LOADTM_BIND_NAME	"load_tm"

#define NO_SCRIPT	-1 /* this is dumb: every module using xlog defines it */


int xll_bind(void)
{
	xl_parse = (xl_parse_format_f *)find_export(PARSE_BIND_NAME, NO_SCRIPT, 0);
	xl_print = (xl_print_log_f *)find_export(PRINT_BIND_NAME, NO_SCRIPT, 0);

	if (xl_parse && xl_print)
		return 0;

	ERR("failed to load 'xl_lib' routines `%s', `%s'; is module XLOG "
			"loaded?\n", PARSE_BIND_NAME, PRINT_BIND_NAME);
	return -1;
}

void xl_elogs_free(xl_elog_t *root)
{
	xl_elog_t *next;
	while (root) {
		next = root->next;
		pkg_free(root);
		root = next;
	}
}

int tm_bind(void)
{
	load_tm_f tm_load;
	if (! (tm_load = (load_tm_f)find_export(LOADTM_BIND_NAME, NO_SCRIPT, 0))) {
		ERR("failed to find '%s' export; is module TM loaded?\n", 
				LOADTM_BIND_NAME);
		return -1;
	}

	if (tm_load(&tmb) < 0) {
		BUG("TM module loaded but failed to load its exported symbols.\n");
		return -1;
	}
	
	return 0;
}
