dnl Process this file with autoconf to produce a configure script.
dnl
dnl $Id: configure.in,v 1.9 1998/12/07 06:01:55 miod Exp $

AC_INIT(include/mikmod.h)
AM_INIT_AUTOMAKE(mikmod,3.1.2)
AM_CONFIG_HEADER(config.h)

dnl
dnl MikMod specific control variables
dnl

driver_af=
driver_alsa=
driver_esd=
driver_oss=
virtch2=
debug=
efence=
headphone=

# We don't want that autoconf messes with the CFLAGS if they are not set,
# we'll provide better CFLAGS for gcc than it. Yet another dirty kluge...
if test -z "$CFLAGS"
then
	CFLAGS=" "
fi

dnl
dnl Configure script options
dnl

AC_ARG_ENABLE(af,
[  --enable-af             include AudioFile driver [default under OSF/1]],
[if test "$enableval" = "yes"; then
  driver_af=yes
else
  driver_af=no
fi])

AC_ARG_ENABLE(alsa,
[  --enable-alsa           Linux only: include Advanced Linux Sound Architecture
                          driver [guessed]],
[if test "$enableval" = "yes"; then
  driver_alsa=yes
else
  driver_alsa=no
fi])

AC_ARG_ENABLE(esd,
[  --enable-esd            include Enlightened sound daemon driver [guessed]],
[if test "$enableval" = "yes"; then
  driver_esd=yes
else
  driver_esd=no
fi])

AC_ARG_ENABLE(oss,
[  --enable-oss            include OSS driver [guessed]],
[if test "$enableval" = "yes"; then
  driver_oss=yes
else
  driver_oss=no
fi])

AC_ARG_ENABLE(hqmixer,
[  --enable-hqmixer        use high-quality mixer [default=no]],
[if test "$enableval" = "yes"; then
  virtch2=yes
else
  virtch2=no
fi])

AC_ARG_ENABLE(headphone,
[  --enable-headphone      SunOS only: redirect sound to the headphone jack
                          [default=no]],
[if test "$enableval" = "yes"; then
  headphone=$enable_headphone
else
  headphone=no
fi])

AC_ARG_ENABLE(snagger,
[  --enable-snagger        enable cpu time snagger feature [default=no]],
[if test "$enableval" = "yes"; then
  snagger=yes
else
  snagger=no
fi])

AC_ARG_ENABLE(debug,
[  --enable-debug          build a debug version of MikMod [default=no]],
[if test "$enableval" = "yes"; then
  debug=yes
else
  debug=no
fi
AM_DISABLE_SHARED
AM_ENABLE_STATIC])

AC_ARG_ENABLE(efence,
[  --enable-efence         build a debug version of MikMod with ElectricFence
                          [default=no]],
[if test "$enableval" = "yes"; then
  efence=yes
else
  efence=no
fi
AM_DISABLE_SHARED
AM_ENABLE_STATIC])

dnl
dnl Checks for programs.
dnl

AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_LN_S
AM_PROG_LIBTOOL
AC_PROG_RANLIB

dnl
dnl Checks for libraries.
dnl

AC_CHECK_LIB(asound,snd_cards,can_alsa=yes,can_alsa=no)

