#!/bin/sh
# $Id: configure,v 1.27 2002/08/06 16:52:58 binny Exp $ 

MAJOR_VERSION=0
MINOR_VERSION=2

############## DO NOT MODIFY #######################

ac_default_prefix=/usr/local

as_me=`basename $0`
debug="no"
trace=""

for ac_option
do
	if test -n "$ac_prev"; then
		eval "$ac_prev=\$ac_opton"
		ac_prev=
		continue
	fi

	ac_optarg=`expr "x$ac_option" : 'x[^=]*=\(.*\)'`

	case $ac_option in
		-help | --help | --hel | --he | -h)
		    ac_init_help=yes ;;
		-prefix | --prefix)
			ac_prev=prefix ;;
		-prefix=* | --prefix=*)
			prefix=$ac_optarg ;;
		--enable-debug)
			debug="yes" ;;
		-debug)
			ac_prev=trace ;;
		-debug=* | --debug=*)
			trace=$ac_optarg ;;
		--enable-optimise | -enable-optimise | -optimise)
			optimise="yes" ;;
		--enable-warning | -enable-warning)
			warning="yes" ;;
		--error | -error)
			werror="yes" ;;
		-*) { echo "$as_me: error: unrecognized option: $ac_option" >&2
			echo "Try \`$0 --help' for more information." >&2
			{ (exit 1); exit 1; }; }
			;;					 
	esac
done

if test "$ac_init_help" == "yes"; then
		cat << EOF
\`configure' configures this package to adapt to many kinds of systems.

Usage: $0 [OPTION]... [VAR=VALUE]...

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
  --enable-debug          enable -g option for \$(CXX)
  --enable-optimise       enable optimisation (Recommended)
  --debug=TYPE            type of debug (log, trace or debug)
  --enable-warning        enable warnings
  --error                 treat warnings as errors (default no)
Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [$ac_default_prefix]
EOF
exit 0
fi

##############" OS DETECTION ##########################
PLATFORM=""
echo "OS name : `uname`"
case `uname | tr 'A-Z' 'a-z'` in
	linux)
		PLATFORM="LINUX"
		kernel=`uname -r`
		case $kernel in
			1.*) kernel_v="1" ;;
			2.[0-2].*) kernel_v="2" ;;
			2.[3-4].*) kernel_v="2.4" ;;
		esac
			
		echo -n "Kernel Version : $kernel... "
		if test "$kernel_v" == "1"; then
			echo "Are you crazy, man ?"
		elif test "$kernel_v" == "2"; then
			echo "You can encounter some problems..."
		elif test "$kernel_v" == "2.4"; then 
			echo "ok"
		fi
		;;
	*) 
		echo "WARNING : you don't use Linux. You can encounter some bugs !!!"
		PLATFORM="UNKNOWN"
esac
#######################################################

############## CPU DETECTION ##########################
# from mplayer configure
# maybe usefull in future
if test -r /proc/cpuinfo; then
	_cpuinfo="cat /proc/cpuinfo"
fi

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`

echo "CPU Type   : $pname"
echo "CPU Vendor : $pvendor"
#######################################################

gl_include_dir_not_set=0
gl_lib_dir_not_set=0
al_include_dir_not_set=0
al_lib_dir_not_set=0
vorbis_include_dir_not_set=0
vorbis_lib_dir_not_set=0

GL_INCLUDE_DIR=""
GL_LIB_DIR=""
AL_INCLUDE_DIR=""
AL_LIB_DIR=""
VORBIS_LIB_DIR=""
VORBIS_INCLUDE_DIR=""
for i in /usr/X11R6 /usr /usr/local ; do
	if test $gl_include_dir_not_set -eq 0; then
		if [ "`find $i/include -type d -name GL`" != "" ]; then
			GL_INCLUDE_DIR=$i; gl_include_dir_not_set=1
		fi
	fi
	if test $gl_lib_dir_not_set -eq 0; then
		if [ "`find $i/lib -type f -maxdepth 1 -name libGL*`" != "" ]; then
			GL_LIB_DIR=$i; gl_lib_dir_not_set=1
		fi
	fi
	if test $al_include_dir_not_set -eq 0; then
		if [ "`find $i/include -type d -name AL`" != "" ]; then
			AL_INCLUDE_DIR=$i; al_include_dir_not_set=1
		fi
	fi
	if test $al_lib_dir_not_set -eq 0; then
		if [ "`find $i/lib -type f -maxdepth 1 -name libopenal*`" != "" ]; then
			AL_LIB_DIR=$i; al_lib_dir_not_set=1
		fi
	fi
	if test $vorbis_include_dir_not_set -eq 0; then
		if [ "`find $i/include -type d -name vorbis`" != "" ]; then
			VORBIS_INCLUDE_DIR=$i; vorbis_include_dir_not_set=1
		fi
	fi
	if test $vorbis_lib_dir_not_set -eq 0; then
		if [ "`find $i/lib -type f -maxdepth 1 -name libvorbis*`" != "" ]; then
			VORBIS_LIB_DIR=$i; vorbis_lib_dir_not_set=1
		fi
	fi
done

