#!/bin/bash
#
# This configure script is based on the one from mplayer
#   http://mplayer.hu
#
# but is largely deviated in all parts except cpu detection
# due to the different focus of shogun.
#
VERSION=`grep "SHOGUN Release version" NEWS 2>&1 | cut -c 27- | cut -f 1 -d ' ' | head -n 1`
LIBSHOGUNSO=`grep "SHOGUN Release version" NEWS 2>&1 | cut -f 2 -d '(' | cut -f 2 -d ' ' | tr -d ',)' | head -n 1 | cut -f 1 -d '.'`
LIBSHOGUNVER=`grep "SHOGUN Release version" NEWS 2>&1 | cut -f 2 -d '(' | cut -f 2 -d ' ' | tr -d ',)' | head -n 1`
DATAVER=`grep "SHOGUN Release version" NEWS 2>&1 | head -n 1 | cut -d ',' -f 3 | cut -d ' ' -f 3 | tr -d ')'`

[ -n "$VERSION" ] && echo "Running configure for SHOGUN version $VERSION"

# some hopefully reasonable defaults
LDD=ldd
POSTLINKCMD="true"
MAKEDEPEND="gcc -MM"
MAKEDEPENDOPTS=""
SRCDIR="`dirname $0`"
LIBRARY_PATH=LD_LIBRARY_PATH
EXT_SRC_HEADER=h
EXT_SRC_C=c
EXT_SRC_CPP=cpp
EXT_SRC_SWIG=_wrap.cxx
EXT_INP_SWIG=i
EXT_IF_SWIG=.py
EXT_OBJ_SWIG=_wrap.cxx.o
EXT_LIB=so
EXT_LIB_SWIG_PYTHON_MODULAR=
EXT_LIB_SWIG_RUBY_MODULAR=.so
EXT_DOXY=.doxy
EXT_SWIG_DOXY=_doxygen.i
SWIGFLAGS="-w473 -w454 -w312 -w325 -fvirtual"
#-builtin -modern -modernargs
SWIG=swig
DOXYGEN=doxygen
RUBY=ruby
PYTHON=python
PYVER=
KERNELNAME=`uname`
MACHINE=`uname -m | tr ' ' '-'`
EXT_OBJ_CPP=cpp.o
EXT_OBJ_C=c.o
EXT_CPP_TMP="${EXT_SRC_CPP}.templ"
EXT_CPP_SH="${EXT_SRC_CPP}.sh"
EXT_CPP_PY="${EXT_SRC_CPP}.py"
CLASSPATH="$CLASSPATH"

#the defaults are
_swig_version=0.0.0
_readline=auto
_hdf5=auto
_ant=no
_json=auto
_xml=auto
_r=no
if [ -e shogun/classifier/svm/SVMLight.cpp ]; then
  _svm_light=yes
  _gpl=no
else
  _svm_light=no
  _gpl=yes
fi
_spinlocks=auto
_cplex=auto
_lpsolve=no
_glpk=auto
_lzo=auto
_snappy=auto
_gzip=auto
_bzip2=auto
_lzma=auto
_doxygen=auto
_shortrealkernelcache=yes
_logcache=no
_mmx=auto
_3dnow=auto
_3dnowext=auto
_mmxext=auto
_sse=auto
_sse2=auto
_ssse3=auto
_cmov=auto
_fast_cmov=auto
_logsumarray=no
_hmmparallel=no
_largefile=yes
_doxygen=auto
_swig=auto
_lapack=auto
_superlu=auto
_arpack=auto
_bigstates=yes
_hmmcache=yes
_debug=yes
_trace_mallocs=no
_reference_counting=yes
_hmmdebug=no
_pathdebug=no
_profile=no
_prefix="/usr/local"
_destdir="/"
_bindir=""
_datadir=""
_mandir=""
_confdir=""
_libdir=""
_incdir=""
_pydir=""
_rubydir=""
_octdir=""
_inc_extra=""
_ld_extra=""
_ld_static=""
_static=no
_optimizing=""
_interfaces=""
_datatypes="bool,char,uint8_t,uint16_t,int32_t,int64_t,uint64_t,float32_t,float64_t"
_libshogun=yes
_python=auto
_octave=auto
_r=auto

#static interfaces
_cmdline_static=auto
_python_static=auto
_octave_static=auto
_r_static=auto
_elwms_static=no
_matlab_static=auto

#modular interfaces
_python_modular=auto
_octave_modular=auto
_java_modular=auto
_ruby_modular=auto
_lua_modular=auto
_r_modular=no
_csharp_modular=auto
_optimization=yes

test -z "$INCLUDES" || _inc_extra="-I`echo $INCLUDES | sed 's,:, -I,g'`"
test -z "$LIBS" || _ld_extra="-L`echo $LIBS | sed 's,:, -L,g'`"

# these ones will be autodetected
CONFIGURE_OPTIONS="configure options $@"
GDB=gdb
MAKE=make
COMP_OPTS="-Wall -O9 -fexpensive-optimizations -frerun-cse-after-loop -fcse-follow-jumps -finline-functions -fschedule-insns2 -fthread-jumps -fforce-addr -fstrength-reduce -funroll-loops -mfpmath=sse"
COMP_DEFAULT_OPTS="-Wall -O2"
COMP_NO_OPTS="-Wall -O0"
COMPFLAGS_C=-fPIC
COMPFLAGS_CPP=-fPIC
COMPFLAGS_SWIG_CPP="-fPIC -O0"
SEDMI='sed -i'
test "$CFLAGS" && COMPFLAGS_C="$COMPFLAGS_C $CFLAGS"
test "$CXXFLAGS" && COMPFLAGS_CPP="$COMPFLAGS_CPP $CXXFLAGS"
INCLUDES=
LINK=c++
SONAMEOPT="-Xlinker -soname="
SHAREDOPTS="-shared -Wl,-export-dynamic"
DEFINES="-DSWIG_TYPE_TABLE=shogun -DSHOGUN"
DEFINES_SWIG=
PRELINKFLAGS=
LINKFLAGS=
POSTLINKFLAGS=

INCLUDES_STATIC_INTERFACES=
LINKFLAGS_STATIC_INTEFACES=
INCLUDES_MODULAR_INTERFACES=
LINKFLAGS_MODULAR_INTEFACES=

LIBSHOGUN_MODE=
LIBSHOGUN_SONAME=libshogun.so.${LIBSHOGUNSO}
LIBSHOGUN_TARGET=libshogun.so.${LIBSHOGUNVER}
LIBSHOGUN_DYNAMIC=libshogun.so
LIBSHOGUN_STATIC=libshogun.a
DEFINES_LIBSHOGUN=
INCLUDES_LIBSHOGUN="-I../libshogun -I.."
PRELINKFLAGS_LIBSHOGUN=
LINKFLAGS_LIBSHOGUN=
POSTLINKFLAGS_LIBSHOGUN=

ELWMS_TARGET=
DEFINES_ELWMS=
INCLUDES_ELWMS=
PRELINKFLAGS_ELWMS=
LINKFLAGS_ELWMS=
POSTLINKFLAGS_ELWMS=

CMDLINE_TARGET=
DEFINES_CMDLINE=
INCLUDES_CMDLINE=
PRELINKFLAGS_CMDLINE=
LINKFLAGS_CMDLINE=
POSTLINKFLAGS_CMDLINE=

MATLAB_TARGET=
DEFINES_MATLAB=
INCLUDES_MATLAB=
PRELINKFLAGS_MATLAB=
LINKFLAGS_MATLAB=
POSTLINKFLAGS_MATLAB=

LUA_TARGET=
DEFINES_LUA=
INCLUDES_LUA=
PRELINKFLAGS_LUA=
LINKFLAGS_LUA=
POSTLINKFLAGS_LUA=

JAVA_TARGET=
DEFINES_JAVA=
INCLUDES_JAVA=
PRELINKFLAGS_JAVA=
LINKFLAGS_JAVA=
POSTLINKFLAGS_JAVA=

CSHARP_TARGET=
DEFINES_CSHARP=
INCLUDES_CSHARP=
PRELINKFLAGS_CSHARP=
LINKFLAGS_CSHARP=
POSTLINKFLAGS_CSHARP=

RUBY_TARGET=
DEFINES_RUBY=
INCLUDES_RUBY=
PRELINKFLAGS_RUBY=
LINKFLAGS_RUBY=
POSTLINKFLAGS_RUBY=

OCTAVE_TARGET=
DEFINES_OCTAVE=
INCLUDES_OCTAVE=
PRELINKFLAGS_OCTAVE=
LINKFLAGS_OCTAVE=
POSTLINKFLAGS_OCTAVE=

R_TARGET=
DEFINES_R=
INCLUDES_R=
PRELINKFLAGS_R=
LINKFLAGS_R=
POSTLINKFLAGS_R=

PYTHON_TARGET=
DEFINES_PYTHON=
INCLUDES_PYTHON=
PRELINKFLAGS_PYTHON=
LINKFLAGS_PYTHON=
POSTLINKFLAGS_PYTHON=

# stuff needed for autodetections (definitely needs cleanup)
TMPLOG="configure.log"
TMPMAK="./configure-$RANDOM-$$-makefile"
TMPC="./configure-$RANDOM-$$.c"
TMPCXX="./configure-$RANDOM-$$.cpp"
TMPJAVA="./configure.java"
TMPJAVAO="./configure.class"
TMPRUBY="./configure-$RANDOM-$$.rb"
TMPO="./configure-$RANDOM-$$.o"
TMPSO="./configure-$RANDOM-$$.so"
TMPS="./configure-$RANDOM-$$.S"
TMPSS="./configure-$RANDOM-$$.s"
# OS name
system_name=`( uname -s ) 2>&1`

echo_check() {
	# Check how echo works in this /bin/sh
	case `echo -n` in
	  -n)	_echo_n=	_echo_c='\c'	;;	# SysV echo
	  *)	_echo_n='-n '	_echo_c=	;;	# BSD echo
	esac
}

head_check() {
	# Check how to call 'head' and 'tail'. Newer versions spit out warnings
	# if used as 'head -1' instead of 'head -n 1', but older versions don't
	# know about '-n'.
	if test "`(echo line1 ; echo line2) | head -1 2>/dev/null`" = "line1" ; then
	  _head() { head -$1 2>/dev/null ; }
	else
	  _head() { head -n $1 2>/dev/null ; }
	fi
	if test "`(echo line1 ; echo line2) | tail -1 2>/dev/null`" = "line2" ; then
	  _tail() { tail -$1 2>/dev/null ; }
	else
	  _tail() { tail -n $1 2>/dev/null ; }
	fi
}

detect_ldd()
{
	if darwin
	then
		LDD="otool -L"
	fi
}

detect_cc()
{
	test "$CC" && COMP_C="$CC"
	test "$CXX" && COMP_CPP="$CXX"

	echocheck "C Compiler"
cat > $TMPC << EOF
int main(void) { return 0; }
EOF
	if sunos
	then
		for COMP_C in suncc
		do
			if cc_check
			then
				cc_vendor=sun
				cc_found=yes
				cc_version=`$COMP_C -V 2>&1 | head -n 1 | cut -f 2 -d ':'`
			fi
		done
	else
		for COMP_C in "$COMP_C" cc gcc
		do
			if cc_check
			then
				cc_name_tmp=$($COMP_C -v 2>&1 | tail -n 1 | tr 'A-Z-' 'a-z ' | cut -d ' ' -f 1)
				if test "$cc_name_tmp" = "gcc"; then
					cc_vendor=gnu
					cc_name=$cc_name_tmp
					cc_version=`$COMP_C -dumpversion 2>&1`
					case $cc_version in
						2.96*)
						cc_version="$cc_version, bad"
						cc_found=no
						;;
						*)
						_cc_major=`echo $cc_version | cut -d '.' -f 1`
						_cc_minor=`echo $cc_version | cut -d '.' -f 2`
						_cc_mini=`echo $cc_version | cut -d '.' -f 3`
						cc_version="$cc_version, ok"
						cc_found=yes
						break
						;;
					esac
				elif $COMP_C -v 2>&1 | grep -q "clang"; then
					cc_name=clang
					cc_vendor=clang
					cc_version=$($COMP_C -dumpversion 2>&1)
					cc_found=yes
					break
				fi
			fi
		done
	fi

	if test "$cc_found" = yes
	then
		echores "$cc_name $cc_version"
	else
		die "no C compiler detected - cannot do anything"
	fi

	if test "$_optimization" = no; then
		_cpudetection=no
		COMP_DEFAULT_OPTS="$COMP_NO_OPTS";
	fi

	#cygwins gcc is buggy - so disable -O9 optimizations
	if cygwin || test "$_cpudetection" = no ; then
		COMP_OPTS=$COMP_DEFAULT_OPTS;
	fi

	if test "$_debug" = yes ; then
		COMP_OPTS="-g $COMP_OPTS";
		COMPFLAGS_SWIG_CPP="-g $COMPFLAGS_SWIG_CPP"
	fi

	echocheck "C Compiler flags"
	for opt in `echo $COMP_OPTS`
	do
		cc_check $opt && COMPFLAGS_C="$COMPFLAGS_C $opt"
	done
	echores "$COMPFLAGS_C"

cat > $TMPCXX << EOF
int main(void) { return 0; }
EOF
	echocheck "C++ Compiler"
	if sunos
	then
		for COMP_CPP in sunCC
		do
			if cxx_check
			then
				cxx_found=yes
				cxx_version=`$COMP_CPP -V 2>&1 | head -n 1 | cut -f 2 -d ':'`
			fi
		done
	else
		for COMP_CPP in "$COMP_CPP" c++ CC g++
		do
			if cxx_check
			then
				cxx_name_tmp=$($COMP_CPP -v 2>&1 | tail -n 1 | tr 'A-Z-' 'a-z ' | cut -d ' ' -f 1)
				if test "$cxx_name_tmp" = "gcc"; then
					cxx_vendor=gnu
					cxx_name=$cxx_name_tmp
					cxx_version=`$COMP_CPP -dumpversion 2>&1`
					case $cxx_version in
						2.96*)
							cxx_version="$cxx_version, bad"
							cxx_found=no
							;;
						*)
							_cxx_major=`echo $cxx_version | cut -d '.' -f 1`
							_cxx_minor=`echo $cxx_version | cut -d '.' -f 2`
							_cxx_mini=`echo $cxx_version | cut -d '.' -f 3`
							cxx_version="$cxx_version, ok"
							cxx_found=yes
							break
							;;
					esac
				elif $COMP_CPP -v 2>&1 | grep -q "clang"; then
					cxx_name=clang
					cxx_vendor=clang
					cxx_version=$($COMP_CPP -dumpversion 2>&1)
					cxx_found=yes
					break
				fi
			fi
		done
	fi

	if test "$cxx_found" = yes
	then
		echores "$cxx_name $cxx_version"
	else
		die "no C++ compiler detected - cannot do anything"
	fi

	echocheck "C++ Compiler flags"
	for opt in `echo $COMP_OPTS`
	do
		cxx_check $opt && COMPFLAGS_CPP="$COMPFLAGS_CPP $opt"
	done
	echores "$COMPFLAGS_CPP"

	#as our code is C++ use CPP compiler for linking
	LINK=$COMP_CPP
}

check_python()
{
	echocheck "Python"
	if $PYTHON -V >/dev/null 2>&1
	then
		PYVER="`$PYTHON -V 2>&1 | grep Python | cut -f 2 -d ' '`"
		echores "${PYVER}"
		PYVER="`echo $PYVER | cut -f 1-2 -d '.'`"
	else
		die "python not detected (required)"
	fi
}

check_pkg_config()
{
	echocheck "pkg-config"
	if pkg-config --version >/dev/null 2>&1
	then
		echores "`pkg-config --version`"
	else
		die "pkg-config not detected (required)"
	fi
}

test_jblas()
{
	echocheck "Jblas"
cat > $TMPJAVA << EOF
import org.jblas.DoubleMatrix;

public class configure {
    public static void main(String[] args) {
        DoubleMatrix a = DoubleMatrix.rand(3,3);
        DoubleMatrix b = DoubleMatrix.rand(3,3);

        DoubleMatrix c = a.add(b);
    }
}
EOF

	if javac $TMPJAVA >/dev/null 2>&1
	then
		echores "yes"
		HAVE_JBLAS='#define HAVE_JBLAS 1'
		DEFINES="$DEFINES -DHAVE_JBLAS"
	else
		
		if javac -cp /usr/share/java/jblas.jar $TMPJAVA >/dev/null 2>&1
		then
			echores "yes"
			HAVE_JBLAS='#define HAVE_JBLAS 1'
			DEFINES="$DEFINES -DHAVE_JBLAS"
			CLASSPATH="$CLASSPATH:/usr/share/java/jblas.jar"
		else
			echores "no (examples require jblas and won't run ...)"
		fi
	fi
}

test_ujmp()
{
	echocheck "UJMP"
cat > $TMPJAVA << EOF
import org.ujmp.core.Matrix;
import org.ujmp.core.matrix.DenseMatrix;

public class configure {
    public static void main(String[] args) {
        Matrix a = DenseMatrix.factory.rand(3, 3);
        Matrix b = DenseMatrix.factory.rand(3, 3);

        Matrix c = a.plus(b);
        System.out.println(c);
    }
}
EOF

	if javac $TMPJAVA >/dev/null 2>&1
	then
		echores "yes"
		HAVE_UJMP='#define HAVE_UJMP 1'
		DEFINES="$DEFINES -DHAVE_UJMP"
	else
		if javac -cp /usr/share/java/ujmp.jar $TMPJAVA >/dev/null 2>&1
		then
			echores "yes"
			HAVE_UJMP='#define HAVE_UJMP 1'
			DEFINES="$DEFINES -DHAVE_UJMP"
			CLASSPATH="$CLASSPATH:/usr/share/java/ujmp.jar"
		else
			echores "no"
		fi
	fi
}


test_java()
{
	if test "$_libshogun" = no
	then
		if test "$_java_modular" = yes
		then
			die "libshogun is required for java_modular"
		else
			_java_modular=no
		fi
	fi

	if test "$_java_modular" = no
	then
		return
	fi
	echocheck "Java"
	if java -version >/dev/null 2>&1
	then
		JAVAVER=`java -version 2>&1 | head -n 1 | cut -f 3 -d ' ' | tr -d '"'`
		echores "${JAVAVER}"

cat > $TMPJAVA << EOF
public class configure {
	public static void main(String[] args) {
		for (String arg: args) {
			System.out.println(System.getProperty(arg));
		}
	}
}
EOF
		echocheck "Java native interface files (jni.h)"
		if javac $TMPJAVA >/dev/null 2>&1 
		then
			EXT_IF_SWIG_JAVA_MODULAR='stop'
			PRE_LIB_SWIG_JAVA_MODULAR='lib'
			EXT_LIB_SWIG_JAVA_MODULAR=.so
			mkdir -p interfaces/java_modular/org/shogun >/dev/null 2>&1
			SWIGFLAGS_JAVA_MODULAR="-c++ -java -package org.shogun -outdir org/shogun"
			java_home_dir="$(dirname $(java configure java.home))"
			if test -d "${java_home_dir}/include"
			then
				INCLUDES_JAVA="-I${java_home_dir}/include"
			elif test -d "${java_home_dir}/Headers"
			then
				INCLUDES_JAVA="-I${java_home_dir}/Headers"
			else
				if test $_java_modular = "yes"
				then
					die "could not find java includes"
				fi
			fi
			INCLUDES_JAVA="$INCLUDES_JAVA $INCLUDES_JAVA/`uname -s | tr 'A-Z' 'a-z'`"

			COMPFLAGS_CPP="$COMPFLAGS_CPP -fno-strict-aliasing"
			COMPFLAGS_C="$COMPFLAGS_C -fno-strict-aliasing"
			LINKFLAGS_JAVA="--shared"
			echores "yes"

			echocheck "Ant"
			if ant -version >/dev/null 2>&1
			then
				echores "`ant -version | cut -f 4 -d ' '`"
				_java_modular=yes
				test_jblas
				test_ujmp
			else
				if test "$_java_modular" = yes
				then
					die "ant required for java interface"
				else
					echores "ant not detected"
					_java_modular=no
				fi
			fi
		else
			if test "$_java_modular" = yes
			then
				die "javac required for java interface"
			else
				echores "javac not detected"
				_java_modular=no
			fi
		fi
	else
		if test "$_java_modular" = yes
		then
			die "java missing"
		else
			echores "not detected"
			_java_modular=no
		fi
	fi
}

test_csharp()
{
	if test "$_csharp_modular" = no
	then
		return
	fi

	echocheck "Mono C#"
	if gmcs --version >/dev/null 2>&1
	then
		MONOVER=`gmcs --version 2>&1 | head -n 1 | cut -f 5 -d ' '`
		echores "${MONOVER}"

		EXT_IF_SWIG_CSHARP_MODULAR='stop'
		PRE_LIB_SWIG_CSHARP_MODULAR='lib'
		EXT_LIB_SWIG_CSHARP_MODULAR=.so
		SWIGFLAGS_CSHARP_MODULAR="-c++ -csharp"
		INCLUDES_CSHARP=
		LINKFLAGS_CSHARP="--shared"

		_csharp_modular=yes
	else
		if test "$_csharp_modular" = yes
		then
			die "csharp/csharp development files missing"
		else
			echores "not detected"
			_csharp_modular=no
		fi
	fi
}

