dnl Process this file with autoconf to produce a configure script.
AC_INIT(rexxcurl.c)
AC_PREFIX_DEFAULT(/usr)

. $srcdir/rexxcurl.ver
VER=\"$VER\"
VER_DOT=\"$VER_DOT\"
VER_DATE=\"$VER_DATE\"
AC_SUBST(VER)
AC_SUBST(VER_DOT)
AC_SUBST(VER_DATE)

dnl allow --with-dist to build a distribution only
AC_ARG_WITH(dist,
	[  --with-dist             specify this just to build a distribution],
	[with_dist=$withval],
	[with_dist=no],
)
if test "$with_dist" != no; then
#	SET_MAKE=""
#	AC_SUBST(SET_MAKE)
#	AC_OUTPUT(Makefile)
#	exit
	mymakefile="Makefile"
else


dnl ---------------------- check for C compiler -----------------
dnl Checks for one of the C compilers below - change if yours not there.
CC_LIST="gcc acc cc c89"
MH_PROG_CC

AC_CONFIG_HEADER(config.h)
AC_ISC_POSIX

dnl Checks for system first
AC_CANONICAL_SYSTEM
AC_PROG_GCC_TRADITIONAL
AC_PROG_RANLIB

mymakefile="Makefile"
on_qnx=no
on_beos=no
case "$target" in
	*hp-hpux*)
		SYS_DEFS="-D_HPUX_SOURCE"
		;;
	*ibm-aix*)
		SYS_DEFS="-D_ALL_SOURCE"
		mymakefile="Makefile.aix"
		;;
	*dec-osf4*)
		AC_DEFINE(HAVE_BROKEN_SYS_ACL_H)
		;;
	*dec-osf*)
		;;
	*sequent-dynix*)
		;;
	*solaris*)
		;;
	sparc*sunos*)
		;;
	*linux*)
		;;
	*nto-qnx*)
		;;
	*qnx*)
		on_qnx=yes
		SYS_DEFS="-Q"
		;;
	*beos*)
		on_beos=yes
		;;
esac
AC_SUBST(SYS_DEFS)

dnl ---------------------- check for REXX argument -----------------
dnl allow --with-rexx for Rexx support
AC_ARG_WITH(rexx,
	[  --with-rexx=int         specify Rexx interpeter (rexxtrans|regina|rexximc|oorexx|objrexx|unirexx|rexx6000)],
	[with_rexx=$withval],
	[with_rexx=no],
)
dnl allow --with-rexxincdir to specify where REXX header file is
AC_ARG_WITH(rexxincdir,
	[  --with-rexxincdir=DIR   specify where REXX header file is],
	[with_rexxincdir=$withval],
	[with_rexxincdir=no],
)
dnl allow --with-rexxlibdir to specify where REXX library file is
AC_ARG_WITH(rexxlibdir,
	[  --with-rexxlibdir=DIR   specify where REXX library file is],
	[with_rexxlibdir=$withval],
	[with_rexxlibdir=no],
)
CHECK_REXX

dnl allow --with-curlincdir to specify where cURL header files are
AC_ARG_WITH(curlincdir,
	[  --with-curlincdir=DIR   specify where cURL header files are],
	[with_curlincdir=$withval],
	[with_curlincdir=no],
)
if test "$with_curlincdir" = no; then
	CURLINCDIR=""
else
	CURLINCDIR="-I$with_curlincdir"
fi
AC_SUBST(CURLINCDIR)

dnl allow --with-curllibdir to specify where cURL library files are
AC_ARG_WITH(curllibdir,
	[  --with-curllibdir=DIR   specify where cURL library files are],
	[with_curllibdir=$withval],
	[with_curllibdir=no],
)
if test "$with_curllibdir" = no; then
	CURLLIBDIR=""
else
	CURLLIBDIR="-L$with_curllibdir"
fi
AC_SUBST(CURLLIBDIR)

dnl -- MUST have curl-config!!!!
AC_CHECK_PROG(mh_curl, curl-config, yes, no )
if test "$mh_curl" = no; then
	AC_MSG_ERROR(Could not find curl-config in PATH. Is cURL installed ?; cannot configure)
fi
CURLCFLAGS=`curl-config --cflags`
AC_SUBST(CURLCFLAGS)
CURLLIBS=`curl-config --libs`
AC_SUBST(CURLLIBS)

dnl ---------------------- check packages -----------------------
dnl
dnl Check for general libraries.
dnl
MH_LIBS_TO_CHECK="m s socket nls"
MH_CHECK_LIB(${MH_LIBS_TO_CHECK})

AC_SUBST(MH_EXTRA_LIBS)

dnl --------------------- Check for other programs. ---------------
AC_PROG_LN_S
AC_PROG_MAKE_SET

dnl -------------------- Checks for header files. -----------------
AC_HEADER_STDC
AC_CHECK_HEADERS( \
	ctype.h \
	assert.h \
	sys/types.h \
	sys/mode.h \
	sys/stat.h \
	sys/file.h \
	sys/wait.h \
	unistd.h \
	errno.h \
	stdlib.h \
	memory.h \
	string.h \
	stdarg.h \
	time.h \
	dlfcn.h \
	dl.h \
	)

