
jpl_java_home('@JAVA_HOME@').
jpl_java_home(JAVA_HOME) :-
	getenv('JAVA_HOME', JAVA_HOME).

load_jpl_lib :-
	jpl_java_home(JavaHome),
	fetch_arch(Arch),
	gen_jvm_lib(JavaHome,Arch,JPL,JLibs),
	load_foreign_files(JPL, JLibs, install), !.

fetch_arch(win32) :-
	current_prolog_flag(windows,true), !.
fetch_arch(Arch) :-
	current_prolog_flag(host_type,Name),
	atom_codes(Name,Codes),
	gen_arch(Codes,Arch).

gen_arch(L,mac) :-
	append(_,[0'd,0'a,0'r,0'w,0'i,0'n|_],L), !.
% handle compilation in 64 bit machine of 32 bit binary.
gen_arch([0'x,0'8,0'6,0'_,0'6,0'4|_],i386) :-
	current_prolog_flag(max_tagged_integer, 67108863), !.
gen_arch([0'x,0'8,0'6,0'_,0'6,0'4|_],amd64).
gen_arch([0'i,_,0'8,0'6|_],i386). % take all versions of X86
gen_arch([0's,0'p,0'a,0'r,0'c|_],sparc).
gen_arch([0'/,0'u,0's,0'r|_],mac).

gen_jvm_lib(_,mac,[libjpl],[]) :- !.
gen_jvm_lib(_,win32,[libjpl],[]) :- !.
gen_jvm_lib(JavaHome,Arch,[libjpl], [JLib]) :-
	atom_concat([JavaHome,'/jre/lib/',Arch,'/client/libjvm.so'],JLib),
	exists(JLib), !.
gen_jvm_lib(JavaHome,Arch,[libjpl], [JLib]) :-
	atom_concat([JavaHome,'/jre/lib/',Arch,'/server/libjvm.so'],JLib),
	exists(JLib), !.
gen_jvm_lib(JavaHome,Arch,[libjpl], [JLib]) :-
	atom_concat([JavaHome,'/jre/lib/',Arch,'/classic/libjvm.so'],JLib),
	exists(JLib), !.
gen_jvm_lib(JavaHome,Arch,[libjpl], [JLib]) :-
	atom_concat([JavaHome,'/jre/lib/',Arch,'/libjvm.so'],JLib),
	exists(JLib), !.