test_ruby()
{
	if test "$_ruby_modular" = no
	then
		return
	fi

	echocheck "Ruby"
	if $RUBY -v >/dev/null 2>&1
	then
		RUBYVER=`$RUBY -v 2>&1 | head -n 1 | cut -f 2 -d ' '`
		echores "${RUBYVER}"

cat >$TMPRUBY << EOF
require('mkmf')
if RUBY_VERSION[0,3] == '1.8'
puts "-I#{Config::expand(CONFIG["archdir"])}" if CONFIG["archdir"]
else
puts "-I#{Config::expand(CONFIG['vendorlibdir'])}/#{Config::expand(CONFIG['arch'])} -I#{Config::expand(CONFIG['archdir'])} -I#{Config::expand(CONFIG['rubyhdrdir'])} -I#{Config::expand(CONFIG['rubyhdrdir'])}/#{Config::expand(CONFIG['arch'])}" if CONFIG['rubyhdrdir']
end
EOF
		echocheck "Ruby Developer Files"
		if $RUBY $TMPRUBY >/dev/null 2>&1 
		then
			echores "yes"
			INCLUDES_RUBY=`$RUBY $TMPRUBY`
			RUBYSO=`$RUBY -e 'require "mkmf"; puts Config::expand("$(RUBY_SO_NAME)")'`
			if ld -o "$TMPO" -l$RUBYSO >/dev/null 2>&1
			then
				LINKFLAGS_RUBY="-l$RUBYSO -fPIC -shared -ldl"
			else
				LINKFLAGS_RUBY="-lruby -fPIC -shared -ldl"
			fi
			EXT_IF_SWIG_RUBY_MODULAR='stop'
			PRE_LIB_SWIG_RUBY_MODULAR=
			if darwin ; then
				EXT_LIB_SWIG_RUBY_MODULAR=.bundle
			fi
			SWIGFLAGS_RUBY_MODULAR="-c++ -ruby"

cat >$TMPRUBY << EOF
require('mkmf')
if RUBY_VERSION[0,3] == '1.8'
	puts "#{Config::expand(CONFIG["archdir"])}" if CONFIG["archdir"]
else
	puts "#{Config::expand(CONFIG['rubylibdir'])}/#{Config::expand(CONFIG['arch'])}" if CONFIG['rubylibdir']
end
EOF
			RUBY_LIBRARY_DIR=`$RUBY "$TMPRUBY" 2>/dev/null`

cat >$TMPRUBY << EOF
require('mkmf')
if RUBY_VERSION[0,3] == '1.8'
	puts "#{Config::expand(CONFIG["archdir"])}" if CONFIG["archdir"]
else
	puts "#{Config::expand(CONFIG['vendorlibdir'])}/#{Config::expand(CONFIG['arch'])}" if CONFIG['vendorlibdir']
end
EOF
			RUBY_VENDOR_LIBRARY_DIR=`$RUBY "$TMPRUBY" 2>/dev/null`
			RUBY_EXTENSIONS_INSTALL_DIR=$_libdir/$_rubydir/`$RUBY -e 'require "mkmf"; puts Config::expand("$(ruby_version)") + "/" + Config::expand("$(arch)")'`
			test_ruby_narray
		else
			if test "$_ruby_modular" = yes
			then
				die "ruby development files missing"
			else
				echores "not detected"
				_ruby_modular=no
			fi
		fi
	else
		if test "$_ruby_modular" = yes
		then
			die "ruby not found"
		else
			echores "not detected"
			_ruby_modular=no
		fi
	fi
}

test_ruby_narray()
{
cat >$TMPRUBY << EOF
require 'narray'
NArray[[1, 0, 0], [0, 1, 2]]
EOF
	echocheck "Ruby NArray"
	if ruby $TMPRUBY >/dev/null 2>&1 
	then
		echores "yes"

cat >$TMPC << EOF
#include <ruby.h>
#include <narray.h>

int main(void)
{
	VALUE cNArray;
    volatile VALUE obj;
    VALUE x = NULL; //na_make_scalar(obj, NA_DFLOAT);
    return IsNArray(x) ? 1 : 0;
}
EOF

		if test -f "$RUBY_LIBRARY_DIR"/narray${EXT_LIB_SWIG_RUBY_MODULAR} ; then
			NARRAY="$RUBY_LIBRARY_DIR"/narray${EXT_LIB_SWIG_RUBY_MODULAR}
		elif gem list narray 2>&1 | grep -q narray
		then
			NARRAY="$(dirname `gem which narray`)/narray${EXT_LIB_SWIG_RUBY_MODULAR}"
			INCLUDES_RUBY="-I$(dirname `gem which narray`) $INCLUDES_RUBY"
		else
			NARRAY="$RUBY_VENDOR_LIBRARY_DIR"/narray${EXT_LIB_SWIG_RUBY_MODULAR}
		fi

		echocheck "Ruby NArray Developer Files"
		if cc_check $INCLUDES_RUBY $LINKFLAGS_RUBY && test -f $NARRAY
		then
			echores "yes"
			DEFINES="$DEFINES -DHAVE_RUBY_NARRAY"
			HAVE_NARRAY='#define HAVE_RUBY_NARRAY 1'
			NARRAY_LIB="#define NARRAY_LIB \"$NARRAY\""
		else
			if test "$_ruby_modular" = yes
			then
				die "ruby NArray header files missing"
			else
				echores "not detected (disabling ruby interface)"
				_ruby_modular=no
			fi
		fi
	else
		if test "$_ruby_modular" = yes
		then
			die "ruby NArray required for ruby_modular"
		else
			echores "not detected"
			_ruby_modular=no
		fi
	fi
}

test_lua()
{
	if test "$_lua_modular" = no
	then
		return
	fi

	echocheck "Lua"
	if lua -v >/dev/null 2>&1
	then
		LUAVER=`lua -v 2>&1 | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '('`
		LV=`echo $LUAVER | cut -f 1-2 -d '.'`
		EXT_IF_SWIG_LUA_MODULAR='stop'
		PRE_LIB_SWIG_LUA_MODULAR=
		EXT_LIB_SWIG_LUA_MODULAR=.so
		SWIGFLAGS_LUA_MODULAR="-c++ -lua"

		INCLUDES_LUA="`pkg-config --cflags-only-I lua$LV 2>/dev/null`"
		POSTLINKFLAGS_LUA="`pkg-config --libs lua$LV 2>/dev/null`"
		LUA_EXTENSIONS_INSTALL_DIR="`pkg-config lua$LV --define-variable prefix=$_prefix --variable INSTALL_CMOD`"

		if test -z "$INCLUDES_LUA" ; then
			INCLUDES_LUA="`pkg-config --cflags-only-I lua 2>/dev/null`"
		fi
		if test -z "$POSTLINKFLAGS_LUA" ; then
			POSTLINKFLAGS_LUA="`pkg-config --libs lua 2>/dev/null`"
		fi
		if test -z "$LUA_EXTENSIONS_INSTALL_DIR" ; then
			LUA_EXTENSIONS_INSTALL_DIR="`pkg-config lua --define-variable prefix=$_prefix --variable INSTALL_CMOD`"
		fi


		if test -z "$INCLUDES_LUA" ; then
			INCLUDES_LUA="-I/usr/include/lua${LV} -I/usr/local/include/lua${LV}"
		fi
		if test -z "$POSTLINKFLAGS_LUA" ; then
			POSTLINKFLAGS_LUA="-llua${LV}"
		fi

		POSTLINKFLAGS_LUA="$POSTLINKFLAGS_LUA --shared"
		echores "${LUAVER}"
		_lua_modular=yes
	else
		if test "$_lua_modular" = yes
		then
			die "lua/lua development files missing"
		else
			echores "not detected"
			_lua_modular=no
		fi
	fi
}

test_spinlocks()
{
	if test "$_spinlocks" = no
	then
		return
	fi

cat > $TMPC << EOF
#include <pthread.h>
int main(void)
{
	volatile pthread_spinlock_t lock;
	pthread_spin_init(&lock, 0);
	return 0;
}
EOF

	echocheck "Posix thread spinlock support"
	if cc_check $pthr
	then
		echores "yes"
		USE_SPINLOCKS='#define USE_SPINLOCKS 1'
		DEFINES="$DEFINES -DUSE_SPINLOCKS"
	else
		if test "$_spinlocks" = yes
		then
			die "not supported"
		else
			echores "no"
			_spinlocks=no
		fi
	fi
}

check_makedepend()
{
	echocheck "makedepend"

cat > $TMPC << EOF
int main(void) { return 0; }
EOF
	if $MAKEDEPEND $TMPC >/dev/null 2>&1
	then
		echores "$MAKEDEPEND"
	elif makedepend -f - $TMPC >/dev/null 2>&1
	then
		MAKEDEPEND=makedepend
		MAKEDEPENDOPTS="-f -"
		echores "$MAKEDEPEND"
	else
		echores "no"
		die "neither gcc nor makedepend found, but required to proceed. please install gcc or xutils-dev"
	fi
}

check_doxygen()
{
	echocheck "Doxygen"

	if test "$_doxygen" = yes || test "$_doxygen" = auto
	then
		if doxygen --version 2>&1 >/dev/null 2>&1
		then

			DOXY_MAJOR=`doxygen --version | cut -d '.' -f 1`
			DOXY_MINOR=`doxygen --version | cut -d '.' -f 2`

			if test "$DOXY_MAJOR" -ge 1 && test "$DOXY_MINOR" -ge 6
			then
				echores "`doxygen --version`"
				_doxygen=yes
			else
				if test "$_doxygen" = yes
				then
					die "doxygen version 1.6.0 or higher required for documentation"
				else
					echores "doxygen version 1.6.0 or higher required for documentation"
				fi
			fi
		else
			if test "$_doxygen" = yes
			then
				die "doxygen not detected"
			else
				echores "doxygen not detected, for documentation please install"
			fi
		fi
	else
		echores "disabled"
	fi
}

check_make()
{
	echocheck "GNU Make"

	if make --version 2>&1 | grep GNU >/dev/null 2>&1
	then
		echores "`make --version | grep GNU`"
		MAKE=make
	else
		if gmake --version 2>&1 | grep GNU >/dev/null 2>&1
		then
			echores "`gmake --version | grep GNU`"
			MAKE=gmake
		else
			echores "non GNU Make detected expect trouble (try running GNU make manually in case of trouble)"
		fi
	fi
}

cc_check() {
  echo >> "$TMPLOG"
  cat "$TMPC" >> "$TMPLOG"
  echo >> "$TMPLOG"
  echo "$COMP_C $INCLUDES $COMPFLAGS_C $TMPC -o $TMPO $LINKFLAGS $@" $POSTLINKFLAGS >> "$TMPLOG"
  rm -f "$TMPO"
  ( "$COMP_C" $INCLUDES $COMPFLAGS_C "$TMPC" -o "$TMPO" $LINKFLAGS "$@" $POSTLINKFLAGS ) >> "$TMPLOG" 2>&1
  TMP="$?"
  if test -f "$TMPO"
  then
	  echo >> "$TMPLOG"
	  echo "$LDD $TMPO" >> "$TMPLOG"
	  ( $LDD "$TMPO" ) >> "$TMPLOG" 2>&1
  else
	  echo "no object file created" >> "$TMPLOG"
  fi
	  echo >> "$TMPLOG"
  return "$TMP"
}

cxx_check() {
  echo >> "$TMPLOG"
  cat "$TMPCXX" >> "$TMPLOG"
  echo >> "$TMPLOG"
  echo "$COMP_CPP $INCLUDES $COMPFLAGS_CPP $TMPCXX -o $TMPO $LINKFLAGS $@" $POSTLINKFLAGS >> "$TMPLOG"
  rm -f "$TMPO"
  ( "$COMP_CPP" $INCLUDES $COMPFLAGS_CPP "$TMPCXX" -o "$TMPO" $LINKFLAGS "$@" $POSTLINKFLAGS ) >> "$TMPLOG" 2>&1
  TMP="$?"
  if test -f "$TMPO"
  then
	  echo >> "$TMPLOG"
	  echo "$LDD $TMPO" >> "$TMPLOG"
	  ( $LDD "$TMPO" ) >> "$TMPLOG" 2>&1
  else
	  echo "no object file created" >> "$TMPLOG"
  fi
	  echo >> "$TMPLOG"
  return "$TMP"
}

cxx_check_so() {
  echo >> "$TMPLOG"
  cat "$TMPCXX" >> "$TMPLOG"
  echo >> "$TMPLOG"
  echo "$COMP_CPP $INCLUDES $COMPFLAGS_CPP $TMPCXX -o $TMPSO $LINKFLAGS $@" $POSTLINKFLAGS >> "$TMPLOG"
  rm -f "$TMPSO"
  ( "$COMP_CPP" $INCLUDES $COMPFLAGS_CPP "$TMPCXX" -o "$TMPSO" $LINKFLAGS "$@" $POSTLINKFLAGS ) >> "$TMPLOG" 2>&1
  TMP="$?"
  if test -f "$TMPSO"
  then
	  echo >> "$TMPLOG"
	  echo "$LDD $TMPSO" >> "$TMPLOG"
	  ( $LDD "$TMPSO" ) >> "$TMPLOG" 2>&1
  else
	  echo "no object file created" >> "$TMPLOG"
  fi
	  echo >> "$TMPLOG"
  return "$TMP"
}


tmp_run() {
  "$TMPO" >> "$TMPLOG" 2>&1
}

# Display error message, flushes tempfile, exit
die () {
  echo
  echo "Error: $@" >&2
  echo >&2
  rm -f "$TMPO" "$TMPC" "$TMPCXX" "$TMPS"
  if test -f "$TMPLOG"
  then
	  echo "Check "$TMPLOG" if you do not understand why it failed."
	  echo "The last 20 lines of "$TMPLOG" are shown below"
	  echo
	  tail -n 20 "$TMPLOG"
  fi
  exit 1
}

# OS test booleans functions
issystem() {
  test "`echo $system_name | tr A-Z a-z`" = "`echo $1 | tr A-Z a-z`"
}
linux()   { issystem "Linux"   ; return "$?" ; }
sunos()   { issystem "SunOS"   ; return "$?" ; }
interix() { issystem "Interix" ; return "$?" ; }
hpux()    { issystem "HP-UX"   ; return "$?" ; }
irix()    { issystem "IRIX"    ; return "$?" ; }
aix()     { issystem "AIX"     ; return "$?" ; }
cygwin()  { issystem "CYGWIN"  ; return "$?" ; }
freebsd() { issystem "FreeBSD" ; return "$?" ; }
netbsd()  { issystem "NetBSD"  ; return "$?" ; }
bsdos()   { issystem "BSD/OS"  ; return "$?" ; }
openbsd() { issystem "OpenBSD" ; return "$?" ; }
bsd()     { freebsd || netbsd || bsdos || openbsd ; return "$?" ; }
qnx()     { issystem "QNX"     ; return "$?" ; }
darwin()  { issystem "Darwin"  ; return "$?" ; }
gnu()     { issystem "GNU"     ; return "$?" ; }
osf1()     { issystem "OSF1"    ; return "$?" ; }
mingw32() { issystem "MINGW32" ; return "$?" ; }
morphos() { issystem "MorphOS" ; return "$?" ; }
win32()   { cygwin || mingw32  ; return "$?" ; }
beos()    { issystem "BEOS"    ; return "$?" ; }

# arch test boolean functions
# x86/x86pc is used by QNX
x86_32() {
	case "$host_arch" in
		i[3-9]86|x86|x86pc|k5|k6|k6-2|k6-3|pentium*|athlon*|i586-i686) return 0 ;;
		*) return 1 ;;
	esac
}

x86_64() {
  case "$host_arch" in
    x86_64) return 0 ;;
    *) return 1 ;;
  esac
}

x86() {
  x86_32 || x86_64
}

ppc() {
  case "$host_arch" in
    ppc) return 0;;
    *) return 1;;
  esac
}

alpha() {
  case "$host_arch" in
    alpha) return 0;;
    *) return 1;;
  esac
}

arm() {
	case "$host_arch" in
		arm) return 0;;
		*) return 1;;
	esac
}

sh() {
  case "$host_arch" in
    sh*) return 0;;
    *) return 1;;
  esac
}


# not boolean test: implement the posix shell "!" operator for a
# non-posix /bin/sh.
#   usage:  not {command}
# returns exit status "success" when the execution of "command"
# fails.
not() {
  eval "$@"
  test $? -ne 0
}

# Use this before starting a check
echocheck() {
  echo "============ Checking for $@ ============" >> "$TMPLOG"
  echo ${_echo_n} "Checking for $@ ... ${_echo_c}"
}

# Use this to echo the results of a check
echores() {
  echo "Result is: $@" >> "$TMPLOG"
  echo "##########################################" >> "$TMPLOG"
  echo "" >> "$TMPLOG"
  echo "$@"
}

mangle_homedir_tilde() {
	if test `echo "$1" | cut -c 1` = '~'
	then
		echo "$1" | sed -e "s#\~#$HOME#g"
	else
		echo "$1"
	fi
}

