AC_INIT(configure.in)

AM_CONFIG_HEADER(config.h)
AC_CANONICAL_HOST
AC_CANONICAL_TARGET

AM_INIT_AUTOMAKE(magic3d, 0.1.0)


AC_LANG_CPLUSPLUS
AC_PROG_CXX
AM_PROG_LIBTOOL

# Checks for libraries.
case "$target" in
    *-*-cygwin* | *-*-mingw32*)
	LIBS=""
	AC_DEFINE(WIN32, 1, [Compiling on cygwin/mingw])
	AC_CHECK_HEADERS([windows.h])
	AC_CHECK_LIB([png], [png_create_read_struct],,AC_MSG_ERROR([*** png library not found ***]))
	PNG_LIBS="$LIBS"
	LIBS=""
	AC_CHECK_LIB([jpeg], [jpeg_read_header],,AC_MSG_ERROR([*** jpeg library not found ***]))
	JPEG_LIBS="$LIBS"
	LIBS=""
	GL_LIBS="-lgdi32 -lopengl32 -lglu32"
	LIBS=""
	AC_CHECK_LIB([glew32], [glewInit],,AC_MSG_ERROR([*** GLEW library not found ***]))
	GLEW_LIBS="$LIBS"
	LIBS=""
        AC_CHECK_LIB([glfw], [glfwInit],,AC_MSG_ERROR([*** GLFW library not found ***]), $GL_LIBS $X11_LIBS $PTHREAD_LIBS)
	GLFW_LIBS="$LIBS"
	LIBS=""
	
	AC_SUBST(PNG_LIBS)
	AC_SUBST(JPEG_LIBS)
	AC_SUBST(GL_LIBS)
	AC_SUBST(GLU_LIBS)
	AC_SUBST(GLEW_LIBS)
        AC_SUBST(GLFW_LIBS)
	;;
    *-*-linux-gnu*)
	AC_DEFINE(LINUX, 1, [Compiling on GNU/Linux])
	
	AC_CHECK_LIB([png], [png_create_read_struct],,AC_MSG_ERROR([*** png library not found ***]))
	PNG_LIBS="$LIBS"
	LIBS=""
	AC_CHECK_LIB([jpeg], [jpeg_read_header],,AC_MSG_ERROR([*** jpeg library not found ***]))
	JPEG_LIBS="$LIBS"
	LIBS=""
	AC_CHECK_LIB([GL], [glEnable],,AC_MSG_ERROR([*** GL library not found ***]))
	GL_LIBS="$LIBS"
	LIBS=""
	AC_CHECK_LIB([GLU], [gluCheckExtension],,AC_MSG_ERROR([*** GLU library not found ***]))
	GLU_LIBS="$LIBS"
	LIBS=""
	AC_CHECK_LIB([GLEW], [glewInit],,AC_MSG_ERROR([*** GLEW library not found ***]))
	GLEW_LIBS="$LIBS"

        AC_PATH_PROG([PKGCONFIG], [pkg-config], [no])  
        AC_MSG_CHECKING([For X11 libraryes])
        if test "$PKGCONFIG" != "no" && `$PKGCONFIG --exists x11`
         then
          X11_L=`$PKGCONFIG --libs x11`
	  xt_have_shout="maybe"
	  AC_MSG_RESULT([$X11_LIBS])
	  LIBS=""
          AC_CHECK_LIB([X11], [XCreateWindow],,AC_MSG_ERROR([*** X11 library not found ***]), $X11_L)
          AC_CHECK_LIB([Xxf86vm], [XF86VidModeAddModeLine],,AC_MSG_ERROR([*** Xxf86vm library not found ***]), $X11_L)
          AC_CHECK_LIB([Xrandr], [XRRUpdateConfiguration],,AC_MSG_ERROR([*** Xrandr library not found ***]), $X11_L)
          X11_LIBS="$X11_L $LIBS"
         else
         if test "$PKGCONFIG" != "no"
         then
	  AC_MSG_ERROR([no])
         else 
          AC_MSG_ERROR([no])
         fi
        fi
	LIBS=""
	
        AC_CHECK_LIB([glfw], [glfwInit],,AC_MSG_ERROR([*** GLFW library not found ***]), $GL_LIBS $X11_LIBS $PTHREAD_LIBS)
	GLFW_LIBS="$LIBS"
	LIBS=""
	
	AC_SUBST(PNG_LIBS)
	AC_SUBST(JPEG_LIBS)
	AC_SUBST(GL_LIBS)
	AC_SUBST(GLU_LIBS)
	AC_SUBST(GLEW_LIBS)
        AC_SUBST(GLFW_LIBS)
        AC_SUBST(X11_LIBS)
	;;
esac

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([stdint.h stdlib.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_C_VOLATILE

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_CHECK_FUNCS([memmove memset])


AC_OUTPUT(Makefile \
	meshconverter/Makefile \
	objectviewer/Makefile \
	src/FileFormats/m3dm/Makefile \
	src/FileFormats/m3dml/Makefile \
	src/FileFormats/m3ds/Makefile \
	src/FileFormats/m3dme/Makefile \
	src/FileFormats/Makefile \
	src/FileFormats/m3dt/Makefile \
	src/GraphicObjects/Makefile \
	src/Interfaces/Makefile \
	src/Makefile \
	src/OpenGLEngine/Makefile \
	src/utils/Makefile \
	test/Makefile \
	textureconverter/Makefile \
	src/structures/Makefile \
	tools/Makefile \
	tools/heightmap2m3dme/Makefile)
