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

dnl Version of this release
AC_DEFINE(VERSION,"0.1.0-pre")

AC_CANONICAL_SYSTEM
AC_LANG_C

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_MAKE_SET

dnl Checks for header files.
AC_CHECK_HEADERS(unistd.h windows.h sys/ioctl.h sys/mman.h sys/asoundlib.h \
	sys/soundcard.h linux/soundcard.h machine/soundcard.h sys/audioio.h \
	dsound.h mmsystem.h mme/mmsystem.h mme/mme_public.h)

dnl checks for compiler characteristics
AC_PROG_GCC_TRADITIONAL
AC_C_BIGENDIAN

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

dnl FIXME: Remove next line when ALSA support is added
AC_ARG_ENABLE(alsa,
[  --enable-alsa           enable alsa support],
)	

dnl Checks for stricmp/strcasecmp
AC_CHECK_FUNC(stricmp,,
  AC_CHECK_FUNC(strcasecmp, AC_DEFINE(stricmp,strcasecmp)))

dnl Checks for working -lm
AC_CHECK_LIB(m, pow,, AC_MSG_ERROR([math library (-lm) appears broken]))

dnl Checks for X11 support
AC_PATH_XTRA
if test "x$no_x" = x; then
   HAS_X11=yes
   AC_CHECK_LIB(Xext, XShmQueryExtension, [X11_SHM_LIB=-lXext],
     [X11_SHM_LIB=""], [ $X_LIBS $X_EXTRA_LIBS ])
fi

dnl Check for DGA support
AC_ARG_ENABLE(DGA,
[  --enable-DGA            use XFree86 DGA extension, if available],
HAS_DGA=$enableval, HAS_DGA=auto)
if test "x$HAS_DGA" != xno; then
   save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$X_CFLAGS $CPPFLAGS"
   AC_CHECK_HEADER(X11/extensions/xf86dga.h, HAS_DGA=yes, HAS_DGA=no)
   CPPFLAGS="$save_CPPFLAGS"
fi


dnl Checks for GGI support
GGI_CFLAGS=""
GGI_LIBS=""
AC_ARG_WITH(ggi,
[  --with-ggi=DIR         use LibGGI found in DIR],
HAS_GGI=$withval, HAS_GGI=auto)
if test "x$HAS_GGI" != xno; then
   if test "x$HAS_GGI" != xauto; then
      GGI_CFLAGS="-I$withval/include"
      GGI_LIBS="-L$withval/lib"
   fi
   save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $GGI_CFLAGS"
   AC_CHECK_HEADER(ggi/ggi.h, HAS_GGI=yes, HAS_GGI=no)
   CPPFLAGS="$save_CPPFLAGS"
fi

dnl Checks for SVGALib support
SVGA_CFLAGS=""
SVGA_LIBS=""
AC_ARG_WITH(svga,
[  --with-svga=DIR         use SVGALib found in DIR],
HAS_SVGA=$withval, HAS_SVGA=auto)
if test "x$HAS_SVGA" != xno; then
   if test "x$HAS_SVGA" != xauto; then
      SVGA_CFLAGS="-I$withval/include"
      SVGA_LIBS="-L$withval/lib"
   dnl The default system location is /usr/include or /usr/local/include
   dnl and we (obviously) don't need to set CFLAGS for that
   fi
   save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $SVGA_CFLAGS"
   AC_CHECK_HEADER(vga.h, HAS_SVGA=yes, HAS_SVGA=no)
   if test "x$HAS_SVGA" != xyes; then
        HAS_TDFXGL=no
   fi
   CPPFLAGS="$save_CPPFLAGS"
fi

dnl Checks for SDL support
SDL_CFLAGS=""
SDL_LIBS=""
AC_ARG_WITH(sdl,
[  --with-sdl=DIR       use SDL found in DIR],
HAS_SDL=$withval, HAS_SDL=auto)
if test "x$HAS_SDL" != xno; then
   if test "x$HAS_SDL" != xauto; then
      SDL_CFLAGS="-I$withval/include"
      SDL_LIB="-L$withbal/lib"
   fi
   save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $SDL_CFLAGS"
   AC_CHECK_HEADER(SDL/SDL.h, HAS_SDL=yes, HAS_SDL=no)
   CPPFLAGS="$save_CPPFLAGS"
fi


dnl Checks for GLIDE support
GLIDE_CFLAGS=""
GLIDE_LIBS=""
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_LIBS="-L$withval/lib"
   else
      GLIDE_CFLAGS="-I/usr/include/glide -I/usr/local/include/glide"
   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
	HAS_GLIDE=no
   fi
   CPPFLAGS="$save_CPPFLAGS"
fi

