dnl Process this file with autoconf to produce a configure script.
dnl

AC_INIT(src/version.c)

dnl Output a header file.
dnl
AC_CONFIG_HEADER(src/include/config.h:autoconf/header.in)

dnl Set default prefix to location of `mconv', if present.
dnl
AC_PREFIX_PROGRAM(mconv)

dnl Set directory to check for Configure scripts in.
dnl
AC_CONFIG_AUX_DIR(autoconf/scripts)

VERSION=`cat $srcdir/doc/VERSION`
AC_SUBST(VERSION)
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")

dnl Checks for programs.
dnl
GROUP=bbs
AC_ARG_ENABLE(debug, [  --enable-debug          compile in debugging support],
  if test "$enable_debug" = "yes"; then
    CFLAGS="-g -Wall"
    AC_DEFINE(DEBUG)
  fi
)
AC_ARG_WITH(group,   [  --with-group=GROUP      use group GROUP [bbs]],
  if test -n "$withval"; then GROUP="$withval"; fi
)
AC_ARG_WITH(editor,  [  --with-editor=PROG      use alternate editor PROG [joe]],
  if test -n "$withval"; then JOE="$withval"; fi
)
AC_ARG_WITH(gzip,    [  --with-gzip=PROG        location of "gzip" [gzip]],
  if test -n "$withval"; then GZIPLOC="$withval"; fi
)
AC_ARG_WITH(bzip2,   [  --with-bzip2=PROG       location of "bzip2" [bzip2]],
  if test -n "$withval"; then BZIP2LOC="$withval"; fi
)
AC_ARG_WITH(telnet,  [  --with-telnet=PROG      location of "telnet" [telnet]],
  if test -n "$withval"; then TELNET="$withval"; fi
)
AC_ARG_WITH(sendmail,[  --with-sendmail=PROG    location of "sendmail" [sendmail]],
  if test -n "$withval"; then SENDMAIL="$withval"; fi
)
AC_ARG_WITH(qmanim,  [  --with-qmanim=PROG      location of "qmanim" [qmanim]],
  if test -n "$withval"; then QMANIM="$withval"; fi
)
CFLAGS=${CFLAGS-"-O2 -Wall -s"}
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_CHECK_PROG(DO_GZIP, gzip, gzip -f9, touch)
AC_PATH_PROG(JOE, joe, /usr/bin/joe)
AC_PATH_PROG(GZIPLOC, gzip, /bin/gzip)
AC_PATH_PROG(BZIP2LOC, bzip2, /usr/bin/bzip2)
AC_PATH_PROG(TELNET, telnet, /bin/telnet)
AC_PATH_PROG(SENDMAIL, sendmail, /usr/lib/sendmail)
AC_PATH_PROG(QMANIM, qmanim, qmanim)
AC_DEFINE_UNQUOTED(JOE_PATH, "$JOE")
AC_DEFINE_UNQUOTED(GZIP_PATH, "$GZIPLOC")
AC_DEFINE_UNQUOTED(BZIP2_PATH, "$BZIP2LOC")
AC_DEFINE_UNQUOTED(TELNET_PATH, "$TELNET")
AC_DEFINE_UNQUOTED(SENDMAIL_PATH, "$SENDMAIL")
AC_DEFINE_UNQUOTED(QMANIM_PATH, "$QMANIM")
AC_DEFINE_UNQUOTED(CF_PREFIX, "$prefix")
AC_SUBST(GROUP)

AC_TYPE_GETGROUPS
AC_TYPE_SIZE_T
AC_CHECK_FUNCS(getgroups)
AC_HEADER_DIRENT

dnl Checks for libraries.
AC_CHECK_LIB(termcap, tgetent, [],
 [AC_CHECK_LIB(ncurses, tgetent, [LIBS="-ltermcap -lncurses"
AC_DEFINE(HAVE_LIBTERMCAP)])])

dnl Checks for library functions.
AC_CHECK_FUNCS(getopt_long, needgetopt="", needgetopt="src/getopt.o")
AC_SUBST(needgetopt)
AC_CHECK_FUNCS(getutent, needgetut="", needgetut="src/getutent.o")
AC_SUBST(needgetut)
AC_CHECK_FUNCS(crypt, CRYPTLIB="", CRYPTLIB="-lcrypt")
AC_SUBST(CRYPTLIB)

dnl Check to see if getutline() needs a const argument.
AC_MSG_CHECKING([whether getutline() needs "const"])
AC_TRY_COMPILE([#include <utmp.h>],[
  return 0;
}
struct utmp * getutline (const struct utmp *ut) {
], [AC_DEFINE(NEED_UTMP_CONST)
AC_MSG_RESULT(yes)],AC_MSG_RESULT(no))

AC_CHECK_FUNCS(scandir, AC_DEFINE(HAVE_SCANDIR))
AC_CHECK_FUNCS(setenv, [
  AC_DEFINE(HAVE_SETENV)
  needsetenv=""
], needsetenv="src/setenv.o")
AC_SUBST(needsetenv)

dnl Fudging for separate build directories.
subdirs="doc extra extra/help"
for i in `find $srcdir/src -type d -print | sed s,$srcdir/,,`; do
  subdirs="$subdirs $i"
done

mk_segments="autoconf/Makefile.in"
for i in vars package filelist unreal modules rules link depend; do
  mk_segments="$mk_segments:autoconf/make/$i.mk"
done

AC_OUTPUT(Makefile:$mk_segments src/.dummy:doc/NEWS mconv.spec:autoconf/mconv.spec.in,
  rm -f src/.dummy
  for i in $subdirs; do
    test -d $i || mkdir $i
  done
, subdirs="$subdirs")

dnl EOF