echo -n "Checking for OpenGL includes : "
if test "$GL_INCLUDE_DIR" == ""; then
	echo "no"
	echo "Please install OpenGL includes"	
	exit 1
else 
	echo "yes"
	GL_INCLUDES="-I$GL_INCLUDE_DIR/include/GL"
	echo "OpenGL include : $GL_INCLUDE_DIR/include"
	version=`grep GL_VERSION $GL_INCLUDE_DIR/include/GL/gl.h`
	if test "`echo $version | grep GL_VERSION_1_2`"; then
		GL_VERSION="1.2"
	elif test "`echo $version | grep GL_VERSION_1_1`"; then
		GL_VERSION="1.1"
	else
		GL_VERSION="unknown"
	fi
	echo "OpenGL version : $GL_VERSION"
	if test "$GL_VERSION" == "unknown"; then
		echo "Please upgrade your OpenGL includes and lib to release 1.2.x"
		exit 1
	fi
fi

echo -n "Checking for OpenGL lib : "
if test "$GL_LIB_DIR" == ""; then
	echo "no"
	echo "Please install OpenGL lib"
	exit 1
else 
	echo "yes"
	GL_LIB="-L$GL_LIB_DIR/lib -lGL -lGLU"
	echo "OpenGL lib : $GL_LIB_DIR/lib -lGL -lGLU"
fi

echo -n "Checking for OpenAL includes : "
if test "$AL_INCLUDE_DIR" == ""; then
	echo "no"
	echo "Please install OpenAL includes"	
	exit 1
else 
	echo "yes"
	AL_INCLUDES="-I$AL_INCLUDE_DIR/include/AL"
	echo "OpenAL include : $AL_INCLUDE_DIR/include"
fi

echo -n "Checking for OpenAL lib : "
if test "$AL_LIB_DIR" == ""; then
	echo "no"
	echo "Please install OpenAL lib"
	exit 1
else 
	echo "yes"
	AL_LIB="-L$AL_LIB_DIR/lib -lopenal"
	echo "OpenAL lib : $AL_LIB_DIR/lib -lopenal"
fi

echo -n "Checking for OggVorbis includes : "
if test "$VORBIS_INCLUDE_DIR" == ""; then
	echo "no"
	echo "Please install OggVorbis includes"	
	exit 1
else 
	echo "yes"
	VORBIS_INCLUDES="-I$VORBIS_INCLUDE_DIR/include/vorbis"
	echo "OggVorbis include : $VORBIS_INCLUDE_DIR/include"
fi

echo -n "Checking for OggVorbis lib : "
if test "$VORBIS_LIB_DIR" == ""; then
	echo "no"
	echo "Please install OggVorbis lib"
	exit 1
else 
	echo "yes"
	VORBIS_LIB="-L$VORBIS_LIB_DIR/lib -lvorbis"
	echo "OggVorbis lib : $VORBIS_LIB_DIR/lib -lvorbis"
fi

echo -n "Checking for glut include : "
if ! test -r $GL_INCLUDE_DIR/include/GL/glut.h; then
	echo "no"
	echo "Please install glut"
	exit 1
else 
	echo "yes"
	version="`cat $GL_INCLUDE_DIR/include/GL/glut.h | grep GLUT_API_VERSION | grep define`"
	if test "`echo $version | grep 3`" != ""; then
		GLUT_VERSION="3"
	else
		GLUT_VERSION="unknown"
	fi
	echo "glut version : $GLUT_VERSION"
	if test "$GLUT_VERSION" == "unknown"; then
		echo "Please upgrade your glut includes and lib to version 3.x"
		exit 1
	fi
fi

echo -n "Checking for glut lib : "
if test -r $GL_LIB_DIR/libglut.so; then
	echo "no"
	echo "Please install glut"
	exit 1
else 
	echo "yes"
	GLUT_LIB="-lglut"
fi

echo -n "Checking for glxext.h : "
if test -r $GL_INCLUDE_DIR/glxext.h; then
	echo "no"
	echo "Please install OpenGL Extensions"
	exit 1
else 
	echo "yes"
fi

if test "$prefix" == ""; then
	PREFIX=$ac_default_prefix
else
	PREFIX=$prefix
fi
echo "Checking for PREFIX : $PREFIX"

echo -n "Checking for gcc version : "
GCC_VERSION=`gcc -dumpversion`
echo $GCC_VERSION

GCC_MAJOR=`echo $GCC_VERSION |  cut -c 1`

case $GCC_VERSION in
	1.*) echo ":) Do you really thing it's a good idea ???" ; exit 1;;
	2.96*) echo "gcc 2.96 is a joke... Upgrade to 3.x or downgrade to 2.95" ; exit 1 ;;
	2.95*) LOOPS="-malign-loops=2 -malign-functions=2 -malign-jumps=2" ;;
	3.*) LOOPS="-falign-loops=2 -falign-functions=2 -falign-jumps=2" ;;
esac

