dnl Process this file with autoconf to produce a configure script.
AC_INIT(common/crc.h)
AC_CANONICAL_SYSTEM
AC_LANG_C

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_MAKE_SET

dnl checks for compiler characteristics
AC_C_BIGENDIAN

AC_ARG_ENABLE(release,
[  --enable-release        compile release (with optimization)],
       RELEASE=$enable_release
)
AC_SUBST(RELEASE)

dnl Checks for OGL support
AC_ARG_WITH(opengl,
[  --with-opengl=DIR       use OpenGL with libraries found in DIR],
[HAS_OGL_LIB=yes; OGL_LIB_PATH=$enable_OpenGLLIB;
   OGL_LDFLAGS=-L$enable_OpenGLLIB])
AC_SUBST(OGL_LDFLAGS)

AC_ARG_WITH(opengl-includes,
[  --with-opengl-includes=DIR
                          use OpenGL with header files found in DIR],
HAS_OGL_INC=yes; OGL_INC_PATH=$enable_OpenGLINC; OGL_CFLAGS=-Wall -I$enable_OpenGLINC)
AC_SUBST(OGL_CFLAGS)

dnl Check for X first...
AC_PATH_XTRA
AC_SUBST(x_libraries)
AC_SUBST(x_includes)
if test "x$no_x" = x; then
HAS_X11=yes
   if test "x$x_libraries" != x; then
      X_LIB_PATH_L=-L${x_libraries};
   else
      X_LIB_PATH_L=
   fi
   AC_CHECK_LIB(Xext, XShmQueryExtension, [X11_SHM_LIB=-lXext],
                [X11_SHM_LIB=], ${X_LIB_PATH_L})
   AC_SUBST(X11_SHM_LIB)
fi
AC_SUBST(HAS_X11)


if test "x$HAS_OGL_LIB" = x; then
AC_CHECK_LIB(GL, glColor4f, [HAS_OGL_LIB=yes],
   AC_CHECK_LIB(MesaGL, glColor4f, [HAS_OGL_LIB=yes],
   if test "x$HAS_OGL_LIB" != xyes; then
      POSS_OGL_LDFLAGS=`echo $enable_OpenGLINC| sed 's/include/lib/i'`;
      AC_MSG_CHECKING(for $POSS_OGL_LDFLAGS/libGL.so)
      if test -f "$POSS_OGL_LDFLAGS"/libGL.so; then
	 AC_MSG_RESULT(yes)
	 HAS_OGL_LIB=yes
	 OGL_LDFLAGS=-L$POSS_OGL_LDFLAGS
	 OGL_LIB_PATH=$POSS_OGL_LDFLAGS
      else
         AC_MSG_RESULT(no)
         if test "x$x_libraries" != x; then
	    AC_MSG_CHECKING(for $x_libraries/libGL.so)
	    if test -f "$x_libraries"/libGL.so; then
	       AC_MSG_RESULT(yes)
	       HAS_OGL_LIB=yes
	       OGL_LDFLAGS=-L$x_libraries
	       OGL_LIB_PATH=$x_libraries
	    else
	       AC_MSG_RESULT(no)
	    fi
	 fi
      fi
   fi
   )
)
fi

if test "x$HAS_OGL_INC" = x; then
   AC_CHECK_HEADER(GL/gl.h, [HAS_OGL_INC=yes],
   if test "x$HAS_OGL_LIB" != x; then
      if test "x$x_includes" != x; then
         AC_MSG_CHECKING(for $x_includes/GL/gl.h)
	 if test -f "$x_includes"/GL/gl.h; then
	    HAS_OGL_INC=yes
	    OGL_CFLAGS=-Wall -I$x_includes
	    OGL_INC_PATH=$x_includes
	    AC_MSG_RESULT(yes)
	 else
	    POSS_OGL_CFLAGS=`echo $enable_OpenGLLIB| sed 's/lib/include/i'`;
            AC_MSG_CHECKING(for $POSS_OGL_CFLAGS/GL/gl.h)
	    if test -f "$POSS_OGL_CFLAGS/GL/gl.h"; then
	       HAS_OGL_INC=yes
	       OGL_CFLAGS=-Wall -I$POSS_OGL_CFLAGS
	       OGL_INC_PATH=$POSS_OGL_CFLAGS
	       AC_MSG_RESULT(yes)
            else
	       AC_MSG_RESULT(no)
	    fi
         fi
      fi
   fi
   )
