/*
 * jdll.c - libjdll implementation
 *
 * (C) 2000 Jess Haas
 * (C) 2000-2001 Jeffrey Bridge
 *  2001/03/04 - Olivier Dagenais - Removed "jdll_ScanDir" function
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "jdll.h"

LIB_HANDLE jdll_Load(const char *lib_name)
{
	LIB_HANDLE lib_handle;
	char *lib_name_tmp;
	char *errmsgt, *errmsg1 = NULL, *errmsg2 = NULL;

	if ((lib_name == NULL) || (strlen(lib_name) < 0))
#ifdef WIN32
		return NULL;
#else
		return (void*)NULL;
#endif

#ifdef WIN32
	lib_handle = LoadLibrary(lib_name);
#else
#ifdef __BEOS__
	lib_handle = load_add_on(lib_name);
#else
#ifdef HPUX
	lib_handle = shl_load(lib_name, BIND_DEFERRED, 0);
	if (lib_handle == NULL) 
	{
		lib_name_tmp = (char*)malloc(strlen(lib_name) + 5);
		if (lib_name_tmp == NULL)
			return (void*)NULL;
		strcpy(lib_name_tmp, lib_name);
		strcat(lib_name_tmp, SHARED_LIB_EXT);
		lib_handle = shl_load(lib_name_tmp, BIND_DEFERRED, 0);
		free(lib_name_tmp);
	}
#else
	lib_handle = dlopen(lib_name, RTLD_NOW);
	if (lib_handle == NULL) 
	{
		errmsgt = (char*)dlerror();
		errmsg1 = (char*)malloc(strlen(errmsgt) + 1);
		if (errmsg1 == NULL)
		{
			fprintf(stderr, "could not malloc error string!\n");
			return NULL;
		}
		strncpy(errmsg1, errmsgt, strlen(errmsgt) + 1);

		lib_name_tmp = (char*)malloc(strlen(lib_name) + 5);
		if (lib_name_tmp == NULL)
			return (void*)NULL;
		strcpy(lib_name_tmp, lib_name);
		strcat(lib_name_tmp, SHARED_LIB_EXT);
		lib_handle = dlopen(lib_name_tmp, RTLD_NOW);
		free(lib_name_tmp);

		if (lib_handle == NULL)
		    errmsg2 = (char*)dlerror();
	}

	if (lib_handle == NULL)
	{
		fprintf(stderr, "library open error!\n");
		if (errmsg1 != NULL)
		{
			fprintf(stderr, "msg1=%s\n", errmsg1);
			free(errmsg1);
		}
		if (errmsg2 != NULL)
			fprintf(stderr, "msg2=%s\n", errmsg2);
	}
#endif /* HPUX */
#endif /* BeOS */
#endif /* win32 */

	if(lib_handle <= NULL)
#ifdef WIN32
		return NULL;
#else
		return (void*)NULL;
#endif

	return lib_handle;
}	

int jdll_Unload(LIB_HANDLE lib_handle)
{
	if (lib_handle == NULL)
		return -1;

#ifdef WIN32
	FreeLibrary(lib_handle);
#else
#ifdef __BEOS__
	unload_add_on(lib_handle);	
#else 
#ifdef HPUX
	shl_unload(lib_handle);
#else
	dlclose(lib_handle);
#endif /* HPUX */
#endif /* BeOS */
#endif /* win32 */

	lib_handle = NULL;

	return 0;
}

void *jdll_GetSym(LIB_HANDLE lib_handle, char *symbol_name)
{
	void *symbol;

	if((lib_handle == NULL) || (symbol_name == NULL))
#ifdef WIN32
		return NULL;
#else
		return (void*)NULL;
#endif

#ifdef WIN32
	symbol = GetProcAddress(lib_handle, symbol_name);
#else
#ifdef __BEOS__
	if (get_image_symbol(lib_handle, symbol_name, B_SYMBOL_TYPE_ANY, &symbol) != B_OK)
		return (void *)NULL;
#else
#ifdef HPUX
	/* fixme: not sure about this next line. -jeff */
	if (shl_findsym(&lib_handle, symbol_name, TYPE_PROCEDURE, &symbol) != 0)
		return (void *)NULL;
#else
	symbol = dlsym(lib_handle, symbol_name);
	if (dlerror() != NULL)
		return (void *)NULL;
#endif /* HPUX */
#endif /* BeOS */
#endif /* win32 */

	return symbol;
}

