#
# configure.in - autoconf source for xaric. 
# Copyright (c) 2000 Rex Feany (laeos@xaric.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# @(#)configure.in 1.8
#

dnl ---------------------------------------------------------------
dnl autoconf prolog
dnl ---------------------------------------------------------------

AC_INIT(source/version.c)dnl
AC_PREREQ(2.13)dnl

AC_HEADLINE(dnl
Xaric, IRC Client, dnl
XARIC_VERSION, source/version.c, dnl
[Copyright (c) 1999-2000 Rex G. Feany <laeos@laeos.net>])

AC_CONFIG_HEADER(include/config.h)dnl
AC_PREFIX_DEFAULT(/usr/local)

CFLAGS="$CFLAGS -D_GNU_SOURCE"

dnl ----------------------------------------------------------------
dnl build tools
dnl ----------------------------------------------------------------

AC_MSG_PART(Build Tools)

AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_SET_MAKE
AC_SANITY_CHECK
AC_ISC_POSIX
AC_AIX

echo ""
AC_PLATFORM(PLATFORM)
echo ""

AC_CHECK_DEBUGGING
AC_CHECK_PROFILING
AC_CHECK_OPTIMIZE

AC_MSG_CHECKING(if we should turn on assert checking)
AC_ARG_ENABLE(assert,dnl
[  --disable-assert          enable runtime assertion checking (default=yes)],
ASSERT="$enableval",
if test ".$ASSERT" = .; then
    ASSERT=yes
fi
)dnl

if test ".$ASSERT" = .yes; then
       AC_MSG_RESULT(yeah)
else
       AC_DEFINE(NDEBUG)
       AC_MSG_RESULT(nope)
fi

AC_MSG_CHECKING(if we should link against efence)
AC_ARG_ENABLE(efence,dnl
[  --enable-efence	     link against electric fence library (default=no)],
EFENCE="$enableval",
if test ".$EFENCE" = .; then
    EFENCE=
fi
)dnl

if test ".$EFENCE" = .yes; then
       AC_MSG_RESULT(yeah)
else
       AC_MSG_RESULT(nope)
fi

dnl ----------------------------------------------------------
dnl Stuff we need
dnl ----------------------------------------------------------

AC_MSG_PART(Mandatory stuff)

dnl # check for standard headers
AC_HEADER_STDC
AC_FUNC_ALLOCA

AC_HAVE_HEADERS(dnl
	stdio.h stdlib.h stdarg.h string.h signal.h unistd.h fcntl.h dnl
	ctype.h errno.h sys/types.h sys/time.h sys/wait.h sys/socket.h dnl
	dirent.h pwd.h sys/stat.h assert.h getopt.h netinet/in.h net/if.h dnl
	netdb.h sys/ioctl.h)

AC_HEADER_TIME

AC_CHECK_FUNC(select, ,
  AC_CHECK_LIB(socket, select, LIBS="$LIBS -lsocket",
    AC_CHECK_LIB(nsl, select, LIBS="$LIBS -lnsl",
      AC_CHECK_LIB(inet, select, LIBS="$LIBS -linet",
        AC_CHECK_LIB(cposix, select, LIBS="$LIBS -lcposix",
          AC_CHECK_LIB(net, select, LIBS="$LIBS -lnet",
            AC_MSG_WARN(i can not find select.  you might need to help me)))))))

AC_CHECK_FUNCTIONS(sigaction memset strncasecmp snprintf vsnprintf getopt_long)

AC_BEGIN_DECISION([mandatory system headers and functions])
AC_IFALLYES(dnl
    header:stdio.h header:stdlib.h header:stdarg.h header:string.h dnl
    header:signal.h header:unistd.h header:fcntl.h header:errno.h dnl
    header:ctype.h header:sys/types.h header:sys/time.h header:sys/wait.h dnl
    header:sys/socket.h header:sys/stat.h header:sys/ioctl.h dnl
    header:netdb.h header:net/if.h header:netinet/in.h dnl
    header:dirent.h func:vsnprintf func:snprintf dnl
    func:select func:sigaction func:memset func:strncasecmp func:snprintf,dnl
AC_DECIDE(fine, [all fine]))
AC_END_DECISION

dnl ----------------------------------------------------------
dnl System services
dnl ----------------------------------------------------------
AC_MSG_PART(System Services)

