AC_INIT(configure.in)


AC_CONFIG_SUBDIRS(me_libs)

AM_CONFIG_HEADER(conf_config.h)

AC_CANONICAL_HOST
AC_CANONICAL_TARGET


AM_INIT_AUTOMAKE(magicengine, 2.0.0)
AC_LANG_CPLUSPLUS
AC_PROG_CXX
AM_PROG_LIBTOOL

MOGUL=""
GLFW="enable_glfw"
AC_ARG_ENABLE(mogul,
    [AC_HELP_STRING([--enable-mogul],
    [Enable mogul driver (disabled by default)])],
    MOGUL="$enableval",
    MOGUL=""
)
AC_ARG_ENABLE(glfw,
    [AC_HELP_STRING([--disable-glfw],
    [Enable glfw driver (enabled by default)])],
    GLFW="$enableval",
    GLFW="enable_glfw"
)

AC_MSG_CHECKING([for FreeType version])
FT_VERSION=`freetype-config --ftversion`

if test "$?" -eq 0 
then 
    AC_MSG_RESULT([$FT_VERSION])
    FREETYPE_CFLAGS=`freetype-config --cflags`
    FREETYPE_LDFLAGS=`freetype-config --libs`
    AC_SUBST(FREETYPE_CFLAGS)
    AC_SUBST(FREETYPE_LDFLAGS)
else
    AC_MSG_ERROR([FreeType library not found])
fi

AC_MSG_CHECKING([Driver dir])
AC_ARG_WITH(driverdir,
    [AC_HELP_STRING([--with-driverdir=PREFIX],
        [Prefix from where drivers will be loaded(optional)])],
    DRIVER_DIR="$withval",
    DRIVER_DIR="drv"
    )
AC_MSG_RESULT($DRIVER_DIR);
CXXFLAGS="$CXXFLAGS -DDRIVER_DIR=\"\\\"$DRIVER_DIR\\\"\""
AC_SUBST(DRIVER_DIR)
VIDEO_DRIVERS=""
#CXXFLAGS="$CXXFLAGS -std=c++0x"

# Checks for libraries.
case "$target" in
    *-*-cygwin* | *-*-mingw32*)
AC_MSG_CHECKING([for FreeType version])
FT_VERSION=`freetype-config --ftversion`

if test "$?" -eq 0 
then 
    AC_MSG_RESULT([$FT_VERSION])
    FREETYPE_CFLAGS=`freetype-config --cflags`
    FREETYPE_LDFLAGS=`freetype-config --libs`
    AC_SUBST(FREETYPE_CFLAGS)
    AC_SUBST(FREETYPE_LDFLAGS)
else
    AC_MSG_ERROR([FreeType library not found])
fi

       AC_DEFINE(WIN32, 1, [Compiling on cygwin/mingw])
       DL_LIBS=""
       PTHREAD_LIBS=""
       LIBS1="$LIBS"
       LIBS=""
       AC_CHECK_LIB([png], [png_create_read_struct],,AC_MSG_ERROR([*** png library not found ***]))
       PNG_LIBS="$LIBS"
       LIBS=""
       GL_LIBS="-lopengl32"
       GLU_LIBS="-lglu32"
       GLEW_LIBS="-lglew32"
       LIBS=""
      
       if test  "$GLFW" == "enable_glfw"
       then
           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(GLFW_LIBS)
           VIDEO_DRIVERS="glfw"
       fi

       if test "$MOGUL" == "enable_mogul" 
       then
           LIBS=""
           AC_CHECK_LIB([mogul], [mogulBegin],,AC_MSG_ERROR([*** mogul library not found ***]), $GL_LIBS $X11_LIBS)
	   MOGUL_LIBS="$LIBS"
           LIBS=""
           AC_SUBST(MOGUL_LIBS)
           VIDEO_DRIVERS="$VIDEO_DRIVERS mogul"
       fi
       LIBS=""
       AC_SUBST(PTHREAD_LIBS)
       AC_SUBST(DL_LIBS)
       AC_SUBST(PNG_LIBS)
       AC_SUBST(GL_LIBS)
       AC_SUBST(GLU_LIBS)
       AC_SUBST(GLEW_LIBS)
       AC_SUBST(X11_LIBS)
       
       AC_CHECK_HEADERS([windows.h])
       
       ;;
    *-*-linux-gnu*)