parse_arguments() {
	for parm in "$@" ; do
	  if test "$parm" = "--help" || test "$parm" = "-help" || test "$parm" = "-h" ; then
		cat << EOF

Usage: $0 [OPTIONS]...

Configuration:
  --interfaces=INTERFACE1,INTERFACE2,...
			   configure shogun for a modular interface (recommended) like

				  python_modular
				  r_modular
				  octave_modular
				  java_modular
				  csharp_modular
				  lua_modular
				  ruby_modular

			  or a static one like

				  cmdline_static
				  r_static
				  python_static
				  octave_static
				  matlab_static

  --datatypes=DATATYPE1,DATATYPE2,...
				configure shogun to support these data types (default uint8_t,float32_t,float64_t)

				can be any of

				   char,bool,uint8_t,uint16_t,uint32_t,uint64_t,
				   int8_t,int16_t,int32_t,int64_t,float32_t,float64_t,floatmax_t

  -h, -help, --help              display this help and exit

Installation directories:
  --destdir=DIR                  use this as the root dir to install shogun to [/]
  --prefix=DIR                   use this prefix for installing shogun [/usr/local]
  --bindir=DIR                   use this prefix for installing shogun binary
                                 [PREFIX/bin]
  --datadir=DIR                  use this prefix for installing machine independent
                                 data files (fonts, skins) [PREFIX/share/shogun]
  --mandir=DIR                   use this prefix for installing manpages [PREFIX/man]
  --confdir=DIR                  use this prefix for installing configuration files
                                 [same as datadir]
  --libdir=DIR                   use this prefix for object code libraries [PREFIX/lib]
  --incdir=DIR                   use this prefix for include files [PREFIX/include]
  --pydir=DIR                    use this prefix for python files [PREFIX/auto-detected]
  --rubydir=DIR                  use this prefix for python files [PREFIX/auto-detected]
  --octdir=DIR                   use this prefix for octave files [PREFIX/auto-detected]
  --rdir=DIR                     use this prefix for r files [PREFIX/auto-detected]

Optional features:
  --enable-hdf5                  enable hdf5 file support [auto]
  --enable-json                  enable json file support [auto]
  --enable-xml                   enable xml file support [auto]
  --enable-readline              enable readline in cmdline interface [auto]
  --enable-largefile             enable large file support (64bit file acces routines) [enabled]
  --enable-spinlocks             enable use of spinlocks instead of mutexes [auto]
  --enable-lapack                enable lapack (fast blas and lapack math routines) and use built in ones (slower!) [auto]
  --enable-arpack                enable arpack [auto]
  --enable-superlu               enable SuperLU [auto]
  --enable-cplex                 enable code using CPLEX  [auto]
  --enable-lpsolve               enable code using lpsolve  [auto]
  --enable-glpk                  enable code using GLPK [auto]
  --enable-lzo                   enable code using LZO compression [auto]
  --enable-snappy                enable code using SNAPPY compression [auto]
  --enable-gzip                  enable code using GZIP compression [auto]
  --enable-bzip2                 enable code using BZIP2 compression [auto]
  --enable-lzma                  enable code using LZMA compression [auto]
  --enable-bigstates             enable big (16bit) state [enabled]
  --enable-hmmcache              enable HMM cache [enabled]
  --enable-svm-light             enable building of SVM-light and thus result in pure GPLv3 code [enabled]
  --enable-logcache              enable log (1+exp(x)) log cache (is much faster but less accurate) [disabled]
  --enable-shortrealkernelcache  enable kernelcache to use 4-byte-floating-point values instead of 8-byte-doubles [enabled]
  --enable-logsum-array          enable log sum array supposed to be a bit more accurate [disabled]
  --enable-hmm-parallel          enable parallel structures in hmm training. shogun will then run as many threads as the machine has (much faster) [disabled]

  --disable-hdf5                 disable hdf5 file support [auto]
  --disable-json                 disable json file support [auto]
  --disable-xml                  disable xml file support [auto]
  --disable-readline             disable readline in cmdline interface [auto]
  --disable-largefile            disable large file support (64bit file acces routines) [enabled]
  --disable-lapack               disable lapack (fast blas and lapack math routines) and use built in ones (slower!) [auto]
  --disable-arpack               disable arpack [auto]
  --disable-superlu              disable superlu [auto]
  --disable-cplex                disable code using CPLEX  [auto]
  --disable-lpsolve              disable code using lpsolve  [auto]
  --disable-glpk                 disable code using GLPK [auto]
  --disable-lzo                  disable code using LZO compression [auto]
  --disable-snappy               disable code using SNAPPY compression [auto]
  --disable-gzip                 disable code using GZIP compression [auto]
  --disable-bzip2                disable code using BZIP2 compression [auto]
  --disable-lzma                 disable code using LZMA compression [auto]
  --disable-bigstates            disable big (16bit) state [enabled]
  --disable-hmmcache             disable HMM cache [enabled]
  --disable-svm-light            disable building of SVM-light and thus result in pure GPLv3 code [enabled]
  --disable-logcache             disable log (1+exp(x)) log cache (is much faster but less accurate) [disabled]
  --disable-shortrealkernelcache disable kernelcache to use 4-byte-floating-point values instead of 8-byte-doubles [enabled]
  --disable-logsum-array         disable log sum array supposed to be a bit more accurate [disabled]
  --disable-hmm-parallel         disable parallel structures in hmm training. shogun will then run as many threads as the machine has (much faster) [disabled]

Miscellaneous options:
  --disable-doxygen              disable documentation generation via doxygen for python_modular interface [enabled]
  --disable-optimization         disable compiler optimizations [enabled]
  --disable-cpudetection         disable cpu flags detection and corresponding optimization options [enabled]
  --enable-debug                 enable debugging [enabled]
  --enable-trace-mallocs         enable memory allocation tracing [disabled]
  --disable-reference-counting   disables reference counting causing severe memory leakage [enabled]
  --enable-path-debug            enable viterbi path debugging [disabled]
  --enable-profile               compile profiling information into shogun [disable]
  --enable-static                build a statically linked binary (to the extend possible);
                                 set further linking options with --enable-static="-lm -lglpk"
  --python=python                use this python executable [python]
  --ruby=ruby                    use this ruby executable [ruby]
  --cc=COMPILER                  use this C compiler to build shogun [autodetected]
  --cxx=COMPILER                 use this C++ compiler to build shogun [autodetected]
  --cflags=OPTIONS               use these C compiler options
  --cxxflags=OPTIONS             use these C++ compiler options
  --ldflags=OPTIONS              use these additional linker options
  --target=PLATFORM              target platform (i386-linux, arm-linux, etc)
  --install-path=PATH            the path to a custom install program (useful if
                                 your OS uses a GNU-incompatible install utility by
                                 default and you want to point to the GNU version)
  --includes=DIR                 include DIR when searching for includes
  --libs=DIR                     include DIR when searching for libraries on linking
EOF
		exit 0
	  fi
	done # for parm in ...

	# remove stale log
	rm -f "$TMPLOG"

	# 1st pass checking for vital options
	_install=install
	_ranlib=ranlib
	_as=auto
	_cpudetection=yes

	for ac_option do
	  case "$ac_option" in
	  --datatypes=*)
		_datatypes=`echo $ac_option | cut -d '=' -f 2`
		;;
	  --interfaces=*)
		_cmdline_static=no
		_python_static=no
		_octave_static=no
		_matlab_static=no
		_elwms_static=no
		_r_static=no
		_python_modular=no
		_octave_modular=no
		_r_modular=no
		_ruby_modular=no
		_lua_modular=no
		_java_modular=no
		_csharp_modular=no

		_interfaces=`echo $ac_option | cut -d '=' -f 2`
		for intf in `echo $_interfaces | cut -d '=' -f 2 | tr ',' ' '`
		do
		  case "$intf" in
		  cmdline_static)
			_cmdline_static=yes
			;;
		  python_static)
			_python_static=yes
			;;
		  octave_static)
			_octave_static=yes
			;;
		  matlab_static)
			_matlab_static=yes
			;;
		  r_static)
			_r_static=yes
			;;
		  elwms_static)
			_elwms_static=yes
			;;
		  python_modular)
			_python_modular=yes
			;;
		  octave_modular)
			_octave_modular=yes
			;;
		  r_modular)
			_r_modular=yes
			;;
		  ruby_modular)
			_ruby_modular=yes
			;;
		  lua_modular)
			_lua_modular=yes
			;;
		  java_modular)
			_java_modular=yes
			;;
		  csharp_modular)
			_csharp_modular=yes
			;;
		  *)
			die "Unknown interface: $intf"
			;;
		  esac
		done
		;;
	  --target=*)
		_target=`echo $ac_option | cut -d '=' -f 2`
		;;
	  --python=*)
		PYTHON=`echo $ac_option | cut -d '=' -f 2`
		;;
	  --ruby=*)
		RUBY=`echo $ac_option | cut -d '=' -f 2`
		;;
	  --cc=*)
		CC=`echo $ac_option | cut -d '=' -f 2`
		;;
	  --cxx=*)
		CXX=`echo $ac_option | cut -d '=' -f 2`
		;;
	  --cflags=*)
		COMPFLAGS_C=`echo $ac_option | cut -d '=' -f 2` ;
		;;
	  --cxxflags=*)
		COMPFLAGS_CPP=`echo $ac_option | cut -d '=' -f 2` ;
		;;
	  --ldflags=*)
		  LINKFLAGS=`echo $ac_option | cut -d '=' -f 2` ;
		;;
	  --as=*)
		_as=`echo $ac_option | cut -d '=' -f 2`
		;;
	  --enable-static)
		_ld_static=''
		_static=yes
		;;
	  --disable-static)
		_ld_static=''
		_static=no
		;;
	  --enable-static=*)
		_ld_static="`echo $ac_option | cut -d '=' -f 2`"
		_static=yes
		;;
	  --includes=*)
		_inc_extra="-I`echo $ac_option | cut -d '=' -f 2 | sed 's,:, -I,g'` $_inc_extra"
		INCLUDES="$INCLUDES $_inc_extra"
		;;
	  --libs=*)
		_ld_extra="-L`echo $ac_option | cut -d '=' -f 2 | sed 's,:, -L,g'` $_ld_extra"
		LINKFLAGS="$_ld_extra $LINKFLAGS "
		;;
	  --install-path=*)
		_install=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2 | sed 's/\/$//'`/install")
		;;
	  --disable-hdf5)		_hdf5=no	;;
	  --enable-hdf5)		_hdf5=yes	;;
	  --disable-json)		_json=no	;;
	  --enable-json)		_json=yes	;;
	  --disable-xml)		_xml=no	        ;;
	  --enable-xml)		        _xml=yes	;;
	  --disable-svm-light)	_svm_light=no;	_gpl=yes;;
	  --enable-svm-light)	_svm_light=yes; _gpl=no;;
	  --disable-readline)	_readline=no	;;
	  --enable-readline)	_readline=yes	;;
	  --disable-logcache)	_logcache=no	;;
	  --enable-logcache)	_logcache=yes	;;
	  --disable-shortrealkernelcache)	_shortrealkernelcache=no	;;
	  --enable-shortrealkernelcache)	_shortrealkernelcache=yes	;;
	  --disable-logsum-array)_logsumarray=no	;;
	  --enable-logsum-array)_logsumarray=yes	;;
	  --disable-hmm-parallel)	_hmmparallel=no	;;
	  --enable-hmm-parallel)	_hmmparallel=yes	;;
	  --disable-largefile)	_largefile=no	;;
	  --enable-largefile)	_largefile=yes	;;
	  --enable-doxygen)		_doxygen=yes ;;
	  --disable-doxygen)	_doxygen=no ;;
	  --disable-lapack)		_lapack=no	;;
	  --enable-lapack)		_lapack=yes	;;
	  --disable-arpack)		_arpack=no	;;
	  --enable-arpack)		_arpack=yes	;;
	  --disable-superlu)		_superlu=no	;;
	  --enable-superlu)		_superlu=yes	;;
	  --disable-cplex)		_cplex=no	;;
	  --enable-cplex)		_cplex=yes	;;
	  --disable-spinlocks)	_spinlocks=no	;;
	  --enable-spinlocks)	_spinlocks=yes	;;
	  --disable-lpsolve)	_lpsolve=no	;;
	  --enable-lpsolve)		_lpsolve=yes;;
	  --disable-glpk)		_glpk=no;;
	  --enable-glpk)		_glpk=yes;;
	  --disable-lzo)		_lzo=no;;
	  --enable-lzo)			_lzo=yes;;
	  --disable-snappy)		_snappy=no;;
	  --enable-snappy)		_snappy=yes;;
	  --disable-gzip)		_gzip=no;;
	  --enable-gzip)		_gzip=yes;;
	  --disable-bzip2)		_bzip2=no;;
	  --enable-bzip2)		_bzip2=yes;;
	  --disable-lzma)		_lzma=no;;
	  --enable-lzma)		_lzma=yes;;
	  --disable-doxygen)	_doxygen=no	;;
	  --enable-doxygen)		_doxygen=yes	;;
	  --disable-bigstates)	_bigstates=no	;;
	  --enable-bigstates)	_bigstates=yes	;;
	  --disable-hmmcache)	_hmmcache=no	;;
	  --enable-hmmcache)	_hmmcache=yes	;;
	  --disable-debug) 		_debug=no ;;
	  --enable-debug) 		_debug=yes ;;
	  --disable-trace-mallocs) _trace_mallocs=no ;;
	  --enable-trace-mallocs) _trace_mallocs=yes ;;
	  --disable-reference-counting) _reference_counting=no ;;
	  --enable-reference-counting) _reference_counting=yes ;;
	  --disable-hmm-debug) 	_hmmdebug=no ;;
	  --enable-hmm-debug)   _hmmdebug=yes ;;
	  --disable-path-debug)	_pathdebug=no ;;
	  --enable-path-debug)  _pathdebug=yes ;;
	  --disable-profile) 	_profile=no ;;
	  --enable-profile) 	_profile=yes ;;
	  --disable-cpudetection) 	_cpudetection=no ;;
	  --enable-cpudetection) 	_cpudetection=yes ;;
	  --disable-optimization)	_optimization=no ;;
	  --enable-optimization)	_optimization=yes ;;
	  --destdir=*)
		_destdir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --prefix=*)
		_prefix=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --bindir=*)
		_bindir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --datadir=*)
		_datadir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --mandir=*)
		_mandir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --confdir=*)
		_confdir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --libdir=*)
		_libdir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --incdir=*)
		_incdir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --pydir=*)
		_pydir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --rubydir=*)
		_rubydir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --octdir=*)
		_octdir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;
	  --rdir=*)
		_rdir=$(mangle_homedir_tilde "`echo $ac_option | cut -d '=' -f 2`")
		;;

	  *)
		die "Unknown parameter: $ac_option"
		;;
	  esac
	done
}

set_install_dirs() {
	test -z "$_bindir" && _bindir="$_prefix/bin"
	test -z "$_datadir" && _datadir="$_prefix/share/shogun"
	test -z "$_mandir" && _mandir="$_prefix/man"
	test -z "$_confdir" && _confdir="$_prefix/etc/shogun"
	test -z "$_libdir" && _libdir="$_prefix/lib"
	test -z "$_incdir" && _incdir="$_prefix/include"
	test -z "$_rubydir" && _rubydir="site_ruby"

	python_major=`$PYTHON -V 2>&1 | cut -f 2 -d ' ' | cut -f 1 -d '.'`
	python_minor=`$PYTHON -V 2>&1 | cut -f 2 -d ' ' | cut -f 2 -d '.'`
	if test -z "$_pydir" ; then
		if test "$python_major" -ge 2 && test "$python_minor" -ge 6 ; then
			_pydir="`$PYTHON -c 'import sys; print \"python\"+sys.version[0:3]'`/dist-packages"
		else
			_pydir="`$PYTHON -c 'import sys; print \"python\"+sys.version[0:3]'`/site-packages"
		fi
	fi
}

determine_system_name()
{
	# Determine our OS name and CPU architecture
	if test -z "$_target" ; then
	  # OS name
	  system_name=`( uname -s ) 2>&1`
	  case "$system_name" in
	  Linux|FreeBSD|NetBSD|BSD/OS|OpenBSD|SunOS|QNX|Darwin|GNU|OSF1|BeOS|MorphOS|AIX)
		;;
	  IRIX*)
		system_name=IRIX
		;;
	  HP-UX*)
		system_name=HP-UX
		;;
	  [cC][yY][gG][wW][iI][nN]*)
		system_name=CYGWIN
		;;
	  Interix)
		system_name=INTERIX
		;;
	  *)
		system_name="$system_name-UNKNOWN"
		;;
	  esac


	  # host's CPU/instruction set
	   host_arch=`( uname -p ) 2>&1`
	   case "$host_arch" in
	   i386|sparc|ppc|alpha|arm|mips)
		 if [ $host_arch=="Darwin" -a "`( uname -m ) 2>&1`"=="x86_64" ]; then
			host_arch=x86_64
		 fi
		 ;;
	   powerpc) # Darwin returns 'powerpc'
		 host_arch=ppc
		 ;;
	   *) # uname -p on Linux returns 'unknown' for the processor type,
		  # OpenBSD returns 'Intel Pentium/MMX ("Genuine Intel" 586-class)'

		  # Maybe uname -m (machine hardware name) returns something we
		  # recognize.

		  # x86/x86pc is used by QNX
		  case "`( uname -m ) 2>&1`" in
		  i[3-9]86*|x86|x86pc|k5|k6|k6_2|k6_3|k6-2|k6-3|pentium*|athlon*|i586_i686|i586-i686|BePC) host_arch=i386 ;;
		  ia64) host_arch=ia64 ;;
		  x86_64|amd64)
			if [ -n "`$COMP_C -dumpmachine 2>&1 | sed -n '/^x86_64-/p;/^amd64-/p'`" -a \
				 -z "`echo $COMPFLAGS_C | grep -- -m32`"  ]; then
			  host_arch=x86_64
			else
			  host_arch=i386
			fi
		  ;;
		  macppc|ppc|ppc64) host_arch=ppc ;;
		  alpha) host_arch=alpha ;;
		  sparc) host_arch=sparc ;;
		  sparc64) host_arch=sparc64 ;;
		  parisc*|hppa*|9000*) host_arch=hppa ;;
		  arm*|zaurus|cats) host_arch=arm ;;
		  s390) host_arch=s390 ;;
		  s390x) host_arch=s390x ;;
		  mips) host_arch=mips ;;
		  vax) host_arch=vax ;;
		  m68|m68k) host_arch=m68 ;;
		  sh*) host_arch=sh ;;
		  *) host_arch=UNKNOWN ;;
		esac
		;;
	  esac
	else
	  system_name=`echo $_target | cut -d '-' -f 2`
	  case "`echo $system_name | tr A-Z a-z`" in
		linux) system_name=Linux ;;
		freebsd) system_name=FreeBSD ;;
		netbsd) system_name=NetBSD ;;
		bsd/os) system_name=BSD/OS ;;
		openbsd) system_name=OpenBSD ;;
		sunos) system_name=SunOS ;;
		qnx) system_name=QNX ;;
		morphos) system_name=MorphOS ;;
		mingw32msvc) system_name=MINGW32 ;;
	  esac
	  # We need to convert underscores so that values like k6-2 and pentium-mmx can be passed
	  host_arch=`echo $_target | cut -d '-' -f 1 | tr '_' '-'`
	fi

	OS="`echo $system_name | tr 'a-z-' 'A-Z_'`"
	echo "Detected Operating System: $system_name"
	echo "Detected Host Architecture: $host_arch"
}

determine_march_mtune()
{

# XXX: this should be ok..
_cpuinfo="echo"
# Cygwin has /proc/cpuinfo, but only supports Intel CPUs
# FIXME: Remove the cygwin check once AMD CPUs are supported
if test -r /proc/cpuinfo && not cygwin; then
  # Linux with /proc mounted, extract CPU information from it
  _cpuinfo="cat /proc/cpuinfo"
elif test -r /compat/linux/proc/cpuinfo && not x86 ; then
  # FreeBSD with Linux emulation /proc mounted,
  # extract CPU information from it
  _cpuinfo="cat /compat/linux/proc/cpuinfo"
elif darwin && not x86 ; then
  # use hostinfo on Darwin
  _cpuinfo="hostinfo"
elif aix; then
  # use 'lsattr' on AIX
  _cpuinfo="lsattr -E -l proc0 -a type"
elif x86; then
  # all other OSes try to extract CPU information from a small helper
  # program .cpuinfo
cat >.cpuinfo.c <<EOF
/* small utility to extract CPU information
   Used by configure to set CPU optimization levels on some operating
   systems where /proc/cpuinfo is non-existent or unreliable. */

#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if defined(__MINGW32__) && (__MINGW32_MAJOR_VERSION <= 3) && (__MINGW32_MINOR_VERSION < 10)
#include <sys/timeb.h>
void gettimeofday(struct timeval* t,void* timezone) {
  struct timeb timebuffer;
  ftime( &timebuffer );
  t->tv_sec=timebuffer.time;
  t->tv_usec=1000*timebuffer.millitm;
}
#endif
#ifdef __MINGW32__
#define MISSING_USLEEP
#define sleep(t) _sleep(1000*t);
#endif

#ifdef __BEOS__
#define usleep(t) snooze(t)
#endif

#ifdef M_UNIX
typedef long long int64_t;
#define MISSING_USLEEP
#else
#include <inttypes.h>
#endif

#ifdef __INTERIX
#include <stdint.h>
#endif

typedef struct cpuid_regs {
  unsigned int eax;
  unsigned int ebx;
  unsigned int ecx;
  unsigned int edx;
} cpuid_regs_t;

static cpuid_regs_t
cpuid(int func) {
  cpuid_regs_t regs;
#define CPUID   ".byte 0x0f, 0xa2; "
#ifdef __x86_64__
  asm("mov %%rbx, %%rsi\n\t"
#else
  asm("mov %%ebx, %%esi\n\t"
#endif
      CPUID"\n\t"
#ifdef __x86_64__
      "xchg %%rsi, %%rbx\n\t"
#else
      "xchg %%esi, %%ebx\n\t"
#endif
      : "=a" (regs.eax), "=S" (regs.ebx), "=c" (regs.ecx), "=d" (regs.edx)
      : "0" (func));
  return regs;
}


static int64_t
rdtsc(void)
{
  uint64_t i;
#define RDTSC   ".byte 0x0f, 0x31; "
  asm volatile (RDTSC : "=A"(i) : );
  return i;
}

static const char*
brandname(int i)
{
  const static char* brandmap[] = {
    NULL,
    "Intel(R) Celeron(R) processor",
    "Intel(R) Pentium(R) III processor",
    "Intel(R) Pentium(R) III Xeon(tm) processor",
    "Intel(R) Pentium(R) III processor",
    NULL,
    "Mobile Intel(R) Pentium(R) III processor-M",
    "Mobile Intel(R) Celeron(R) processor"
  };

  if (i >= sizeof(brandmap))
    return NULL;
  else
    return brandmap[i];
}

static void
store32(char *d, unsigned int v)
{
  d[0] =  v        & 0xff;
  d[1] = (v >>  8) & 0xff;
  d[2] = (v >> 16) & 0xff;
  d[3] = (v >> 24) & 0xff;
}


int
main(int argc, char **argv)
{
  cpuid_regs_t regs, regs_ext;
  char idstr[13];
  unsigned max_cpuid;
  unsigned max_ext_cpuid;
  unsigned int amd_flags;
  unsigned int amd_flags2;
  const char *model_name = NULL;
  int i;
  char processor_name[49];

  regs = cpuid(0);
  max_cpuid = regs.eax;
  /* printf("%d CPUID function codes\n", max_cpuid+1); */

  store32(idstr+0, regs.ebx);
  store32(idstr+4, regs.edx);
  store32(idstr+8, regs.ecx);
  idstr[12] = 0;
  printf("vendor_id\t: %s\n", idstr);

  regs_ext = cpuid((1<<31) + 0);
  max_ext_cpuid = regs_ext.eax;
  if (max_ext_cpuid >= (1<<31) + 1) {
    regs_ext = cpuid((1<<31) + 1);
    amd_flags = regs_ext.edx;
    amd_flags2 = regs_ext.ecx;

    if (max_ext_cpuid >= (1<<31) + 4) {
      for (i = 2; i <= 4; i++) {
        regs_ext = cpuid((1<<31) + i);
        store32(processor_name + (i-2)*16, regs_ext.eax);
        store32(processor_name + (i-2)*16 + 4, regs_ext.ebx);
        store32(processor_name + (i-2)*16 + 8, regs_ext.ecx);
        store32(processor_name + (i-2)*16 + 12, regs_ext.edx);
      }
      processor_name[48] = 0;
      model_name = processor_name;
    }
  } else {
    amd_flags = 0;
    amd_flags2 = 0;
  }

  if (max_cpuid >= 1) {
    static struct {
      int bit;
      char *desc;;
      char *description;
    } cap[] = {
      { 0,  "fpu",   "Floating-point unit on-chip" },
      { 1,  "vme",   "Virtual Mode Enhancements" },
      { 2,  "de",    "Debugging Extension" },
      { 3,  "pse",   "Page Size Extension" },
      { 4,  "tsc",   "Time Stamp Counter" },
      { 5,  "msr",   "Pentium Processor MSR" },
      { 6,  "pae",   "Physical Address Extension" },
      { 7,  "mce",   "Machine Check Exception" },
      { 8,  "cx8",   "CMPXCHG8B Instruction Supported" },
      { 9,  "apic",  "On-chip APIC Hardware Enabled" },
      { 11, "sep",   "SYSENTER and SYSEXIT" },
      { 12, "mtrr",  "Memory Type Range Registers" },
      { 13, "pge",   "PTE Global Bit" },
      { 14, "mca",   "Machine Check Architecture" },
      { 15, "cmov",  "Conditional Move/Compare Instruction" },
      { 16, "pat",   "Page Attribute Table" },
      { 17, "pse36", "Page Size Extension 36-bit" },
      { 18, "pn",    "Processor Serial Number" },
      { 19, "cflsh", "CFLUSH instruction" },
      { 21, "dts",   "Debug Store" },
      { 22, "acpi",  "Thermal Monitor and Clock Ctrl" },
      { 23, "mmx",   "MMX Technology" },
      { 24, "fxsr",  "FXSAVE/FXRSTOR" },
      { 25, "sse",   "SSE Extensions" },
      { 26, "sse2",  "SSE2 Extensions" },
      { 27, "ss",    "Self Snoop" },
      { 28, "ht",    "Multi-threading" },
      { 29, "tm",    "Therm. Monitor" },
      { 30, "ia64",  "IA-64 Processor" },
      { 31, "pbe",   "Pend. Brk. EN." },
      { -1 }
    };
    static struct {
      int bit;
      char *desc;
      char *description;
    } cap2[] = {
      { 0, "pni", "SSE3 Extensions" },
      { 3, "monitor", "MONITOR/MWAIT" },
      { 4, "ds_cpl", "CPL Qualified Debug Store" },
      { 5, "vmx", "Virtual Machine Extensions" },
      { 7, "est", "Enhanced Intel SpeedStep Technology" },
      { 8, "tm2", "Thermal Monitor 2" },
      { 10, "cid", "L1 Context ID" },
      { 13, "cx16", "CMPXCHG16B Available" },
      { 14, "xtpr", "xTPR Disable" },
      { -1 }
    };
    static struct {
      int bit;
      char *desc;;
      char *description;
    } cap_amd[] = {
      { 11, "syscall", "SYSCALL and SYSRET" },
      { 19, "mp", "MP Capable" },
      { 20, "nx", "No-Execute Page Protection" },
      { 22, "mmxext","MMX Technology (AMD Extensions)" },
      { 25, "fxsr_opt", "Fast FXSAVE/FXRSTOR" },
      { 27, "rdtscp", "RDTSCP Instruction" },
      { 29, "lm", "Long Mode Capable" },
      { 30, "3dnowext","3DNow! Extensions" },
      { 31, "3dnow", "3DNow!" },
      { -1 }
    };
    static struct {
      int bit;
      char *desc;
      char *description;
    } cap_amd2[] = {
      { 0, "lahf_lm", "LAHF/SAHF Supported in 64-bit Mode" },
      { 1, "cmp_legacy", "Chip Multi-Core" },
      { 2, "svm", "Secure Virtual Machine" },
      { 4, "cr8legacy", "CR8 Available in Legacy Mode" },
      { -1 }
    };
    unsigned int family, model, stepping;

    regs = cpuid(1);
    family = (regs.eax >> 8) & 0xf;
    model = (regs.eax >> 4) & 0xf;
    stepping = regs.eax & 0xf;

    if (family == 0xf)
    {
      family += (regs.eax >> 20) & 0xff;
      model += ((regs.eax >> 16) & 0xf) << 4;
    }

    printf("cpu family\t: %d\n"
           "model\t\t: %d\n"
           "stepping\t: %d\n" ,
           family,
           model,
           stepping);

    if (strstr(idstr, "Intel") && !model_name) {
      if (family == 6 && model == 0xb && stepping == 1)
        model_name = "Intel (R) Celeron (R) processor";
      else
        model_name = brandname(regs.ebx & 0xf);
    }

    printf("flags\t\t:");
    for (i = 0; cap[i].bit >= 0; i++) {
      if (regs.edx & (1 << cap[i].bit)) {
        printf(" %s", cap[i].desc);
      }
    }
    for (i = 0; cap2[i].bit >= 0; i++) {
      if (regs.ecx & (1 << cap2[i].bit)) {
        printf(" %s", cap2[i].desc);
      }
    }
    /* k6_mtrr is supported by some AMD K6-2/K6-III CPUs but
       it is not indicated by a CPUID feature bit, so we
       have to check the family, model and stepping instead. */
    if (strstr(idstr, "AMD") &&
        family == 5 &&
        (model >= 9 || model == 8 && stepping >= 8))
      printf(" %s", "k6_mtrr");
    /* similar for cyrix_arr. */
    if (strstr(idstr, "Cyrix") &&
        (family == 5 && model < 4 || family == 6))
      printf(" %s", "cyrix_arr");
    /* as well as centaur_mcr. */
    if (strstr(idstr, "Centaur") &&
        family == 5)
      printf(" %s", "centaur_mcr");

    for (i = 0; cap_amd[i].bit >= 0; i++) {
      if (amd_flags & (1 << cap_amd[i].bit)) {
        printf(" %s", cap_amd[i].desc);
      }
    }
    for (i = 0; cap_amd2[i].bit >= 0; i++) {
      if (amd_flags2 & (1 << cap_amd2[i].bit)) {
        printf(" %s", cap_amd2[i].desc);
      }
    }
    printf("\n");

    if (regs.edx & (1 << 4)) {
      int64_t tsc_start, tsc_end;
      struct timeval tv_start, tv_end;
      int usec_delay;

      tsc_start = rdtsc();
      gettimeofday(&tv_start, NULL);
#ifdef  MISSING_USLEEP
      sleep(1);
#else
      usleep(100000);
#endif
      tsc_end = rdtsc();
      gettimeofday(&tv_end, NULL);

      usec_delay = 1000000 * (tv_end.tv_sec - tv_start.tv_sec)
        + (tv_end.tv_usec - tv_start.tv_usec);

      printf("cpu MHz\t\t: %.3f\n",
             (double)(tsc_end-tsc_start) / usec_delay);
    }
  }

  printf("model name\t: ");
  if (model_name)
    printf("%s\n", model_name);
  else
    printf("Unknown %s CPU\n", idstr);
}
EOF

		$COMP_C -o .cpuinfo .cpuinfo.c
		_cpuinfo="./.cpuinfo"
	fi

if x86 || x86_64 ; then
  # gather more CPU information
  pname=`$_cpuinfo | grep 'model name' | cut -d ':' -f 2 | _head 1`
  pvendor=`$_cpuinfo | grep 'vendor_id' | cut -d ':' -f 2  | cut -d ' ' -f 2 | _head 1`
  pfamily=`$_cpuinfo | grep 'cpu family' | cut -d ':' -f 2 | cut -d ' ' -f 2 | _head 1`
  pmodel=`$_cpuinfo | grep -v 'model name' | grep 'model' | cut -d ':' -f 2 | cut -d ' ' -f 2 | _head 1`
  pstepping=`$_cpuinfo | grep 'stepping' | cut -d ':' -f 2 | cut -d ' ' -f 2 | _head 1`

  exts=`$_cpuinfo | egrep 'features|flags' | cut -d ':' -f 2 | _head 1`

  pparam=`echo $exts | sed -e s/k6_mtrr/mtrr/ -e s/cyrix_arr/mtrr/ -e s/centaur_mcr/mtrr/ \
                           -e s/xmm/sse/ -e s/kni/sse/`

  for ext in $pparam ; do
    eval test \"\$_$ext\" = auto 2>/dev/null && eval _$ext=kernel_check
  done

  # SSE implies MMX2, but not all SSE processors report the mmxext CPU flag.
  test $_sse = kernel_check && _mmxext=kernel_check

  echocheck "CPU vendor"
  echores "$pvendor ($pfamily:$pmodel:$pstepping)"

  echocheck "CPU type"
  echores "$pname"
fi

case "$host_arch" in
  i[3-9]86|x86|x86pc|k5|k6|k6-2|k6-3|pentium*|athlon*|i586-i686)
  _def_arch="#define ARCH_X86 1"
  _target_arch="TARGET_ARCH_X86 = yes"

  case "$pvendor" in
  AuthenticAMD)
    case "$pfamily" in
    3) proc=i386 iproc=386 ;;
    4) proc=i486 iproc=486 ;;
    5) iproc=586       # LGB: models are: K5/SSA5 K5 K5 K5 ? ? K6 K6 K6-2 K6-3
	# K6 model 13 are the K6-2+ and K6-III+, only differing in cache size.
	if test "$pmodel" -eq 9 -o "$pmodel" -eq 13; then
		proc=k6-3
	elif test "$pmodel" -ge 8; then
		proc=k6-2
	elif test "$pmodel" -ge 6; then
		proc=k6
	else
		proc=i586
	fi
	;;
    6) iproc=686
	# It's a bit difficult to determine the correct type of Family 6
	# AMD CPUs just from their signature. Instead, we check directly
	# whether it supports SSE.
	if test "$_sse" = yes; then
		# gcc treats athlon-xp, athlon-4 and athlon-mp similarly.
		proc=athlon-xp
	else
		# Again, gcc treats athlon and athlon-tbird similarly.
		proc=athlon
	fi
	;;
    15) iproc=686
    	# k8 cpu-type only supported in gcc >= 3.4.0, but that will be
    	# caught and remedied in the optimization tests below.
    	proc=k8
    	;;

    *) proc=k8 iproc=686 ;;
    esac
    ;;
  GenuineIntel)
    case "$pfamily" in
    3) proc=i386 iproc=386 ;;
    4) proc=i486 iproc=486 ;;
    5) iproc=586
	if test "$pmodel" -eq 4 || test "$pmodel" -eq 8; then
		proc=pentium-mmx # 4 is desktop, 8 is mobile
	else
		proc=i586
	fi
	;;
    6) iproc=686
	if test "$pmodel" -ge 15; then
		proc=core2
	elif test "$pmodel" -eq 9 -o "$pmodel" -ge 13; then
		proc=pentium-m
	elif test "$pmodel" -ge 7; then
		proc=pentium3
	elif test "$pmodel" -ge 3; then
		proc=pentium2
	else
		proc=i686
	fi
	;;
    15) iproc=686
	# A nocona in 32-bit mode has no more capabilities than a prescott.
	if test "$pmodel" -ge 3; then
		proc=prescott
	else
		proc=pentium4
	fi
	;;
    *) proc=prescott iproc=686 ;;
    esac
    ;;
  CentaurHauls)
    case "$pfamily" in
    5) iproc=586
	if test "$pmodel" -ge 8; then
		proc=winchip2
	elif test "$pmodel" -ge 4; then
		proc=winchip-c6
	else
		proc=i586
	fi
	;;
    6) iproc=686
	if test "$pmodel" -ge 9; then
		proc=c3-2
	else
		proc=c3
		iproc=586
	fi
	;;
    *) proc=i686 iproc=i686 ;;
    esac
    ;;
  unknown)
    case "$pfamily" in
    3) proc=i386 iproc=386 ;;
    4) proc=i486 iproc=486 ;;
    *) proc=i586 iproc=586 ;;
    esac
    ;;
  *)
    proc=i586 iproc=586 ;;
  esac

    # check that gcc supports our CPU, if not, fall back to earlier ones
    # LGB: check -mcpu and -march swithing step by step with enabling
    # to fall back till 386.

    # gcc >= 3.4.0  doesn't support -mcpu, we have to use -mtune instead

    if [ "$cc_vendor" = "gnu" ] && ([ "$_cc_major" -gt 3 ] || ( [ "$_cc_major" = 3 ] && [ "$_cc_minor" -ge 4 ])) ; then
	cpuopt=-mtune
    else
	cpuopt=-mcpu
    fi

    echocheck "GCC & CPU optimization abilities"
cat > $TMPC << EOF
int main(void) { return 0; }
EOF
  if test "$_cpudetection" = yes ; then
	cc_check -march=native && proc=native
    if test "$proc" = "k8" -o "$proc" = "opteron" \
		-o "$proc" = "athlon64" -o "$proc" = "athlon-fx" ; then
      cc_check -march=$proc $cpuopt=$proc || proc=athlon-xp
    fi
    if test "$proc" = "athlon-xp" || test "$proc" = "athlon-4" \
		|| test "$proc" = "athlon-tbird"; then
      cc_check -march=$proc $cpuopt=$proc || proc=athlon
    fi
    if test "$proc" = "k6-3" || test "$proc" = "k6-2"; then
      cc_check -march=$proc $cpuopt=$proc  || proc=k6
    fi
    if test "$proc" = "k6" || test "$proc" = "c3"; then
      if not cc_check -march=$proc $cpuopt=$proc; then
        if cc_check -march=i586 $cpuopt=i686; then
          proc=i586-i686
        else
          proc=i586
	fi
      fi
    fi
    if test "$proc" = "prescott" ; then
      cc_check -march=$proc $cpuopt=$proc || proc=pentium4
    fi
    if test "$proc" = "core2" ; then
      cc_check -march=$proc $cpuopt=$proc || proc=pentium-m
    fi
    if test "$proc" = "pentium4" || test "$proc" = "pentium-m" || test "$proc" = "pentium3" || test "$proc" = "pentium2" || test "$proc" = "athlon" || test "$proc" = "c3-2" || test "$proc" = "geode"; then
      cc_check -march=$proc $cpuopt=$proc  || proc=i686
    fi
    if test "$proc" = "i686" || test "$proc" = "pentium-mmx" || test "$proc" = "winchip-c6" || test "$proc" = "winchip2"; then
      cc_check -march=$proc $cpuopt=$proc  || proc=i586
    fi
    if test "$proc" = "i586"; then
      cc_check -march=$proc $cpuopt=$proc  || proc=i486
    fi
    if test "$proc" = "i486" ; then
      cc_check -march=$proc $cpuopt=$proc  || proc=i386
    fi
    if test "$proc" = "i386" ; then
      cc_check -march=$proc $cpuopt=$proc  || proc=error
    fi
    if test "$proc" = "error" ; then
        echores "CPU optimization disabled. CPU not recognized or your compiler is too old."
      _mcpu=""
      _march=""
    elif test "$proc" = "i586-i686"; then
      _march="-march=i586"
      _mcpu="$cpuopt=i686"
    else
      _march="-march=$proc"
      _mcpu="$cpuopt=$proc"
    fi
  else # if test "$_cpudetection" = yes
    # i686 is probably the most common CPU - optimize for it
    _mcpu="$cpuopt=i686"
    # at least i486 required, for bswap instruction
    _march="-march=i486"
    cc_check $_mcpu || _mcpu=""
    cc_check $_march $_mcpu || _march=""
  fi

    ## Gabucino : --target takes effect here (hopefully...) by overwriting
    ##             autodetected mcpu/march parameters
    if test "$_target" ; then
      # TODO: it may be a good idea to check GCC and fall back in all cases
      if test "$host_arch" = "i586-i686"; then
        _march="-march=i586"
        _mcpu="$cpuopt=i686"
      else
        _march="-march=$host_arch"
        _mcpu="$cpuopt=$host_arch"
      fi

      proc="$host_arch"

      case "$proc" in
        i386) iproc=386 ;;
        i486) iproc=486 ;;
        i586|k5|k6|k6-2|k6-3|pentium|pentium-mmx) iproc=586 ;;
        i686|athlon*|pentium*) iproc=686 ;;
        *) iproc=586 ;;
      esac
    fi

    echores "$proc"
    ;;

  ia64)
    _def_arch='#define ARCH_IA64 1'
    _target_arch='TARGET_ARCH_IA64 = yes'
    iproc='ia64'
    proc=''
    _march=''
    _mcpu=''
    ;;

  x86_64|amd64)
    _def_arch='#define ARCH_X86_64 1'
    _target_arch='TARGET_ARCH_X86_64 = yes'
    iproc='x86_64'

    # gcc >= 3.4.0  doesn't support -mcpu, we have to use -mtune instead
    if test "$cc_vendor" = "gnu" && test "$_cc_major" -gt 3 -o "$_cc_major" -eq 3 -a "$_cc_minor" -ge 4 ; then
	cpuopt=-mtune
    else
	cpuopt=-mcpu
    fi
    case "$pvendor" in
    AuthenticAMD)
      proc=k8;;
    GenuineIntel)
      # 64-bit prescotts exist, but as far as GCC is concerned they have the
      # same capabilities as a nocona.
      proc=nocona;;
    *)
      proc=error;;
    esac

    echocheck "GCC & CPU optimization abilities"
cat > $TMPC << EOF
int main(void) { return 0; }
EOF
    # This is a stripped-down version of the i386 fallback.
    if test "$_cpudetection" = yes ; then
      cc_check -march=native && proc=native
      # --- AMD processors ---
      if test "$proc" = "k8" -o "$proc" = "opteron" -o "$proc" = "athlon64" -o "$proc" = "athlon-fx" ; then
        cc_check -march=$proc $cpuopt=$proc || proc=athlon-xp
      fi
      # This will fail if gcc version < 3.3, which is ok because earlier
      # versions don't really support 64-bit on amd64.
      # Is this a valid assumption? -Corey
      if test "$proc" = "athlon-xp" || test "$proc" = "athlon-4" ; then
        cc_check -march=$proc $cpuopt=$proc || proc=error
      fi
      # --- Intel processors ---
      if test "$proc" = "core2"; then
        cc_check -march=$proc $cpuopt=$proc || proc=nocona
      fi
      if test "$proc" = "nocona" || test "$proc" = "prescott" ; then
        cc_check -march=$proc $cpuopt=$proc || proc=pentium4
      fi
      if test "$proc" = "pentium4" || test "$proc" = "pentium4m" ; then
        cc_check -march=$proc $cpuopt=$proc || proc=error
      fi

      _march="-march=$proc"
      _mcpu="$cpuopt=$proc"
      if test "$proc" = "error" ; then
        echores "CPU optimization disabled. CPU not recognized or your compiler is too old."
        _mcpu=""
        _march=""
      fi
    else # if test "$_cpudetection" = yes
      # x86-64 is an undocumented option, an intersection of k8 and nocona.
      _march="-march=x86-64"
      _mcpu="$cpuopt=generic"
      cc_check $_mcpu || _mcpu="x86-64"
      cc_check $_mcpu || _mcpu=""
      cc_check $_march $_mcpu || _march=""
    fi

    echores "$proc"
    ;;

  sparc)
    _def_arch='#define ARCH_SPARC 1'
    _target_arch='TARGET_ARCH_SPARC = yes'
    iproc='sparc'
    if sunos ; then
	echocheck "CPU type"
	karch=`uname -m`
	case "`echo $karch`" in
	    sun4) proc=v7 ;;
	    sun4c) proc=v7 ;;
	    sun4d) proc=v8 ;;
	    sun4m) proc=v8 ;;
	    sun4u) proc=v9 _vis='yes' _def_vis='#define HAVE_VIS = yes' ;;
	    sun4v) proc=v9 ;;
	    *) proc=v8 ;;
	esac
	echores "$proc"
    else
	proc=v8
    fi
    _march=''
    _mcpu="-mcpu=$proc"
    ;;

  sparc64)
    _def_arch='#define ARCH_SPARC 1'
    _target_arch='TARGET_ARCH_SPARC = yes'
    _vis='yes'
    _def_vis='#define HAVE_VIS = yes'
    iproc='sparc'
    proc='v9'
    _march=''
    _mcpu="-mcpu=$proc"
    ;;

  arm|armv4l|armv5tel)
    _def_arch='#define ARCH_ARMV4L 1'
    _target_arch='TARGET_ARCH_ARMV4L = yes'
    iproc='arm'
    proc=''
    _march=''
    _mcpu=''
    ;;
  sh)
    _def_arch='#define ARCH_SH 1'
    _target_arch='TARGET_ARCH_SH = yes'
    iproc='sh'
    proc=''
    _march=''
    _mcpu=''
    ;;

  ppc)
    _def_arch='#define ARCH_POWERPC 1'
    _def_dcbzl='#define NO_DCBZL 1'
    _target_arch='TARGET_ARCH_POWERPC = yes'
    iproc='ppc'
    proc=''
    _march=''
    _mcpu=''
    _altivec=no

    echocheck "CPU type"
    case $system_name in
      Linux)
	proc=`$_cpuinfo | grep 'cpu' | cut -d ':' -f 2 | cut -d ',' -f 1 | cut -b 2- | _head 1`
	if test -n "`$_cpuinfo | grep altivec`"; then
	    _altivec=yes
	fi
        ;;
      Darwin)
	proc=`$_cpuinfo | grep "Processor type" | cut -f 3 -d ' ' | sed 's/ppc//'`
	if [ `sysctl -n hw.vectorunit` -eq 1 -o \
	    "`sysctl -n hw.optional.altivec 2> /dev/null`" = "1" ]; then
	    _altivec=yes
	fi
        ;;
      NetBSD)
        # only gcc 3.4 works reliably with AltiVec code under NetBSD
	case $cc_version in
	    2*|3.0*|3.1*|3.2*|3.3*)
		;;
	    *)
		if [ `sysctl -n machdep.altivec` -eq 1 ]; then
		    _altivec=yes
		fi
		;;
	esac
        ;;
      AIX)
	proc=`$_cpuinfo | grep 'type' | cut -f 2 -d ' ' | sed 's/PowerPC_//'`
	;;
    esac
    if test "$_altivec" = yes; then
        echores "$proc altivec"
    else
        echores "$proc"
    fi

    echocheck "GCC & CPU optimization abilities"

    if test -n "$proc"; then
        case "$proc" in
	    601) _march='-mcpu=601' _mcpu='-mtune=601' ;;
	    603) _march='-mcpu=603' _mcpu='-mtune=603' ;;
	    603e|603ev) _march='-mcpu=603e' _mcpu='-mtune=603e' ;;
	    604|604e|604r|604ev) _march='-mcpu=604' _mcpu='-mtune=604' ;;
	    740|740/750|745/755) _march='-mcpu=740' _mcpu='-mtune=740' ;;
	    750|750CX) _march='-mcpu=750' _mcpu='-mtune=750' ;;
	    POWER)  _march='-mcpu=power'  _mcpu='-mtune=power'  ;;
	    POWER2) _march='-mcpu=power2' _mcpu='-mtune=power2' ;;
	    POWER3) _march='-mcpu=power3' _mcpu='-mtune=power3' ;;
    	    *) ;;
        esac
	# gcc 3.1(.1) and up supports 7400 and 7450
	if test "$_cc_major" -ge "3" && test "$_cc_minor" -ge "1" || test "$_cc_major" -ge "4"; then
	    case "$proc" in
		7400*|7410*) _march='-mcpu=7400' _mcpu='-mtune=7400' ;;
		7447*|7450*|7455*) _march='-mcpu=7450' _mcpu='-mtune=7450' ;;
    		*) ;;
    	    esac
	fi
	# gcc 3.2 and up supports 970
	if test "$_cc_major" -ge "3" && test "$_cc_minor" -ge "3" || test "$_cc_major" -ge "4"; then
	    case "$proc" in
		970*|PPC970*) _march='-mcpu=970' _mcpu='-mtune=970'
                      _def_dcbzl='#undef NO_DCBZL' ;;
    		*) ;;
    	    esac
	fi
	# gcc 3.3 and up supports POWER4
	if test "$_cc_major" -ge "3" && test "$_cc_minor" -ge "3" || test "$_cc_major" -ge "4"; then
	    case "$proc" in
		POWER4) _march='-mcpu=power4' _mcpu='-mtune=power4' ;;
		*) ;;
	    esac
	fi
	# gcc 4.0 and up supports POWER5
	if test "$_cc_major" -ge "4"; then
	    case "$proc" in
		POWER5) _march='-mcpu=power5' _mcpu='-mtune=power5' ;;
		*) ;;
	    esac
	fi
    fi

	echores "none"
    ;;

  alpha)
    _def_arch='#define ARCH_ALPHA 1'
    _target_arch='TARGET_ARCH_ALPHA = yes'
    iproc='alpha'
    _march=''

    echocheck "CPU type"
    cat > $TMPC << EOF
int main() {
    unsigned long ver, mask;
    asm ("implver %0" : "=r" (ver));
    asm ("amask %1, %0" : "=r" (mask) : "r" (-1));
    printf("%ld-%x\n", ver, ~mask);
    return 0;
}
EOF
    $COMP_C -o "$TMPO" "$TMPC"
    case `"$TMPO"` in

        0-0)    proc="ev4";   cpu_understands_mvi="0";;
        1-0)    proc="ev5";   cpu_understands_mvi="0";;
        1-1)    proc="ev56";  cpu_understands_mvi="0";;
        1-101)  proc="pca56"; cpu_understands_mvi="1";;
        2-303)  proc="ev6";   cpu_understands_mvi="1";;
        2-307)  proc="ev67";  cpu_understands_mvi="1";;
        2-1307) proc="ev68";  cpu_understands_mvi="1";;
    esac
    echores "$proc"

    echocheck "GCC & CPU optimization abilities"
    if test "$proc" = "ev68" ; then
      cc_check -mcpu=$proc || proc=ev67
    fi
    if test "$proc" = "ev67" ; then
      cc_check -mcpu=$proc || proc=ev6
    fi
    _mcpu="-mcpu=$proc"
    echores "$proc"

    echocheck "MVI instruction support in GCC"
    if test "$_cc_major" -ge "3" && test "$cpu_understands_mvi" = "1" ; then
        _def_gcc_mvi_support="#define CAN_COMPILE_ALPHA_MVI 1"
	echores "yes"
    else
	_def_gcc_mvi_support="#undef CAN_COMPILE_ALPHA_MVI"
	echores "no, GCC = `$COMP_C -dumpversion 2>&1` (must be >= 3), CPU = $proc (must be pca56 or later)"
    fi
    ;;

  mips)
    _def_arch='#define ARCH_SGI_MIPS 1'
    _target_arch='TARGET_ARCH_SGI_MIPS = yes'
    iproc='sgi-mips'
    proc=''
    _march=''
    _mcpu=''

    if irix ; then
	echocheck "CPU type"
	proc=`hinv -c processor | grep CPU | cut -d " " -f3`
	case "`echo $proc`" in
	    R3000) _march='-mips1' _mcpu='-mtune=r2000' ;;
	    R4000) _march='-mips3' _mcpu='-mtune=r4000' ;;
	    R4400) _march='-mips3' _mcpu='-mtune=r4400' ;;
	    R4600) _march='-mips3' _mcpu='-mtune=r4600' ;;
	    R5000) _march='-mips4' _mcpu='-mtune=r5000' ;;
	    R8000|R10000|R12000|R14000|R16000) _march='-mips4' _mcpu='-mtune=r8000' ;;
	esac
        echores "$proc"
    fi

    ;;

  hppa)
    _def_arch='#define ARCH_PA_RISC 1'
    _target_arch='TARGET_ARCH_PA_RISC = yes'
    iproc='PA-RISC'
    proc=''
    _march=''
    _mcpu=''
    ;;

  s390)
    _def_arch='#define ARCH_S390 1'
    _target_arch='TARGET_ARCH_S390 = yes'
    iproc='390'
    proc=''
    _march=''
    _mcpu=''
    ;;

  s390x)
    _def_arch='#define ARCH_S390X 1'
    _target_arch='TARGET_ARCH_S390X = yes'
    iproc='390x'
    proc=''
    _march=''
    _mcpu=''
    ;;

  vax)
    _def_arch='#define ARCH_VAX 1'
    _target_arch='TARGET_ARCH_VAX = yes'
    iproc='vax'
    proc=''
    _march=''
    _mcpu=''
	;;
  m68)
    _def_arch='#define ARCH_M68K 1'
    _target_arch='TARGET_ARCH_M68K = yes'
    iproc=''
    proc=''
    _march=''
    _mcpu=''
    ;;

  *)
    echo "The architecture of your CPU ($host_arch) is not supported by this configure script"
    echo "It seems nobody has ported SHOGUN to your OS or CPU type yet."
    die "unsupported architecture $host_arch"
    ;;
esac # case "$host_arch" in

if test "$_cpudetection" = no ; then
  if x86 ; then
    test "$_cmov"     != no && _cmov=yes
    x86_32 && _cmov=no
    test "$_mmx"      != no && _mmx=yes
    test "$_3dnow"    != no && _3dnow=yes
    test "$_3dnowext" != no && _3dnowext=yes
    test "$_mmxext"   != no && _mmxext=yes
    test "$_sse"      != no && _sse=yes
    test "$_sse2"     != no && _sse2=yes
    test "$_sse3"    != no && _sse3=yes
    test "$_mtrr"     != no && _mtrr=yes
  fi
  if ppc; then
    _altivec=yes
  fi
fi

if x86 || x86_64 && test "$_cpudetection" = yes ; then
  extcheck() {
    if test "$1" = kernel_check ; then
      echocheck "compiler and kernel support of $2"
      cat > $TMPC <<EOF
#include <stdlib.h>
#include <signal.h>
void catch() { exit(1); }
int main(void){
  signal(SIGILL, catch);
  __asm__ volatile ("$3":::"memory"); return(0);
}
EOF

      if cc_check && tmp_run ; then
        eval _$2=yes
        echores "yes"
		_optimizing="$_optimizing -m$2"
        return 0
      else
        eval _$2=no
        echores "failed"
        #echo "It seems that your kernel does not correctly support $2."
	return 1
      fi
    fi
    return 0
  }

  extcheck $_mmx "mmx" "emms"
  extcheck $_mmxext   "mmxext"   "sfence" 
  extcheck $_3dnow "3dnow" "femms"
  extcheck $_sse "sse" "xorps %%xmm0, %%xmm0" || _gcc3_ext="$_gcc3_ext -mno-sse"
  extcheck $_sse2 "sse2" "xorpd %%xmm0, %%xmm0" || _gcc3_ext="$_gcc3_ext -mno-sse2"
  extcheck $_sse3 "sse3" "addsubpd %%xmm0, %%xmm1" || _gcc3_ext="$_gcc3_ext -mno-sse3"
  extcheck $_cmov     "cmov"     "cmovb %%eax,%%ebx"

  if test "$_gcc3_ext" != ""; then
    # if we had to disable sse/sse2 because the active kernel does not
    # support this instruction set extension, we also have to tell
    # gcc3 to not generate sse/sse2 instructions for normal C code
    cat > $TMPC << EOF
int main(void) { return 0; }
EOF
    cc_check $_march $_gcc3_ext && _march="$_march $_gcc3_ext"
  fi

fi


# intrinsics headers for use with FFmpeg
if test "$_sse" = yes ; then
  echocheck "xmmintrin.h"
  cat > $TMPC << EOF
#include <xmmintrin.h>
int main() { _mm_sfence(); return 0; }
EOF
  _builtin_vector=no
  cc_check -msse && _builtin_vector=yes
  if test "$_builtin_vector" = yes ; then
    _def_builtin_vector='#define HAVE_BUILTIN_VECTOR 1'
  else
    _def_builtin_vector='#undef HAVE_BUILTIN_VECTOR'
  fi
  echores "$_builtin_vector"
fi

if test "$_3dnow" = yes ; then
  echocheck "mm3dnow.h"
  cat > $TMPC << EOF
#include <mm3dnow.h>
int main() { _m_femms(); return 0; }
EOF
  _mm3dnow=no
  cc_check -m3dnow && _mm3dnow=yes
  if test "$_mm3dnow" = yes ; then
    _def_mm3dnow='#define HAVE_MM3DNOW 1'
  else
    _def_mm3dnow='#undef HAVE_MM3DNOW'
  fi
  echores "$_mm3dnow"
fi

  DEFINES="$DEFINES -D$OS"

  if test "$_cpudetection" = yes
  then
	  COMPFLAGS_C="$COMPFLAGS_C $_march $_mcpu"
	  COMPFLAGS_CPP="$COMPFLAGS_CPP $_march $_mcpu"
  fi

  if test "$_profile" = yes
  then
	  COMPFLAGS_CPP="$COMPFLAGS_CPP -pg"
	  COMPFLAGS_C="$COMPFLAGS_CPP -pg"
	  LINKFLAGS="$LINKFLAGS -pg"
  fi
}

get_cflags()
{
    def=$1; pkgs="${*#$def}"
    if test -n "`which pkg-config 2>/dev/null`"; then
	for i in $pkgs; do
	    if pkg-config --exists $i; then
		pkg-config --cflags $i; return
	    fi
	done
    fi
    echo $def
}

get_lflags()
{
    def=$1; pkgs="${*#$def}"
    if test -n "`which pkg-config 2>/dev/null`"; then
	for i in $pkgs; do
	    if pkg-config --exists $i; then
		pkg-config --libs $i; return
	    fi
	done
    fi
    echo $def
}

check_datatypes()
{
    for d in `echo $_datatypes | cut -d '=' -f 2 | tr ',' ' '`
	do
	  case "$d" in
		  char)
			  DEFINES="$DEFINES -DUSE_CHAR"
			  ;;
		  bool)
			  DEFINES="$DEFINES -DUSE_BOOL"
			  ;;
		  uint8_t)
			  DEFINES="$DEFINES -DUSE_UINT8"
			  ;;
		  uint16_t)
			  DEFINES="$DEFINES -DUSE_UINT16"
			  ;;
		  uint32_t)
			  DEFINES="$DEFINES -DUSE_UINT32"
			  ;;
		  uint64_t)
			  DEFINES="$DEFINES -DUSE_UINT64"
			  ;;
		  int8_t)
			  DEFINES="$DEFINES -DUSE_INT8"
			  ;;
		  int16_t)
			  DEFINES="$DEFINES -DUSE_INT16"
			  ;;
		  int32_t)
			  DEFINES="$DEFINES -DUSE_INT32"
			  ;;
		  int64_t)
			  DEFINES="$DEFINES -DUSE_INT64"
			  ;;
		  float32_t)
			  DEFINES="$DEFINES -DUSE_FLOAT32"
			  ;;
		  float64_t)
			  DEFINES="$DEFINES -DUSE_FLOAT64"
			  ;;
		  floatmax_t)
			  DEFINES="$DEFINES -DUSE_FLOATMAX"
			  ;;
		  *)
			  die "Unknown datatype: $d"
			  ;;
	  esac
	done
}

check_wordsize()
{
	echocheck "Word Size"
cat > $TMPC << EOF
#include <stdint.h>
int main(void)
{
#ifdef __WORDSIZE
	return __WORDSIZE;
#else
	return 0;
#endif
}
EOF

	"$COMP_C" $INCLUDES "$TMPC" -o ./"$TMPO"
	$POSTLINKCMD ./"$TMPO"
	./"$TMPO"
	WORDSIZE=$?
	rm -f ./"$TMPO" "$TMPC"

	echores "$WORDSIZE bit"
	if ! darwin && test "$WORDSIZE" = 64
	then
		DEFINES_SWIG="${DEFINES_SWIG} -DSWIGWORDSIZE64"
	fi
}

check_math()
{
	cat > $TMPC << EOF
#include <math.h>
int main(void) { return 0; }
EOF
	echocheck "Math support (-lm)"
	if cc_check -lm
	then
		POSTLINKFLAGS="$POSTLINKFLAGS -lm"
		echores "yes"
	else
		echores "no"
		die "could not find math library (libm)"
	fi
}

check_powl()
{
	cat > $TMPCXX << EOF
#include <math.h>
int main(int argc, char** argv)
{
	long double a=powl((long double) argc, (long double) argc);
	return (int) a;
}
EOF
	echocheck "Math powl() support"
	if cxx_check -lm
	then
		HAVE_POWL='#define HAVE_POWL 1'
		DEFINES="$DEFINES -DHAVE_POWL"
		echores "yes"
	else
		echores "no - resorting to pow()"
	fi
}

check_gammal()
{
	cat > $TMPCXX << EOF
#include <math.h>
int main(int argc, char** argv)
{
	long double a=lgammal((long double) argc);
	return (int) a;
}
EOF
	echocheck "Math lgammal() support"
	if cxx_check -lm
	then
		HAVE_LGAMMAL='#define HAVE_LGAMMAL 1'
		DEFINES="$DEFINES -DHAVE_LGAMMAL"
		echores "yes"
	else
		echores "no - resorting to lgamma()"
	fi
}

check_sqrtl()
{
	cat > $TMPCXX << EOF
#include <math.h>
int main(int argc, char** argv)
{
	long double a=sqrtl((long double) argc);
	return (int) a;
}
EOF
	echocheck "Math sqrtl() support"
	if cxx_check -lm
	then
		HAVE_SQRTL='#define HAVE_SQRTL 1'
		DEFINES="$DEFINES -DHAVE_SQRTL"
		echores "yes"
	else
		echores "no - resorting to sqrt()"
	fi
}

check_log2()
{
	cat > $TMPCXX << EOF
#include <math.h>
int main(int argc, char** argv)
{
	double a=log2((double) argc);
	return (int) a;
}
EOF
	echocheck "Math log2() support"
	if cxx_check -lm
	then
		HAVE_LOG2='#define HAVE_LOG2 1'
		DEFINES="$DEFINES -DHAVE_LOG2"
		echores "yes"
	else
		echores "no - resorting to emulation()"
	fi
}

check_pthreads()
{
	cat > $TMPC << EOF
#include <pthread.h>
int main(void) { return 0; }
EOF
	echocheck "Posix threads support"
	_pthread=no

	for pthr in -pthread -lpthread
	do
		if cc_check $pthr
		then
			COMPFLAGS_C="$COMPFLAGS_C $pthr"
			COMPFLAGS_CPP="$COMPFLAGS_CPP $pthr"
			POSTLINKFLAGS="$POSTLINKFLAGS $pthr"
			_pthread=yes
			break
		fi
	done

	if test "$_pthread" = yes
	then
		HAVE_PTHREAD='#define HAVE_PTHREAD 1'
		DEFINES="$DEFINES -DHAVE_PTHREAD"
		echores "yes"
	else
		echores "no"
		die "no posix threading support found (libpthread)"
	fi
}

check_hdf5()
{
	if test "$_hdf5" = yes || test "$_hdf5" = auto
	then
		_cflags="`get_cflags '' libhdf5 hdf5`"
		_lflags="`get_lflags '-lhdf5' libhdf5 hdf5`"


		test -z "$_lflags" || _lflags="-lhdf5"
		cat > $TMPC << EOF
#include <hdf5.h>
int main(void)
{
	hid_t file = H5Fcreate("foobar.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
	hid_t group = H5Gcreate2(file, "groupname", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

	return 0;
}
EOF
		echocheck "HDF5 (>= 1.8.0) support"
		if cc_check $_cflags $_lflags
		then
			HAVE_HDF5='#define HAVE_HDF5 1'
			DEFINES="$DEFINES -DHAVE_HDF5"
			INCLUDES="$INCLUDES $_cflags"
			POSTLINKFLAGS="$POSTLINKFLAGS $_lflags"
			echores "yes"
		else
			echores "no"
		fi
	fi
}

check_json()
{
	if test "$_json" = yes || test "$_json" = auto
	then
		_cflags="`get_cflags '' libjson json json-c`"
		_lflags="`get_lflags '-ljson' libjson json json-c`"
		cat > $TMPC << EOF
#include <json.h>
int main(void)
{
	json_object* x = json_object_new_object();
	json_object_put(x);

	return 0;
}
EOF
		echocheck "libjson (json-c)"
		if cc_check $_cflags $_lflags
		then
			HAVE_JSON='#define HAVE_JSON 1'
			DEFINES="$DEFINES -DHAVE_JSON"
			INCLUDES="$INCLUDES $_cflags"
			POSTLINKFLAGS="$POSTLINKFLAGS $_lflags"
			echores "yes"
		else
			echores "no"
		fi
	fi
}

check_xml()
{
	if test "$_xml" = yes || test "$_xml" = auto
	then
		_cflags="`get_cflags '' libxml-2.0 libxml2 xml2`"
		_lflags="`get_lflags '-lxml2' libxml-2.0 libxml2 xml2`"
		cat > $TMPC << EOF
#include <libxml/parser.h>
#include <libxml/tree.h>

int main(void)
{
	LIBXML_TEST_VERSION;

#if !defined(LIBXML_TREE_ENABLED) || !defined(LIBXML_OUTPUT_ENABLED)
	this_is_not_compilable :)
#endif

	xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
	xmlNode* node = xmlNewNode(NULL, BAD_CAST "Hello");
	xmlDocSetRootElement(doc, node);

	xmlChildElementCount(node);

	xmlFreeDoc(doc);
	xmlCleanupParser();

	return 0;
}
EOF
		echocheck "libxml2 (>= 2.7.0)"
		if cc_check $_cflags $_lflags
		then
			HAVE_XML='#define HAVE_XML 1'
			DEFINES="$DEFINES -DHAVE_XML"
			INCLUDES="$INCLUDES $_cflags"
			POSTLINKFLAGS="$POSTLINKFLAGS $_lflags"
			echores "yes"
		else
			echores "no"
		fi
	fi
}

check_lapack()
{
	if test "$_lapack" = yes || test "$_lapack" = auto
	then
		cat > $TMPC << EOF
#include <cblas.h>
void dpotrf(char uplo, int n, double *a, int lda, int *info);

int main(int argc, char** argv)
{
	char uplo = 'U';
	int info=0;
	int skip=argc;
	int n=argc; double* a=0; double* v1=0; double* v2=0;
	int lda=1;
	dpotrf(uplo, n, a, lda, &info);
	int r = cblas_ddot(n, v1, skip, v2, skip);
	return 0;
}
EOF
		echocheck "AMD ACML support"
		if cc_check -lacml -lcblas -lgfortran
		then
			echores "yes"
			HAVE_ACML='#define HAVE_ACML 1'
			HAVE_LAPACK='#define HAVE_LAPACK 1'
			DEFINES="$DEFINES -DHAVE_ACML -DHAVE_LAPACK"
			POSTLINKFLAGS="$POSTLINKFLAGS -lacml -lcblas -lgfortran"
		else
			echores "no"

			cat > $TMPC << EOF
#include <mkl_cblas.h>
void dpotrf(char* uplo, int* n, double* a, int* lda, int* info);

int main(int argc, char** argv)
{
	char uplo = 'U';
	int info=0;
	int skip=argc;
	int n=argc; double* a=0; double* v1=0; double* v2=0;
	int lda=1;
	dpotrf(&uplo, &n, a, &lda, &info);
	int r = cblas_ddot(n, v1, skip, v2, skip);
	return 0;
}
EOF
			echocheck "Intel MKL support"
			if cc_check -lmkl -lguide -lmkl_lapack
			then
				echores "yes"
				HAVE_MKL='#define HAVE_MKL 1'
				HAVE_LAPACK='#define HAVE_LAPACK 1'
				DEFINES="$DEFINES -DHAVE_MKL -DHAVE_LAPACK"
				POSTLINKFLAGS="$POSTLINKFLAGS -lmkl -lguide -lmkl_lapack"
			else
				if cc_check -lmkl -lguide -lmkl_lapack64
				then
					echores "yes"
					HAVE_MKL='#define HAVE_MKL 1'
					HAVE_LAPACK='#define HAVE_LAPACK 1'
					DEFINES="$DEFINES -DHAVE_MKL -DHAVE_LAPACK"
					POSTLINKFLAGS="$POSTLINKFLAGS -lmkl -lguide -lmkl_lapack64"
				else
					echores "no"

					cat > $TMPC << EOF
#include <cblas.h>
#include <atlas/clapack.h>

int main(int argc, char** argv)
{
	int skip=argc;
	int n=argc; double* a=0; double* v1=0; double* v2=0;
	int result=clapack_dpotrf(CblasRowMajor, CblasUpper, n, a, n);
	int r = cblas_ddot(n, v1, skip, v2, skip);
	return 0;
}
EOF
					echocheck "Atlas support"
					if ! darwin && test "$WORDSIZE" = 64
					then
						ALTERNATIVE_ATLASDIR="/usr/lib64/atlas"
					else
						ALTERNATIVE_ATLASDIR="/usr/lib/atlas"
					fi

					if cc_check -llapack -lcblas -llapack_atlas
					then
						echores "yes"
						HAVE_ATLAS='#define HAVE_ATLAS 1'
						HAVE_LAPACK='#define HAVE_LAPACK 1'
						DEFINES="$DEFINES -DHAVE_ATLAS -DHAVE_LAPACK"
						POSTLINKFLAGS="$POSTLINKFLAGS -llapack -lcblas -llapack_atlas"
					elif cc_check -L${ALTERNATIVE_ATLASDIR} -llapack -lcblas -llapack_atlas
					then
						echores "yes"
						HAVE_ATLAS='#define HAVE_ATLAS 1'
						HAVE_LAPACK='#define HAVE_LAPACK 1'
						DEFINES="$DEFINES -DHAVE_ATLAS -DHAVE_LAPACK"
						POSTLINKFLAGS="$POSTLINKFLAGS -L${ALTERNATIVE_ATLASDIR} -llapack -lcblas -llapack_atlas"
					else
						if cc_check -llapack -lcblas -latlas
						then
							echores "yes"
							HAVE_ATLAS='#define HAVE_ATLAS 1'
							HAVE_LAPACK='#define HAVE_LAPACK 1'
							DEFINES="$DEFINES -DHAVE_ATLAS -DHAVE_LAPACK"
							POSTLINKFLAGS="$POSTLINKFLAGS -llapack -lcblas -latlas"
						elif cc_check -L${ALTERNATIVE_ATLASDIR} -llapack -lcblas -latlas
						then
							echores "yes"
							HAVE_ATLAS='#define HAVE_ATLAS 1'
							HAVE_LAPACK='#define HAVE_LAPACK 1'
							DEFINES="$DEFINES -DHAVE_ATLAS -DHAVE_LAPACK"
							POSTLINKFLAGS="$POSTLINKFLAGS -L${ALTERNATIVE_ATLASDIR} -llapack -lcblas -latlas"
						else
							echores "no"
						fi


						cat > $TMPC << EOF
#include </System/Library/Frameworks/vecLib.framework/Headers/cblas.h>
#include </System/Library/Frameworks/vecLib.framework/Headers/clapack.h>

int dpotrf_(char* uplo, int* n, double* a, int* lda, int* info);
int main(int argc, char** argv)
{
	char uplo = 'U';
	int info=0;
	int skip=argc;
	int n=argc; double* a=0; double* v1=0; double* v2=0;
	dpotrf_(uplo, &n, a, 1, &info);
	int r = cblas_ddot(n, v1, skip, v2, skip);
	return 0;
}
EOF
						echocheck "Mac OS vector library"
						if cc_check -llapack -lcblas
						then
							echores "yes"
							HAVE_LAPACK='#define HAVE_LAPACK 1'
							HAVE_MVEC='#define HAVE_MVEC 1'
							DEFINES="$DEFINES -DHAVE_MVEC -DHAVE_LAPACK"
							POSTLINKFLAGS="$POSTLINKFLAGS -llapack -lcblas"
						else
							echores "no"


							cat > $TMPC << EOF
#include <cblas.h>
int dpotrf_(char* uplo, int* n, double* a, int* lda, int* info);
int main(int argc, char** argv)
{
	char uplo = 'U';
	int info=0;
	int skip=argc;
	int n=argc; double* a=0; double* v1=0; double* v2=0;
	dpotrf_(uplo, &n, a, 1, &info);
	int r = cblas_ddot(n, v1, skip, v2, skip);
	return 0;
}
EOF
							echocheck "Vanilla lapack and blas support"
							if cc_check -llapack -lcblas
							then
								echores "yes"
								HAVE_LAPACK='#define HAVE_LAPACK 1'
								DEFINES="$DEFINES -DHAVE_LAPACK -DHAVE_LAPACK"
								POSTLINKFLAGS="$POSTLINKFLAGS -llapack -lcblas"
							else
								if cc_check -llapack -lblas
								then
									echores "yes"
									HAVE_LAPACK='#define HAVE_LAPACK 1'
									DEFINES="$DEFINES -DHAVE_LAPACK -DHAVE_LAPACK"
									POSTLINKFLAGS="$POSTLINKFLAGS -llapack -lblas"
								else
									echores "no (missing some features: LDA, liblinear, ...)"
								fi
							fi
						fi
					fi
				fi
			fi
		fi
	fi
}

check_superlu()
{
	if test "$_superlu" = yes || test "$_superlu" = auto
	then
		cat > $TMPCXX << EOF
#include <superlu/slu_ddefs.h>
int main(int argc, char **argv)
{
	SuperMatrix A;
	return 0;
}
EOF
		echocheck "SuperLU support"
		if cxx_check -lsuperlu
		then
			echores "yes"
			HAVE_ARPACK='#define HAVE_SUPERLU 1'
			DEFINES="$DEFINES -DHAVE_SUPERLU"
			POSTLINKFLAGS="$POSTLINKFLAGS -lsuperlu"
		else
			if test "$_superlu" = yes
			then
				die "superlu not found"
			else
				echores "no"
			fi
		fi
	fi
}

check_arpack()
{
	if test "$_arpack" = yes || test "$_arpack" = auto
	then
		cat > $TMPCXX << EOF
extern "C" void dsaupd_(int *ido, char *bmat, int *n, char *which,
int *nev, double *tol, double *resid, int *ncv,
double *v, int *ldv, int *iparam, int *ipntr,
double *workd, double *workl, int *lworkl,
int *info);

int main(int argc, char **argv)
{
	int ido = 0,n = 3, nev=1;
	double tol = 0.0;
	double* resid = new double[n];
	int ncv = n;
	int ldv = n;
	double* v = new double[ldv*ncv];
	int* iparam = new int[11];
	iparam[0] = 1;
	iparam[2] = 3*n;
	iparam[6] = 1;
	int* ipntr = new int[11];
	double* workd = new double[3*n];
	double* workl = new double[ncv*(ncv+8)];
	int lworkl = ncv*(ncv+8);
	char bmat[2] = "I";
	char which[3] = "SM";
	int info = 0;
	dsaupd_(&ido,bmat,&n,which,
	&nev,&tol,resid,&ncv,
	v,&ldv,iparam,ipntr,
	workd,workl,&lworkl,&info);
	delete[] resid;
	delete[] v;
	delete[] iparam;
	delete[] ipntr;
	delete[] workd;
	delete[] workl;
	return 0;
}
EOF
		echocheck "ARPACK support"
		if cxx_check -larpack
		then
			echores "yes"
			HAVE_ARPACK='#define HAVE_ARPACK 1'
			DEFINES="$DEFINES -DHAVE_ARPACK"
			POSTLINKFLAGS="$POSTLINKFLAGS -larpack"
		else
			if test "$_arpack" = yes
			then
				die "libarpack not found"
			else
				echores "no"
			fi
		fi
	fi
}

check_cplex()
{
	if test "$_cplex" = yes || test "$_cplex" = auto
	then
		cat > $TMPC << EOF
#include <ilcplex/cplex.h>
int main (int argc, char **argv)
{
	int status = argc;
	CPXENVptr     env = NULL;
	env = CPXopenCPLEX (&status);
	status = CPXcloseCPLEX (&env);
	return status;
}
EOF
		echocheck "CPLEX support"
		if echo quit | cplex >/dev/null 2>&1
		then
			CPLEXVER=`echo quit | cplex 2>&1 | grep ^Welcome | cut -d ' ' -f 6`
			if [ -z "$CPLEXVER" ]
			then
				echores "no"
			else
				echores "$CPLEXVER"

				echocheck "CPLEX developer files"
				CPLEXLINKFLAGS="-lcplex"
				CPLEXINCLUDES=""

				if linux
				then
					CPLEXVER=`echo $CPLEXVER | cut -f 1 -d '.'``echo $CPLEXVER | cut -f 2 -d '.' | cut -c 1`
					CPLEXPATH=`env | grep ^ILOG_LICENSE_FILE | cut -f 2 -d '=' | sed 's/ilog.*$/ilog/'`/cplex$CPLEXVER
					if x86_64
					then
						for cpx_path in "$CPLEXPATH/lib/x86-64_RHEL3.0_3.2/static_pic" \
							"$CPLEXPATH/lib/x86-64_rhel4.0_3.4/static_pic" \
							"$CPLEXPATH/lib/x86-64_debian4.0_4.1/static_pic" \
							"$CPLEXPATH/lib/x86-64_sles9.0_3.3/static_pic"
					do
						if test -d $cpx_path
						then
							CPLEXLINKFLAGS="-L$cpx_path -lcplex"
							CPLEXINCLUDES="-I$CPLEXPATH/include"
							break
						fi
					done
				elif x86
				then
					for cpx_path in "$CPLEXPATH/lib/i86_linux2_glibc2.3_gcc3.2/static_pic" \
						"$CPLEXPATH/lib/x86_RHEL3.0_3.2/static_pic" \
						"$CPLEXPATH/lib/x86_rhel4.0_3.4/static_pic" \
						"$CPLEXPATH/lib/x86_debian4.0_4.1/static_pic" \
						"$CPLEXPATH/lib/x86_sles9.0_3.3/static_pic"
				do
					if test -d $cpx_path
					then
						CPLEXLINKFLAGS="-L$cpx_path -lcplex"
						CPLEXINCLUDES="-I$CPLEXPATH/include"
						break
					fi
				done
			fi

			if cc_check $CPLEXINCLUDES $CPLEXLINKFLAGS
			then
				echores "yes"
				USE_CPLEX='#define USE_CPLEX 1'
				DEFINES="$DEFINES -DUSE_CPLEX"
				INCLUDES="$CPLEXINCLUDES $INCLUDES"
				POSTLINKFLAGS="$POSTLINKFLAGS $CPLEXLINKFLAGS"
				POSTLINKFLAGS="$POSTLINKFLAGS $CPLEXLINKFLAGS"
			else
				echores "no"
			fi
		else
			die "non supported CPLEX architecture"
		fi
		fi
		else
			if test "$_cplex" = yes
			then
				die "CPLEX not found"
			fi
			echores "no"
		fi
	fi
}

check_glpk()
{
	if test "$_glpk" = yes || test "$_glpk" = auto
	then
		cat >$TMPC<<EOF
#include <glpk.h>
int main()
{
	glp_term_out(GLP_OFF);
	LPX* lp=lpx_create_prob();
	lpx_delete_prob(lp);
	return 0;
}
EOF
		echocheck "GLPK"
		GLPKLINKFLAGS="-lglpk -lm"
		if cc_check $GLPKLINKFLAGS
		then
			echores "yes"
			USE_GLPK='#define USE_GLPK 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $GLPKLINKFLAGS"
			DEFINES="$DEFINES -DUSE_GLPK"
		elif cc_check -I/usr/include/glpk $GLPKLINKFLAGS
		then
			echores "yes"
			USE_GLPK='#define USE_GLPK 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $GLPKLINKFLAGS"
			INCLUDES="$INCLUDES -I/usr/include/glpk"
			DEFINES="$DEFINES -DUSE_GLPK"
		else
			echores "no"
		fi
	fi
}

check_lzo()
{
	if test "$_lzo" = yes || test "$_lzo" = auto
	then
		cat >$TMPC<<EOF
#include "lzo/lzoconf.h"
#include "lzo/lzo1x.h"
int main(int argc, char** argv)
{
	volatile lzo_bytep in;
	volatile lzo_uint in_len;
	return (lzo_init() != LZO_E_OK) ? 1 : 0;
}
EOF
		echocheck "lzo"
		LZOLINKFLAGS="-llzo2"
		if cc_check $LZOLINKFLAGS
		then
			echores "yes"
			USE_LZO='#define USE_LZO 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $LZOLINKFLAGS"
			DEFINES="$DEFINES -DUSE_LZO"
		else
			echores "no"
		fi
	fi
}

check_snappy()
{
	if test "$_snappy" = yes || test "$_snappy" = auto
	then
		cat >$TMPCXX<<EOF
#include <snappy.h>
int main(int argc, char** argv)
{
	size_t input_length = (size_t) argc;
	char* output = new char[snappy::MaxCompressedLength(input_length)];
	size_t output_length;
	RawCompress(input, input_length, output, &output_length);
	delete [] output;
	return int(output_length);
}
EOF
		echocheck "snappy"
		SNAPPYLINKFLAGS="-lsnappy"
		if cxx_check $SNAPPYLINKFLAGS
		then
			echores "yes"
			USE_SNAPPY='#define USE_SNAPPY 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $SNAPPYLINKFLAGS"
			DEFINES="$DEFINES -DUSE_SNAPPY"
		else
			echores "no"
		fi
	fi
}

check_zlib()
{
	if test "$_gzip" = yes || test "$_gzip" = auto
	then
		cat >$TMPC<<EOF
#include <zlib.h>
int main(int argc, char** argv)
{
	uLongf l=argc;
	return (compress2((Bytef*) argv, &l, (Bytef*) argv, l, argc) != Z_OK);
}
EOF
		echocheck "gzip"
		GZIPLINKFLAGS="-lz"
		if cc_check $GZIPLINKFLAGS
		then
			echores "yes"
			USE_GZIP='#define USE_GZIP 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $GZIPLINKFLAGS"
			DEFINES="$DEFINES -DUSE_GZIP"
		else
			echores "no"
		fi
	fi
}

check_bzip2()
{
	if test "$_bzip2" = yes || test "$_bzip2" = auto
	then
		cat >$TMPC<<EOF
#include <bzlib.h>
int main(int argc, char** argv)
{
	bz_stream* strm=NULL;
	return BZ2_bzCompress(strm, argc);
}
EOF
		echocheck "bzip2"
		BZIP2LINKFLAGS="-lbz2"
		if cc_check $BZIP2LINKFLAGS
		then
			echores "yes"
			USE_BZIP2='#define USE_BZIP2 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $BZIP2LINKFLAGS"
			DEFINES="$DEFINES -DUSE_BZIP2"
		else
			echores "no"
		fi
	fi
}

check_lzma()
{
	if test "$_lzma" = yes || test "$_lzma" = auto
	then
		cat >$TMPC<<EOF
#include <lzma.h>
int main(int argc, char** argv)
{
	return (int) lzma_stream_buffer_bound((size_t) argc) ;
}
EOF
		echocheck "lzma"
		LZMALINKFLAGS="-llzma"
		if cc_check $LZMALINKFLAGS
		then
			echores "yes"
			USE_LZMA='#define USE_LZMA 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $LZMALINKFLAGS"
			DEFINES="$DEFINES -DUSE_LZMA"
		else
			echores "no"
		fi
	fi
}

check_lpsolve()
{
	if test "$_lpsolve" = yes || test "$_lpsolve" = auto
	then
		cat > $TMPC << EOF
#include <lpsolve/lp_lib.h>
int main (int argc, char **argv)
{
	lprec* lp=make_lp(0,0);
	delete_lp(lp);
	return 0;
}
EOF
		echocheck "lpsolve Developer Files"
		LPSOLVELINKFLAGS="-L /usr/lib/lp_solve -llpsolve55"
		if cc_check $LPSOLVELINKFLAGS
		then
			echores "yes"
			USE_LPSOLVE='#define USE_LPSOLVE 1'
			POSTLINKFLAGS="$POSTLINKFLAGS $LPSOLVELINKFLAGS"
			POSTLINKFLAGS="$POSTLINKFLAGS $LPSOLVELINKFLAGS"
			DEFINES="$DEFINES -DUSE_LPSOLVE -DYY_NEVER_INTERACTIVE -DPARSER_LP -DINVERSE_ACTIVE=INVERSE_LUSOL -DRoleIsExternalInvEngine"
		else
			echores "no"
		fi
	fi

}

set_opts()
{
	if echo " $MACHINE " | grep "iPhone" >/dev/null
	then
		POSTLINKCMD="ldid -S"
	fi

	if darwin
	then
		if ! sed --version 2>/dev/null | grep "GNU sed" >/dev/null
		then
			SEDMI='sed -i ""'
		fi

		LIBRARY_PATH="DYLD_LIBRARY_PATH"
		EXT_LIB=dylib
		SONAMEOPT="-install_name "
		SHAREDOPTS="-dynamiclib"
		LIBSHOGUN_TARGET="libshogun.${LIBSHOGUNVER}.dylib"
		LIBSHOGUN_SONAME="libshogun.${LIBSHOGUNSO}.dylib"
		LIBSHOGUN_DYNAMIC="libshogun.dylib"

		if test "$_matlab_static" = yes
		then
			true #nop
		elif test "$_r" = yes
		then
			true #nop
		else
			true #nop LINKFLAGS="$LINKFLAGS -undefined error"
		fi
	elif interix
	then
		COMPFLAGS_C="$COMPFLAGS_C -D_ALL_SOURCE -D_REENTRANT"
		COMPFLAGS_CPP="$COMPFLAGS_CPP -D_ALL_SOURCE -D_REENTRANT"
	elif sunos
	then
		POSTLINKFLAGS="$POSTLINKFLAGS -lsocket  -lnsl"
		SONAMEOPT="-h "
		SHAREDOPTS="-G"
	elif win32
	then
		EXT_LIB=dll
		LIBSHOGUN_SONAME=libshogun-${LIBSHOGUNSO}.dll
		LIBSHOGUN_TARGET=libshogun-${LIBSHOGUNVER}.dll
		LIBSHOGUN_DYNAMIC=libshogun.dll
		LIBSHOGUN_STATIC=libshogun.a
	else
		LINKFLAGS="$LINKFLAGS -Xlinker --no-undefined"
	fi

	if test "$_largefile" = yes
	then
		HAVE_LARGEFILE='#define HAVE_LARGEFILE 1'
		DEFINES="$DEFINES -DHAVE_LARGEFILE"
	fi

	if test "$_logcache" = yes
	then
		USE_LOGCACHE='#define USE_LOGCACHE 1'
		DEFINES="$DEFINES -DUSE_LOGCACHE"
	fi

	if test "$_shortrealkernelcache" = yes
	then
		USE_SHORTREAL_KERNELCACHE='#define USE_SHORTREAL_KERNELCACHE 1'
		DEFINES="$DEFINES -DUSE_SHORTREAL_KERNELCACHE"
	fi

	if test "$_logsumarray" = yes
	then
		USE_LOGSUMARRAY='#define USE_LOGSUMARRAY 1'
		DEFINES="$DEFINES -DUSE_LOGSUMARRAY"
	fi

	if test "$_hmmparallel" = yes
	then
		USE_HMMPARALLELSTRUCTURES='#define USE_HMMPARALLELSTRUCTURES 1'
		DEFINES="$DEFINES -DUSE_HMMPARALLELSTRUCTURES"
		USE_HMMPARALLEL='#define USE_HMMPARALLEL 1'
		DEFINES="$DEFINES -DUSE_HMMPARALLEL"
	fi

	if test "$_bigstates" = yes
	then
		USE_BIGSTATES='#define USE_BIGSTATES 1'
		DEFINES="$DEFINES -DUSE_BIGSTATES"
	fi

	if test "$_hmmcache" = yes
	then
		USE_HMMCACHE='#define USE_HMMCACHE 1'
		DEFINES="$DEFINES -DUSE_HMMCACHE"
	fi

	if test "$_debug" = yes
	then
		_debug=yes
	fi

	if test "$_trace_mallocs" = yes
	then
		DEFINES="$DEFINES -DTRACE_MEMORY_ALLOCS"
		TRACE_MEMORY_ALLOCS='#define TRACE_MEMORY_ALLOCS 1'
	fi

	if test "$_reference_counting" = yes
	then
		USE_REFERENCE_COUNTING='#define USE_REFERENCE_COUNTING 1'
		DEFINES="$DEFINES -DUSE_REFERENCE_COUNTING"
	fi

	if test "$_hmmdebug" = yes
	then
		USE_HMMDEBUG='#define USE_HMMDEBUG 1'
		DEFINES="$DEFINES -DUSE_HMMDEBUG"
	fi

	if test "$_pathdebug" = yes
	then
		USE_PATHDEBUG='#define USE_PATHDEBUG 1'
		DEFINES="$DEFINES -DUSE_PATHDEBUG"
	fi

	if test "$_svm_light" = yes
	then
		USE_SVMLIGHT='#define USE_SVMLIGHT 1'
		DEFINES="$DEFINES -DUSE_SVMLIGHT"
		GPL=""
	else
		GPL='#define GPL 1'
		DEFINES="$DEFINES -DGPL"
	fi

}

test_modular()
{
	if test "$_swig" = yes || test "$_swig" = auto
	then
		echocheck "SWIG"
		if ${SWIG} -version >/dev/null 2>&1
		then
			_swig_version=`swig -version 2>&1 | grep Version | cut -f 3 -d ' '`
			if test -z "$_swig_version"; then
				die "swig did not return version information"
			fi
			if [ `echo $_swig_version | cut -f 1 -d '.'` -ge 2 ] || \
				[ `echo $_swig_version | cut -f 2 -d '.'` -ge 4 ] || \
				[ `echo $_swig_version | cut -f 3 -d '.'` -ge 29 ]
			then
				echores "`${SWIG} -version 2>&1 | grep Version | cut -f 3 -d ' '`"

				_swig=yes

				if cygwin
				then
					POSTLINKFLAGS="$POSTLINKFLAGS $LINKFLAGS"
				fi
			else
				echores "`echo $_swig_version` ... too old version 1.3.29 or later required"
				if test "$_swig" = yes ; then
					die "swig outdated"
				else
					echores "no"
					_swig='no'
				fi
			fi
		else
			echores "no"
			if test "$_swig" = yes ; then
				die "cannot find swig executable in path"
			fi
			_swig="no"
		fi
	fi

	if test "$_swig" = 'yes'
	then
		if test "$_doxygen" = yes
		then
			HAVE_DOXYGEN='#define HAVE_DOXYGEN 1'
			DEFINES="$DEFINES -DHAVE_DOXYGEN"

			USE_DOXYGEN='yes'
		else
			USE_DOXYGEN='no'
		fi

		if test "$_octave" = yes && test "$_octave_modular" != no
		then
			echocheck "SWIG Octave Support"
			if [ `echo "$_swig_version" | cut -f 1 -d '.'` -ge 2 ] || \
				[ `echo "$_swig_version" | cut -f 2 -d '.'` -ge 4 ] || \
				[ `echo "$_swig_version" | cut -f 3 -d '.'` -ge 36 ]
			then
				if [ `echo "$_octave_version" | cut -f 1 -d '.'` -ge 3 ] || \
					[ `echo "$_octave_version" | cut -f 2 -d '.'` -ge 2 ]
				then
					if [ `echo "$_swig_version" | cut -f 1 -d '.'` -ge 2 ] || \
						[ `echo "$_swig_version" | cut -f 2 -d '.'` -ge 4 ] || \
						[ `echo "$_swig_version" | cut -f 3 -d '.'` -ge 40 ]
					then
						PARTS="$PARTS octave_modular"
						OCTAVE_MODULAR_TARGET=octave_modular
						echores "yes"
					else
						if test "$_octave_modular" = yes ; then
							die "swig too old: Version 1.3.40 or later required for octave 3.2.x or later; swig version 1.3.36 or later is sufficient for octave 3.0.x."
						else
							echores "swig too old: Version 1.3.40 or later required for octave 3.2.x or later; swig version 1.3.36 or later is sufficient for octave 3.0.x."
						fi

					fi
				else
					PARTS="$PARTS octave_modular"
					OCTAVE_MODULAR_TARGET=octave_modular
					echores "yes"
				fi
			else
				if test "$_octave_modular" = yes ; then
					die "swig too old version 1.3.36 or later required"
				else
					echores "too old version 1.3.36 or later required"
				fi
			fi
		fi

		if test "$_python" = yes && test "$_python_modular" != no
		then
			echocheck "SWIG Python Support"
			PARTS="$PARTS python_modular"
			PYTHON_MODULAR_TARGET=python_modular
			echores "yes"
		fi

		if test "$_r" = yes && test "$_r_modular" != no
		then
			echocheck "SWIG R Support"
			if [ `echo $_swig_version | cut -f 1 -d '.'` -ge 2 ] || \
				[ `echo $_swig_version | cut -f 2 -d '.'` -ge 4 ] || \
				[ `echo $_swig_version | cut -f 3 -d '.'` -ge 37 ]
			then
				PARTS="$PARTS r_modular"
				R_MODULAR_TARGET=r_modular
				echores "yes"
			else
				if test "$_r_modular" = yes ; then
					die "swig too old version 1.3.37 or later required"
				else
					echores "too old version 1.3.37 or later required"
				fi
			fi
		fi

		if test "$_java_modular" != no
		then
			PARTS="$PARTS java_modular"
			JAVA_MODULAR_TARGET=java_modular
		fi

		if test "$_csharp_modular" != no
		then
			PARTS="$PARTS csharp_modular"
			CSHARP_MODULAR_TARGET=csharp_modular
		fi

		if test "$_ruby_modular" != no
		then
			PARTS="$PARTS ruby_modular"
			RUBY_MODULAR_TARGET=ruby_modular
		fi

		if test "$_lua_modular" != no
		then
			PARTS="$PARTS lua_modular"
			LUA_MODULAR_TARGET=lua_modular
		fi
	else
		if test "$_octave_modular" == yes || \
			test "$_python_modular" == yes || \
			test "$_r_modular" == yes || \
			test "$_java_modular" == yes || \
			test "$_lua_modular" == yes || \
			test "$_ruby_modular" == yes || \
			test "$_csharp_modular" == yes
		then
				die "swig required for modular interfaces"
		fi
	fi
}

test_matlab()
{
	if test "$_libshogun" = no
	then
		if test "$_matlab_static" = yes
		then
			die "libshogun is required for matlab"
		else
			_matlab_static=no
		fi
	fi

	if test "$_matlab_static" = yes || test "$_matlab_static" = auto
	then
		if win32
		then
			echocheck "Matlab"
			matlab.exe /nosplash /minimize /r "fid=fopen('matlab_version.txt','wb');fprintf(fid,version);fclose(fid);quit" >/dev/null 2>&1 
			if test -f matlab_version.txt
			then
				echores `cat matlab_version.txt`
				matlab.exe /nosplash /minimize /r "system('env > matlab_path.txt');quit"
				MATLAB_DIR=`grep '^_=' matlab_path.txt | cut -c 3- | sed 's#/bin/win32/matlab.exe##'`
				MATLAB_SRCDIR="$MATLAB_DIR/extern/src"
				MATLAB_INCDIR="$MATLAB_DIR/extern/include"
				MATLAB_LIBDIR="$MATLAB_DIR/extern/lib"
				MATLAB_BINDIR="$MATLAB_DIR/bin"
			else
				echores "no"

				if test "$_matlab_static" = yes
				then
					die "could not find matlab in path / matlab crashed"
				fi
			fi

		else
			echocheck "Matlab"
			if matlab -nojvm -nodisplay -r 'version;quit' >/dev/null 2>&1
			then
				 echores "`echo \"fprintf(2,'ver:%s\n',version);quit\" | matlab -nojvm -nodisplay  2>&1 | grep '^>> ver:' | cut -f '2-' -d ':'`"
				 MATLAB_DIR=`echo 'disp(matlabroot); quit;' | matlab -nojvm -nodisplay  2>&1 | grep '^>> ' | cut -c 4-`
				 MATLAB_SRCDIR="$MATLAB_DIR/extern/src"
				 MATLAB_INCDIR="$MATLAB_DIR/extern/include"
				 MATLAB_LIBDIR="$MATLAB_DIR/extern/lib"
				 MATLAB_BINDIR="$MATLAB_DIR/bin"
			else
				echores "no"

				if test "$_matlab_static" = yes
				then
					die "could not find matlab in path / matlab crashed"
				fi
			fi
		fi

		if test -n "$MATLAB_DIR"
		then
			if osf1
			then
				COMPFLAGS_C_MATLAB="-DMATLAB_MEX_FILE $COMPFLAGS_C_MATLAB -shared -ieee -std1"
				COMPFLAGS_CPP_MATLAB="-DMATLAB_MEX_FILE $COMPFLAGS_CPP_MATLAB -shared -ieee -std1"
				LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -shared -Wl,-expect_unresolved,'*',-hidden,-exported_symbol,mexFunction,-exported_symbol,mexVersion,-exported_symbol,'__*' -L$MATLAB_BINDIR/alpha -lmx -lmex -lmat"
				INCLUDES_MATLAB="$INCLUDES_MATLAB -I$MATLAB_INCDIR -I$MATLAB_SRCDIR"
				MATLAB_TARGET="sg.mexaxp"
			elif darwin
			then
				INCLUDES_MATLAB="$INCLUDES_MATLAB -I$MATLAB_INCDIR -I$MATLAB_SRCDIR"
				if ppc
				then
					LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -bundle -Wl,-flat_namespace -undefined suppress -Wl,-exported_symbols_list,$MATLAB_LIBDIR/mac/mexFunction.map -L$MATLAB_BINDIR/mac -lmx -lmex -lmat"
					MATLAB_TARGET="sg.mexmac"
				elif x86_64
				then
					DEFINES_MATLAB="$DEFINES_MATLAB -DMX_COMPAT_32_OFF"
					COMPFLAGS_C_MATLAB="$COMPFLAGS_CPP_MATLAB -DMX_COMPAT_32_OFF"
					COMPFLAGS_CPP_MATLAB="$COMPFLAGS_CPP_MATLAB -DMX_COMPAT_32_OFF"
					LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -bundle -Wl,-flat_namespace -undefined suppress -Wl,-exported_symbols_list,$MATLAB_LIBDIR/maci64/mexFunction.map -L$MATLAB_BINDIR/maci64 -lmx -lmex -lmat"
					MATLAB_TARGET="sg.mexmaci64"
				elif x86
				then
					DEFINES_MATLAB="$DEFINES_MATLAB -DMX_COMPAT_32"
					COMPFLAGS_C_MATLAB="$COMPFLAGS_CPP_MATLAB -DMX_COMPAT_32"
					COMPFLAGS_CPP_MATLAB="$COMPFLAGS_CPP_MATLAB -DMX_COMPAT_32"
					LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -bundle -Wl,-flat_namespace -undefined suppress -Wl,-exported_symbols_list,$MATLAB_LIBDIR/maci/mexFunction.map -L$MATLAB_BINDIR/maci -lmx -lmex -lmat"
					MATLAB_TARGET="sg.mexmaci"
				else
					if test "$_matlab_static" = yes
					then
						die "non supported matlab architecture"
					fi
				fi
			elif linux
			then
				if x86_64
				then
					COMPFLAGS_C_MATLAB="$COMPFLAGS_C_MATLAB -fPIC -fno-omit-frame-pointer -ansi -D_GNU_SOURCE"
					COMPFLAGS_CPP_MATLAB="$COMPFLAGS_CPP_MATLAB -fPIC -fno-omit-frame-pointer -ansi -D_GNU_SOURCE"
					LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -shared -Wl,--version-script,$MATLAB_LIBDIR/glnxa64/mexFunction.map -Wl,--rpath-link,$MATLAB_BINDIR/glnxa64 -L$MATLAB_BINDIR/glnxa64 -lmx -lmex -lmat"
					INCLUDES_MATLAB="$INCLUDES_MATLAB -I$MATLAB_INCDIR -I$MATLAB_SRCDIR"
					MATLAB_TARGET="sg.mexa64"
				elif x86
				then
					LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -shared -Wl,--version-script,$MATLAB_LIBDIR/glnx86/mexFunction.map -Wl,--rpath-link,$MATLAB_LIBDIR/glnx86 -L$MATLAB_BINDIR/glnx86 -lmx -lmex -lmat"
					INCLUDES_MATLAB="$INCLUDES_MATLAB -I$MATLAB_INCDIR -I$MATLAB_SRCDIR"
					MATLAB_TARGET="sg.mexglx"
				else
					if test "$_matlab_static" = yes
					then
						die "matlab only supports x86_84 and x86 linux architectures"
					fi
				fi
			elif sunos
			then
					COMPFLAGS_C=_MATLAB"$COMPFLAGS_C_MATLAB -KPIC -dalign -xlibmieee -D__EXTENSIONS__ -D_POSIX_C_SOURCE=199506L -mt -xs -g -xO3 -xlibmil"

					COMPFLAGS_CPP_MATLAB="$COMPFLAGS_CPP_MATLAB -KPIC -dalign -xlibmieee -D__EXTENSIONS__ -D_POSIX_C_SOURCE=199506L -mt -library=stlport4,Crun -xs -g -xO3 -xlibmil"
					LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -G -mt -M$MATLAB_LIBDIR/sol64/mexFunction.map -L$MATLAB_BINDIR/sol64 -lmx -lmex -lmat"
					INCLUDES_MATLAB="$INCLUDES_MATLAB -I$MATLAB_INCDIR -I$MATLAB_SRCDIR"
					MATLAB_TARGET="sg.mexs64"
			elif cygwin
			then