fi

AC_MSG_CHECKING(for OpenGL support)
if test "x$HAS_OGL_INC" != x; then
    if test "x$HAS_OGL_LIB" != x; then
	HAS_OGL=yes
	if test "x$OGL_CFLAGS" != x; then
	    AC_MSG_RESULT([yes, libraries $OGL_LDFLAGS, headers $OGL_CFLAGS])
	else
	    AC_MSG_RESULT(yes)
	fi
	if test "x$OGL_LDFLAGS" = x-L; then
	    AC_MSG_ERROR(OpenGL LDFLAGS is -L with no directory)
	fi
	if test "x$OGL_INC_PATH" != x; then
	    OGL_INC_PATH_TO_GL="$OGL_INC_PATH/GL/gl.h"
	else
	    OGL_INC_PATH_TO_GL="GL/gl.h"
	fi
	AC_MSG_CHECKING(for GL_COLOR_INDEX8_EXT)
	AC_TRY_COMPILE([#include "$OGL_INC_PATH_TO_GL"],
			[ int x = (int) GL_COLOR_INDEX8_EXT;],
			HAVE_GL_COLOR_INDEX8_EXT=yes,
			HAVE_GL_COLOR_INDEX8_EXT=no)
	if test "x$HAVE_GL_COLOR_INDEX8_EXT" != xno; then
	    AC_MSG_RESULT(yes)
	    AC_DEFINE(HAVE_GL_COLOR_INDEX8_EXT, 1)
	else
	    AC_MSG_RESULT(no)
	    AC_MSG_ERROR(Error, HAVE_GL_COLOR_INDEX8_EXT not found.)
	fi

	AC_MSG_CHECKING(for GLAPIENTRY)
	AC_TRY_COMPILE([#include "$OGL_INC_PATH_TO_GL"],
			[ int x = GLAPIENTRY printf("");],
			HAS_GLAPIENTRY=yes,
			HAS_GLAPIENTRY=no)
	if test "x$HAS_GLAPIENTRY" != xno; then
	    AC_MSG_RESULT(yes)
	else
	    AC_MSG_RESULT(no)

	    AC_MSG_CHECKING(for APIENTRY)
	    AC_TRY_COMPILE([#include "$OGL_INC_PATH_TO_GL"],
			    [ int x = APIENTRY printf("");],
			    HAS_APIENTRY=yes,
			    HAS_APIENTRY=no)
	    if test "x$HAS_APIENTRY" != xno; then
		AC_MSG_RESULT(yes)
		AC_DEFINE(GLAPIENTRY_IS_APIENTRY, 1)
	    else
		AC_MSG_RESULT(no)
		AC_MSG_ERROR(Error, neither GLAPIENTRY nor APIENTRY are defined)
	    fi
	fi

    else
	AC_MSG_RESULT(no)
    fi
else
    AC_MSG_RESULT(no)
fi
AC_SUBST(HAS_OGL)
AC_SUBST(HAS_APIENTRY)
AC_SUBST(HAS_GLAPIENTRY)

dnl Check for other libraries
AC_CHECK_LIB(vga, vga_getmousetype, [HAS_VGA=yes], [HAS_VGA=])
AC_SUBST(HAS_VGA)
AC_CHECK_LIB(xil,xil_import, [HAS_XIL=yes], [HAS_XIL=])
AC_SUBST(HAS_XIL)

dnl Checks for GLIDE support
dnl this rule lifted almost wholesale from Mesa
GLIDE_CFLAGS=""
GLIDE_LDFLAGS=""
AC_ARG_WITH(glide,
[  --with-glide=DIR        use the GLIDE 2.x SDK found in DIR],
HAS_GLIDE=$withval, HAS_GLIDE=auto
)
if test "x$HAS_GLIDE" != xno; then
   if test "x$HAS_GLIDE" != xauto; then
      GLIDE_CFLAGS="-I$withval/include"
      GLIDE_LDFLAGS="-L$withval/lib"
   fi
   save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $GLIDE_CFLAGS"
   AC_CHECK_HEADER(glide.h, HAS_GLIDE=yes, HAS_GLIDE=no)
   if test "x$HAS_GLIDE" != xyes; then
      CPPFLAGS="$save_CPPFLAGS -I/usr/include/glide -I/usr/local/include/glide"
      AC_CHECK_HEADER(glide/glide.h,
	GLIDE_CFLAGS="-I/usr/include/glide -I/usr/local/include/glide"
	HAS_GLIDE=yes)
   fi
   CPPFLAGS="$save_CPPFLAGS"
   if test "x$HAS_GLIDE" = xyes; then
      AC_CHECK_LIB(glide2x, main, GLIDE_LDFLAGS="$GLIDE_LDFLAGS -lglide2x"
        HAS_GLIDE=yes, HAS_GLIDE=no, -lm)
      LIBS="$save_LIBS"
   fi
   if test "x$HAS_GLIDE" != xyes; then
     GLIDE_CFLAGS="" GLIDE_LIBS=""
   fi
fi
AC_SUBST(HAS_GLIDE)
AC_SUBST(GLIDE_CFLAGS)
AC_SUBST(GLIDE_LDFLAGS)

TDFXGL_LDFLAGS=""
AC_ARG_WITH(3dfxgl,
[  --with-3dfxgl=DIR       use 3Dfx MiniGL library found in DIR],
HAS_TDFXGL=$withval, HAS_TDFXGL=auto
)
if test "x$HAS_TDFXGL" != xno; then
   if test "x$HAS_TDFXGL" != xauto; then
      TDFXGL_LDFLAGS="-L$withval"
   fi
   dnl FIX: is it safe to assume test has -o ?
   if test "x$HAS_GLIDE" != x; then
      if test "x$HAS_OGL" != x; then
         if test "x$HAS_VGA" != x; then
	    save_LIBS="$LIBS"
	    AC_CHECK_LIB(3dfxgl, glColor4f, HAS_TDFXGL=yes, HAS_TDFXGL=, -lglide2x)
	 fi
      fi
   fi
fi
if test "x$HAS_TDFXGL" = xyes; then
   TDFXGL_LDFLAGS="$TDFXGL_LDFLAGS -l3dfxgl"
else
   TDFXGL_LDFLAGS="$TDFXGL_LDFLAGS -lGL"
   HAS_TDFXGL="no"
fi
AC_SUBST(HAS_TDFXGL)
AC_SUBST(TDFXGL_LDFLAGS)

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_CHECK_FUNC(stricmp, 
	[STRICMP_DEF=],
	AC_CHECK_FUNC(strcasecmp, [STRICMP_DEF=-Dstricmp=strcasecmp]))
AC_SUBST(STRICMP_DEF)


##########################################################
#
# Release (optimized) compiler settings (GCC only)
#
if test "x$RELEASE" = xyes; then
   if test "x$GCC" = xyes; then
      AC_MSG_CHECKING(for special release compiler settings)
      BASE_RELEASE_CFLAGS="-Wall -O6 -ffast-math -funroll-loops -fomit-frame-pointer -fexpensive-optimizations"
dnl FIX:  Someone PLEASE be so kind as to make this -mcpu stuff sane?
      case "${target}" in
         changequote(,)dnl
         i[34567]86-*-linux-gnu*)
         changequote([,])dnl
            case "${target_cpu}" in
	       changequote(,)dnl
               i[456]86) CPU_SUBMODEL="-mcpu=${target_cpu}" ;;
	       changequote([,])dnl
            esac
            MORE_RELEASE_CFLAGS="${GCC_EMULATION} -malign-loops=2 -malign-jumps=2 -malign-functions=2" ;;
         *) MORE_RELEASE_CFLAGS="" ;;
      esac
      if test "x$MORE_RELEASE_CFLAGS" = x; then
         RELEASE_CFLAGS=-O
         AC_MSG_RESULT([no])
         RELEASE_CFLAGS=$BASE_RELEASE_CFLAGS
      else
         AC_MSG_RESULT(yes)
         RELEASE_CFLAGS="$BASE_RELEASE_CFLAGS $MORE_RELEASE_CFALGS"
      fi
   else
      RELEASE_CFLAGS=-O2
   fi