AC_MSG_CHECKING([for FreeType version])
FT_VERSION=`freetype-config --ftversion`

if test "$?" -eq 0 
then 
    AC_MSG_RESULT([$FT_VERSION])
    FREETYPE_CFLAGS=`freetype-config --cflags`
    FREETYPE_LDFLAGS=`freetype-config --libs`
    AC_SUBST(FREETYPE_CFLAGS)
    AC_SUBST(FREETYPE_LDFLAGS)
else
    AC_MSG_ERROR([FreeType library not found])
fi
       LIBS1="$LIBS"
       LIBS=""
       AC_DEFINE(LINUX, 1, [Compiling on GNU/Linux])
       AC_CHECK_LIB([pthread], [pthread_create],,AC_MSG_ERROR([*** pthread library not found ***]))
       PTHREAD_LIBS="$LIBS"
       LIBS=""  
       AC_CHECK_LIB([dl], [dlopen],,AC_MSG_ERROR([*** dl library not found ***]))
       DL_LIBS="$LIBS"
       LIBS=""
       AC_CHECK_LIB([png], [png_create_read_struct],,AC_MSG_ERROR([*** png library not found ***]))
       PNG_LIBS="$LIBS"
       LIBS=""
       AC_CHECK_LIB([GL], [glBegin],,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"
       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

       if test  "$GLFW" == "enable_glfw"
       then
           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(GLFW_LIBS)
           VIDEO_DRIVERS="glfw"
       fi

       if test "$MOGUL" == "enable_mogul" 
       then
           LIBS=""
           AC_CHECK_LIB([mogul], [mogulBegin],,AC_MSG_ERROR([*** mogul library not found ***]), $GL_LIBS $X11_LIBS)
	   MOGUL_LIBS="$LIBS"
           LIBS=""
           AC_SUBST(MOGUL_LIBS)
           VIDEO_DRIVERS="$VIDEO_DRIVERS mogul"
       fi
       LIBS=""
       AC_SUBST(PTHREAD_LIBS)
       AC_SUBST(DL_LIBS)
       AC_SUBST(PNG_LIBS)
       AC_SUBST(GL_LIBS)
       AC_SUBST(GLU_LIBS)
       AC_SUBST(GLEW_LIBS)
       AC_SUBST(X11_LIBS)

       ;;
esac

sed -i -e "s/^SUBDIRS =.*/SUBDIRS = $VIDEO_DRIVERS/" ./video/video_drv/Makefile.in

# 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])
if test  "$GLFW" == "enable_glfw"
then
   AC_OUTPUT(video/video_drv/glfw/Makefile)
fi

if test "$MOGUL" == "enable_mogul" 
then
   AC_OUTPUT(video/video_drv/mogul/Makefile)
fi

AC_CONFIG_FILES([Makefile
                 config/Makefile
                 config/config/Makefile
                 config/config_drv/Makefile
                 dynamicloader/Makefile
                 sceneengine/Makefile
                 sceneengine/objectfabric/Makefile
                 sceneengine/scenemanager/Makefile
                 sceneengine/sceneobjects/Makefile
                 video/Makefile
                 video/imageloaders/Makefile
                 video/imageloaders/imageloaders/Makefile
                 video/imageloaders/jpg_loader/Makefile
                 video/imageloaders/png_loader/Makefile
                 video/textrenderer/Makefile
                 video/textrenderer/text_renderer/Makefile
                 video/textrenderer/ttf_renderer/Makefile
                 video/video/Makefile
                 video/video_drv/Makefile
                 signals/Makefile
                 tests/Makefile
                 time/Makefile
                 ])
AC_OUTPUT