dnl AC_CHECK_LIB(esd,esd_open_sound,can_esd=yes,can_esd=no)
dnl If esd is compiled with alsa support, AC_CHECK_LIB will fail
dnl So try this :
if test "$can_alsa" = "yes"
then
	AC_MSG_CHECKING([if esd is present and requires alsa])
	oldlibs=$LIBS
	LIBS="$LIBS -lesd"
	AC_TRY_LINK([#include <esd.h>],[void *p=esd_open_sound;],
		can_esd=yes,can_esd=no)
	LIBS=$oldlibs
	if test "$can_esd" = "yes"
	then
		esd_needs_alsa=no
		AC_MSG_RESULT([yes, and does not require alsa])
	else
		oldlibs=$LIBS
		LIBS="$LIBS -lesd -lasound"
		AC_TRY_LINK([#include <esd.h>],[void *p=esd_open_sound;],
			can_esd=yes,can_end=no)
		LIBS=$oldlibs
		if test "$can_esd" = "yes"
		then
			esd_needs_alsa=yes
			AC_MSG_RESULT([yes, and requires alsa])
		else
			AC_MSG_RESULT(no)
		fi
	fi
else
	AC_CHECK_LIB(esd,esd_open_sound,can_esd=yes,can_esd=no)
	esd_needs_alsa=no
fi

AC_CHECK_LIB(audio,ALseterrorhandler,driver_sgi=yes,driver_sgi=no)
AC_CHECK_LIB(AF,AFOpenAudioConn,can_af=yes,can_af=no)
AC_CHECK_LIB(ncurses,initscr,enable_ncurses=yes)

dnl
dnl Checks for header files.
dnl

AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fnmatch.h)
AC_CHECK_HEADERS(ncurses.h)
AC_CHECK_HEADERS(fcntl.h limits.h malloc.h sys/ioctl.h sys/time.h unistd.h)
AC_CHECK_HEADERS(sched.h)

dnl Checks for bogus Linux old libc5 sched.h

if test "`uname`" = "Linux"
then
	AC_MSG_CHECKING([whether sched.h is correct])
	AC_TRY_COMPILE([#include <sched.h>],[sched_yield();],
		broken_sched=no,broken_sched=yes)
	if test "$broken_sched" = "yes"
	then
		AC_DEFINE(BROKEN_SCHED)
		AC_MSG_RESULT(no)
	else
		AC_MSG_RESULT(yes)
	fi
fi

if test "$can_esd" = "yes"
then
	AC_MSG_CHECKING([for esd_close in -lesd])
	oldlibs=$LIBS
	LIBS="$LIBS -lesd"
	if test "$esd_needs_alsa" = "yes"
	then
		LIBS="$LIBS -lasound"
	fi
	AC_TRY_LINK([#include <esd.h>],[void *p=esd_close;],
		has_esd_close=yes,has_esd_close=no)
	LIBS=$oldlibs
	if test "$has_esd_close" = "yes"
	then
		AC_MSG_RESULT(yes)
		AC_DEFINE(HAVE_ESD_CLOSE)
	else
		AC_MSG_RESULT(no)
	fi
fi

dnl
dnl Checks for typedefs, structures, and compiler characteristics.
dnl

AC_C_CONST
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME

dnl
dnl Checks for library functions.
dnl

AC_FUNC_FNMATCH
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(usleep,have_usleep=yes)
AC_CHECK_FUNCS(getopt_long_only,have_getopt_long_only=yes)
AC_HEADER_EGREP(usleep,unistd.h,have_usleep_h=yes)
AC_HEADER_EGREP(usleep,sys/unistd.h,have_usleep_h=yes)

dnl
dnl Choose settings
dnl

# If we can guess the drivers needed from the system, no need to ask the user
# to specify it manually
case `uname` in
	AIX)
		AC_DEFINE(AIX)
		AC_DEFINE(DRV_AIX)
		DRIVER_OBJ="drv_aix.lo $DRIVER_OBJ"
	;;
	HP-UX)
		AC_DEFINE(DRV_HP)
		DRIVER_OBJ="drv_hp.lo $DRIVER_OBJ"
	;;
	IRIX*)
		if test "$driver_sgi" = "yes"
		then
			AC_DEFINE(DRV_SGI)
			DRIVER_OBJ="drv_sgi.lo $DRIVER_OBJ"
			PLAYER_LIB="-laudio $PLAYER_LIB"
			LIBRARY_LIB="-laudio $LIBRARY_LIB"
		fi
	;;
	NetBSD|OpenBSD)
		AC_DEFINE(DRV_SUN)
		DRIVER_OBJ="drv_sun.lo $DRIVER_OBJ"
	;;
	SunOS)
		AC_DEFINE(DRV_SUN)
		DRIVER_OBJ="drv_sun.lo $DRIVER_OBJ"
		case `uname -r` in
			4.*)
				AC_DEFINE(SUNOS)
			;;
			*) ;;
		esac
		if test "$headphone" = "yes"
		then
			AC_DEFINE(USE_HEADPHONE)
		fi
	;;
	FreeBSD)
		driver_oss=yes
	;;
	OSF1)
		if test "$can_af" = "yes"
		then
			driver_af=yes
		fi
	;;
	*) ;;
esac
			
if test "$driver_af" = "yes"
then
	AC_DEFINE(DRV_AF)
	DRIVER_OBJ="drv_AF.lo $DRIVER_OBJ"
	PLAYER_LIB="-lAF $PLAYER_LIB"
	LIBRARY_LIB="-lAF $LIBRARY_LIB"