cat >mexFunction.map <<EOF
MEX {
_mexFunction;
_mexVersion;
};
EOF
				COMPFLAGS_C_MATLAB="$COMPFLAGS_C_MATLAB -mno-cygwin -DMATLAB_MEX_FILE"
				COMPFLAGS_CPP_MATLAB="$COMPFLAGS_CPP_MATLAB -mno-cygwin -DMATLAB_MEX_FILE"
				LINKFLAGS_MATLAB="$LINKFLAGS_MATLAB -shared -mno-cygwin -Wl,--version-script,mexFunction.map -Wl,--rpath-link,$MATLAB_LIBDIR/win32 -L$MATLAB_BINDIR/win32 -lmx -lmex -lmat"
				INCLUDES_MATLAB="$INCLUDES_MATLAB -I$MATLAB_INCDIR"
				MATLAB_TARGET="sg.dll"
			else
				if test "$_matlab_static" = yes
				then
					die "non supported matlab architecture"
				fi
			fi

			if test -n "$MATLAB_TARGET"
			then
				echocheck "Matlab developer support (-lmx -lmex -lmat)"
cat > $TMPC << EOF
#include <mex.h>
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{

}

int main(void) { return 0; }
EOF
				if cc_check $COMPFLAGS_CPP_MATLAB $INCLUDES_MATLAB $LINKFLAGS_MATLAB
				then
					echores "yes"
					PARTS="$PARTS matlab_static"
					_matlab_static=yes
					DEFINES_MATLAB="$DEFINES_MATLAB -DHAVE_MATLAB"
				else
					echores "no"
					if test "$_matlab_static" = yes
					then
						die "could not find matlab developer files (e.g. mex.h)"
					fi
				fi
			fi
		fi
	fi
}

