dnl This must be first.
AC_INIT(config.flags.in)
dnl Some reminders:
dnl	"dnl" is a "delete to newline" command for m4 - the remainder of the
dnl		line it's on, including the newline, will be discarded
dnl	"#" comments will usually be copied through
dnl	"[]" are quoting characters; use changequote around code that really
dnl		needs to use them, but try not to invoke autoconf macros
dnl		within such code
dnl
dnl This file does require autoconf 2.0 or better...
AC_PREREQ(2.0)dnl
dnl
dnl Hook for the "--enable-dce-compat" option, when Chris actually
dnl implements it.
AC_ARG_ENABLE(dce-compat,
    --enable-dce-compat	  DCE compatibility,
[dnl "true" action
  AC_MSG_ERROR([Actually, DCE compatibility doesn't work yet...])
],
[dnl "false" action
  true
])

dnl What C compiler?
AC_PROG_CC
dnl Apparently autoconf doesn't test to see if the C compiler it locates
dnl actually works... oops!
AC_TRY_COMPILE(,[ exit(0); ],[
  dnl true
  AC_TRY_COMPILE(,[ syntax errors galore! ],
    dnl true
    AC_MSG_ERROR(Can't detect syntax errors!  Is CC set right?),
    dnl false
    true)],
  dnl false
  AC_MSG_ERROR(Can't compile test program!  Is CC set right?))
AC_PROG_CPP

dnl Default value of prefix should be /usr/local/pthreads, not /usr/local.
AC_PREFIX_DEFAULT(/usr/local/pthreads)

dnl Determine $host, by guessing if necessary.
AC_CANONICAL_SYSTEM

syscall=NONE
sysincludes=NONE

changequote(,)dnl
case $host in
    alpha-*-osf*)
	name=alpha-osf1
	sysincludes=alpha-osf1
	makefile_frag=alpha-osf1.mk
	;;
    hppa1.1-*-hpux*)
	name=hppa-hpux-9.03
	sysincludes=hpux-9.03
	config_flags="HPPA_HPUX_903=1"
	;;
    sparc-*-sunos4.1.3)
	name=sparc-sunos-4.1.3
	sysincludes=sunos-4.1.3
	makefile_frag=sun4os4.mk
	;;
    sparc-*-solaris2*)
	name=sparc-sunos-5.3
	sysincludes=sunos-5.3
	;;
    i[3-5]86-*-linux*)
	name=i386-linux-1.0
	sysincludes=linux-1.0
	;;
    i[3-5]86-*-bsdi)
	name=i386-bsdi-1.1
	sysincludes=bsdi-1.1
	;;
    i[3-5]86-*-netbsd0.9*)
	name=i386-netbsd-0.9
	sysincludes=netbsd-0.9
	;;
    i[3-5]86-*-netbsd1.0*)
	name=i386-netbsd-1.0
	sysincludes=netbsd-1.0
	makefile_frag=netbsd1.0.mk
	syscall=i386-netbsd1.0
	;;
    m68*-*-netbsd*)
	name=m68000-netbsd
	;;
    romp-*-bsd*)
	name=romp-bsd
	;;
    mips-*-ultrix*)
	name=r2000-ultrix-4.2
	sysincludes=ultrix-4.2
	;;
changequote([,])dnl
    *)
	AC_MSG_ERROR(System type $host not recognized or not supported.
See $srcdir/configure.in for supported systems.)
	exit 1
	;;
esac

dnl Drop in $config_flags for @config_flags@ in output file.
dnl Since we're using system-specific makefile fragments for other purposes
dnl anyways, this should probably go away eventually.
AC_SUBST(config_flags)


dnl Generate a list of system calls that we could generate.  We later use
dnl a syscall template .S file to produce a .o file for each syscall.  This
dnl is a bit better for porting and development purposes than having every
dnl syscall listed for every target system; this way we only need the
dnl template, and special hanlding for only those syscalls that can't be
dnl handled by the templates.
AC_CHECK_HEADERS(syscall.h sys/syscall.h)

PTHREADS_CHECK_SYSCALLS(open write read creat close fcntl lseek dup2 dup pipe
	fchmod fchown execve fstat link unlink chdir chown chmod stat rename
	select
	dnl - directory reading
	getdents readdir getdirentries
	dnl - variants of `wait' syscalls
	wait4 wait3 waitpid
	dnl - BSD socket calls
	socket bind connect accept listen getsockopt setsockopt
	dnl - Linux version uses one syscall
	socketcall

	sigprocmask
	exit
	readv writev send sendto sendmsg recv recvfrom recvmsg
	getpeername getsockname
	shutdown
	getpgrp fork)

AC_SUBST(available_syscalls)
AC_SUBST(missing_syscalls)


dnl Now we deal with symlinks &c.

test -d include || mkdir include || \
	AC_MSG_ERROR(Can't create include directory.)

test -d tests || mkdir tests || \
	AC_MSG_ERROR(Can't create tests directory.)

links="include/pthread/machdep.h include/pthread/posix.h \
	machdep.c syscall.S"
targets="../machdep/engine-$name.h ../machdep/posix-$sysincludes.h \
	../machdep/engine-$name.c ../machdep/syscall-$name.S"

# Both these targets are optional.  (Autoconf-generated configure scripts
# will require the existence of link targets, so check before adding them
# to the list.)
if test x$sysincludes != xNONE ; then
  links="$links include/sys"
  targets="$targets ../machdep/$sysincludes"
fi

if test -r $srcdir/../machdep/unistd-$name.h ; then
  links="$links include/unistd.h"
  targets="$targets ../machdep/unistd-$name.h"
fi

if test x$syscall = xNONE ; then
  syscall=$name
fi
syscall_file=../machdep/syscall-template-$syscall.S
if test -r $srcdir/$syscall_file ; then
  links="$links syscall-template.S"
  targets="$targets $syscall_file"
  HAVE_SYSCALL_TEMPLATE=yes
else
  # This really isn't a fatal problem.  In fact, it's expected, initially,
  # for some targets.  This is just to persuade people to fix the targets
  # they deal with to provide some sort of template.
  #
  # Eventually this file probably will be required...
  AC_MSG_WARN(No syscall template file found.)
  HAVE_SYSCALL_TEMPLATE=no
fi
AC_SUBST(HAVE_SYSCALL_TEMPLATE)

dnl Now tell it to make the links.
dnl (The links are actually made at the end.)
AC_LINK_FILES($targets, $links)

if test x$makefile_frag != x ; then
  makefile_frag=${srcdir}/$makefile_frag
else
  makefile_frag=/dev/null
fi
dnl Drop in file indicated by $makefile_frag on *the line after* the
dnl magic @makefile_frag@ sequence.
AC_SUBST_FILE(makefile_frag)


dnl Generate output files...
AC_CONFIG_HEADER(config.h)

dnl This must be last.
AC_OUTPUT(config.flags GNUmakefile Makefile)
