dnl Process this file with autoconf to produce a configure script.

AC_INIT(include/alleggl.h)
AC_PREREQ(2.53)
AC_CONFIG_AUX_DIR(misc)
AC_CONFIG_HEADER(include/alleggl_config.h:include/alleggl_config.hin)

dnl Build CFLAGS from scratch
CFLAGS=

dnl General configuration
AH_TEMPLATE([ALLEGROGL_HAVE_XF86VIDMODE], [Define if XF86VidMode extension is supported.])
AC_ARG_ENABLE(
	xf86vidmode,
	[  --enable-xf86vidmode[=x]    use XF86VidMode Extension [default=yes]],
	test "X$enableval" != "Xno" && agl_enable_xf86vidmode=yes,
	agl_enable_xf86vidmode=yes
)


dnl Enable generic driver building
AH_TEMPLATE([ALLEGROGL_GENERIC_DRIVER], [Define if the Mesa generic driver must be built.])
AC_ARG_ENABLE(
	generic,
	[  --enable-generic[=x]        build Mesa generic driver [default=no]],
	test "X$enableval" != "Xno" && agl_build_generic=yes,
	PLATFORM_SOURCE="x.c"
)


dnl Enable shared library building
AC_ARG_ENABLE(
	shared,
	[  --enable-shared[=x]         build shared libraries [default=yes]],
	test "X$enableval" != "Xno" && agl_build_shared=yes,
	agl_build_shared=yes
)

if test -n "$agl_build_shared"; then
	LIB_BUILDER="\$(CC) -shared -Wl,-soname,lib\$(LIB_NAME).so.\$(shared_version) -o"
	LIB_PATH_U="\$(LIB_DIR)/lib\$(LIB_NAME).so.\$(shared_version)"
	LIB_LINKS_U="\$(LIB_DIR)/lib\$(LIB_NAME).so.\$(shared_major_minor) \$(LIB_DIR)/lib\$(LIB_NAME).so.\$(shared_major) \$(LIB_DIR)/lib\$(LIB_NAME).so"
	SHARED="-fPIC"
else
	LIB_BUILDER="ar -rs"
	LIB_PATH_U="\$(LIB_DIR)/lib\$(LIB_NAME).a"
fi


AC_PROG_CC
AC_PROG_INSTALL


dnl Check for pthread support, which GL libs may require (e.g. FreeBSD)
AC_CHECK_HEADER(pthread.h, , dontcare=y)
AC_CHECK_LIB(pthread, pthread_create, LIBS="-lpthread $LIBS")


dnl Build Mesa generic driver
if test -n "$agl_build_generic"; then
	AC_PROG_CXX
	AC_DEFINE(ALLEGROGL_GENERIC_DRIVER)
	PLATFORM_SOURCE="\$(DOS_SOURCE)"
	MESABUILD="yes"
	MESACLEAN="mesaclean"

else

dnl These libs are not needed if we build the generic driver

dnl Test for location of X headers and libraries
	AC_PATH_X
	if test -n "$x_includes"; then
		CPPFLAGS="-I$x_includes $CPPFLAGS"
	fi
	if test -n "$x_libraries"; then
		LDFLAGS="-L$x_libraries $LDFLAGS"
	fi
	LIBS="-lXext -lX11 -lm $LIBS"


dnl Test for VidMode extension
	if test -n "$agl_enable_xf86vidmode"; then
		AC_CHECK_LIB(Xxf86vm, XF86VidModeQueryExtension,
			AC_DEFINE(ALLEGROGL_HAVE_XF86VIDMODE) LIBS="-lXxf86vm $LIBS",
			,
			$LIBS
		)
	fi

dnl Check if the dynamic loading library (libdl.so) is available
	AH_TEMPLATE([ALLEGROGL_HAVE_DYNAMIC_LINK], [Define if dynamic linking is supported.])
	AC_CHECK_HEADER(dlfcn.h,
		AC_CHECK_LIB(dl, dlsym, 
			LIBS="-ldl $LIBS"
			AC_DEFINE(ALLEGROGL_HAVE_DYNAMIC_LINK),
			fail_dl=y
		)
	)
fi


dnl Find the name of glXGetProcAddress[ARB]
	AH_TEMPLATE([ALLEGROGL_GLXGETPROCADDRESSARB], [Define if glXGetProcAddress must be appended by ARB.])
	AC_CHECK_LIB(GL, glXGetProcAddress, , fail=y, $LIBS)
	
	if test -n "$fail"; then
		fail=
		AC_CHECK_LIB(GL, glXGetProcAddressARB,
			AC_DEFINE(ALLEGROGL_GLXGETPROCADDRESSARB), 
			fail=y, $LIBS)
		if test -n "$fail" && test -n "$fail_dl"; then
			AC_MSG_ERROR([OpenGL does not export symbol glXGetProcAddress(ARB)])
		fi
	fi

dnl Locate GL headers and libraries
	AC_CHECK_HEADER(GL/glx.h, , fail=y)
	AC_CHECK_HEADER(GL/gl.h, , fail=y)
	AC_CHECK_LIB(GL, glBlendFunc, LIBS="-lGL $LIBS", fail=y, $LIBS)

	if test -n "$fail"; then
		AC_MSG_ERROR([Unable to locate required OpenGL headers/libraries])
	fi


dnl Locate GLU headers and libraries
AC_CHECK_HEADER(GL/glu.h, , fail_glu=y)
AC_CHECK_LIB(GLU, gluSphere, LIBS="-lGLU $LIBS", fail_glu=y, -lGL)
if test -n "$fail_glu"; then
	if test -n "$agl_build_generic"; then
		BUILD_GLU="\$(GLU_OBJECTS)"
		GLU_HEADERS="\$(GLU_HEADERS)"
	else
		AC_MSG_ERROR([Unable to locate required GLU headers/libraries])
	fi
fi


dnl Check Allegro is installed
AM_PATH_ALLEGRO([4.2.0], ,AC_MSG_ERROR([AllegroGL requires Allegro >= 4.2.0 to be installed!]))


CPPFLAGS="$allegro_CFLAGS $CPPFLAGS"


dnl Prepare makefile

AC_SUBST(LIBS)
AC_SUBST(LDFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(CC)
AC_SUBST(LIB_BUILDER)
AC_SUBST(LIB_PATH_U)
AC_SUBST(LIB_LINKS_U)
AC_SUBST(SHARED)
AC_SUBST(ALLEGRO_CONFIG)

dnl Additional substitutions for the generic driver

AC_SUBST(CXX)
AC_SUBST(PLATFORM_SOURCE)
AC_SUBST(MESABUILD)
AC_SUBST(MESACLEAN)
AC_SUBST(BUILD_GLU)
AC_SUBST(GLU_HEADERS)


dnl if a makefile already exists then remove the older objects files
dnl This is done because some files must be re-built if we switch between
dnl the Mesa generic driver and the (hopefully) accelerated one
if test -f makefile; then
	echo '# Remove older build'
	make veryclean
	agl_make_depend="yes";
fi


dnl If dependencies are not built yet then do it !
if (test ! -f make/makefile.dep); then
	echo '# Execute "make depend".' > make/makefile.dep
	agl_make_depend="yes";
fi


AC_OUTPUT(makefile:make/makefile.unx,
  [test -z "$CONFIG_HEADERS" || echo timestamp > stamp-h])


dnl Run "make depend" if necessary
if test -n "$agl_make_depend"; then
	make depend
fi