test_elwms()
{
	if test "$_libshogun" = no 
	then
		if test "$_elwms_static" = yes
		then
			die "libshogun is required for elwms"
		else
			_elwms_static=no
		fi
	fi

	if test "$_elwms_static" = yes || test "$_elwms_static" = auto
	then
		if test "$_octave_static" = yes || test "$_r_static" = yes || \
			test "$_matlab_static" = yes || test "$_python_static" = yes
		then
			_elwms_static=yes
			PARTS="$PARTS elwms_static"
			DEFINES_ELWMS="-DHAVE_ELWMS -DR_HOME_ENV=\\\"${R_HOME}\\\" -DLIBPYTHON=\\\"libpython${PYVER}.${EXT_LIB}\\\" $DEFINES_PYTHON $DEFINES_R $DEFINES_MATLAB $DEFINES_OCTAVE"
			INCLUDES_ELWMS="$INCLUDES_R $INCLUDES_PYTHON $INCLUDES_OCTAVE $INCLUDES_MATLAB "
			LINKFLAGS_ELWMS="$LINKFLAGS_PYTHON `echo $LINKFLAGS_R | sed 's/-dynamiclib//g'` `echo $LINKFLAGS_MATLAB | sed 's/-Wl,--version-script,.*mexFunction.map / /g'` $LINKFLAGS_OCTAVE -ldl"
			POSTLINKFLAGS_ELWMS="$POSTLINKFLAGS_OCTAVE"

			ELWMS_TARGET="elwms.${EXT_LIB}"
		fi
	fi
}

