dnl Process this file with autoconf to produce a configure script.
AC_PREREQ(2.0)
AC_INIT(ncurses/lib_initscr.c)

NC_VERSION
NC_SYSTYPE

###	Default install-location
AC_PREFIX_DEFAULT(/usr/local)

###	Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_GCC_TRADITIONAL
dnl PROBLEM ON NEXT MACHINES:  g++ exists but is called cc++.  AC_PROG_CXX doesn't check
dnl                            for that name,
AC_PROG_CXX
AC_PROG_AWK
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_RANLIB

NC_MAKEFLAGS

### Options to allow the user to specify the set of libraries which are used.
### Use "--without-normal --with-shared" to allow the default model to be
### shared, for example.
nc_list_models=""
AC_ARG_WITH(shared,
	[  --with-shared           generate shared-libraries],
	[test "$withval" = "yes" && nc_list_models="$nc_list_models shared"])
AC_ARG_WITH(normal,
	[  --with-normal           generate normal-libraries (default)],
	[test "$withval" = "yes" && nc_list_models="$nc_list_models normal"],
	[nc_list_models="$nc_list_models normal"])
AC_ARG_WITH(debug,
	[  --with-debug            generate debug-libraries (default)],
	[test "$withval" = "yes" && nc_list_models="$nc_list_models debug"],
	[nc_list_models="$nc_list_models debug"])
AC_ARG_WITH(profile,
	[  --with-profile          generate profile-libraries],
	[test "$withval" = "yes" && nc_list_models="$nc_list_models profile"])

AC_MSG_CHECKING(for specified models)
test -z "$nc_list_models" && nc_list_models=normal
AC_MSG_RESULT($nc_list_models)

### Use the first model as the default, and save its suffix for use in building
### up test-applications.
DFT_LWR_MODEL=`echo $nc_list_models | $AWK '{print $1}'`
NC_UPPERCASE($DFT_LWR_MODEL,DFT_UPR_MODEL)dnl
NC_LIB_SUFFIX($DFT_LWR_MODEL,DFT_DEP_SUFFIX)dnl
NC_LIB_TYPE($DFT_LWR_MODEL,DFT_ARG_SUFFIX)dnl
NC_OBJ_SUBDIR($DFT_LWR_MODEL,DFT_OBJ_SUBDIR)dnl
AC_SUBST(DFT_OBJ_SUBDIR)dnl	the default object-directory ("obj")
AC_SUBST(DFT_LWR_MODEL)dnl	the default model ("normal")
AC_SUBST(DFT_UPR_MODEL)dnl	the default model ("NORMAL")
AC_SUBST(DFT_DEP_SUFFIX)dnl	the corresponding library-suffix (".a")
AC_SUBST(DFT_ARG_SUFFIX)dnl	the string to append to "-lncurses" ("")
AC_SUBST(nc_list_models)dnl	the complete list of models ("normal debug")

LIB_NAME=ncurses
AC_SUBST(LIB_NAME)

LIB_PREFIX=../lib/lib
AC_SUBST(LIB_PREFIX)

dnl Not all ports of gcc support the -g option
CC_G_OPT='-g'
test -n "$GCC" && test "$ac_cv_prog_gcc_g" != yes && CC_G_OPT=''
AC_SUBST(CC_G_OPT)

CXX_G_OPT='-g'
test -n "$GXX" && test "$ac_cv_prog_gxx_g" != yes && CXX_G_OPT=''
AC_SUBST(CXX_G_OPT)

case $DFT_LWR_MODEL in
normal)  LD_MODEL=''   ;;
debug)   LD_MODEL=$CC_G_OPT ;;
profile) LD_MODEL='-pg';;
shared)  LD_MODEL=''   ;;
esac
AC_SUBST(LD_MODEL)dnl		the type of link (e.g., -g or -pg)
NC_SHARED_OPTS

if test "$CC_SHARED_OPTS" = "unknown"; then
	for model in $nc_list_models; do
		if test "$model" = "shared"; then
			echo '** Shared libraries are not supported in this version'
			exit 1
		fi
	done
fi

###	use option --with-datadir=DIR to specify an alternate data-directory
AC_ARG_WITH(datadir,
	[  --with-datadir=DIR      specify alternate terminfo data-directory],
	[datadir=$withval],
	[datadir='$(exec_prefix)/lib/terminfo'])
AC_SUBST(datadir)

###	use option --with-includedir=DIR to specify an alternate includedir
AC_ARG_WITH(includedir,
	[  --with-includedir=DIR   specify alternate include-directory],
	[includedir=$withval],
	[includedir='$(exec_prefix)/include'])
AC_SUBST(includedir)