MH_HAVE_PROTO

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
MH_CHECK_CC_O
MH_C_LONG_LONG

dnl Checks for library functions.
AC_FUNC_MEMCMP
AC_CHECK_FUNCS(getopt strerror)
if test "$ac_cv_func_getopt_func" = no; then
	GETOPT="getopt.o"
	AC_SUBST(GETOPT)
fi

dnl --------------- allow --enable-debug to compile in debug mode --------
AC_ARG_ENABLE(debug,
	[  --enable-debug          turn on debugging],
	[with_debug=$enableval],
	[with_debug=no],
)
cflags_g="`echo $CFLAGS | grep -c '\-g'`"
cflags_O="`echo $CFLAGS | grep -c '\-O'`"

DEBUG=""
DEBUGGING=""
if test "$with_debug" = yes; then
	DEBUG="-g"
	DEBUGGING="-DDEBUG"
	if test "$cflags_g" = "0"; then
		CFLAGS="${CFLAGS} -g"
	fi
	if test "$cflags_O" != "0"; then
		CFLAGS="`echo ${CFLAGS} | sed -e s/-O.//`"
	fi
else
	if test "$cflags_O" = "0"; then
		CFLAGS="${CFLAGS} -O"
	fi
	if test "$cflags_g" != "0"; then
		CFLAGS="`echo ${CFLAGS} | sed -e s/-g//`"
	fi
fi
AC_SUBST(DEBUG)
AC_SUBST(DEBUGGING)

dnl --------------- allow --enable-purify to enable linking with Purify
AC_ARG_ENABLE(purify,
	[  --enable-purify         link with Purify (TM)],
	[with_purify=$enableval],
	[with_purify=no],
)
if test "$with_purify" = yes; then
	PURIFY="purify"
else
	PURIFY=""
fi
AC_SUBST(PURIFY)


dnl --------------- allow --withdlfcnincdir to enable compiling with dlfcn.h under AIX 3/4.1
AC_ARG_WITH(dlfcnincdir,
	[  --with-dlfcnincdir      location of dlfcn.h (AIX only)],
	[with_dlfcnincdir=$withval],
	[with_dlfcnincdir=no],
)
if test "$with_dlfcnincdir" = no; then
	DLFCNINCDIR=""
else
	DLFCNINCDIR="-I$with_dlfcnincdir"
	HAVE_DLFCN_H="1"
	AC_DEFINE(HAVE_DLFCN_H)
fi
AC_SUBST(DLFCNINCDIR)

dnl --------------- allow --withdlfcnlibdir to enable compiling with dlfcn.h under AIX 3/4.1
AC_ARG_WITH(dlfcnlibdir,
	[  --with-dlfcnlibdir      location of libdl.a (AIX only)],
	[with_dlfcnlibdir=$withval],
	[with_dlfcnlibdir=no],
)
if test "$with_dlfcnlibdir" = no; then
	DLFCNLIBDIR=""
else
	DLFCNLIBDIR="-L$with_dlfcnlibdir -ldl"
fi
AC_SUBST(DLFCNLIBDIR)

MH_CHECK_RPM(/usr/src/rpm /usr/src/redhat)

dnl --------------- Force definition of pre-compiler macro UNIX ----------
AC_DEFINE(UNIX)

dnl --------------- Check how to make shared libraries -------------------
MH_SHARED_LIBRARY(rexxcurl)

thisdir="`pwd`"
AC_SUBST(thisdir)
fi

AC_OUTPUT($mymakefile rexxcurl-config)

case "$target" in
	*ibm-aix*)
		mv Makefile.aix Makefile
		AC_MSG_RESULT(renaming Makefile.aix to Makefile)
		;;
	*)
		;;
esac

echo
echo "Rexx/CURL has now been configured with the following options:"
echo
echo "                 Rexx Interpreter: $REXX_INT"
if test "$with_rexxincdir" != no ; then
echo "                  rexx headers in: $with_rexxincdir"
else
echo "                  rexx headers in: $mh_rexx_inc_dir"
fi
if test "$with_rexxlibdir" != no ; then
echo "                rexx libraries in: $with_rexxlibdir"
else
echo "                rexx libraries in: $mh_rexx_lib_dir"
fi
if test "$with_curlincdir" != no ; then
echo "                cURL headers in: $with_curlincdir"
fi
if test "$with_curllibdir" != no ; then
echo "              cURL libraries in: $with_curllibdir"
fi
myopts=""
if test "$with_debug" = yes ; then
	myopts="$myopts DEBUG"
fi
if test "$with_purify" = yes ; then
	myopts="$myopts PURIFY"
fi
if test "$with_hpux_portable" = yes ; then
	myopts="$myopts HPUX-PORTABLE"
fi
if test "$exec_prefix" = '${prefix}' ; then
	macro_dir="$prefix"
else
	macro_dir="$exec_prefix"
fi
echo "                    Build options: $myopts"
echo "   Rexx/CURL will be installed in: $macro_dir/lib"
echo
