
dnl Written by Peter Ekberg, peda@lysator.liu.se
dnl Process this file with autoconf to produce a configure script.

AC_DEFUN(PE_PATH_SVGA_INCLUDES,
[
AC_ARG_ENABLE(svga-includes,
[  --enable-svga-includes=DIR
                          SVGAlib include files are in DIR],
[
  dnl User specified where includes are.
  if test "x$enableval" = x; then
    pe_cv_svga_includes=NONE
  else
    pe_cv_svga_includes="$enableval"
  fi
],
[
  AC_CACHE_VAL(pe_cv_svga_includes, [
    dnl User did not specify where includes are.
    dnl Check first if they are in the compiler include path.
    AC_TRY_CPP([
#include <vga.h>
#include <vgagl.h>
#include <vgakeyboard.h>
],
    [
      dnl Includes are in the compiler include path.
      pe_cv_svga_includes=NONE
    ],
    [
      dnl Includes are not in the compiler include path.
      dnl Try to locate them.
      for pe_dir in             \
        /usr/local/svga/include \
        /usr/local/include      \
        /usr/include            \
        ; \
      do
        if test -r "$pe_dir/vga.h" && \
           test -r "$pe_dir/vgagl.h" && \
           test -r "$pe_dir/vgakeyboard.h"; then
          pe_cv_svga_includes="$pe_dir"
          break
        fi
      done
    ])
    if test "x$pe_cv_svga_includes" = x; then
      pe_cv_svga_includes=no
    fi
  ])
])
])