dnl Checks for OGL support
OGL_CFLAGS=""
OGL_LIBS=""
dnl GL is a special case, we need seperate includes/libs entires
AC_ARG_WITH(opengl,
[  --with-opengl=DIR       use OpenGL with libraries found in DIR],
HAS_OGL=$withval, HAS_OGL=auto)
AC_ARG_WITH(opengl-includes,
[  --with-opengl-includes=DIR
                          use OpenGL with header files found in DIR],
OGL_INCLUDES=$withval, OGL_INCLUDES=auto)
if test "x$OGL_INCLUDES" = xno; then
   HAS_OGL="no"
fi
if test "x$HAS_OGL" != xno; then
   if test "x$HAS_OGL" != xauto; then
      OGL_LIBS="-L$HAS_OGL"
   fi
   if test "x$OGL_INCLUDES" != xauto; then
      OGL_CFLAGS="-I$OGL_INCLUDES"
   else
      OGL_CFLAGS="-I/usr/local/include -I$x_includes"
   fi
   save_CPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS $OGL_CFLAGS"
   AC_CHECK_HEADER(GL/gl.h, HAS_OGL=yes, HAS_OGL=no)
   if test "x$HAS_OGL" != xno; then
      AC_MSG_CHECKING(for GL_COLOR_INDEX8_EXT in GL/gl.h)
      AC_TRY_COMPILE([#include "GL/gl.h"],
        [ int x = (int) GL_COLOR_INDEX8_EXT;],
        AC_DEFINE(HAVE_GL_COLOR_INDEX8_EXT) AC_MSG_RESULT(yes),
        AC_MSG_RESULT(no))

      AC_MSG_CHECKING(for GLAPIENTRY in GL/gl.h)
      AC_TRY_COMPILE([#include "GL/gl.h"],
        [ 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 in GL/gl.h)
         AC_TRY_COMPILE([#include "GL/gl.h"],
           [ int x = APIENTRY printf("");],
           AC_DEFINE(GLAPIENTRY, APIENTRY) AC_MSG_RESULT(yes),
	   AC_DEFINE(GLAPIENTRY, ) AC_MSG_RESULT(no))
      fi
      if test "x$HAS_GLIDE" != xno; then
         CPPFLAGS="$GLIDE_CFLAGS $CPPFLAGS"
         AC_CHECK_HEADER(GL/fxmesa.h, HAS_FXMESA=yes, HAS_FXMESA=no)
      fi
   fi
   CPPFLAGS="$save_CPPFLAGS"
fi

dnl Make sure DGA works
if test "x$HAS_DGA" = xyes; then
   dnl FIXME: figure out why -lXxf86dga -lXxf86vm is getting into LIBS if
   dnl        we don't save/restore them
   save_LIBS="$LIBS"
   AC_CHECK_LIB(Xxf86dga, XF86DGAQueryVersion,,
     HAS_DGA=no, [$X_LIBS -lXext -lX11 $X_EXTRA_LIBS])
   AC_CHECK_LIB(Xxf86vm, XF86VidModeSwitchToMode,,
     HAS_DGA=no, [$X_LIBS -lXext -lX11 $X_EXTRA_LIBS])
   LIBS="$save_LIBS"
fi
if test "x$HAS_DGA" != xyes; then
   DGA_LIBS=""
else
   AC_DEFINE(HAS_DGA,1)
   DGA_LIBS="-lXxf86dga -lXxf86vm"
fi

dnl Make sure -lggi works
if test "x$HAS_GGI" = xyes; then
   AC_CHECK_LIB(ggi, ggiEventsQueued, GGI_LIBS="$GGI_LIBS -lggi"
      HAS_GGI=yes, HAS_GGI=no, [$GGI_LIBS])
fi
if test "x$HAS_GGI" != xyes; then
   GGI_CFLAGS="" GGI_LIBS=""
fi
     
dnl Make sure -lvga works
if test "x$HAS_SVGA" = xyes; then
   AC_CHECK_LIB(vga, vga_getmousetype, SVGA_LIBS="$SVGA_LIBS -lvga"
     HAS_VGA=yes, HAS_VGA=no, [$SVGA_LIBS])
fi 
if test "x$HAS_SVGA" != xyes; then
   SVGA_CFLAGS="" SVGA_LIBS=""
fi

dnl Make sure -lpthread works (for SDL)
if test "x$HAS_SDL" = xyes; then
   AC_CHECK_LIB(pthread, pthread_exit ,SDL_LIBS="$SDL_LIBS -lpthread"
     HAS_SDL=yes, HAS_SDL=no, [$SDL_LIBS])
fi
if test "x$HAS_SDL" != xyes; then
   SDL_CFLAGS="" SDL_LIBS=""
fi

dnl Make sure -lSDL works
if test "x$HAS_SDL" = xyes; then
   AC_CHECK_LIB(SDL, SDL_Init, SDL_LIBS="$SDL_LIBS -lSDL"
     HAS_SDL=yes, HAS_SDL=no, [$SDL_LIBS])
fi
if test "x$HAS_SDL" != xyes; then
   SDL_CFLAGS="" SDL_LIBS=""
fi


dnl Make sure -lglide2x works
if test "x$HAS_GLIDE" = xyes; then
   AC_CHECK_LIB(glide2x, main, GLIDE_LIBS="$GLIDE_LIBS -lglide2x"
     HAS_GLIDE=yes, HAS_GLIDE=no, [$GLIDE_LIBS -lm])
fi
if test "x$HAS_GLIDE" != xyes; then
   GLIDE_CFLAGS="" GLIDE_LIBS=""
fi

dnl Make sure -lGL or -lMesaGL works
if test "x$HAS_OGL" = xyes; then
   AC_CHECK_LIB(GL, glColor4f, OGL_LIBS="$OGL_LIBS -lGL"
     HAS_OGL=yes, HAS_OGL=no,
     [$OGL_LIBS $X_LIBS -lXext -lX11 $X_EXTRA_LIBS $LIBS])
   if test "x$HAS_OGL" != xyes; then
      AC_CHECK_LIB(MesaGL, glColor4f, OGL_LIBS="$OGL_LIBS -lMesaGL"
        HAS_OGL=yes, HAS_OGL=no,
	[$OGL_LIBS $X_LIBS -lXext -lX11 $X_EXTRA_LIBS $LIBS])
   fi
fi
if test "x$HAS_OGL" != xyes; then
   OGL_CFLAGS="" OGL_LIBS=""
fi

dnl Check for xil
AC_CHECK_LIB(xil,xil_import, HAS_XIL=yes, HAS_XIL=no)

TDFXGL_CFLAGS=""
TDFXGL_LIBS=""
TDFXGL_NAME=""
AC_ARG_WITH(3dfxgl,
[  --with-3dfxgl=DIR       use 3Dfx MiniGL library found in DIR],
HAS_TDFXGL=$withval, HAS_TDFXGL=auto)
AC_ARG_WITH(3dfxgl-name,
[  --with-3dfxgl-name=NAME
                          use -lNAME instead of -l3dfxgl],
TDFXGL_NAME=$withval, TDFXGL_NAME=3dfxgl)
if test "x$HAS_TDFXGL" != xno; then
   if test "x$HAS_TDFXGL" != xauto; then
      TDFXGL_LIBS="-L$withval"
   fi
   if test "x$HAS_GLIDE" != x; then
      if test "x$HAS_OGL" != x; then
         if test "x$HAS_VGA" != x; then
	    AC_CHECK_LIB($TDFXGL_NAME, glColor4f, 
	      HAS_TDFXGL=yes, HAS_TDFXGL=no, [$GLIDE_LIBS $TDFXGL_LIBS])
	 fi
      fi
   fi
fi
if test "x$HAS_TDFXGL" = xyes; then
   TDFXGL_CFLAGS="$GLIDE_CFLAGS"
   TDFXGL_LIBS="$TDFXGL_LIBS $GLIDE_LIBS -l$TDFXGL_NAME"
else
   TDFXGL_NAME=""
   TDFXGL_LIBS=""
   HAS_TDFXGL="no"
fi



dnl CFLAGS for release and devel versions
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"
      case "${target}" in
         changequote(,)dnl
         i?86-*-*)
         changequote([,])dnl
            MORE_RELEASE_CFLAGS="-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


dnl Check for ia32
AC_MSG_CHECKING(for an ia32 machine)
case "${target}" in
   i?86-*-*)
      INTEL_ARCH=yes
      AC_MSG_RESULT(yes)
          ;;
   *) AC_MSG_RESULT(no)
esac


dnl Check for sound libraries
AC_CHECK_LIB(mme,waveOutOpen,HAVE_LIBMME=yes)

SND_LIBS=""
SND_STYLE=""
AC_MSG_CHECKING(for sound support)
if test "x$enable_alsa" = "xyes"; then
   if test -z "$SND_STYLE" -a "x$ac_cv_header_sys_asoundlib_h" = "xyes"; then
      AC_EGREP_CPP([QF_maGiC_VALUE],[
#include <sys/asoundlib.h>
#if defined(SND_LIB_MAJOR) && defined(SND_LIB_MINOR)
#if SND_LIB_MAJOR>0 || (SND_LIB_MAJOR==0 && SND_LIB_MINOR>=5)
QF_maGiC_VALUE
#endif
#endif
      ],[ SND_STYLE="ALSA" SND_LIBS="-lasound"])
   fi
fi

if test -z "$SND_STYLE" -a "x$ac_cv_header_sys_soundcard_h" = "xyes"; then
   AC_EGREP_CPP([QF_maGiC_VALUE],[
#include <sys/soundcard.h>
#ifdef SNDCTL_DSP_SETTRIGGER
QF_maGiC_VALUE
#endif
    ], SND_STYLE="OSS")
fi
if test -z "$SND_STYLE" -a "x$ac_cv_header_linux_soundcard_h" = "xyes"; then
   AC_EGREP_CPP([QF_maGiC_VALUE],[
#include <linux/soundcard.h>
#ifdef SNDCTL_DSP_SETTRIGGER
QF_maGiC_VALUE
#endif
    ], SND_STYLE="OSS")
fi
if test -z "$SND_STYLE" -a "x$ac_cv_header_machine_soundcard_h" = "xyes"; then
   AC_EGREP_CPP([QF_maGiC_VALUE],[
#include <machine/soundcard.h>
#ifdef SNDCTL_DSP_SETTRIGGER
QF_maGiC_VALUE
#endif
    ], SND_STYLE="OSS")
fi

if test -z "$SND_STYLE" -a "x$ac_cv_header_sys_audioio_h" = "xyes"; then
  AC_EGREP_CPP([QF_maGiC_VALUE],[
#include <sys/audioio.h>
#ifdef AUDIO_SETINFO
QF_maGiC_VALUE
#endif
    ], SND_STYLE="Solaris")
fi
if test -z "$SND_STYLE" -a "x$ac_cv_header_mme_mmsystem_h" = "xyes" -a \
	"x$HAVE_LIBMME" = "xyes"; then
  AC_EGREP_CPP([QF_maGiC_VALUE],[
#include <mme/mmsystem.h>
#ifdef WAVE_OPEN_SHAREABLE
QF_maGiC_VALUE
#endif
    ], SND_STYLE="MME"
       SND_LIBS="-lmme")
fi

if test -z "$SND_STYLE" -a "x$ac_cv_header_windows_h" = "xyes" -a \
	"x$ac_cv_header_mmsystem_h" = "xyes"; then
  AC_EGREP_CPP([QF_maGiC_VALUE],[
#include <windows.h>
#include <mmsystem.h>
#ifdef GMEM_MOVEABLE
#ifdef WAVE_FORMAT_PCM
QF_maGiC_VALUE
#endif
#endif
    ], SND_STYLE="Windows"
    )
fi

if test "x$SND_STYLE" = "x"; then
   AC_MSG_RESULT(no)
else
   AC_MSG_RESULT(yes ($SND_STYLE style))
fi

dnl Checks for which system driver to use
AC_MSG_CHECKING(for system driver)
case "${target}" in
   *-*-linux-gnu*)
      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