AC_MSG_CHECKING(for IPv4 support)
AC_CACHE_VAL(ac_cv_ipv4,
	AC_TRY_RUN([
#	include <sys/types.h>
#	include <sys/socket.h>
#	include <netinet/in.h>
	/* Make sure the definitions for AF_INET and struct sockaddr_in
	 * are defined, and that we can actually create an IPv4 TCP socket.
	 */
	main()
	{
		int fd;
		struct sockaddr_in foo;
		fd = socket(AF_INET, SOCK_STREAM, 0);
		exit(fd >= 0 ? 0 : 1);
	}],
	ac_cv_ipv4=yes,
	ac_cv_ipv4=no,
	ac_cv_ipv4=no))
AC_MSG_RESULT($ac_cv_ipv4)
if test $ac_cv_ipv4 = yes ; then
	AC_DEFINE(IPV4)
fi

dnl Let's see if the system really supports IPv6.
dnl
AC_MSG_CHECKING(for IPv6 support)
AC_CACHE_VAL(ac_cv_ipv6,
	AC_TRY_RUN([
#	include <sys/types.h>
#	include <sys/socket.h>
#	include <netinet/in.h>
	/* Make sure the definitions for AF_INET6 and struct sockaddr_in6
	 * are defined, and that we can actually create an IPv6 TCP socket.
	 */
	main()
	{
		int fd;
		struct sockaddr_in6 foo;
		fd = socket(AF_INET6, SOCK_STREAM, 0);
		exit(fd >= 0 ? 0 : 1);
	}],
	ac_cv_ipv6=yes,
	ac_cv_ipv6=no,
	ac_cv_ipv6=no))
AC_MSG_RESULT($ac_cv_ipv6)
if test $ac_cv_ipv6 = yes ; then
	AC_DEFINE(IPV6)
fi

AC_CHECK_LIB(pthread, pthread_create)
if test "$ac_cv_lib_pthread_pthread_create" = yes ; then
   CFLAGS="$CFLAGS -D_REENTRANT"
else
   AC_CHECK_LIB(pthreads, pthread_create)
   if test "$ac_cv_lib_pthreads_pthread_create" = yes ; then
      CFLAGS="$CFLAGS -D_REENTRANT"
   fi
fi

dnl ----------------------------------------------------------
dnl Data Types
dnl ----------------------------------------------------------

AC_MSG_PART(Types)

AC_TYPE_SIGNAL
AC_TYPE_MODE_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_UID_T

AC_C_CONST
AC_C_INLINE

AC_CACHE_CHECK(for addrinfo{}, ac_cv_have_addrinfo_struct,
	AC_EGREP_HEADER(addrinfo, netdb.h,
		ac_cv_have_addrinfo_struct=yes,
		ac_cv_have_addrinfo_struct=no))
if test $ac_cv_have_addrinfo_struct = yes ; then
	AC_DEFINE(HAVE_ADDRINFO_STRUCT)
fi