###	use option --disable-overwrite to leave out the link to -lcurses
AC_ARG_ENABLE(overwrite,
	[  --disable-overwrite     leave out the link to -lcurses],
	[with_overwrite=$enableval
	 test "$with_overwrite" = no && \
	 test -z "$with_includedir" && \
	 	includedir='$(exec_prefix)/include/ncurses'
	],
	[with_overwrite=yes])

###	use option --disable-termcap to leave out the termcap fallback support
AC_ARG_ENABLE(termcap,
	[  --disable-termcap       leave out the termcap fallback support],
	[with_termcap=$enableval],
	[with_termcap=yes])
test "$with_termcap" != "yes" && AC_DEFINE(PURE_TERMINFO)

###	use option --enable-getcap to use a hacked getcap for reading termcaps
AC_ARG_ENABLE(getcap,
	[  --enable-getcap         fast termcap load, no xrefs to terminfo],
	[with_getcap=$enableval],
	[with_getcap=no])
test "$with_getcap" = "yes" && AC_DEFINE(USE_GETCAP)

###   use option --enable-symlinks to make tic use symlinks, not hard links
AC_ARG_ENABLE(symlinks,
	[  --enable-symlinks       make tic use symbolic links not hard links],
	[with_symlinks=$enableval]
	[AC_DEFINE(USE_SYMLINKS)])

###   use option --enable-bsdpad to have tputs process BSD-style prefix padding
AC_ARG_ENABLE(bsdpad,
	[  --enable-bsdpad         recognize BSD-style prefix padding],
	[with_bsdpad=$enableval]
	[AC_DEFINE(BSD_TPUTS)])