AC_DEFUN(PE_PATH_SVGA_LIBRARIES,
[
AC_ARG_ENABLE(svga-libraries,
[  --enable-svga-libraries=DIR
                          SVGAlib library files are in DIR],
[
  dnl User specified where libraries are.
  if test "x$enableval" = x; then
    pe_cv_svga_libraries=NONE
  else
    pe_cv_svga_libraries="$enableval"
  fi
],
[
  AC_CACHE_VAL(pe_cv_svga_libraries, [
    dnl User did not specify where libraries are.
    pe_libs="$LIBS"
    LIBS="$LIBS -lvga"
    if test "$pe_cv_svga_includes" != NONE; then
      pe_cflags="$CFLAGS"
      CFLAGS="-I$pe_cv_svga_includes $CFLAGS"
    fi
    AC_TRY_LINK([#include <vga.h>],
    [
      vga_init();
    ],
    [
      dnl Libraries are found without -L{path}.
      pe_cv_svga_libraries=NONE
    ],
    [
      dnl Libraries are not found without -L{path}.
      dnl Try to locate them.
      for pe_dir in             \
        /usr/local/svga/lib     \
        /usr/local/lib/svga     \
        /usr/local/lib          \
        /usr/lib                \
        /lib                    \
        ; \
      do
        if test -r "$pe_dir/libvga.so.1"; then
          pe_cv_svga_libraries="$pe_dir"
          break
        fi
      done
      if test "x$pe_cv_svga_libraries" = x; then
        pe_cv_svga_libraries=no
      fi
    ])
    LIBS="$pe_libs"
    if test "$pe_cv_svga_includes" != NONE; then
      CFLAGS="$pe_cflags"
    fi
  ])
])
])

AC_DEFUN(PE_PATH_SVGA,
[
AC_MSG_CHECKING(for SVGAlib)
AC_ARG_WITH(svga,
  [  --with-svga             use SVGAlib],
  [
    if test "x$withval" = xno; then
      no_svga=yes
    fi
  ])
if test "x$no_svga" = x; then
  PE_PATH_SVGA_INCLUDES
  PE_PATH_SVGA_LIBRARIES
  if test "$pe_cv_svga_includes" != no && \
     test "$pe_cv_svga_libraries" != no; then
    AC_MSG_RESULT(
     [headers $pe_cv_svga_includes, libraries $pe_cv_svga_libraries])
    if test "$pe_cv_svga_includes" != NONE; then
      SVGA_CFLAGS="-I$pe_cv_svga_includes"
    fi
    if test "$pe_cv_svga_libraries" != NONE; then
      SVGA_LIBS="-L$pe_cv_svga_libraries"
    fi
    AC_SUBST(SVGA_CFLAGS)
    AC_SUBST(SVGA_LIBS)
  else
    AC_MSG_RESULT(no)
    no_svga=yes
  fi
else
  AC_MSG_RESULT(disabled)
fi
])

AC_DEFUN(PE_HEADER_VGAKEYBOARD_DEFINE_P,
[
  AC_REQUIRE([PE_PATH_SVGA])
  pe_cppflags="$CPPFLAGS"
  CPPFLAGS="$SVGA_CFLAGS $CPPFLAGS"
  AC_CHECK_HEADER(vgakeyboard.h, [
    AC_CACHE_CHECK(if vgakeyboard.h defines SCANCODE_P,
                   pe_cv_header_vgakeyboard_define_p, [
      AC_EGREP_CPP([scancode_p_defined], [
#include <vgakeyboard.h>
#ifdef SCANCODE_P
int scancode_p_defined;
#endif],
      [pe_cv_header_vgakeyboard_define_p=yes],
      [pe_cv_header_vgakeyboard_define_p=no])
    ])
  ], [no_svga=yes])
  if test "x$pe_cv_header_vgakeyboard_define_p" = xyes; then
    AC_DEFINE(HAVE_DEFINE_SCANCODE_P)
  fi
  CPPFLAGS="$pe_cppflags"
])




AC_DEFUN(PE_PATH_PBM_INCLUDES,
[
AC_ARG_ENABLE(pbm-includes,
[  --enable-pbm-includes=DIR
                          pbm include files are in DIR],
[
  dnl User specified where includes are.
  if test "x$enableval" = x; then
    pe_cv_pbm_includes=NONE
  else
    pe_cv_pbm_includes="$enableval"
  fi
],
[
  AC_CACHE_VAL(pe_cv_pbm_includes, [
    dnl User did not specify where includes are.
    dnl Check first if they are in the compiler include path.
    AC_TRY_CPP([
#include <pnm.h>
],
    [
      dnl Includes are in the compiler include path.
      pe_cv_pnm_includes=NONE
    ],
    [
      dnl Includes are not in the compiler include path.
      dnl Try to locate them.
      for pe_dir in                 \
        /usr/local/pbm/include/X11  \
        /usr/local/pbm/include      \
        /usr/local/include/X11      \
        /usr/local/include          \
        /usr/pbm/include/X11        \
        /usr/pbm/include            \
        /usr/include/X11            \
        /usr/include                \
        ; \
      do
        if test -r "$pe_dir/pnm.h" && \
           test -r "$pe_dir/ppm.h" && \
           test -r "$pe_dir/pgm.h" && \
           test -r "$pe_dir/pbm.h"; then
          pe_cv_pbm_includes="$pe_dir"
          break
        fi
      done
    ])
    if test "x$pe_cv_pbm_includes" = x; then
      pe_cv_pbm_includes=no
    fi
  ])
])
])

AC_DEFUN(PE_PATH_PBM_LIBRARIES,
[
AC_ARG_ENABLE(pbm-libraries,
[  --enable-pbm-libraries=DIR
                          pbm library files are in DIR],
[
  dnl User specified where libraries are.
  if test "x$enableval" = x; then
    pe_cv_pbm_libraries=NONE
  else
    pe_cv_pbm_libraries="$enableval"
  fi
],
[
  AC_CACHE_VAL(pe_cv_pbm_libraries, [
    dnl User did not specify where libraries are.
    pe_libs="$LIBS"
    LIBS="$LIBS -lpnm -lppm -lpgm -lpbm"
    if test "$pe_cv_pbm_includes" != NONE; then
      pe_cflags="$CFLAGS"
      CFLAGS="-I$pe_cv_pbm_includes $CFLAGS"
    fi
    AC_TRY_LINK([#include <pnm.h>],
    [
      pnm_allocarray(4, 6);
    ],
    [
      dnl Libraries are found without -L{path}.
      pe_cv_pbm_libraries=NONE
    ],
    [
      dnl Libraries are not found without -L{path}.
      dnl Try to locate them.
      for pe_dir in             \
        /usr/local/pbm/lib      \
        /usr/local/lib/pbm      \
        /usr/local/lib          \
        /usr/pbm/lib            \
        /usr/lib/pbm            \
        /usr/lib                \
        /lib                    \
        ; \
      do
        if test -r "$pe_dir/libpnm.so.1"; then
          pe_cv_pbm_libraries="$pe_dir"
          break
        fi
      done
      if test "x$pe_cv_pbm_libraries" = x; then
        pe_cv_pbm_libraries=no
      fi
    ])
    LIBS="$pe_libs"
    if test "$pe_cv_pbm_includes" != NONE; then
      CFLAGS="$pe_cflags"
    fi
  ])
])
])

AC_DEFUN(PE_PATH_PBM,
[
AC_MSG_CHECKING(for pbm)
AC_ARG_WITH(pbm,
  [  --with-pbm              use pbm, required to rebuild datasrc/title.c],
  [
    if test "x$withval" = xno; then
      no_pbm=yes
    fi
  ])
if test "x$no_pbm" = x; then
  PE_PATH_PBM_INCLUDES
  PE_PATH_PBM_LIBRARIES
  if test "$pe_cv_pbm_includes" != no && \
     test "$pe_cv_pbm_libraries" != no; then
    AC_MSG_RESULT(
     [headers $pe_cv_pbm_includes, libraries $pe_cv_pbm_libraries])
    if test "$pe_cv_pbm_includes" != NONE; then
      PBM_CFLAGS="-I$pe_cv_pbm_includes"
    fi
    if test "$pe_cv_pbm_libraries" != NONE; then
      PBM_LIBS="-L$pe_cv_pbm_libraries"
    fi
    AC_SUBST(PBM_CFLAGS)
    AC_SUBST(PBM_LIBS)
  else
    AC_MSG_RESULT(no)
    no_pbm=yes
  fi
else
  AC_MSG_RESULT(disabled)
fi
])





AC_DEFUN(PE_FUNC_PRINTF_RETURN,
[
AC_MSG_CHECKING(return value of printf)
AC_CACHE_VAL(pe_cv_func_printf_return,
  AC_TRY_RUN([
#include <stdio.h>
void main()
{
  FILE *f;
  int return_val;

  f=fopen("conftest.prf", "w");
  if(f == NULL)
    exit(2);
  return_val = fprintf(f, "p");
  fclose(f);
  if(return_val==1)
    exit(0);
  exit(1);
}
    ],
    pe_cv_func_printf_return=POSIX,
    pe_cv_func_printf_return=try_bsd,
    pe_cv_func_printf_return=unknown)
  if test "x$pe_cv_func_printf_return" = xtry_bsd; then
    AC_TRY_RUN([
#include <stdio.h>
void main()
{
  FILE *f;
  int return_val;

  f=fopen("conftest.prf", "w");
  if(f == NULL)
    exit(2);
  return_val = fprintf(f, "p");
  fclose(f);
  exit(!!return_val);
}
        ],
      pe_cv_func_printf_return=BSD,
      pe_cv_func_printf_return=none,
      pe_cv_func_printf_return=unknown)
  fi)

AC_MSG_RESULT($pe_cv_func_printf_return)
case "$pe_cv_func_printf_return" in
  BSD)
    AC_DEFINE(PRINTF_RETURN, 0);;
  POSIX)
    AC_DEFINE(PRINTF_RETURN, 1);;
  *)
    AC_DEFINE(PRINTF_RETURN, -1);;
esac
])




