dnl configuure.in file for "partiview"
dnl == Process this file with autoconf to produce a configure script ==
dnl ---------------------------------------------------------------------------------

AC_MSG_CHECKING([partiview])

AC_INIT(partibrains.c)

AC_CONFIG_HEADER(config.h)

dnl -- checks for programs
dnl -- checks for libraries
dnl -- checks for header files
dnl -- checks for typedefs
dnl -- checks for structures
dnl -- checks for compiler characteristics
dnl -- checks for library functions
dnl -- checks for system services


AC_HEADER_STDC
AC_CHECK_FUNCS(strchr memcpy)

AC_PROG_CPP
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_RANLIB

AC_ARG_PROGRAM





AC_ARG_ENABLE(debug, [  --enable-debug          compile for debugging], ok=$enableval, ok=no)
if test "$ok" = "yes"; then
        CFLAGS="-g"
        LDFLAGS="-g"
        # should we define DEBUG too?
fi


AC_ARG_WITH(mesa, [  --with-mesa        force the use of Mesa instead of other graphics libs], with_mesa=$withval, with_mesa=maybe)

AC_ARG_WITH(cave, [  --enable-cave      compile for CAVE], with_cave=$enableval, with_cave=no)

dnl ---------------------------------------------------------------------
dnl             Check for basic X windows stuff
AC_PATH_X
AC_PATH_XTRA
CFLAGS="$CFLAGS $X_CFLAGS"
XLIBS="$X_LIBS $X_PRE_LIBS -lXext -lX11 $X_EXTRA_LIBS"
AC_SUBST(XLIBS)