###	use option --disable-echo to suppress full display compiling commands
AC_ARG_ENABLE(echo,
	[  --enable-echo           display \"compiling\" commands (default)],
	[with_echo=$enableval],
	[with_echo=yes])
if test "$with_echo" = yes; then
	ECHO_LINK=
else
	ECHO_LINK='@ echo linking $@ ... ;'
fi
AC_SUBST(ECHO_LINK)

###	use option --with-warnings to turn on all gcc warnings
AC_ARG_ENABLE(warnings,
	[  --enable-warnings       turn on GCC compiler warnings],
	[with_warnings=$enableval])
if test -n "$GCC"
then
	# gcc 2.5.8 doesn't recognize some other useful options that could
	# be configured if you have gcc 2.6.3:
	# -Wbad-function-cast
	# -Wmissing-declarations
	if test -n "$with_warnings"
	then
		CFLAGS="$CFLAGS -Wall -Winline -Wshadow -Wstrict-prototypes -Wmissing-prototypes"
	fi
fi

###	use option --with-assertions to turn on generation of assertion code
AC_ARG_ENABLE(assertions,
	[  --enable-assertions     turn on generation of assertion code],
	[with_assertions=$enableval])
if test -n "$GCC"
then
	# gcc 2.5.8 doesn't recognize some other useful options that could
	# be configured if you have gcc 2.6.3:
	# -Wbad-function-cast
	# -Wmissing-declarations
	if test -z "$with_assertions"
	then
		CPPFLAGS="$CPPFLAGS -DNDEBUG"
	fi
fi

###	Checks for libraries.
AC_CHECK_LIB(bsd, gettimeofday)dnl CLIX: bzero, select, gettimeofday

###	Checks for header files.
AC_STDC_HEADERS
AC_HEADER_DIRENT

dnl These are some other potentially nonportable headers.
dnl PROBLEMS ON RECENT NS3.2+ MACHINES:  regex.h and termios.h exist but don't define posix
dnl                                      functions.  For now user will have to remove those
dnl                                      defines in config.status or config.h

AC_CHECK_HEADERS( \
fcntl.h \
getopt.h \
libc.h \
limits.h \
regex.h \
regexp.h \
sys/ioctl.h \
sys/param.h \
sys/select.h \
sys/time.h \
termios.h \
ttyent.h \
unistd.h \
values.h \
)

# check for SCO.  this is a hack, and should be split into individual
# features -- the sys/time.h and select.h conflict, and the need for
# ptem.h due to winsize struct.
AC_CHECKING([for SCO by checking on /usr/bin/scosh])
AC_PROGRAM_CHECK(COULD_BE_SCO, [scosh], maybe, maybenot)
if test "$COULD_BE_SCO" = "maybe"
then
	AC_DEFINE(SYSTEM_LOOKS_LIKE_SCO)
fi

###	checks for compiler characteristics
AC_LANG_C
AC_C_CONST
AC_C_INLINE
test $ac_cv_c_inline != no && AC_DEFINE(CC_HAS_INLINE_FUNCS)

###	Checks for external-data
NC_ERRNO
NC_SYS_ERRLIST
NC_LINK_DATAONLY
NC_SPEED_TYPE

###	Checks for library functions.
AC_CHECK_FUNCS( \
gettimeofday \
getttynam \
memccpy \
setbuffer \
setvbuf \
sigaction \
strdup \
usleep \
vsscanf \
)

if test $ac_cv_func_sigaction = yes; then
AC_MSG_CHECKING(whether sigaction needs _POSIX_SOURCE)
AC_TRY_COMPILE([#include <sys/types.h>
#include <signal.h>], [struct sigaction act;],
  sigact_bad=no, sigact_bad=yes AC_DEFINE(SVR4_ACTION))
fi
AC_MSG_RESULT($sigact_bad)

NC_TYPE_SIGACTION
NC_TIOCGWINSZ

dnl FIXME checks we don't do (but neither does the old Configure script):
dnl -DGOOD_SELECT

dnl We'll do our own -g libraries
NC_STRIP_G_OPT(CFLAGS)
NC_STRIP_G_OPT(CXXFLAGS)

dnl Check for C++ compiler characteristics (and ensure that it's there!)
AC_CHECK_PROG(CXX_EXISTS, $CXX, yes, no)
if test "$ac_cv_prog_CXX_EXISTS" = yes; then
	AC_LANG_CPLUSPLUS
	NC_CXX_LIBRARY
	NC_BOOL_DECL
	NC_BOOL_SIZE
else
	nc_cxx_library=no
	AC_CACHE_VAL(nc_cv_builtin_bool,[nc_cv_builtin_bool=0])
	AC_CACHE_VAL(nc_cv_sizeof_bool,[nc_cv_sizeof_bool=int])
fi

### It's not possible to appease gcc 2.6.3's conversion-warnings if we're
### using a 'char' for bools.  gcc 2.7.0's conversion-warnings are broken too
### badly to consider using for development purposes, but 2.5.8 is okay.
if test -n "$with_warnings"; then
	if test -n "$GCC"; then
		case "`gcc --version`" in
		2.6.3)
			if test "$nc_cv_sizeof_bool" != "char"; then
				CFLAGS="$CFLAGS -Wconversion"
			fi
			;;
		2.5*)
			CFLAGS="$CFLAGS -Wconversion"
			;;
		esac
	fi
fi

### Construct the list of include-directories to be generated
NC_INCLUDE_DIRS

### Construct the list of subdirectories for which we'll customize makefiles
### with the appropriate compile-rules.
NC_SRC_MODULES(ncurses progs panel menu form)
NC_DIRS_TO_MAKE

################################################################################
changequote({,})dnl
AC_OUTPUT(include/config_h $SUB_MAKEFILES Makefile,{

NC_LIB_RULES

### Special editing.  We generate config.h directly to allow all filenames
### to be MSDOS-compatible, as well as to make the list of definitions be
### dynamically determined by the configuration script -- a consideration when
### doing type-clean development testing.

echo creating include/config.h
rm -f include/config.h
echo "/* generated by configure-script
 * On host: $SYS_NAME
 */
#ifndef NC_CONFIG_H
#define NC_CONFIG_H" >include/config.h
sed	-e '/^ -D/!d' \
	-e 's/ -D/\
#define /g' \
	-e 's/\(#define [A-Za-z_][A-Za-z0-9_]*\)=/\1	/g' \
	-e 's/\\//g' \
	include/config_h | sort >>include/config.h
echo "
	/* The C compiler may not treat these properly, but C++ has to */
#ifdef __cplusplus
#undef const
#undef inline
#endif

#endif /* NC_CONFIG_H */" >> include/config.h
echo removing include/config_h
rm include/config_h
},{
### Special initialization commands, used to pass information from the
### configuration-run into config.status

SYS_NAME="`(uname -a || hostname) 2>/dev/null | sed 1q`"
if test -z "\$SYS_NAME"; then SYS_NAME=unknown;fi

AWK=$AWK
DFT_LWR_MODEL="$DFT_LWR_MODEL"
NC_LIST_MODELS="$nc_list_models"
WITH_ECHO="$with_echo"
WITH_OVERWRITE="$with_overwrite"
SRC_SUBDIRS="$SRC_SUBDIRS"
nc_cxx_library="$nc_cxx_library"
nc_cv_systype="$nc_cv_systype"
nc_cv_rel_version="$nc_cv_rel_version"
nc_cv_abi_version="$nc_cv_abi_version"
nc_cv_do_symlinks="$nc_cv_do_symlinks"
nc_cv_rm_so_locs="$nc_cv_rm_so_locs"

})
changequote([,])dnl
