dnl
dnl Celestia autoconf file
dnl Process this file with autoconf to make a configure script
dnl

AC_PREREQ(2.57)
AC_INIT([celestia], [1.6.4], [https://github.com/CelestiaProject/Celestia])
AC_CONFIG_SRCDIR(acinclude.m4)
AC_CONFIG_MACRO_DIRS([macros])

dnl The following section confirms that the user provided necessary option
dnl BEFORE anything is checked.

AC_ARG_WITH([glut],
            AC_HELP_STRING([--with-glut], [Use Glut for the UI]),
            ui_glut="yes"; ui_gtk="no"; ui_gnome="no"; ui_kde="no",
            ui_glut="no")

AC_ARG_WITH([gtk],
            AC_HELP_STRING([--with-gtk], [Use Gtk for an enhanced GUI]),
	        ui_gtk="yes"; ui_glut="no"; ui_gnome="no"; ui_kde="no",
            ui_gtk="no")

AC_ARG_WITH([gnome],
            AC_HELP_STRING([--with-gnome], [Use Gnome for an enhanced GUI]),
            ui_gnome="yes"; ui_gtk="yes"; ui_glut="no"; ui_kde="no",
            ui_gnome="no")

AC_ARG_WITH([kde],
            AC_HELP_STRING([--with-kde], [Use KDE for an enhanced GUI]),
            ui_kde="yes"; ui_glut="no"; ui_gtk="no"; ui_gnome="no",
            ui_kde="no")

dnl Following line left in: great for debugging.
dnl AC_MSG_ERROR([$ui_glut $ui_gtk $ui_gnome $ui_kde])

dnl Check that an interface was provided
if (test "$ui_glut" != "yes" -a "$ui_gtk" != "yes" -a "$ui_gnome" != "yes" -a "$ui_kde" != "yes"); then
	AC_MSG_ERROR([You must select an interface to build.
                  Possible options are:
                    --with-glut      GLUT front-end
                    --with-gtk       Enhanced GTK GUI
                    --with-gnome     GTK GUI with Gnome features
                    --with-kde       Enhanced KDE GUI]);
fi

AC_ARG_ENABLE([cairo],
              AC_HELP_STRING([--enable-cairo],
                             [use cairo for GTK splash screen]),
              enable_cairo="$enableval", enable_cairo="auto")

AC_ARG_ENABLE([theora],
              AC_HELP_STRING([--enable-theora],
                             [create Ogg/Theora video]),
              enable_theora="$enableval", enable_theora="auto")


dnl For KDE interface
AC_CONFIG_AUX_DIR(admin)

dnl "admin/acinclude.m4.in" generates "acinclude.m4", which has a broken
dnl conditional define for Qt embedded. This does not apply to Celestia
dnl anyway.
include_x11_FALSE='#'

dnl This ksh/zsh feature conflicts with `cd blah ; pwd`
unset CDPATH

dnl Check system type
AC_CANONICAL_HOST
dnl Checking host/target/build systems, for make, install etc.
AC_CANONICAL_SYSTEM
dnl Perform program name transformation
AC_ARG_PROGRAM

AM_INIT_AUTOMAKE([-Wno-portability subdir-objects])
AM_CONFIG_HEADER(config.h)

AM_PO_SUBDIRS
dnl Specifying the AM_GNU_GETTEXT_VERSION macro causes autopoint to run,
dnl replacing many files from the KDE project in the admin/ directory. We
dnl will leave it out for now.
dnl AM_GNU_GETTEXT_VERSION([0.15])
AM_GNU_GETTEXT([external])

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_CPP
AC_PROG_CXXCPP
AC_PROG_INSTALL
AC_PROG_LIBTOOL

dnl
dnl Compilation options
dnl

CFLAGS="$CFLAGS -ffast-math -fexpensive-optimizations"
CXXFLAGS="$CXXFLAGS -ffast-math -fexpensive-optimizations"

AC_MSG_CHECKING([whether to include debugging code])
AC_ARG_ENABLE([debug],
              AC_HELP_STRING([--enable-debug],
                             [Produce an executable with debugging symbols]), ,
              enable_debug="no")
if (test "$enable_debug" = "yes"); then
	CFLAGS="$CFLAGS -g -Wall";
	CXXFLAGS="$CXXFLAGS -g -Wall"
	AC_DEFINE(DEBUG, 1, [Are we debugging ?])
fi
AC_MSG_RESULT($enable_debug)

AC_MSG_CHECKING([whether to be pedantic])
AC_ARG_ENABLE([pedantic],
              AC_HELP_STRING([--enable-pedantic],
                             [Enable -pedantic when compiling]), ,
              enable_pedantic="no")
if (test "$enable_pedantic" = "yes"); then
	CFLAGS="$CFLAGS -ansi -pedantic";
	CXXFLAGS="$CXXFLAGS -pedantic"
fi
AC_MSG_RESULT($enable_pedantic)

AC_MSG_CHECKING([whether to do profiling])
AC_ARG_ENABLE([profile],
              AC_HELP_STRING([--enable-profile],
                             [Produce a profiled executable[default=no]]), ,
              enable_profile="no")
if (test "$enable_profile" = "yes"); then
	CFLAGS="$CFLAGS -pg";
	CXXFLAGS="$CXXFLAGS -pg"
else
	if (test "$enable_debug" != "yes"); then
		dnl  must be set here instead of above because -pg and
		dnl -fomit-frame-pointer are incompatible
		CFLAGS="$CFLAGS -fomit-frame-pointer";
		CXXFLAGS="$CXXFLAGS -fomit-frame-pointer"
	fi
fi
AC_MSG_RESULT($enable_profile)


dnl
dnl GL and GLUT libs
dnl

AC_ARG_WITH([gl-libs],
            AC_HELP_STRING([--with-gl-libs=DIR],
                           [Specify OpenGL library location]),
            LIBS="$LIBS -L$withval")

AC_ARG_WITH([gl-inc],
            AC_HELP_STRING([--with-gl-inc=DIR],
                           [Specify OpenGL header file location]),
            CXXFLAGS="$CXXFLAGS -I$withval")

AC_ARG_WITH([glut-libs],
            AC_HELP_STRING([--with-glut-libs=DIR],
                           [Specify GLUT library location]),
            LIBS="$LIBS -L$withval")

AC_ARG_WITH([glut-inc],
            AC_HELP_STRING([--with-glut-inc=DIR],
                           [Specify GLUT header file location]),
            CXXFLAGS="$CXXFLAGS -I$withval")


dnl
dnl SPICE lib
dnl

AC_ARG_WITH([cspice-dir],
            AC_HELP_STRING([--with-cspice-dir=DIR], [Specify SPICE directory location]),
            SPICE_CFLAGS="-I$withval/include -DUSE_SPICE";
            SPICE_LIBS="$withval/lib/cspice.a")

AC_SUBST(SPICE_CFLAGS)
AC_SUBST(SPICE_LIBS)
AM_CONDITIONAL(ENABLE_SPICE, test "$SPICE_LIBS" != "")

AC_MSG_CHECKING([whether to enable GLUT])
if (test "$ui_glut" != "no"); then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING([whether to enable GTK])
if (test "$ui_gtk" != "no"); then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING([whether to enable Gnome])
if (test "$ui_gnome" != "no"); then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING([whether to enable KDE])
if (test "$ui_kde" != "no"); then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi


AC_CHECK_COMPILERS

dnl (Copied from Pidgin's configure.ac)
dnl FreeBSD doesn't have libdl, dlopen is provided by libc
AC_CHECK_FUNC(dlopen, DL_LIBS="", [AC_CHECK_LIB(dl, dlopen, DL_LIBS="-ldl")])
AC_SUBST(DL_LIBS)

dnl Check for zlib -- libGL requires it.
AC_CHECK_LIB(z, deflate, ,
             [AC_MSG_ERROR([zlib not found.])])

dnl Check for OpenGL headers first.
AC_CHECK_HEADERS(GL/gl.h, ,
                 [AC_MSG_ERROR([No gl.h found. See INSTALL file for help.])])

dnl Check for OpenGL. Taken partly from the plib sources.
AC_CHECK_LIB(GL, glNewList, GL_LIBS="-lGL")
AC_SUBST(GL_LIBS)

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

dnl Check for GLU headers.
AC_CHECK_HEADERS(GL/glu.h, ,
                 [AC_MSG_ERROR([No glu.h found. See INSTALL file for help.])])
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

PKG_PROG_PKG_CONFIG

if (test "$ui_glut" = "yes"); then
	dnl Check for GLUT headers first.
	AC_CHECK_HEADERS(GL/glut.h, ,
	                 [AC_MSG_ERROR([No glut.h found. See INSTALL file for help.])])

	dnl Check for GLUT.
	AC_CHECK_LIB(glut, glutKeyboardUpFunc, ,
	             [AC_MSG_ERROR([GLUT library version >= 3.7 not found])])
fi
AM_CONDITIONAL(ENABLE_GLUT, test "$ui_glut" = "yes")

dnl Default GConf to FALSE
dnl (this is a silly trick to make configure behave)
AM_CONDITIONAL(GCONF_SCHEMAS_INSTALL, test "x" = "y")

if (test "$ui_gtk" != "no"); then
	dnl GNOME is an extension to the GTK options
	if (test "$ui_gnome" = "yes"); then
		PKG_CHECK_MODULES(GTK, libgnomeui-2.0 gtk+-2.0 >= 2.6 gtkglext-1.0)
		AM_GCONF_SOURCE_2
		AC_DEFINE(GNOME, 1, [Use Gnome Flag])
	else
		dnl Otherwise, vanilla GTK
		PKG_CHECK_MODULES(GTK, gtk+-2.0 >= 2.6 gtkglext-1.0)
	fi

	dnl Check for cairo. Default to trying yes. Want this structure to not
	dnl fail when not found if not explicitly enabled.
	if (test "x$enable_cairo" != "xno"); then
		PKG_CHECK_MODULES(CAIRO, cairo, enable_cairo="yes",
		                  if (test "x$enable_cairo" = "xauto"); then
		                      enable_cairo="no"
		                  else
		                      AC_ERROR([Cairo not found (explicitly enabled)!])
		                  fi)
	fi

        check_theora="yes"

	if (test "x$enable_cairo" = "xyes"); then
		AC_DEFINE(CAIRO, 1, [Use Cairo for Splash])
	fi
fi

dnl If all the GTK tests succeeded, safe to enable GTK
AM_CONDITIONAL(ENABLE_GTK, test "$ui_gtk" = "yes")
AM_CONDITIONAL(ENABLE_GNOME, test "$ui_gnome" = "yes")


dnl
dnl KDE
dnl

dnl Make certain ARTS is not conditionally defined, like GConf for Gnome
AM_CONDITIONAL(include_ARTS, test "x" = "y")

if (test "$ui_kde" != "no"); then
	dnl KDE_SET_PREFIX
	AC_ENABLE_SHARED(yes)
	AC_ENABLE_STATIC(no)

	KDE_PROG_LIBTOOL
	AM_KDE_WITH_NLS

	dnl KDE_USE_QT(3)
	AC_PATH_KDE

        check_theora="yes"
fi
AM_CONDITIONAL(ENABLE_KDE, test "$ui_kde" = "yes")

if (test "$check_theora" = "yes"); then
	dnl Check for theora. Default to trying yes. Want this structure to not
	dnl fail when not found if not explicitly enabled.
	if (test "x$enable_theora" != "xno"); then
		PKG_CHECK_MODULES(THEORA, theora, enable_theora="yes",
		                  if (test "x$enable_theora" = "xauto"); then
		                      enable_theora="no"
		                  else
		                      AC_ERROR([Theora not found (explicitly enabled)!])
		                  fi)
	fi

	if (test "x$enable_theora" = "xyes"); then
		AC_DEFINE(THEORA, 1, [Use OGG Theora for video])
	fi
fi
AM_CONDITIONAL(ENABLE_THEORA, test "$enable_theora" = "yes")

dnl Check for JPEG library.
AC_CHECK_LIB(jpeg, jpeg_start_decompress, ,
             [AC_MSG_ERROR([jpeg library not found])])


dnl Check for PNG library.
AC_CHECK_LIB(png, png_create_info_struct,,
             [AC_MSG_ERROR([png library not found])])


dnl Checks for header files.

AC_HEADER_STDC
AC_CHECK_HEADERS(byteswap.h)
AC_C_BIGENDIAN

dnl Establish the main data directory (defined in Makefiles)
PKGDATADIR='${datadir}'/$PACKAGE
AC_SUBST(PKGDATADIR)

enable_hipparcos="no"
AC_ARG_ENABLE([hipparcos-dir],
              AC_HELP_STRING([--enable-hipparcos-dir=DIR],
                             [Specify location of HIPPARCOS/TYCHO stardata]),
              HIPDIR="$enableval",HIPDIR="$PKGDATADIR")
AC_SUBST(HIPDIR)
AC_MSG_CHECKING([whether we can create a new star database])
if (test -f "$HIPDIR/hip_main.dat"); then
	enable_hipparcos="yes"
fi
AC_MSG_RESULT($enable_hipparcos)
AM_CONDITIONAL(ENABLE_HIPPARCOS, test "x$enable_hipparcos" = "xyes")


AC_ARG_WITH([lua],
            AC_HELP_STRING([--with-lua],
                           [Use Lua for Celestia Extension Language support]),
            enable_lua="$withval",
            enable_lua="auto")

if (test "$enable_lua" != "no"); then
	LUA_VER=0
	PKG_CHECK_MODULES(LUA, lua5.3 >= 5.3.0, LUA_VER=0x050300, [
	PKG_CHECK_MODULES(LUA, lua >= 5.3.0, LUA_VER=0x050300, [
	PKG_CHECK_MODULES(LUA, lua5.2 >= 5.2.0, LUA_VER=0x050200, [
	PKG_CHECK_MODULES(LUA, lua >= 5.2.0, LUA_VER=0x050200, [
	PKG_CHECK_MODULES(LUA, lua5.1 >= 5.1.0, LUA_VER=0x050100, [
	PKG_CHECK_MODULES(LUA, lua >= 5.1.0, LUA_VER=0x050100, AC_MSG_RESULT([no])) ]
	) ] ) ] ) ] ) ] )
	if (test "$LUA_VER" = "0"); then
		if (test "x$enable_lua" != "xauto"); then
			AC_ERROR([Lua not found (explicitly enabled)!])
		else
			enable_lua="no"
		fi
	else
		enable_lua="yes"
	fi
fi

if (test "$enable_lua" = "yes"); then
	LUA_CFLAGS="$LUA_CFLAGS $LUALIB_CFLAGS -DLUA_VER=$LUA_VER -DCELX"
fi

AM_CONDITIONAL(ENABLE_CELX, test "$enable_lua" != "no")


AC_CONFIG_FILES([ Makefile ])
AC_CONFIG_FILES([ src/Makefile ])
AC_CONFIG_FILES([ src/celutil/Makefile ])
AC_CONFIG_FILES([ src/celmath/Makefile ])
AC_CONFIG_FILES([ src/cel3ds/Makefile ])
AC_CONFIG_FILES([ src/celtxf/Makefile ])
AC_CONFIG_FILES([ src/celengine/Makefile ])
AC_CONFIG_FILES([ src/celestia/Makefile ])
AC_CONFIG_FILES([ data/Makefile ])
AC_CONFIG_FILES([ extras/Makefile ])
AC_CONFIG_FILES([ extras-standard/Makefile ])
AC_CONFIG_FILES([ extras-standard/cassini/Makefile ])
AC_CONFIG_FILES([ extras-standard/cassini/models/Makefile ])
AC_CONFIG_FILES([ extras-standard/cassini/data/Makefile ])
AC_CONFIG_FILES([ extras-standard/mir/Makefile ])
AC_CONFIG_FILES([ extras-standard/mir/models/Makefile ])
AC_CONFIG_FILES([ extras-standard/galileo/Makefile ])
AC_CONFIG_FILES([ extras-standard/galileo/models/Makefile ])
AC_CONFIG_FILES([ extras-standard/galileo/data/Makefile ])
AC_CONFIG_FILES([ extras-standard/hubble/Makefile ])
AC_CONFIG_FILES([ extras-standard/hubble/models/Makefile ])
AC_CONFIG_FILES([ extras-standard/iss/Makefile ])
AC_CONFIG_FILES([ extras-standard/iss/models/Makefile ])
AC_CONFIG_FILES([ extras-standard/iss/textures/Makefile ])
AC_CONFIG_FILES([ extras-standard/iss/textures/medres/Makefile ])
AC_CONFIG_FILES([ textures/Makefile ])
AC_CONFIG_FILES([ textures/lores/Makefile ])
AC_CONFIG_FILES([ textures/medres/Makefile ])
AC_CONFIG_FILES([ textures/hires/Makefile ])
AC_CONFIG_FILES([ models/Makefile ])
AC_CONFIG_FILES([ shaders/Makefile ])
AC_CONFIG_FILES([ fonts/Makefile ])
AC_CONFIG_FILES([ src/celestia/res/Makefile ])
AC_CONFIG_FILES([ src/celestia/kde/Makefile ])
AC_CONFIG_FILES([ src/celestia/kde/data/Makefile ])
AC_CONFIG_FILES([ src/celestia/kde/doc/Makefile ])
AC_CONFIG_FILES([ src/celestia/kde/doc/celestia/Makefile ])
AC_CONFIG_FILES([ src/celestia/gtk/Makefile ])
AC_CONFIG_FILES([ src/celestia/gtk/data/Makefile ])
AC_CONFIG_FILES([ po/Makefile.in ])
AC_CONFIG_FILES([ po2/Makefile.in ])
AC_CONFIG_FILES([ locale/Makefile ])
AC_CONFIG_FILES([ scripts/Makefile ])
AC_OUTPUT()

AC_MSG_RESULT()
AC_MSG_RESULT()
AC_MSG_RESULT(***************************************************************)
AC_MSG_RESULT(** Celestia configuration complete. Now do a 'make' followed **)
AC_MSG_RESULT(** by 'make install'                                         **)
AC_MSG_RESULT(***************************************************************)
AC_MSG_RESULT()

if (test "$ui_glut" = "yes"); then
	AC_MSG_RESULT([Front-End: GLUT]);
fi

if (test "$ui_gtk" = "yes" -a "$ui_gnome" = "no"); then
	AC_MSG_RESULT([Front-End: GTK]);
fi

if (test "$ui_gnome" = "yes"); then
	AC_MSG_RESULT([Front-End: Gnome]);
fi

if (test "$ui_kde" = "yes"); then
	AC_MSG_RESULT([Front-End: KDE]);
fi

if (test "$ui_gtk" = "yes" -o "$ui_gnome" = "yes"); then
	AC_MSG_RESULT([Use Cairo: $enable_cairo]);
fi

AC_MSG_RESULT([Use Lua: $enable_lua]);

if (test "$LUA_VER" = "0x050000"); then
	AC_MSG_RESULT([    Warning: Lua 5.1 is not available on your system, Lua 5.0 will be used
    instead but it may not be fully compatible with existing CELX scripts.
    It is recommanded that you install Lua 5.0 and rerun configure.]);
fi

AC_MSG_RESULT([Use Theora: $enable_theora]);

AC_MSG_RESULT()