fi
AC_SUBST(RELEASE_CFLAGS)

##########################################################
#
# is it an intel box?
#
AC_MSG_CHECKING(for an Intel machine)
case "${target}" in
changequote(,)dnl
   i[34567]86-*-linux-gnu*)
changequote([,])dnl
      INTEL_ARCH=yes
      AC_MSG_RESULT(yes)
          ;;
   *) AC_MSG_RESULT(no)
esac
AC_SUBST(INTEL_ARCH)

##########################################################
#
# Sound available?
#
SND_LIBS=""
AC_MSG_CHECKING(for sound support)
case "${target}" in
changequote(,)dnl
   i[34567]86-*-linux-gnu*)
      SND_STYLE="Linux" ;;
   *-solaris*) 
      SND_STYLE="Solaris" ;;
   *-dec-osf[34]*)                                                             
      SND_STYLE="MME"                                            
      SND_LIBS="-lmme"                                                         
       ;;          
   *)
      SND_STYLE="Null" ;;
# XXX - need to add dos, Windows
changequote([,])dnl
esac
if test "x$SND_STYLE" = xNull; then
AC_MSG_RESULT(no)
else
AC_MSG_RESULT(yes ($SND_STYLE style))
fi
AC_SUBST(SND_STYLE)
AC_SUBST(SND_LIBS)