test_octave()
{
	if test "$_libshogun" = no
	then
		if test "$_octave_static" = yes
		then
			die "libshogun is required for octave"
		else
			_octave_static=no
		fi
	fi

	if test "$_libshogun" = no
	then
		if test "$_octave_modular" = yes
		then
			die "libshogun is required for octave_modular"
		else
			_octave_modular=no
		fi
	fi

	if test "$_octave_static" != no || test "$_octave_modular" != no
	then
		echocheck "Octave"
		if echo version | octave --no-history >/dev/null 2>&1
		then
			_octave_version="`echo version | octave --no-history 2>&1 | grep ans | cut -f 3- -d ' '`"
			if test "`echo $_octave_version | cut -f 1 -d '.'`" -ge 3
			then
				echores "$_octave_version"
			else
				if test "$_octave" = yes
				then
					die "$_octave_version - too old, at least octave version 3.0 required"
				fi
				echores "$_octave_version - too old, at least octave version 3.0 required"
				_octave="no"
			fi
		else
			echores "no"
			if test "$_octave" = yes
			then
				die "could not find octave in path / octave crashed"
			fi
			_octave="no"
		fi

		if test "$_octave" != no
		then
			echocheck "octave-config"
			if test -n "`octave-config -p OCTLIBDIR 2>/dev/null`" \
				&& test -n "`octave-config -p OCTINCLUDEDIR 2>/dev/null`" \
				&& test -n "`octave-config -p INCLUDEDIR 2>/dev/null`"
			then
				test -n "$_octdir" || _octdir=`octave-config --print LOCALAPIOCTFILEDIR`
				echores "`octave-config -p API_VERSION`"
			else
				echores "no"
				if test "$_octave" = yes
				then
					die "could not find octave-config in path / octave-config crashed"
				fi
				_octave="no"
			fi

			if test "$_octave" != no
			then
cat > $TMPCXX << EOF

#include <octave/config.h>

#include <octave/defun-dld.h>
#include <octave/error.h>
#include <octave/oct-obj.h>
#include <octave/pager.h>
#include <octave/symtab.h>
#include <octave/variables.h>

DEFUN_DLD (sg, prhs, nlhs, "shogun.")
{
	int nrhs = prhs.length();
	octave_value_list plhs;
	return plhs;
}
EOF
				EXT_IF_SWIG_OCTAVE_MODULAR='stop'
				PRE_LIB_SWIG_OCTAVE_MODULAR=
				EXT_LIB_SWIG_OCTAVE_MODULAR=.oct
				SWIGFLAGS_OCTAVE_MODULAR="-c++ -octave"

				if darwin
				then
					LINKFLAGS_OCTAVE="-bundle -Wl,-flat_namespace -L`octave-config -p OCTLIBDIR` -loctinterp -loctave -lcruft $LINKFLAGS_OCTAVE"
				else
					if sunos
					then
						OCTLIBDIR=`octave-config -p OCTLIBDIR`
						LINKFLAGS_OCTAVE="-G -L $OCTLIBDIR -loctinterp -loctave -lcruft $LINKFLAGS_OCTAVE"
					else
						OCTLIBDIR=`octave-config -p OCTLIBDIR`
						LINKFLAGS_OCTAVE="-shared $LINKFLAGS_OCTAVE"
						POSTLINKFLAGS_OCTAVE="-L $OCTLIBDIR -loctinterp -loctave -lcruft $POSTLINKFLAGS_OCTAVE"
					fi
				fi
				OCTAVE_TARGET="sg.oct"
				oct_inc_dir=`octave-config -p INCLUDEDIR`
				oct_oct_inc_dir=`octave-config -p OCTINCLUDEDIR`
				oct_oct_inc_dir=`echo ${oct_oct_inc_dir} | sed 's/\/octave$//'`
				INCLUDES_OCTAVE="$INCLUDES_OCTAVE -I${oct_oct_inc_dir} -I${oct_inc_dir}"
				COMPFLAGS_CPP_OCTAVE="$COMPFLAGS_CPP -fPIC"
				COMPFLAGS_C_OCTAVE="$COMPFLAGS_C -fPIC"

				echocheck "Octave Developer Files"
				if LD_LIBRARY_PATH=`octave-config -p OCTLIBDIR` cxx_check_so $COMPFLAGS_CPP_OCTAVE $INCLUDES_OCTAVE $LINKFLAGS_OCTAVE $POSTLINKFLAGS_OCTAVE
				then
					echores "yes"
					_octave=yes
				else
					echores "no"
					if test "$_octave" = yes
					then
						die "octave developer files missing"
					fi
				fi
			fi
		fi
	fi

	if test "$_octave" = yes
	then
		OCTAVE_APIVERSION=`octave-config -p API_VERSION | tr -d 'a-z-+'`
		DEFINES_OCTAVE="$DEFINES_OCTAVE -DHAVE_OCTAVE -DOCTAVE_APIVERSION=${OCTAVE_APIVERSION}"

		if test "$_octave_static" != no
		then
			PARTS="$PARTS octave_static"
			_octave_static=yes
		fi
	fi
}

