# Process this file with autoconf to produce a configure script.
AC_INIT(src/driver.cpp)
AM_INIT_AUTOMAKE(rss-glx,0.7.3)
AM_CONFIG_HEADER(config.h)

# Checks for programs.
AC_PROG_AWK
AC_PROG_CXX
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S

# SSE available?
AC_ARG_ENABLE(sse, [  --disable-sse           Don't compile with sse.])
if test "x$enable_sse" != "xno" ; then
	echo -n "checking for sse... "
	AC_COMPILE_IFELSE([int main() { __asm__ ( "xorps %%xmm0, %%xmm0" : : ); exit(0); }], [ echo "yes"; AC_DEFINE([USE_SSE], [], [SSE is available.]) ], [ echo "no" ])
fi

# 3DNow available?
AC_ARG_ENABLE(3dnow, [  --disable-3dnow         Don't compile with 3dnow.])
if test "x$enable_3dnow" != "xno" ; then
	echo -n "checking for 3dnow... "
	AC_COMPILE_IFELSE([int main() { __asm__ ( "pfmul %%mm0, %%mm0" : : ); exit(0); }], [ echo "yes"; AC_DEFINE([USE_3DNOW], [], [3DNow is available.]) ], [ echo "no" ])
fi

# Checks for libraries.
AC_CHECK_LIB([m], [pow],,AC_MSG_ERROR(-lm was not found but required.))
AC_ARG_ENABLE(bzip2, [  --disable-bzip2         Don't compress textures/audio with bzip2.])
if test "x$enable_bzip2" != "xno" ; then
	AC_CHECK_LIB([bz2], [BZ2_bzBuffToBuffCompress], AC_SUBST(BZIP2, -lbz2) AC_DEFINE([HAVE_LIBBZ2], [], [libbz2 exists.]))
fi

# Checks for header files.
AC_HEADER_STDC
AC_PATH_XTRA 

CFLAGS="$CFLAGS $X_CFLAGS"
CPPFLAGS="$CPPFLAGS $X_CFLAGS"
LIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS $LIBS"

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_HEADER_TIME

# Checks for library functions.
AC_CHECK_FUNCS(getopt,,AC_MSG_ERROR([getopt is missing but required.]))
AC_CHECK_HEADERS([getopt.h bzlib.h])

AC_CHECK_FUNCS(gettimeofday,,AC_MSG_ERROR([gettimeofday is missing but required.]))

AC_CHECK_FUNCS(nanosleep)
if test "x$ac_cv_func_nanosleep" = "xno" ; then
	echo "Hrm, maybe nanosleep is in -lrt";
	AC_CHECK_LIB([rt], [nanosleep])

	unset ac_cv_func_nanosleep
	AC_CHECK_FUNCS(nanosleep)
fi

if test -d reallyslick/cpp_src/skyrocket_sounds ; then
	AC_ARG_ENABLE(sound, [  --disable-sound         Don't compile with sound.])
else
	echo "Audio samples not found, disabling sound.";
	enable_sound=no
fi

AC_CHECK_HEADERS([pthread.h])
if test "x$ac_cv_header_pthread_h" = "xyes" ; then
	# Maybe we don't need to link squat
	AC_CHECK_FUNCS(pthread_create)

	if test "x$ac_cv_func_pthread_create" = "xno" ; then
		AC_CHECK_LIB([pthread], [pthread_create], AC_SUBST(PTHREADS, "-lpthread") AC_DEFINE([HAVE_PTHREADS], [], [pthreads is available for use.]))

		if test "x$ac_cv_lib_pthread_pthread_create" == "xno" ; then
			AC_CHECK_LIB([c_r], [pthread_create], AC_SUBST(PTHREADS, "-lc_r") AC_DEFINE([HAVE_PTHREADS], [], [pthreads is available for use.]))
			AM_CONDITIONAL(WITHPTHREADS, test "x$ac_cv_lib_c_r_pthread_create" == "xyes")
		else
			AM_CONDITIONAL(WITHPTHREADS, true)
		fi
	else 
		AM_CONDITIONAL(WITHPTHREADS, true)
	fi
else
	AM_CONDITIONAL(WITHPTHREADS, false)
fi

if test "x$enable_sound" != "xno" ; then

	# Check for OpenAL headers first.
	AC_CHECK_HEADERS([AL/al.h])
	if test "x$ac_cv_header_AL_al_h" = "xno" ; then
		cflags_sav=$CFLAGS
		cppflags_sav=$CPPFLAGS
		CFLAGS="$CFLAGS -I/usr/local/include"
		CPPFLAGS="$CPPFLAGS -I/usr/local/include"

		echo "Hrm, maybe AL/al.h is in /usr/local/include";
		unset ac_cv_header_AL_al_h
		AC_CHECK_HEADERS([AL/al.h])

		if test "x$ac_cv_header_AL_al_h" = "xno" ; then
			CFLAGS=$cflags_sav
			CPPFLAGS=$cppflags_sav
		fi
	fi

	if test "x$ac_cv_header_AL_al_h" = "xyes" ; then
		AC_CHECK_HEADERS([AL/alut.h])

		if test "x$ac_cv_header_AL_alut_h" = "xyes" ; then
			# Check for OpenAL library
			AC_CHECK_LIB([openal], [alutInit], AC_SUBST(OPENAL, -lopenal) AC_DEFINE([HAVE_OPENAL], [], [OpenAL is available for use.]))

			if test "x$ac_cv_lib_openal_alutInit" = "xno" ; then
				libs_sav=$LIBS
				LIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS $LIBS -L/usr/local/lib"

				echo "Hrm, maybe -lopenal is in /usr/local/lib";
				unset ac_cv_lib_openal_alutInit
				AC_CHECK_LIB([openal], [alutInit], AC_SUBST(OPENAL, -lopenal) AC_DEFINE([HAVE_OPENAL], [], [OpenAL is available for use.]))

				if test "x$ac_cv_lib_openal_alutInit" = "xno" ; then
					# For FreeBSD -current
					echo "And perhaps -lopenal needs pthreads (-lc_r on FreeBSD -current)";

					libs_cr_sav=$LIBS
					LIBS="$LIBS -lc_r"

					unset ac_cv_lib_openal_alutInit
					AC_CHECK_LIB([openal], [alutInit], AC_SUBST(OPENAL, "-lopenal -lc_r") AC_DEFINE([HAVE_OPENAL], [], [OpenAL is available for use.]))

					LIBS=$libs_cr_sav

					if test "x$ac_cv_lib_openal_alutInit" = "xno" ; then
						LIBS=$libs_sav
					fi
				fi
			fi
		fi
	fi

	AM_CONDITIONAL(WITHSOUND, test "x$ac_cv_lib_openal_alutInit" == "xyes")
else
	AM_CONDITIONAL(WITHSOUND, false)
fi

# Check for OpenGL headers first.
AC_CHECK_HEADER(GL/gl.h)

if test "x$ac_cv_header_GL_gl_h" = "xno" ; then
	CFLAGS="$CFLAGS -I/usr/openwin/share/include"
	CPPFLAGS="$CPPFLAGS -I/usr/openwin/share/include"

	echo "Hrm, maybe GL/gl.h is in /usr/openwin/share/include";
        unset ac_cv_header_GL_gl_h
	AC_CHECK_HEADER(GL/gl.h,,AC_MSG_ERROR([[No gl.h found.]]))
fi

AC_CHECK_HEADER(GL/glx.h,,AC_MSG_ERROR([[No glx.h found.]]))

AC_CHECK_LIB(GL,glNewList)
if test "x$ac_cv_lib_GL_glNewList" = "xno" ; then
        dnl Check for MesaGL.
        AC_CHECK_LIB(MesaGL, glNewList,,AC_MSG_ERROR(GL library was not found.))
fi

# Check for GLU headers.
AC_CHECK_HEADER(GL/glu.h,,AC_MSG_ERROR([[No glu.h found.]]))

AC_CHECK_LIB(GLU,gluLookAt)
if test "x$ac_cv_lib_GLU_gluLookAt" = "xno"; then
        dnl Check for MesaGLU.
        AC_CHECK_LIB(MesaGLU,gluLookAt,,AC_MSG_ERROR(GLU library was not found.))
fi

# Check for ImageMagick headers.
AC_CHECK_HEADERS([magick/api.h])

if test "x$ac_cv_header_magick_api_h" = "xyes" ; then
	AC_CHECK_LIB([Magick], [InitializeMagick], AC_SUBST(IMAGEMAGICK, -lMagick) AC_DEFINE([HAVE_IMAGEMAGICK], [], [ImageMagick is available for use.]))

	AM_CONDITIONAL(WITHIMAGEMAGICK, test "x$ac_cv_lib_Magick_InitializeMagick" == "xyes")
else
	AM_CONDITIONAL(WITHIMAGEMAGICK, false)
fi

AC_ARG_WITH(configdir, [  --with-configdir        Where is xscreensaver stashing its screensaver configs?])

if test "$with_configdir" != "" ; then
	AC_SUBST(configdir, $with_configdir)
	INSTALL_XSCREENSAVER_CONFIG_FILES=yes
else 
	which_xscreensaverdemo=`which xscreensaver-demo`
	if test -x "$which_xscreensaverdemo" ; then
		with_configdir=`strings $which_xscreensaverdemo | grep "\/config"`

		if test -d "$with_configdir" ; then
			AC_SUBST(configdir, $with_configdir)
			INSTALL_XSCREENSAVER_CONFIG_FILES=yes
		fi
	fi
fi

AM_CONDITIONAL(INSTALL_XSCREENSAVER_CONFIG_FILES, test "$INSTALL_XSCREENSAVER_CONFIG_FILES" == "yes")

AC_ARG_WITH(kdessconfigdir, [  --with-kdessconfigdir   Where is KDE stashing its screensaver configs?])

if test "$with_kdessconfigdir" != "" ; then
	AC_SUBST(kdessconfigdir, $with_kdessconfigdir)
	INSTALL_KDE_CONFIG_FILES=yes
else 
	with_kdessconfigdir=/usr/share/applnk/System/ScreenSavers

	if test -d "$with_kdessconfigdir" ; then
		AC_SUBST(kdessconfigdir, $with_kdessconfigdir)
		INSTALL_KDE_CONFIG_FILES=yes
	fi
fi

AM_CONDITIONAL(INSTALL_KDE_CONFIG_FILES, test "$INSTALL_KDE_CONFIG_FILES" == "yes")

AC_OUTPUT([
	Makefile
	oglc_src/Makefile
	other_src/Makefile
	reallyslick/Makefile
	reallyslick/c_src/Makefile
	reallyslick/cpp_src/Makefile
	utils/Makefile
])