dnl ---------------------------------------------------------------------
dnl 3D Graphics Libraries (see also vis5d's configure.in)

GLLIBS=""

# First, look for OpenGL or Mesa:
if test "$with_mesa" != "yes"; then
        AC_CHECK_LIB(GL, glBegin,
            AC_CHECK_LIB(GLU, gluProject, GLLIBS="-lGLU -lGL",
                         :, -lGL $XLIBS), :, $XLIBS)
fi
if test -z "$GLLIBS" -a "$with_mesa" != "no"; then
        AC_CHECK_LIB(MesaGL, glBegin,
            AC_CHECK_LIB(MesaGLU, gluProject, GLLIBS="-lMesaGLU -lMesaGL",
                         :, -lMesaGL $XLIBS), :, $XLIBS)
fi
if test "$with_mesa" = "yes"; then
        AC_CHECK_LIB(GL, glBegin,
            AC_CHECK_LIB(GLU, gluProject, GLLIBS="-lGLU -lGL",
                         :, -lGL $XLIBS), :, $XLIBS)
fi
if test -n "$GLLIBS"; then
        AC_DEFINE(HAVE_OPENGL)
else
        echo "couldn't find OpenGL libraries!"
        if test "$with_mesa" = "yes"; then
                AC_MSG_ERROR([couldn't find Mesa library])
        fi
fi

# Next, look for SGI IrisGL:
if test -z "$GLLIBS"; then
        AC_CHECK_LIB(gl_s, bgnqstrip, ok=yes,ok=no, $XLIBS)
        if test "$ok" = "yes"; then
                AC_CHECK_LIB(fm, fminit, GLLIBS="-lfm -lgl_s",:,-lgl_s $XLIBS)
                if test -z "$GLLIBS"; then
                        AC_CHECK_LIB(fm_s, fminit, 
                                GLLIBS="-lfm_s -lgl_s",:,-lgl_s $XLIBS)
                fi
        fi
        if test -n "$GLLIBS"; then
                AC_DEFINE(HAVE_SGI_GL)
                OPT_UTILS="$OPT_UTILS listfonts"
        fi
fi
# Finally, look for PEX (crufty old 3d extensions for X):
# (do these even work in Vis5d anymore?)
if test -z "$GLLIBS"; then
        # first, check for possible include & lib directory locations:
        if test -d /usr/lib/PEX5/utilities; then
                CPPFLAGS="$CPPFLAGS -I/usr/lib/PEX5/utilities"
        elif test -d /opt/graphics/PEX5/include/X11R6; then
                CPPFLAGS="$CPPFLAGS -I/opt/graphics/PEX5/include/X11R6"
        fi
        if test -d /opt/graphics/PEX5/lib; then
                LDFLAGS="$LDFLAGS -L/opt/graphics/PEX5/lib"
        fi

        # now, check for the actual library:
        save_LDFLAGS="$LDFLAGS"; LDFLAGS="$LDFLAGS $X_LIBS"
        AC_CHECK_LIB(PEX5, PEXInitialize, GLLIBS="$X_LIBS -lPEX5", :, $XLIBS)
        LDFLAGS="$save_LDFLAGS"

        if test -n "$GLLIBS"; then
                # finally, look for the pexut.a library
                AC_MSG_CHECKING([for pexut.a library])
                if test -z "$PEXUT_LIB"; then
                        if test -r /usr/lib/PEX5/utilities/pexut.a; then
                                PEXUT_LIB="/usr/lib/PEX5/utilities/pexut.a"
                        elif test -r /var/opt/PEX5/pexut.a; then
                                PEXUT_LIB="/var/opt/PEX5/pexut.a"
                        fi
                fi
                AC_MSG_RESULT(${PEXUT_LIB-no})
                if test -z "$PEXUT_LIB"; then
                        AC_MSG_WARN([pexut.a is required when using PEX])
                        GLLIBS=""
                elif test -r "$PEXUT_LIB"; then
                        GLLIBS="$PEXUT_LIB $GLLIBS"
                else
                        # this could happen if the user specified a bogus
                        # PEXUT_LIB environment variable:
                        AC_MSG_ERROR($PEXUT_LIB is not readable)
                fi
        fi

        if test -n "$GLLIBS"; then
                AC_DEFINE(HAVE_PEX)
        fi
fi

if test -z "$GLLIBS"; then
        echo "****************************************************"
        echo "You need to install a 3D graphics library, preferably"
        echo "the free OpenGL replacement, Mesa.  You can download"
        echo "Mesa from the Mesa home page:"
        echo "              http://www.mesa3d.org/"
        echo "and install it by running:"
        echo "       ./configure && make && su -c 'make install'"
        echo "in the Mesa directory."

        if test -r /sbin/ldconfig; then
                echo "You may also need to run /sbin/ldconfig as root"
                echo "to update the system after installing Mesa."
                if test -r /etc/ld.so.conf; then
                if test -z "`grep /usr/local/lib /etc/ld.so.conf`"; then
                echo "(First, add '/usr/local/lib' to /etc/ld.so.conf if"
                echo "you installed Mesa under /usr/local, the default.)"
                fi
                fi
        fi
        echo "****************************************************"
dnl        AC_MSG_ERROR([couldn't find 3D graphics library])
fi
AC_SUBST(GLLIBS)

dnl -- Check for any particular functions in the GL libs:
save_LIBS="$LIBS"
LIBS="$GLLIBS $XLIBS $LIBS"
AC_CHECK_FUNCS(XMesaGetBackBuffer)
LIBS="$save_LIBS"
  

dnl AC_CHECK_FUNC(vprintf, AC_DEFINE(HAVE_VPRINTF))
dnl if test "$ac_cv_func_vprintf" != yes; then


dnl AC_CHECK_FUNC(_doprnt, AC_DEFINE(HAVE_DOPRNT))
dnl fi
dnl ---------------------------------------------------------------------


dnl Checks for libraries.

dnl Checks for header files.


AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h limits.h malloc.h sgtty.h strings.h sys/file.h sys/ioctl.h sys/time.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_TYPE_GETGROUPS
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_FUNC_MMAP
AC_TYPE_SIGNAL
AC_FUNC_VFORK
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(ftime gethostname gettimeofday getwd mkdir select strdup strtod strtol strtoul)

AC_C_BIGENDIAN


AC_OUTPUT(Makefile)