EXTRA_LIBS=""
TRACE="-DNORMAL"
echo -n "extra cflags : "
if test "$debug" == "yes"; then
	if test "$trace" == "trace"; then
		TRACE="-DTRACE"
		EXTRA_LIBS="-lpthread"
	elif test "$trace" == "log"; then
		TRACE="-DLOG"
	else 
		TRACE="-DDEBUG";
	fi
	echo "-g $TRACE $EXTRA_LIBS"
	DEBUG="-g $TRACE"
else
	DEBUG="$TRACE"
	echo "$TRACE $EXTRA_LIBS"
fi

FLAGS="-O3"
if test "$optimise" == "yes"; then
	FLAGS="-O6  -fstrength-reduce -frerun-loop-opt \\
			-fexpensive-optimizations -fschedule-insns2 -funroll-loops \\
			-fomit-frame-pointer -malign-double -fno-strict-aliasing -pipe \\
			$LOOPS -ffast-math -fexceptions \\
			-fstrict-aliasing \\
			-mcpu=`uname -m` -march=`uname -m` -pipe \\
			-fPIC -finline-functions -frename-registers"
fi

case $GCC_VERSION in
	3.*) WARNING="-Wno-deprecated" ;;
	*) WARNING="" ;;
esac
if test "$warning" == "yes"; then
		WARNING="$WARNING -Wall -Wunused"
fi

if test "$werror" == "yes"; then
		WARNING="$WARNING -Werror"
fi

echo "Creating Makefile.config"
cat <<EOF >Makefile.config
PREFIX = $PREFIX
INSTALL = ginstall

EXNIHILO_LIB = libExNihilo.so
MAJOR_VERSION = $MAJOR_VERSION
MINOR_VERSION = $MINOR_VERSION

LN = ln -s
RM = rm -f

GL_INCLUDES = $GL_INCLUDES
AL_INCLUDES = $AL_INCLUDES
VORBIS_INCLUDES = $VORBIS_INCLUDES

INCLUDES = \$(GL_INCLUDES) \$(AL_INCLUDES) \\
			\$(VORBIS_INCLUDES) \\
			-I. -I.. -I../../include \\
			-I../../include/GlutEngine -I../../include/Manager \\
			-I../../include/Math -I../../include/MenuInterface \\
			-I../../include/Object -I../../include/Tools

CXXFLAGS = $WARNING \\
			$FLAGS \\
			-DUNIX_SRC -D$PLATFORM \\
			\$(INCLUDES) \\
			-DPREFIX=\"\$(PREFIX)/share\" \\
			-DVERSION=\"\$(MAJOR_VERSION).\$(MINOR_VERSION)\" $DEBUG

GL_LIB = $GL_LIB
AL_LIB = $AL_LIB
VORBIS_LIB = $VORBIS_LIB
GLUT_LIB = $GLUT_LIB

CXXLIB = \$(GL_LIB) \$(AL_LIB) \$(VORBIS_LIB) -lXmu -lXt -lSM -lICE \\
				-lXext -lX11 -lXi -lXext -lXft \$(GLUT_LIB) \\
				-lm $EXTRA_LIBS

CXX = g++
EOF

echo "Creating exnihilo-config"
cat <<EOF >exnihilo-config
#!/bin/sh

exnihilo_dir=$PREFIX
exnihilo_libs="-L$PREFIX/lib -lExNihilo"
exnihilo_cflags="-DUNIX_SRC -I$PREFIX/include/ExNihilo -I$PREFIX/include/ExNihilo/GlutEngine -I$PREFIX/include/ExNihilo/Manager -I$PREFIX/include/ExNihilo/Math -I$PREFIX/include/ExNihilo/MenuInterface -I$PREFIX/include/ExNihilo/Object -I$PREFIX/include/ExNihilo/Tools"


if test \$# -eq 0; then
	echo "Usage: exnihilo-config [OPTIONS]"
	echo "Options:"
	echo "   [--version]"
	echo "   [--libs]"
	echo "   [--cflags]"
	echo "   [--install-dir]"
	exit \$1
fi

while test \$# -gt 0; do
  case "\$1" in
  -*=*) optarg=`echo "\$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case \$1 in
    --version)
      echo $MAJOR_VERSION.$MINOR_VERSION
      ;;
    --cflags)
      echo_cflags=yes
      ;;
    --libs)
      echo_libs=yes
      ;;
		--install-dir)
			echo_install_dir=yes
			;;
    *)
			echo "Usage: exnihilo-config [OPTIONS]"
			echo "Options:"
			echo "   [--version]"
			echo "   [--libs]"
			echo "   [--cflags]"
			echo "   [--install-dir]"
			exit \$1
      ;;
  esac
  shift
done

if test "\$echo_cflags" = "yes"; then
	echo \$exnihilo_cflags -I/usr/X11R6/include $GL_INCLUDES $AL_INCLUDES
fi

if test "\$echo_libs" = "yes"; then
	echo $GL_LIB -lXmu -lXt -lSM -lICE -lXext -lX11 -lXi -lXext -lXft $GLUT_LIB $AL_LIB $VORBIS_LIB \$exnihilo_libs -lm $EXTRA_LIBS
fi 

if test "\$echo_install_dir" = "yes"; then
	echo \$exnihilo_dir
fi

EOF
chmod 755 exnihilo-config