AC_CACHE_CHECK(if sockaddr{} has sa_len member, ac_cv_sockaddr_has_sa_len,
	AC_TRY_COMPILE([
#		include <sys/types.h>
#		include <sys/socket.h>],
		[unsigned int i = sizeof(((struct sockaddr *)0)->sa_len)],
	ac_cv_sockaddr_has_sa_len=yes,
	ac_cv_sockaddr_has_sa_len=no))
if test $ac_cv_sockaddr_has_sa_len = yes ; then
	AC_DEFINE(HAVE_SOCKADDR_SA_LEN)
fi

AC_CACHE_CHECK(for socklen_t, ac_cv_socklen_t,
	AC_TRY_COMPILE([
#		include <sys/types.h>
#		include <sys/socket.h>],
		[socklen_t foo = 0],
	ac_cv_socklen_t=yes,
	ac_cv_socklen_t=no))
if test $ac_cv_socklen_t = no ; then
	AC_DEFINE_UNQUOTED(socklen_t, unsigned int)
fi


dnl ugh can we do this better?
AC_CHECK_SIZEOF(unsigned int, 4)
if test "$ac_cv_sizeof_unsigned_int" = 4; then
	AC_CHECK_TYPE(u_int32_t, unsigned int)
else
	AC_CHECK_SIZEOF(unsigned long, 4)
	if test "$ac_cv_sizeof_unsigned_long" = 4; then
		AC_CHECK_TYPE(u_int32_t, unsigned long)
	else
		AC_ERROR(i need a 32 bit type!)
	fi
fi

dnl ----------------------------------------------------------
dnl Extra libraries
dnl ----------------------------------------------------------

AC_MSG_PART(Libraries)

AC_MSG_CHECKING(for svr4's /usr/ccs/lib)
if test -d /usr/ccs/lib; then
	LIBS="-L/usr/ccs/lib $LIBS"
	AC_MSG_RESULT(found it!)
else
	AC_MSG_RESULT(no)
fi

if test -z "$libsocket"; then
	AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket",)
fi

dnl this is for isc.  need the nsl_s library as well.
if test -z "$libinet"; then
	AC_CHECK_LIB(inet, socket, libnsl=1; LIBS="$LIBS -linet -lnsl_s",)
fi

if test -z "$libnsl"; then
	AC_CHECK_LIB(nsl, gethostname, LIBS="$LIBS -lnsl",)
fi

if test ".$EFENCE" = .yes; then
	AC_CHECK_LIB(efence, malloc, LIBS="$LIBS -lefence", )
fi

AC_CHECK_LIB(sun, getpwnam, LIBS="$LIBS -lsun",)
AC_CHECK_LIB(dgc, inet_addr, LIBS="$LIBS -ldgc",)
AC_CHECK_LIB(resolv, gethostbyname, LIBS="$LIBS -lresolv",)
AC_CHECK_LIB(gnumalloc, malloc, LIBS="$LIBS -lgnumalloc",
AC_CHECK_LIB(bsdmalloc, malloc, LIBS="$LIBS -lbsdmalloc",))

dnl not yet!
dnl AC_PATH_REP(0.13)
AC_SUBST(REP_CFLAGS)
AC_SUBST(REP_LIBS)

dnl ----------------------------------------------------------
dnl termcap or curses
dnl ----------------------------------------------------------

if test x$hpux = x1 -o x$aix = x1
then
	LIBS="-lcurses $LIBS"
	AC_DEFINE(USING_CURSES)
else
  olibs="$LIBS"
  AC_CHECK_LIB(termcap, tgetent, LIBS="-ltermcap $olibs",
    AC_CHECK_LIB(termlib, tgetent, LIBS="-ltermlib $olibs",
      AC_CHECK_LIB(curses, tgetent, LIBS="-lcurses $olibs",
        AC_CHECK_LIB(xtermcap, tgetent, LIBS="-lxtermcap $olibs",
          AC_ERROR(i can not find tgetent.  you need to help me)))))
fi


dnl ----------------------------------------------------------
dnl things we need that we have replacements for
dnl ----------------------------------------------------------

AC_MSG_PART(Replaceable)
needaddr=0

AC_CHECK_FUNC(inet_ntop,, EXTRA_SRC="$EXTRA_SRC inet_ntop.c")
AC_CHECK_FUNC(inet_pton,, EXTRA_SRC="$EXTRA_SRC inet_pton.c")
AC_CHECK_FUNC(strtoul,,   EXTRA_SRC="$EXTRA_SRC strtoul.c")
AC_CHECK_FUNC(setenv,,    EXTRA_SRC="$EXTRA_SRC setenv.c")

AC_CHECK_FUNC(freeaddrinfo, AC_DEFINE(HAVE_FREEADDRINFO), EXTRA_SRC="$EXTRA_SRC freeaddrinfo.c")
AC_CHECK_FUNC(gai_strerror, AC_DEFINE(HAVE_GAI_STRERROR), EXTRA_SRC="$EXTRA_SRC gai_strerror.c")
AC_CHECK_FUNC(getaddrinfo, AC_DEFINE(HAVE_GETADDRINFO),  needaddr=1)
AC_CHECK_FUNC(getnameinfo, AC_DEFINE(HAVE_GETNAMEINFO),  EXTRA_SRC="$EXTRA_SRC getnameinfo.c gn_ipv46.c")

if test "$needaddr" -eq "1" ; then
	EXTRA_SRC="$EXTRA_SRC ga_aistruct.c ga_echeck.c"
	EXTRA_SRC="$EXTRA_SRC ga_nsearch.c ga_serv.c" 
	EXTRA_SRC="$EXTRA_SRC getaddrinfo.c ga_port.c"
fi

AC_SUBST(EXTRA_SRC)

dnl ----------------------------------------------------------
dnl Random header files and functions
dnl ----------------------------------------------------------

AC_MSG_PART(other stuff)

AC_CHECK_HEADERS(sys/ptem.h sys/select.h)
AC_CHECK_HEADERS(sys/syslimits.h sys/twg_config.h sys/un.h)
AC_CHECK_HEADERS(sys/termios.h sys/ttycom.h termcap.h)

AC_CHECK_FUNC(getpass, AC_DEFINE(HAVE_GETPASS),)
AC_CHECK_FUNC(fpathconf, AC_DEFINE(HAVE_FPATHCONF),)
AC_CHECK_FUNC(gettimeofday, AC_DEFINE(HAVE_GETTIMEOFDAY),)
AC_CHECK_FUNC(sysconf, AC_DEFINE(HAVE_SYSCONF),)
AC_CHECK_FUNC(uname, AC_DEFINE(HAVE_UNAME),)
AC_CHECK_FUNC(killpg, AC_DEFINE(HAVE_KILLPG),)
AC_CHECK_FUNC(putenv,, AC_DEFINE(NEED_PUTENV))
AC_CHECK_FUNC(getenv,, AC_DEFINE(NEED_GETENV))



AC_MSG_CHECKING(for struct linger)
AC_EGREP_HEADER([struct( |	)*linger], sys/socket.h, AC_MSG_RESULT(yes), AC_DEFINE(NO_STRUCT_LINGER) AC_MSG_RESULT(no, ugh.))

dnl ----------------------------------------------------------
dnl sun_len in <sys/un.h>?
dnl ----------------------------------------------------------

AC_MSG_CHECKING(for sun_len in <sys/un.h>)
AC_EGREP_CPP([sun_len], 
[
#include <sys/un.h>
], AC_DEFINE(HAVE_SUN_LEN) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))


dnl ----------------------------------------------------------
dnl
dnl svr3's getpgrp() didnt take arguments.  BSD's did.
dnl POSIX said that there wasnt any usefulness to giving arguments
dnl to getpgrp(), so that was forbidden.  So svr4 made a new syscall
dnl getpgid() to fill in this functionality gap (grrrr).  BSD still
dnl doesnt have any way to do it, even though they were the ones that
dnl originally wanted to have it around, but svr3 insisted not so. (grrr)
dnl

AC_CHECK_FUNC(getpgid, AC_DEFINE(HAVE_GETPGID),)
AC_CHECK_FUNC(getpgrp, AC_FUNC_GETPGRP,)


dnl ----------------------------------------------------------
dnl Check for sys_siglist declaration
dnl ----------------------------------------------------------

AC_MSG_CHECKING(for sys_siglist declaration)
AC_TRY_RUN([
#include <sys/types.h>
#include <signal.h>
main()
{
	char *s = sys_siglist[0];
	exit(0);
}
],
  AC_MSG_RESULT(yes) 
  sys_siglist="1"
  AC_DEFINE(SYS_SIGLIST_DECLARED), 
  AC_MSG_RESULT(no), :)

dnl ----------------------------------------------------------
dnl Generate signal list
dnl This is all stolen from perl-4.036's Configure.  larry is god.
dnl ----------------------------------------------------------

if test x$sys_siglist != x1
then
echo "generating a list of signal names..."
set X `cat /usr/include/signal.h /usr/include/sys/signal.h /usr/include/linux/signal.h 2>&1 | sed 's/^#[	]*/#/' | awk '
$1 ~ /^#define$/ && $2 ~ /^SIG[A-Z0-9]*$/ && $3 ~ /^[1-9][0-9]*$/ {
  [sig[$3] = substr($2,4,20)]
  if (max < $3 && $3 < 60) {
	max = $3
  }
}

END {
  for (i=1; i<=max; i++) {
    [if (sig[i] == "")]
      printf "%d", i
    else
      [printf "%s", sig[i]]
    if (i < max)
      printf " "
  }
  printf "\n"
}
'`
shift
case $# in
0)
  if test -f /bin/csh; then
     set X `/bin/csh -cf 'kill -l'`
     shift
     case $* in
     0) 
       set HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM
       ;;
    esac
  fi
  ;;
esac

sig_name="ZERO $*"
if test -f source/sig.inc; then
  /bin/rm -f source/sig.inc
fi
[echo $sig_name | sed -e 's/$/"};/' -e 's/ /", "/g' -e 's/^/char *sys_siglist[] = { "/' >> source/sig.inc]
fi

dnl ----------------------------------------------------------
dnl check for sys_errlist declaration
dnl ----------------------------------------------------------

AC_CHECK_FUNC(strerror, AC_DEFINE(HAVE_STRERROR),
AC_MSG_CHECKING(for sys_errlist declaration)
AC_TRY_RUN([
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
main()
{
  char *s = sys_errlist[0];
  exit(0);
}
],
  AC_MSG_RESULT(yes)
  AC_DEFINE(SYS_ERRLIST_DECLARED),
  AC_MSG_RESULT(no), :))

dnl ----------------------------------------------------------
dnl All done!
dnl ----------------------------------------------------------

AC_MSG_PART(Finished)

AC_OUTPUT(Makerules, echo timestamp > include/stamp-h)

echo ""
echo "There we go! That didn't hurt, did it? You might want to"
echo "look in include/config.h and see if there is anything"
echo "you want to change, else you can just run make here..."
echo "Good luck..."
echo ""