test_python()
{
	if test "$_libshogun" = no 
	then
		if test "$_python_static" = yes
		then
			die "libshogun is required for python"
		else
			_python_static=no
		fi
	fi

	if test "$_libshogun" = no
	then
		if test "$_python_modular" = yes
		then
			die "libshogun is required for python_modular"
		else
			_python_modular=no
		fi
	fi

	if test "$_python_static" != no || test "$_python_modular" != no
	then
		echocheck "Python numpy support"
		if $PYTHON -c "import sys; import numpy" >/dev/null 2>&1
		then
			if [ `$PYTHON -c 'import numpy; print numpy.__version__' | cut -c 1` -gt 0 ]
			then
				echores "`$PYTHON -c 'import numpy; print numpy.__version__'`"
			else
				echores "`$PYTHON -c 'import numpy; print numpy.__version__'`... too old version 1.0 or later required"
				if test "$_python" = yes
				then
					die "python numpy outdated"
				fi
				_python="no"
			fi
		else
			echores "no"
			if test "$_python" = yes || test "$_python_static" = yes \
				|| test "$_python_modular" = yes
			then
				die "python numpy package missing"
			fi
			_python="no"
		fi

		if test "$_python" != no
		then
			PRE_LIB_SWIG_PYTHON_MODULAR=_
			EXT_IF_SWIG_PYTHON_MODULAR='.py'
			test -n "$_pyinclude" && _pyinclude="-I$_pyinclude"
			test -n "$_pyinclude" || _pyinclude=`$PYTHON -c "from distutils.sysconfig import get_python_inc; import numpy; print '-I%s -I%s' % (get_python_inc(), numpy.get_include())"`
			_pylibpath=`$PYTHON -c 'from distutils.sysconfig import get_config_vars; print "-L%(LIBPL)s" % get_config_vars()'`
			EXT_LIB_SWIG_PYTHON_MODULAR=`$PYTHON -c 'from distutils.sysconfig import get_config_vars; print "%(SO)s" % get_config_vars()'`
			test -n "$_pylibs" || _pylibs=`$PYTHON -c "import sys; print 'python'+sys.version[0:3]"`${EXT_LIB_SWIG_PYTHON_MODULAR%.*}
			INCLUDES_PYTHON="$_pyinclude $INCLUDES_PYTHON"
			COMPFLAGS_CPP_PYTHON="-fno-strict-aliasing -fPIC $COMPFLAGS_CPP_PYTHON"
			COMPFLAGS_C_PYTHON="-fno-strict-aliasing -fPIC $COMPFLAGS_C_PYTHON"

			if darwin
			then
				LINKFLAGS_PYTHON="$_pylibpath -l$_pylibs -ldl -bundle -Wl,-flat_namespace $LINKFLAGS_PYTHON"
				POSTLINKFLAGS_PYTHON="$_pylibpath -l$_pylibs -ldl -bundle -Wl,-flat_namespace $POSTLINKFLAGS_PYTHON"
			else
				LINKFLAGS_PYTHON="-shared $_pylibpath -l$_pylibs $LINKFLAGS_PYTHON"
				POSTLINKFLAGS_PYTHON="-shared $_pylibpath -l$_pylibs $POSTLINKFLAGS_PYTHON"
			fi

			PYTHON_TARGET="sg$EXT_LIB_SWIG_PYTHON_MODULAR"

cat > $TMPC << EOF
#include <Python.h>
#include <stdlib.h>
#include <numpy/arrayobject.h>

PyMODINIT_FUNC initsg(void)
{
	double* array=(double*) malloc(10*sizeof(double));
	npy_intp* dims=(npy_intp*) malloc(sizeof(npy_int));
	dims[0]=10;
	Py_Initialize();
	PyObject* outArray = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, (void*)array);
}

PyMODINIT_FUNC initsg(void);
EOF
			echocheck "Python/Python Numpy Developer Files"
			if cc_check $COMPFLAGS_CPP_PYTHON $INCLUDES_PYTHON $LINKFLAGS_PYTHON
			then
				echores "yes"
				_python=yes
				SWIGFLAGS_PYTHON_MODULAR="-c++ -python"
			else
				# add -ldl and -lutil for some self compiled python versions as
				# they for some reason are not automgically pulled in
				LINKFLAGS_PYTHON="-lutil -ldl $LINKFLAGS_PYTHON"
				POSTLINKFLAGS_PYTHON="-lutil -ldl $POSTLINKFLAGS_PYTHON"
				if cc_check $COMPFLAGS_CPP_PYTHON $INCLUDES_PYTHON $LINKFLAGS_PYTHON
				then
					echores "yes"
					_python=yes
					SWIGFLAGS_PYTHON_MODULAR="-c++ -python"
				else
					echores "no"
					if test "$_python" = yes || test "$_python_static" = yes \
						|| test "$_python_modular" = yes
					then
						die "python/numpy developer files missing"
					fi
				fi
			fi
		fi
	fi

	if test "$_python" = yes
	then
		DEFINES_PYTHON="$DEFINES_PYTHON -DHAVE_PYTHON"

		if test "$_python_static" != no
		then
			PARTS="$PARTS python_static"
			_python_static=yes
		fi
	else
		_python_static=no
		INCLUDES_PYTHON=""
		LINKFLAGS_PYTHON=""
		COMPFLAGS_CPP_PYTHON=""
		COMPFLAGS_C_PYTHON=""
	fi
}

test_r()
{
	if test "$_libshogun" = no
	then
		if test "$_r_static" = yes
		then
			die "libshogun is required for r"
		else
			_r_static=no
		fi
	fi

	if test "$_libshogun" = no
	then
		if test "$_r_modular" = yes
		then
			die "libshogun is required for r_modular"
		else
			_r_modular=no
		fi
	fi

	if test "$_r_static" != no || test "$_r_modular" != no
	then
		echocheck "R"
		if ( echo 'version; quit()' | R --no-save ) >/dev/null 2>&1
		then
			echores "`echo 'version; quit()' | R  --no-save 2>&1 | grep version\.string | cut -f 4 -d ' '`"
			test -n "$_rdir" || _rdir=`echo '.libPaths()[1]' | R --no-save 2>/dev/null | grep '^\[1\]' | cut -f 2 -d '"'`
			if cygwin
			then
				R_HOME=`echo 'system("c:/cygwin/bin/env.exe")' | R -q --no-save | grep '^R_HOME' | cut -f 2 -d '='`
			else
				R_HOME=`echo 'system("env")' | R -q --no-save | grep '^R_HOME' | cut -f 2 -d '='`
			fi

			PRE_LIB_SWIG_R_MODULAR=
			EXT_LIB_SWIG_R_MODULAR='.so'
			EXT_IF_SWIG_R_MODULAR='.R'
		else
			echores "no"
			if test "$_r" = yes
			then
				die "cannot find R executable in path"
			fi
			_r=no
		fi

		if test "$_r" != no
		then

			if win32
			then
				RPATH=$(cygpath -u $(cygpath -d "`which R | sed 's#/bin/R##'`"))
				R_TARGET="sg.dll"
				COMPFLAGS_C_R="$COMPFLAGS_C_R -mno-cygwin"
				COMPFLAGS_CPP_R="$COMPFLAGS_CPP_R -mno-cygwin"
				INCLUDES_R="$INCLUDES_R -I$RPATH/include"
				LINKFLAGS_R="$LINKFLAGS_R -L$RPATH/bin -lR -shared -mno-cygwin"
			else
				R_TARGET="sg.so"
				#unset makeflags as R uses -w and that leads to screwed up R CMD config --ldflags
				unset MAKEFLAGS MFLAGS
				COMPFLAGS_C_R="$COMPFLAGS_C_R `R CMD config CPICFLAGS`"
				COMPFLAGS_CPP_R="$COMPFLAGS_CPP_R `R CMD config CXXPICFLAGS`"
				INCLUDES_R="$INCLUDES_R `R CMD config --cppflags`"
				LINKFLAGS_R="$LINKFLAGS_R `R CMD config --ldflags` `R CMD config DYLIB_LDFLAGS`"
			fi


cat > $TMPC << EOF
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#include <Rdefines.h>

static SEXP sg_helper(SEXP args)
{
	SEXP result=NEW_LOGICAL(args);
	return result;
}
EOF
			echocheck "R Developer Files"
			if cc_check $COMPFLAGS_CPP_R $INCLUDES_R $LINKFLAGS_R
			then
				echores "yes"
				SWIGFLAGS_R_MODULAR="-c++ -r"
				_r=yes
			else
				echores "no"
				if test "$_r" = yes
				then
					die "cannot find R developer files"
				fi
			fi
		fi
	fi

	if test "$_r" = yes
	then
		DEFINES_R="$DEFINES_R -DHAVE_R"

		if test "$_r_static" != no
		then
			PARTS="$PARTS r_static"
			_r_static=yes
		fi
	fi
}

test_libshogun()
{
	if test "$_libshogun" = yes || test "$_libshogun" = auto
	then

cat > $TMPCXX << EOF
#include <stdio.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/base/init.h>

int main(int argc, char** argv)
{
	init_shogun();
	CGaussianKernel* k=new CGaussianKernel(10, 1.0);
	exit_shogun();
	return 0;
}
EOF
		echocheck "libshogun"

		if test -d shogun
		then
			echores "yes (internal in source tree)"
			INCLUDES="-I${SRCDIR} -I.. $INCLUDES"
			INCLUDES_STATIC_INTERFACES="$INCLUDES_STATIC_INTERFACES -I../../shogun -I../.."
			INCLUDES_MODULAR_INTERFACES="$INCLUDES_MODULAR_INTERFACES -I../../shogun -I../.."

			if test "$_static" = no
			then
				LINKFLAGS_STATIC_INTERFACES="$LINKFLAGS_STATIC_INTERFACES -L../../shogun -lshogun"
				LINKFLAGS_MODULAR_INTERFACES="$LINKFLAGS_MODULAR_INTERFACES -L../../shogun -lshogun"
			fi
			_libshogun=internal
			LIBSHOGUN_MODE=internal
		else
			if cxx_check -lshogun
			then
				echores "yes (found installed)"
				_libshogun=external
				LIBSHOGUN_MODE=external
				LINKFLAGS_STATIC_INTERFACES="$LINKFLAGS_STATIC_INTERFACES -lshogun"
				INCLUDES_MODULAR_INTERFACES="$INCLUDES_MODULAR_INTERFACES -I/usr/local/include -I/usr/include"
				LINKFLAGS_MODULAR_INTERFACES="$LINKFLAGS_MODULAR_INTERFACES -lshogun"
			else
				echores "no"
				if test "$_libshogun" = yes
				then
					die "libshogun neither found installed in system include path nor locally"
				fi
			fi
		fi
	fi

	if darwin; then
		 POSTLINKFLAGS_LIBSHOGUN="-fPIC ${SONAMEOPT}${_libdir}/${LIBSHOGUN_SONAME} ${SHAREDOPTS}"
	else
		POSTLINKFLAGS_LIBSHOGUN="-fPIC ${SONAMEOPT}${LIBSHOGUN_SONAME} ${SHAREDOPTS}"
	fi
}

test_cmdline()
{
	if test "$_libshogun" = no
	then
		if test "$_cmdline_static" = yes
		then
			die "libshogun is required for cmdline"
		else
			_cmdline_static=no
		fi
	fi

	if test "$_cmdline_static" != no
	then
		PARTS="$PARTS cmdline_static"
		CMDLINE_TARGET=shogun
		if darwin
		then
			PRELINKFLAGS_CMDLINE="$PRELINKFLAGS_CMDLINE -L../libshogun -lshogun"
		fi

		if test "$_readline" = yes || test "$_readline" = auto;
		then
cat > $TMPC << EOF
#include <readline/readline.h>
int main(void) { return 0; }
EOF
			echocheck "Readline support"
			if cc_check -lreadline >/dev/null 2>&1
			then
				echores "yes"

				HAVE_READLINE='#define HAVE_READLINE 1'
				DEFINES_CMDLINE="$DEFINES_CMDLINE -DHAVE_READLINE"
				LINKFLAGS_CMDLINE="$LINKFLAGS_CMDLINE -lreadline"
			else
				echores "no"
			fi
		fi
	fi
}