dnl check for X11 video driver
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

dnl Check for system services
AC_EXEEXT
AC_OBJEXT

dnl if the user doesn't say otherwise, games belong in $prefix/games
if test "x$bindir" = 'x${exec_prefix}/bin'; then
   bindir='${exec_prefix}/games'
fi


AC_SUBST(RELEASE)
AC_SUBST(RELEASE_CFLAGS)
AC_SUBST(HAS_XIL)
AC_SUBST(X_LIBS)
AC_SUBST(X_CFLAGS)
AC_SUBST(X11_SHM_LIB)
AC_SUBST(HAS_X11)
AC_SUBST(HAS_DGA)
AC_SUBST(DGA_LIBS)
AC_SUBST(HAS_GGI)
AC_SUBST(GGI_CFLAGS)
AC_SUBST(GGI_LIBS)
AC_SUBST(HAS_SVGA)
AC_SUBST(SVGA_CFLAGS)
AC_SUBST(SVGA_LIBS)
AC_SUBST(HAS_GLIDE)
AC_SUBST(GLIDE_CFLAGS)
AC_SUBST(GLIDE_LIBS)
AC_SUBST(HAS_OGL)
AC_SUBST(OGL_CFLAGS)
AC_SUBST(OGL_LIBS)
AC_SUBST(HAS_TDFXGL)
AC_SUBST(TDFXGL_CFLAGS)
AC_SUBST(TDFXGL_LIBS)
AC_SUBST(HAS_SDL)
AC_SUBST(SDL_CFLAGS)
AC_SUBST(SDL_LIBS)
AC_SUBST(SND_STYLE)
AC_SUBST(SND_LIBS)
AC_SUBST(INTEL_ARCH)
AC_SUBST(SYS_SRC)
AC_SUBST(QW_CL_SYS_SRC)
AC_SUBST(QW_SRV_SYS_SRC)
AC_SUBST(X11_VID_SRC)

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