AC_DEFUN(PE_LINUX_SOUND,
[
AC_CHECK_HEADERS(linux/soundcard.h, [
  linux_sound=yes
  AC_DEFINE(HAVE_LINUX_SOUND)
  sound=yes
  AC_DEFINE(HAVE_SOUND)
  ])
])


AC_DEFUN(PE_SUN_SOUND,
[
AC_CHECK_HEADERS(sys/audioio.h, sun_sound=yes)
if test "x$sun_sound" != xyes; then
  AC_CHECK_HEADERS(sun/audioio.h, sun_sound=yes)
fi
if test "x$sun_sound" = xyes; then
  AC_MSG_CHECKING(for linear sound encoding)
  AC_CACHE_VAL(pe_cv_sun_linear_sound_encoding,
    AC_EGREP_CPP(Have_audio_encoding_linear, [
#if defined(HAVE_SYS_AUDIOIO_H)
#include <sys/audioio.h>
#elif defined(HAVE_SUN_AUDIOIO_H)
#include <sun/audioio.h>
#endif
#ifdef AUDIO_ENCODING_LINEAR
  Have_audio_encoding_linear
#endif ],
      pe_cv_sun_linear_sound_encoding=yes,
      pe_cv_sun_linear_sound_encoding=no))
  AC_MSG_RESULT($pe_cv_sun_linear_sound_encoding)
  if test "x$pe_cv_sun_linear_sound_encoding" = xyes; then
    AC_DEFINE(HAVE_16BIT_SOUND)
    AC_DEFINE(HAVE_SUN_SOUND)
    sound=yes
    AC_DEFINE(HAVE_SOUND)
  else
    sun_sound=
  fi
fi
if test "x$sun_sound" = xyes; then

  AC_MSG_CHECKING(for speaker)
  AC_CACHE_VAL(pe_cv_sun_speaker,
    AC_EGREP_CPP(Have_audio_speaker, [
#if defined(HAVE_SYS_AUDIOIO_H)
#include <sys/audioio.h>
#elif defined(HAVE_SUN_AUDIOIO_H)
#include <sun/audioio.h>
#endif
#ifdef AUDIO_SPEAKER
  Have_audio_speaker
#endif ],
      pe_cv_sun_speaker=yes,
      pe_cv_sun_speaker=no))
  AC_MSG_RESULT($pe_cv_sun_speaker)
  if test "x$pe_cv_sun_speaker" = xyes; then
    AC_DEFINE(HAVE_SUN_SPEAKER)
  fi

  AC_MSG_CHECKING(for headphone)
  AC_CACHE_VAL(pe_cv_sun_headphone,
    AC_EGREP_CPP(Have_audio_headphone, [
#if defined(HAVE_SYS_AUDIOIO_H)
#include <sys/audioio.h>
#elif defined(HAVE_SUN_AUDIOIO_H)
#include <sun/audioio.h>
#endif
#ifdef AUDIO_HEADPHONE
  Have_audio_headphone
#endif ],
      pe_cv_sun_headphone=yes,
      pe_cv_sun_headphone=no))
  AC_MSG_RESULT($pe_cv_sun_headphone)
  if test "x$pe_cv_sun_headphone" = xyes; then
    AC_DEFINE(HAVE_SUN_HEADPHONE)
  fi

  AC_MSG_CHECKING(for line out)
  AC_CACHE_VAL(pe_cv_sun_line_out,
    AC_EGREP_CPP(Have_audio_line_out, [
#if defined(HAVE_SYS_AUDIOIO_H)
#include <sys/audioio.h>
#elif defined(HAVE_SUN_AUDIOIO_H)
#include <sun/audioio.h>
#endif
#ifdef AUDIO_LINE_OUT
  Have_audio_line_out
#endif ],
      pe_cv_sun_line_out=yes,
      pe_cv_sun_line_out=no))
  AC_MSG_RESULT($pe_cv_sun_line_out)
  if test "x$pe_cv_sun_line_out" = xyes; then
    AC_DEFINE(HAVE_SUN_LINE_OUT)
  fi
fi
])


AC_DEFUN(PE_HP_SOUND,
[
AC_CHECK_HEADERS(sys/audio.h, hp_sound=yes)
if test "x$hp_sound" = xyes; then
  AC_MSG_CHECKING(for linear sound encoding)
  AC_CACHE_VAL(pe_cv_hp_linear_sound_encoding,
    AC_EGREP_CPP(Have_audio_encoding_linear, [
#include <sys/audio.h>
#ifdef AUDIO_FORMAT_LINEAR16BIT
  Have_audio_encoding_linear
#endif ],
      pe_cv_hp_linear_sound_encoding=yes,
      pe_cv_hp_linear_sound_encoding=no))
  AC_MSG_RESULT($pe_cv_hp_linear_sound_encoding)
  if test "x$pe_cv_hp_linear_sound_encoding" = xyes; then
    AC_DEFINE(HAVE_16BIT_SOUND)
    AC_DEFINE(HAVE_HP_SOUND)
    sound=yes
    AC_DEFINE(HAVE_SOUND)
  else
    hp_sound=
  fi
fi
if test "x$hp_sound" = xyes; then

  AC_MSG_CHECKING(for internal_speaker)
  AC_CACHE_VAL(pe_cv_hp_internal_speaker,
    AC_EGREP_CPP(Have_audio_internal_speaker, [
#include <sys/audio.h>
#ifdef AUDIO_OUT_INTERNAL
  Have_audio_internal_speaker
#endif ],
      pe_cv_hp_internal_speaker=yes,
      pe_cv_hp_internal_speaker=no))
  AC_MSG_RESULT($pe_cv_hp_internal_speaker)
  if test "x$pe_cv_hp_internal_speaker" = xyes; then
    AC_DEFINE(HAVE_HP_INTERNAL_SPEAKER)
  fi

  AC_MSG_CHECKING(for external speaker)
  AC_CACHE_VAL(pe_cv_hp_external_speaker,
    AC_EGREP_CPP(Have_audio_external_speaker, [
#include <sys/audio.h>
#ifdef AUDIO_OUT_EXTERNAL
  Have_audio_external_speaker
#endif ],
      pe_cv_hp_external_speaker=yes,
      pe_cv_hp_external_speaker=no))
  AC_MSG_RESULT($pe_cv_hp_external_speaker)
  if test "x$pe_cv_hp_external_speaker" = xyes; then
    AC_DEFINE(HAVE_HP_EXTERNAL_SPEAKER)
  fi

  AC_MSG_CHECKING(for line out)
  AC_CACHE_VAL(pe_cv_hp_line_out,
    AC_EGREP_CPP(Have_audio_line_out, [
#include <sys/audio.h>
#ifdef AUDIO_OUT_LINE
  Have_audio_line_out
#endif ],
      pe_cv_hp_line_out=yes,
      pe_cv_hp_line_out=no))
  AC_MSG_RESULT($pe_cv_hp_line_out)
  if test "x$pe_cv_hp_line_out" = xyes; then
    AC_DEFINE(HAVE_HP_LINE_OUT)
  fi
fi
])





AC_INIT(thrust.pod.in)

version_nr=0.83
version=${version_nr}c

config_args=["$0 $@"]

AC_CONFIG_HEADER(src/config.h)

AC_CANONICAL_HOST

dnl Alternative programs
AC_PROG_CC
cflags=
optimize=
if test "x$GCC" = xyes; then
  cflags="$cflags -Wall -Wstrict-prototypes -Wmissing-prototypes"
  optimize="$optimize -fomit-frame-pointer -O9 -s"
else
  case "$host" in
    *hpux*)
      cflags="$cflags -Ae"
      optimize="$optimize +O4"
      ;;
  esac
fi
case "$host" in
  *-osf*)
    AC_DEFINE(_POSIX_C_SOURCE)
    AC_DEFINE(_XOPEN_SOURCE)
    ;;
esac
    
AC_PROG_INSTALL
AC_C_CONST
AC_C_INLINE
AC_PATH_PROG(pod2man, pod2man)

dnl Libraries

dnl Library Functions
AC_CHECK_FUNCS(atexit on_exit)
PE_FUNC_PRINTF_RETURN

dnl Header Files
dnl AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h getopt.h values.h)

pe_sound=yes
AC_ARG_ENABLE(sound,
  [  --enable-sound          try to include sound],
  [
    if test "x$enableval" = xno; then
      pe_sound=no
    fi
  ])
if test "x$pe_sound" = xyes; then
  case "$host" in
    *linux*)
      PE_LINUX_SOUND
      ;;
    *sun*)
      PE_SUN_SOUND
      ;;
    *hpux*)
      PE_HP_SOUND
      ;;
  esac
fi

AC_CHECK_FUNCS(getopt_long_only, , LIBOBJS="$LIBOBJS getopt.o getopt1.o")
AC_CHECK_FUNCS(random)
AC_REPLACE_FUNCS(usleep strdup)

dnl Structures
AC_TYPE_SIGNAL

dnl Typedefs

dnl System Services
AC_PATH_X
AC_PATH_XTRA
AC_CHECK_LIB(Xext, XShmPutImage, AC_DEFINE(HAVE_XSHMPUTIMAGE), ,
             $X_PRE_LIBS $X_LIBS $X_EXTRA_LIBS -lX11)

PE_PATH_SVGA
if test "x$no_svga" = x; then
  PE_HEADER_VGAKEYBOARD_DEFINE_P
  if test "x$no_svga" != x; then
    AC_MSG_WARN([SVGAlib disabled, could not find "vgakeyboard.h"])
  fi
fi

PE_PATH_PBM

dnl UNIX Variants

dnl Output

if test "x$no_svga" = xyes && test "x$no_x" = xyes; then
  AC_MSG_WARN([Either X or SVGAlib found. Impossible to build game.])
fi

AC_SUBST(version)
AC_SUBST(version_nr)
AC_SUBST(cflags)
AC_SUBST(optimize)
AC_SUBST(config_args)
AC_SUBST(no_pbm)
AC_SUBST(no_svga)
AC_SUBST(no_x)
AC_SUBST(sound)

AC_OUTPUT(Makefile thrust-${version}.lsm:thrust.lsm Makefile.bindist dosfiles/Makefile thrust.pod)