fi

if test "$can_alsa" = "yes"
then
	if test "$driver_alsa" != "no"
	then
dnl testing /proc/asound is not reliable, if alsa is not loaded at this time...
dnl 	AC_MSG_CHECKING([for ALSA (/proc/asound)])
dnl 	if test -c /proc/asound/cards
dnl 	then
dnl 		AC_MSG_RESULT(found)
 		AC_DEFINE(DRV_ALSA)
 		DRIVER_OBJ="drv_alsa.lo $DRIVER_OBJ"
 		PLAYER_LIB="-lasound $PLAYER_LIB"
 		LIBRARY_LIB="-lasound $LIBRARY_LIB"
dnl 	else
dnl 		AC_MSG_RESULT(not found)
dnl	fi
	else
		if test "$esd_needs_alsa" = "yes"
		then
			driver_esd=no
		fi
	fi
else
	if test "$esd_needs_alsa" = "yes"
	then
		driver_esd=no
	fi
fi

if test "$can_esd" = "yes"
then
	if test "$driver_esd" != "no"
	then
		AC_DEFINE(DRV_ESD)
		DRIVER_OBJ="drv_esd.lo $DRIVER_OBJ"
		PLAYER_LIB="-lesd $PLAYER_LIB"
		LIBRARY_LIB="-lesd $LIBRARY_LIB"
	fi
fi

if test "$driver_oss" != "no"
then
	AC_MSG_CHECKING([for OSS (/dev/sndstat)])
	if test -c /dev/sndstat
	then
		AC_MSG_RESULT(found)
		AC_DEFINE(DRV_OSS)
		DRIVER_OBJ="drv_oss.lo $DRIVER_OBJ"
	else
		AC_MSG_RESULT(not found)
	fi
fi

if test "$virtch2" = "yes"
then
	AC_DEFINE(VIRTCH2)
	VIRTCH="virtch2.lo"
else
	VIRTCH="virtch.lo"
fi

if test "$snagger" = "yes"
then
	AC_DEFINE(CPUTIME_SNAGGER)
fi

if test "$efence" = "yes"
then
	debug=yes
	PLAYER_LIB="$PLAYER_LIB -lefence"
fi
if test "$debug" = "yes"
then
	AC_DEFINE(MIKMOD_DEBUG)
	CC="$CC -g"
fi

# If compiling with gcc, use adequate optimization flags
case $CC in
	gcc*)
		CFLAGS="$CFLAGS -O3 -funroll-loops -ffast-math -Wall"
		# No inlining for debug version
		if test "$debug" != "yes"
		then
			CFLAGS="$CFLAGS -finline-functions"
		fi
		;;
	*) ;;
esac

dnl
dnl Choose extra stuff
dnl

if test "$have_usleep_h" != "yes"
then
	AC_DEFINE(NEED_USLEEP)
fi
if test "$have_getopt_long_only" != "yes"
then
	EXTRA_OBJ="getopt.o getopt1.o $EXTRA_OBJ"
fi
# Yet another kluge to get the result of AC_FUNC_FNMATCH. Might break with newer
# versions of autoconf.
if test "$ac_cv_func_fnmatch_works" != "yes"
then
	EXTRA_OBJ="fnmatch.o $EXTRA_OBJ"
fi
if test "$have_usleep" != "yes"
then
	EXTRA_OBJ="usleep.o $EXTRA_OBJ"
fi

if test "$enable_ncurses" = "yes"
then
	PLAYER_LIB="-lncurses $PLAYER_LIB"
else
	PLAYER_LIB="-lcurses $PLAYER_LIB"
fi

dnl
dnl Create Makefiles
dnl

AC_SUBST(DRIVER_OBJ)
AC_SUBST(VIRTCH)
AC_SUBST(LIBRARY_LIB)
AC_SUBST(EXTRA_OBJ)
AC_SUBST(PLAYER_LIB)

dnl AC_SUBST(PACKAGE)
dnl AC_SUBST(VERSION)

AC_OUTPUT(Makefile
          docs/Makefile
          drivers/Makefile
          extra/Makefile
          include/Makefile
          libmikmod/Makefile
          loaders/Makefile
          mikmod/Makefile
          mmio/Makefile
          playercode/Makefile)