#write the actual config

write_dot_config()
{

#enable additional warnings / -Wfloat-equal -W
if test "$_cc_major" -ge "3" 2>/dev/null ; then
	COMPFLAGS_C=`echo "$COMPFLAGS_C" | sed -e 's/\(-Wall\)/\1 -Wno-unused-parameter -Wformat -Wformat-security -Wparentheses -Wshadow/'`
	COMPFLAGS_CPP=`echo "$COMPFLAGS_CPP" | sed -e 's/\(-Wall\)/\1 -Wno-unused-parameter -Wformat -Wformat-security -Wparentheses -Wshadow -Wno-deprecated/'`
fi

cat > "$1" <<EOF
SRCDIR			= ${SRCDIR}
PARTS			= ${PARTS}
LIBRARY_PATH			= ${LIBRARY_PATH}
LIBSHOGUN_MODE			= ${LIBSHOGUN_MODE}
LIBSHOGUN_TARGET		= ${LIBSHOGUN_TARGET}
LIBSHOGUN_DYNAMIC		= ${LIBSHOGUN_DYNAMIC}
LIBSHOGUN_STATIC		= ${LIBSHOGUN_STATIC}
CMDLINE_STATIC_TARGET	= ${CMDLINE_TARGET}
R_STATIC_TARGET			= ${R_TARGET}
PYTHON_STATIC_TARGET	= ${PYTHON_TARGET}
OCTAVE_STATIC_TARGET	= ${OCTAVE_TARGET}
MATLAB_STATIC_TARGET	= ${MATLAB_TARGET}
ELWMS_STATIC_TARGET		= ${ELWMS_TARGET}
R_MODULAR_TARGET		= ${R_MODULAR_TARGET}
JAVA_MODULAR_TARGET		= ${JAVA_MODULAR_TARGET}
CSHARP_MODULAR_TARGET	= ${CSHARP_MODULAR_TARGET}
RUBY_MODULAR_TARGET		= ${RUBY_MODULAR_TARGET}
LUA_MODULAR_TARGET		= ${LUA_MODULAR_TARGET}
PYTHON_MODULAR_TARGET	= ${PYTHON_MODULAR_TARGET}
OCTAVE_MODULAR_TARGET	= ${OCTAVE_MODULAR_TARGET}
MAKE 			= ${MAKE}
MAKEDEPEND		= ${MAKEDEPEND}
MAKEDEPENDOPTS	= ${MAKEDEPENDOPTS}
SWIG 			= ${SWIG}
SEDMI 			= ${SEDMI}
PYTHON 			= ${PYTHON}
RUBY 			= ${RUBY}
PYVER 			= ${PYVER}
DOXYGEN 		= ${DOXYGEN}
USE_DOXYGEN 	= ${USE_DOXYGEN}
SWIGFLAGS		= ${SWIGFLAGS}
EXT_LIB			= ${EXT_LIB}
EXT_INP_SWIG	= ${EXT_INP_SWIG}
EXT_OBJ_SWIG	= ${EXT_OBJ_SWIG}
EXT_SRC_HEADER	= ${EXT_SRC_HEADER}
EXT_SRC_C 		= ${EXT_SRC_C}
EXT_SRC_CPP 	= ${EXT_SRC_CPP}
EXT_SRC_SWIG	= ${EXT_SRC_SWIG}
KERNELNAME  	= ${KERNELNAME}
MACHINE  		= ${MACHINE}
EXT_OBJ_CPP 	= ${EXT_OBJ_CPP}
EXT_OBJ_C   	= ${EXT_OBJ_C}
EXT_SRC_CPP 	= ${EXT_SRC_CPP}
EXT_SRC_C   	= ${EXT_SRC_C}
EXT_CPP_TMP     = ${EXT_CPP_TMP}
EXT_CPP_SH      = ${EXT_CPP_SH}
EXT_CPP_PY      = ${EXT_CPP_PY}
EXT_DOXY        = ${EXT_DOXY}
EXT_SWIG_DOXY  	= ${EXT_SWIG_DOXY}
GDB				= ${GDB}
COMP_C			= ${COMP_C}
COMP_CPP		= ${COMP_CPP}

DEFINES				= ${DEFINES}
DEFINES_SWIG		= ${DEFINES_SWIG}
COMPFLAGS_C			= ${COMPFLAGS_C}
COMPFLAGS_CPP		= ${COMPFLAGS_CPP}
COMPFLAGS_SWIG_CPP	= ${COMPFLAGS_SWIG_CPP}
INCLUDES			= ${INCLUDES}
PRELINKFLAGS		= ${PRELINKFLAGS}
LINKFLAGS 			= ${LINKFLAGS}
POSTLINKFLAGS		= ${POSTLINKFLAGS}

INCLUDES_STATIC_INTERFACES	= ${INCLUDES_STATIC_INTERFACES}
LINKFLAGS_STATIC_INTERFACES	= ${LINKFLAGS_STATIC_INTERFACES}
INCLUDES_MODULAR_INTERFACES	= ${INCLUDES_MODULAR_INTERFACES}
LINKFLAGS_MODULAR_INTERFACES= ${LINKFLAGS_MODULAR_INTERFACES}
DEFINES_LIBSHOGUN		= ${DEFINES_LIBSHOGUN}
INCLUDES_LIBSHOGUN		= ${INCLUDES_LIBSHOGUN}
PRELINKFLAGS_LIBSHOGUN	= ${PRELINKFLAGS_LIBSHOGUN}
LINKFLAGS_LIBSHOGUN 	= ${LINKFLAGS_LIBSHOGUN}
POSTLINKFLAGS_LIBSHOGUN	= ${POSTLINKFLAGS_LIBSHOGUN}
LIBSHOGUN_SONAME	= ${LIBSHOGUN_SONAME}

DEFINES_CMDLINE_STATIC			= ${DEFINES_CMDLINE}
INCLUDES_CMDLINE_STATIC			= ${INCLUDES_CMDLINE}
PRELINKFLAGS_CMDLINE_STATIC		= ${PRELINKFLAGS_CMDLINE}
LINKFLAGS_CMDLINE_STATIC 		= ${LINKFLAGS_CMDLINE}
POSTLINKFLAGS_CMDLINE_STATIC	= ${POSTLINKFLAGS_CMDLINE}

DEFINES_MATLAB_STATIC			= ${DEFINES_MATLAB}
INCLUDES_MATLAB_STATIC			= ${INCLUDES_MATLAB}
PRELINKFLAGS_MATLAB_STATIC		= ${PRELINKFLAGS_MATLAB}
LINKFLAGS_MATLAB_STATIC 		= ${LINKFLAGS_MATLAB}
POSTLINKFLAGS_MATLAB_STATIC		= ${POSTLINKFLAGS_MATLAB}

DEFINES_JAVA		= ${DEFINES_JAVA}
INCLUDES_JAVA		= ${INCLUDES_JAVA}
PRELINKFLAGS_JAVA	= ${PRELINKFLAGS_JAVA}
LINKFLAGS_JAVA 		= ${LINKFLAGS_JAVA}
POSTLINKFLAGS_JAVA	= ${POSTLINKFLAGS_JAVA}

DEFINES_CSHARP		= ${DEFINES_CSHARP}
INCLUDES_CSHARP		= ${INCLUDES_CSHARP}
PRELINKFLAGS_CSHARP	= ${PRELINKFLAGS_CSHARP}
LINKFLAGS_CSHARP 	= ${LINKFLAGS_CSHARP}
POSTLINKFLAGS_CSHARP= ${POSTLINKFLAGS_CSHARP}

DEFINES_RUBY		= ${DEFINES_RUBY}
INCLUDES_RUBY		= ${INCLUDES_RUBY}
PRELINKFLAGS_RUBY	= ${PRELINKFLAGS_RUBY}
LINKFLAGS_RUBY 		= ${LINKFLAGS_RUBY}
POSTLINKFLAGS_RUBY	= ${POSTLINKFLAGS_RUBY}

DEFINES_LUA			= ${DEFINES_LUA}
INCLUDES_LUA		= ${INCLUDES_LUA}
PRELINKFLAGS_LUA	= ${PRELINKFLAGS_LUA}
LINKFLAGS_LUA 		= ${LINKFLAGS_LUA}
POSTLINKFLAGS_LUA	= ${POSTLINKFLAGS_LUA}

OCTAVE_APIVERSION			= ${OCTAVE_APIVERSION}
DEFINES_OCTAVE_STATIC		= ${DEFINES_OCTAVE}
INCLUDES_OCTAVE_STATIC		= ${INCLUDES_OCTAVE}
PRELINKFLAGS_OCTAVE_STATIC	= ${PRELINKFLAGS_OCTAVE}
LINKFLAGS_OCTAVE_STATIC 	= ${LINKFLAGS_OCTAVE}
POSTLINKFLAGS_OCTAVE_STATIC	= ${POSTLINKFLAGS_OCTAVE}

DEFINES_R_STATIC		= ${DEFINES_R}
INCLUDES_R_STATIC		= ${INCLUDES_R}
LINKFLAGS_R_STATIC 		= ${LINKFLAGS_R}
POSTLINKFLAGS_R_STATIC	= ${POSTLINKFLAGS_R}

DEFINES_PYTHON_STATIC		= ${DEFINES_PYTHON}
INCLUDES_PYTHON_STATIC		= ${INCLUDES_PYTHON}
PRELINKFLAGS_PYTHON_STATIC	= ${PRELINKFLAGS_PYTHON}
LINKFLAGS_PYTHON_STATIC 	= ${LINKFLAGS_PYTHON}
POSTLINKFLAGS_PYTHON_STATIC	= ${POSTLINKFLAGS_PYTHON}

DEFINES_ELWMS_STATIC		= ${DEFINES_ELWMS}
INCLUDES_ELWMS_STATIC		= ${INCLUDES_ELWMS}
PRELINKFLAGS_ELWMS_STATIC	= ${PRELINKFLAGS_ELWMS}
LINKFLAGS_ELWMS_STATIC		= ${LINKFLAGS_ELWMS}
POSTLINKFLAGS_ELWMS_STATIC	= ${POSTLINKFLAGS_ELWMS}

PRE_LIB_SWIG_OCTAVE_MODULAR	= ${PRE_LIB_SWIG_OCTAVE_MODULAR}
EXT_LIB_SWIG_OCTAVE_MODULAR	= ${EXT_LIB_SWIG_OCTAVE_MODULAR}
EXT_IF_SWIG_OCTAVE_MODULAR	= ${EXT_IF_SWIG_OCTAVE_MODULAR}
DEFINES_OCTAVE_MODULAR		= ${DEFINES_OCTAVE}
INCLUDES_OCTAVE_MODULAR		= ${INCLUDES_OCTAVE}
PRELINKFLAGS_OCTAVE_MODULAR	= ${PRELINKFLAGS_OCTAVE}
LINKFLAGS_OCTAVE_MODULAR 	= ${LINKFLAGS_OCTAVE}
POSTLINKFLAGS_OCTAVE_MODULAR= ${POSTLINKFLAGS_OCTAVE}
SWIGFLAGS_OCTAVE_MODULAR = ${SWIGFLAGS_OCTAVE_MODULAR}

PRE_LIB_SWIG_R_MODULAR	= ${PRE_LIB_SWIG_R_MODULAR}
EXT_LIB_SWIG_R_MODULAR	= ${EXT_LIB_SWIG_R_MODULAR}
EXT_IF_SWIG_R_MODULAR	= ${EXT_IF_SWIG_R_MODULAR}
DEFINES_R_MODULAR		= ${DEFINES_R} -DDISABLE_CANCEL_CALLBACK
INCLUDES_R_MODULAR		= ${INCLUDES_R}
PRELINKFLAGS_R_MODULAR	= ${PRELINKFLAGS_R}
LINKFLAGS_R_MODULAR 	= ${LINKFLAGS_R}
POSTLINKFLAGS_R_MODULAR	= ${POSTLINKFLAGS_R}
SWIGFLAGS_R_MODULAR = ${SWIGFLAGS_R_MODULAR}

PRE_LIB_SWIG_PYTHON_MODULAR	= ${PRE_LIB_SWIG_PYTHON_MODULAR}
EXT_LIB_SWIG_PYTHON_MODULAR	= ${EXT_LIB_SWIG_PYTHON_MODULAR}
EXT_IF_SWIG_PYTHON_MODULAR	= ${EXT_IF_SWIG_PYTHON_MODULAR}
DEFINES_PYTHON_MODULAR		= ${DEFINES_PYTHON}
INCLUDES_PYTHON_MODULAR		= ${INCLUDES_PYTHON}
PRELINKFLAGS_PYTHON_MODULAR	= ${PRELINKFLAGS_PYTHON}
LINKFLAGS_PYTHON_MODULAR 	= ${LINKFLAGS_PYTHON}
POSTLINKFLAGS_PYTHON_MODULAR= ${POSTLINKFLAGS_PYTHON}
SWIGFLAGS_PYTHON_MODULAR 	= ${SWIGFLAGS_PYTHON_MODULAR}

PRE_LIB_SWIG_JAVA_MODULAR	= ${PRE_LIB_SWIG_JAVA_MODULAR}
EXT_LIB_SWIG_JAVA_MODULAR	= ${EXT_LIB_SWIG_JAVA_MODULAR}
EXT_IF_SWIG_JAVA_MODULAR	= ${EXT_IF_SWIG_JAVA_MODULAR}
DEFINES_JAVA_MODULAR		= ${DEFINES_JAVA}
INCLUDES_JAVA_MODULAR		= ${INCLUDES_JAVA}
PRELINKFLAGS_JAVA_MODULAR	= ${PRELINKFLAGS_JAVA}
LINKFLAGS_JAVA_MODULAR 		= ${LINKFLAGS_JAVA}
POSTLINKFLAGS_JAVA_MODULAR	= ${POSTLINKFLAGS_JAVA}
SWIGFLAGS_JAVA_MODULAR 		= ${SWIGFLAGS_JAVA_MODULAR}

PRE_LIB_SWIG_CSHARP_MODULAR	= ${PRE_LIB_SWIG_CSHARP_MODULAR}
EXT_LIB_SWIG_CSHARP_MODULAR	= ${EXT_LIB_SWIG_CSHARP_MODULAR}
EXT_IF_SWIG_CSHARP_MODULAR	= ${EXT_IF_SWIG_CSHARP_MODULAR}
DEFINES_CSHARP_MODULAR		= ${DEFINES_CSHARP}
INCLUDES_CSHARP_MODULAR		= ${INCLUDES_CSHARP}
PRELINKFLAGS_CSHARP_MODULAR	= ${PRELINKFLAGS_CSHARP}
LINKFLAGS_CSHARP_MODULAR 	= ${LINKFLAGS_CSHARP}
POSTLINKFLAGS_CSHARP_MODULAR= ${POSTLINKFLAGS_CSHARP}
SWIGFLAGS_CSHARP_MODULAR	= ${SWIGFLAGS_CSHARP_MODULAR}

PRE_LIB_SWIG_RUBY_MODULAR	= ${PRE_LIB_SWIG_RUBY_MODULAR}
EXT_LIB_SWIG_RUBY_MODULAR	= ${EXT_LIB_SWIG_RUBY_MODULAR}
EXT_IF_SWIG_RUBY_MODULAR	= ${EXT_IF_SWIG_RUBY_MODULAR}
DEFINES_RUBY_MODULAR		= ${DEFINES_RUBY}
INCLUDES_RUBY_MODULAR		= ${INCLUDES_RUBY}
PRELINKFLAGS_RUBY_MODULAR	= ${PRELINKFLAGS_RUBY}
LINKFLAGS_RUBY_MODULAR		= ${LINKFLAGS_RUBY}
POSTLINKFLAGS_RUBY_MODULAR	= ${POSTLINKFLAGS_RUBY}
SWIGFLAGS_RUBY_MODULAR		= ${SWIGFLAGS_RUBY_MODULAR}
RUBY_LIBRARY_DIR			= ${RUBY_LIBRARY_DIR}
RUBY_EXTENSIONS_INSTALL_DIR	= ${RUBY_EXTENSIONS_INSTALL_DIR}

PRE_LIB_SWIG_LUA_MODULAR	= ${PRE_LIB_SWIG_LUA_MODULAR}
EXT_LIB_SWIG_LUA_MODULAR	= ${EXT_LIB_SWIG_LUA_MODULAR}
EXT_IF_SWIG_LUA_MODULAR		= ${EXT_IF_SWIG_LUA_MODULAR}
DEFINES_LUA_MODULAR			= ${DEFINES_LUA}
INCLUDES_LUA_MODULAR		= ${INCLUDES_LUA}
PRELINKFLAGS_LUA_MODULAR	= ${PRELINKFLAGS_LUA}
LINKFLAGS_LUA_MODULAR		= ${LINKFLAGS_LUA}
POSTLINKFLAGS_LUA_MODULAR	= ${POSTLINKFLAGS_LUA}
SWIGFLAGS_LUA_MODULAR		= ${SWIGFLAGS_LUA_MODULAR}
LUA_EXTENSIONS_INSTALL_DIR	= ${LUA_EXTENSIONS_INSTALL_DIR}

CLASSPATH		= ${CLASSPATH}
LINK			= ${LINK}
POSTLINKCMD		= ${POSTLINKCMD}
PREFIX			= ${_prefix}
DESTDIR			= ${_destdir}
BINDIR			= ${_bindir}
DATADIR			= ${_datadir}
MANDIR			= ${_mandir}
CONFDIR			= ${_confdir}
INCDIR			= ${_incdir}
LIBDIR			= ${_libdir}
PYDIR			= ${_libdir}/${_pydir}
OCTDIR			= ${_octdir}
RDIR			= ${_rdir}
EOF
}

svmlight_warn_license()
{
	if test "$_svm_light" = yes
	then
		cat <<EOF

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
WARNING: SHOGUN is built using SVMlight which was written
by Thorsten Joachims and uses a different non GPL compatible license.
To build a fully GPL'd SHOGUN use

./configure --disable-svm-light

The SVMlight license can be found in LICENSE.SVMlight.  In case
you do not know or cannot agree to the licensing terms expressed in
LICENSE.SVMlight press ctrl+c to abort configure now.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

EOF
		sleep 1
	fi
}

write_config_h()
{
cat >"$1" <<EOF
$HAVE_READLINE
$HAVE_HDF5
$HAVE_JSON
$HAVE_XML
$HAVE_LARGEFILE
$HAVE_DOXYGEN
$HAVE_LAPACK
$HAVE_MVEC
$HAVE_ARPACK
$HAVE_ATLAS
$HAVE_PTHREAD
$HAVE_ACML
$HAVE_MKL
$HAVE_MVEC
$HAVE_POWL
$HAVE_LGAMMAL
$HAVE_SQRTL
$HAVE_LOG2
$HAVE_JBLAS
$HAVE_UJMP
$HAVE_RUBY_NARRAY
$USE_SPINLOCKS
$USE_LOGCACHE
$USE_SHORTREAL_KERNELCACHE
$USE_LOGSUMARRAY
$USE_HMMPARALLELSTRUCTURES
$USE_HMMPARALLEL
$USE_SVMPARALLEL
$USE_BIGOBS
$NARRAY_LIB
$USE_BIGSTATES
$USE_HMMCACHE
$USE_KERNELCACHE
$USE_DEBUG
$USE_HMMDEBUG
$USE_PATHDEBUG
$USE_SVMLIGHT
$GPL
$USE_CPLEX
$USE_LPSOLVE
$USE_GLPK
$USE_LZO
$USE_GZIP
$USE_BZIP2
$USE_LZMA
$TRACE_MEMORY_ALLOCS
$USE_REFERENCE_COUNTING
#define OCTAVE_APIVERSION $OCTAVE_APIVERSION
#define $OS 1
#define CONFIGURE_OPTIONS "$CONFIGURE_OPTIONS"
#define MACHINE "$MACHINE"
#define COMPFLAGS_C	"$COMPFLAGS_C"
#define COMPFLAGS_CPP "$COMPFLAGS_CPP"
#define LINKFLAGS "$LINKFLAGS"
EOF
}

type_buildinfo()
{
	cfgopts=""
	if test -n "$CONFIGURE_OPTIONS"
	then
		cfgopts="with the following $CONFIGURE_OPTIONS"
	fi

	moreinfo=""
	test -n "$PARTS" && moreinfo="and the following interfaces:$PARTS"

	cat <<EOF

SHOGUN was configured for

  libshogun $moreinfo

interface(s) $cfgopts

It will support the following data types $_datatypes.

type "make" now to compile

EOF
}

clean_up()
{
	rm -f "$TMPC" "$TMPC" "$TMPCXX" "$TMPO" "$TMPS"\
		"$TMPSS" "$TMPSO" "$TMPJAVA" "$TMPJAVAO" "$TMPRUBY"
}

# Mac OS gcc tends to make external debug files when testing compiler
darwin_clean_up()
{
	rm -rf configure-*.dSYM
}

test_static_linking()
{

	if test "$_static" = yes
	then
		if test "$_octave_static" = yes
		then
			POSTLINKFLAGS_OCTAVE="$POSTLINKFLAGS_OCTAVE ../../shogun/libshogun.a"
		fi

		if test "$_python_static" = yes
		then
			POSTLINKFLAGS_PYTHON="$POSTLINKFLAGS_PYTHON ../../shogun/libshogun.a"
		fi

		if test "$_r_static" = yes
		then
			POSTLINKFLAGS_R="$POSTLINKFLAGS_R ../../shogun/libshogun.a"
		fi

		if test "$_cmdline_static" = yes
		then
			LINKFLAGS_CMDLINE="$LINKFLAGS_CMDLINE ../../shogun/libshogun.a"
		fi

		if test "$_matlab_static" = yes
		then
			POSTLINKFLAGS_MATLAB="$POSTLINKFLAGS_MATLAB ../../shogun/libshogun.a"
		fi
	fi
}

echo_check
head_check
parse_arguments $@
set_install_dirs
determine_system_name
detect_ldd
detect_cc
check_makedepend
check_python
check_pkg_config
check_make
check_doxygen
determine_march_mtune
check_datatypes
check_wordsize
check_math
check_powl
check_gammal
check_sqrtl
check_log2
check_pthreads
check_hdf5
check_json
check_xml
check_lapack
check_superlu
check_arpack
check_cplex
check_glpk
check_lzo
check_snappy
check_zlib
check_bzip2
check_lzma
check_lpsolve
set_opts
test_spinlocks
test_libshogun
test_cmdline
test_python
test_octave
test_r
test_matlab
test_elwms
test_java
test_csharp
test_ruby
test_lua
test_modular
test_static_linking
svmlight_warn_license
write_dot_config "$SRCDIR/.config"

if test "$_libshogun" = internal
then
	write_config_h "$SRCDIR/shogun/lib/config.h"
fi
clean_up
if darwin
then
	darwin_clean_up
fi	
type_buildinfo