##########################################################
#
# System driver?
#
AC_MSG_CHECKING(for system driver)
case "${target}" in
changequote(,)dnl
   i[34567]86-*-linux-gnu*)
changequote([,])dnl
      SYS_SRC="sys_linux.c"
      QW_CL_SYS_SRC="sys_linux.c"
      QW_SRV_SYS_SRC="sys_unix.c"
      ;;
   *)
      SYS_SRC="sys_unix.c" 
      QW_CL_SYS_SRC="sys_linux.c"
      QW_SRV_SYS_SRC="sys_unix.c"
      DEFAULT_SYS=yes ;;
# XXX - windows - conproc.c sys_win.c
# XXX - dos  - conproc.c(?) sys_dos.c
# XXX - need to add dos, Windows
esac
if test "x$DEFAULT_SYS" = xyes; then
AC_MSG_RESULT([no, using default])
else
AC_MSG_RESULT(yes ($SYS_SRC $QW_CL_SYS_SRC $QW_SRV_SYS_SRC))
fi
AC_SUBST(SYS_SRC)
AC_SUBST(QW_CL_SYS_SRC)
AC_SUBST(QW_SRV_SYS_SRC)

##########################################################
#
# X11 video driver?
#
# If Solaris, use vid_sunx.c,
# otherwise (if X available) use vid_x.c
AC_MSG_CHECKING(for X11 video driver)
case "${target}" in
   *-solaris*) 
      X11_VID_SRC="vid_sunx.c" ;;
   *)
      if test "x$HAS_X11"; then
         X11_VID_SRC="vid_x.c";
      else
         X11_VID_SRC=
      fi
     ;;
esac
if test "x$X11_VID_SRC" = x; then
AC_MSG_RESULT(no)
else
AC_MSG_RESULT(yes ($X11_VID_SRC))
fi
AC_SUBST(X11_VID_SRC)

dnl Check for system services
AC_EXEEXT
AC_OBJEXT
AC_PATH_XTRA

##########################################################
#
# Output files
#
AC_CONFIG_HEADER(common/config/config.h)
AC_OUTPUT(qw_client/Makefile qw_server/Makefile uquake/Makefile Makefile)