dnl $Id: configure.in,v 1.29 2005/05/23 12:01:45 leeh Exp $

AC_INIT

AC_CONFIG_AUX_DIR(autoconf)
AC_CONFIG_HEADER(include/setup.h)
AC_PREFIX_DEFAULT(/usr/local/ratbox-services)

AC_PROG_MAKE_SET
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_YACC
AC_PROG_LEX
AC_PROG_RANLIB
AC_EXEEXT

AC_PATH_PROG(RM, rm)
AC_PATH_PROG(CP, cp)
AC_PATH_PROG(MV, mv)
AC_PATH_PROG(AR, ar)

AC_CHECK_FUNC(crypt,, AC_CHECK_LIB(crypt, crypt,,))

AC_HEADER_STDC
AC_CHECK_HEADERS(sys/time.h stdlib.h stdarg.h string.h strings.h unistd.h errno.h getopt.h crypt.h)

AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_CHECK_LIB(socket, socket)
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_FUNCS(select strlcpy strlcat gethostbyname mmap getaddrinfo)

AC_ARG_WITH(logdir,
[ --with-logdir=DIR         logfiles in DIR [localstatedir/log] ],
[ logdir=$with_logdir ],[ logdir=$localstatedir'/log' ])

AC_ARG_WITH(rundir,
[ --with-rundir=DIR         pidfile in DIR [localstatedir/run] ],
[ rundir=$with_rundir ],[ rundir=$localstatedir'/run' ])

AC_ARG_WITH(helpdir,
[ --with-helpdir=DIR        helpfiles in DIR [datadir/ratbox-services/help] ],
[ helpdir=$with_helpdir ],[ helpdir=$datadir'/ratbox-services/help' ])

AC_SUBST(logdir)
AC_SUBST(rundir)
AC_SUBST(helpdir)

AC_MSG_CHECKING(Compile with warning flags)
AC_ARG_ENABLE(warnings,
[ --enable-warnings         Enable warning flags to compiler
 --disable-warnings        Disable warning flags to compiler],
[ cf_enable_warnings=$enableval ],[ cf_enable_warnings=no ])

if test "X$cf_enable_warnings" = "Xyes"; then
	CFLAGS="$CFLAGS -O0 -Wall -Werror -Wunused -Wshadow -Wmissing-declarations -Wwrite-strings"
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(Nickname Service)
AC_ARG_ENABLE(nickserv,
[ --enable-nickserv         Enable Nickname Service
 --disable-nickserv        Disable Nickname Service],
[ cf_enable_nickserv=$enableval ],[ cf_enable_nickserv=yes ])

if test "X$cf_enable_nickserv" = "Xyes"; then
	S_NICKSERV="s_nickserv.c"
	AC_DEFINE(ENABLE_NICKSERV, 1, Nickname Service)
	AC_MSG_RESULT(yes)
else
	S_NICKSERV=""
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(Channel Service)
AC_ARG_ENABLE(chanserv,
[ --enable-chanserv         Enable Channel Service
 --disable-chanserv        Disable Channel Service],
[ cf_enable_chanserv=$enableval ],[ cf_enable_chanserv=yes ])

if test "X$cf_enable_chanserv" = "Xyes"; then
	S_CHANSERV="s_chanserv.c"
	AC_DEFINE(ENABLE_CHANSERV, 1, Channel Service)
	AC_MSG_RESULT(yes)
else
	S_CHANSERV=""
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(User Service)
AC_ARG_ENABLE(userserv,
[ --enable-userserv         Enable User Service
 --disable-userserv        Disable User Service],
[ cf_enable_userserv=$enableval ],[ cf_enable_userserv=yes ])

if test "X$cf_enable_chanserv" = "Xyes"; then
	cf_enable_userserv=yes
fi

if test "X$cf_enable_nickserv" = "Xyes"; then
	cf_enable_userserv=yes
fi

if test "X$cf_enable_userserv" = "Xyes"; then
	S_USERSERV="s_userserv.c"
	AC_DEFINE(ENABLE_USERSERV, 1, User Service)
	AC_MSG_RESULT(yes)
else
	S_USERSERV=""
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(Oper Service)
AC_ARG_ENABLE(operserv,
[ --enable-operserv         Enable Oper Service
 --disable-operserv        Disable Oper Service],
[ cf_enable_operserv=$enableval ],[  cf_enable_operserv=yes ])

if test "X$cf_enable_operserv" = "Xyes"; then
 	S_OPERSERV="s_operserv.c"
	AC_DEFINE(ENABLE_OPERSERV, 1, Oper Service)
	AC_MSG_RESULT(yes)
else
	S_OPERSERV=""
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(List Service)
AC_ARG_ENABLE(alis,
[ --enable-alis             Enable ALIS (List Service)
 --disable-alis            Disable (List Service)],
[ cf_enable_alis=$enableval ],[ cf_enable_alis=yes ])

if test "X$cf_enable_alis" = "Xyes"; then
	S_ALIS="s_alis.c"
	AC_DEFINE(ENABLE_ALIS, 1, List Service)
	AC_MSG_RESULT(yes)
else
	S_ALIS=""
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(Jupe Service)
AC_ARG_ENABLE(jupeserv,
[ --enable-jupeserv         Enable Jupe Service
 --disable-jupeserv        Disable Jupe Service],
[ cf_enable_jupe=$enableval ],[ cf_enable_jupe=yes ])

if test "X$cf_enable_jupe" = "Xyes"; then
	S_JUPESERV="s_jupeserv.c"
	AC_DEFINE(ENABLE_JUPESERV, 1, Jupe Service)
	AC_MSG_RESULT(yes)
else
	S_JUPESERV=""
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(Operbot Service)
AC_ARG_ENABLE(operbot,
[ --enable-operbot          Enable Operbot Service
 --disable-operbot         Disable Operbot Service],
 [ cf_enable_operbot=$enableval ],[ cf_enable_operbot=yes ])

if test "X$cf_enable_operbot" = "Xyes"; then
	S_OPERBOT="s_operbot.c"
	AC_DEFINE(ENABLE_OPERBOT, 1, Operbot Service)
	AC_MSG_RESULT(yes)
else
	S_OPERBOT=""
	AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(Global Service)
AC_ARG_ENABLE(global,
[ --enable-global           Enable global messaging service
 --disable-global          Disable global messaging service],
 [ cf_enable_global=$enableval ],[ cf_enable_global=yes ])

if test  "X$cf_enable_global" =  "Xyes"; then
	S_GLOBAL="s_global.c"
	AC_DEFINE(ENABLE_GLOBAL, 1, Global Messaging Service)
	AC_MSG_RESULT(yes)
else
	S_GLOBAL=""
	AC_MSG_RESULT(no)
fi

ac_cv_found_sqlite_inc="no"
ac_cv_found_sqlite_lib="no"

SQLITE_LIB="-lsqlite"

dnl Try to find SQLite.
SQLITE_INCLUDES=""

AC_ARG_WITH([sqlitebuild],
[ --with-sqlitebuild        Forces packaged sqlite to be used for build],
[ac_cv_sqlitebuild=$withval], [ac_cv_sqlitebuild=no])

AC_ARG_WITH([sqlite],
[ --with-sqlite=DIR         Path to search for sqlite], 
[SQLITE_DIR=$withval], [SQLITE_DIR="/usr"])

AC_CHECK_HEADER(sqlite.h, 
[
	ac_cv_found_sqlite_inc="yes"
], [
	AC_MSG_CHECKING(for sqlite.h in given path)
	if test -f "$SQLITE_DIR/include/sqlite.h"; then
		SQLITE_INCLUDES="-I $SQLITE_DIR/include/"
		AC_MSG_RESULT(yes)
		ac_cv_found_sqlite_inc="yes"
	else
		AC_MSG_RESULT(no)
	fi
], [])

AC_CHECK_LIB([sqlite], [main], 
[
	ac_cv_found_sqlite_lib="yes"
], [
	AC_MSG_CHECKING(for main in -lsqlite in given path)

	if test -e "$SQLITE_DIR/lib/libsqlite.so"; then
		LDFLAGS="$LDFLAGS -L$SQLITE_DIR/lib/"
		LIBS="$LIBS -lsqlite"
		AC_MSG_RESULT(yes)
		ac_cv_found_sqlite_lib="yes"
	else
		AC_MSG_RESULT(no)
	fi
])

AC_MSG_CHECKING(sqlite was found)

if test "X$ac_cv_found_sqlite_inc" != "Xyes" ||  
   test "X$ac_cv_found_sqlite_lib" != "Xyes" || 
   test "X$ac_cv_sqlitebuild" = "Xyes"; then
	AC_CONFIG_SUBDIRS(sqlite)
	AC_DEFINE(SQLITE_BUILD, 1, Build packaged sqlite)
	SQLITE_INCLUDES="-I ../sqlite/"
	SQLITE_SUBDIR="sqlite"
	SQLITE_LIB="../sqlite/libsqlite.a"

	if test "X$ac_cv_sqlitebuild" = "Xyes"; then
		AC_MSG_RESULT("yes.. building anyway")
	else
		AC_MSG_RESULT(no)
	fi
else
	AC_MSG_RESULT(yes)
	SQLITE_SUBDIR=""
	dnl dont go into sqlite/ if we dont need to.
	no_recursion="yes"
fi

AC_ARG_WITH([nicklen],
[ --with-nicklen=LENGTH     Sets nick length to LENGTH],
[ac_cv_nicklen=$withval], [ac_cv_nicklen="9"])

AC_MSG_RESULT("checking nick length... $ac_cv_nicklen")
AC_DEFINE_UNQUOTED(NICKLEN, (${ac_cv_nicklen}+1), Nick Length)

AC_ARG_WITH([topiclen],
[ --with-topiclen=LENGTH    Sets topic length to LENGTH],
[ac_cv_topiclen=$withval], [ac_cv_topiclen="160"])

AC_MSG_RESULT("checking topic length... $ac_cv_topiclen")
AC_DEFINE_UNQUOTED(TOPICLEN, (${ac_cv_topiclen}+1), Topic Length)

AC_SUBST(SQLITE_INCLUDES)
AC_SUBST(SQLITE_SUBDIR)
AC_SUBST(SQLITE_LIB)

AC_SUBST(S_NICKSERV)
AC_SUBST(S_CHANSERV)
AC_SUBST(S_USERSERV)
AC_SUBST(S_OPERSERV)
AC_SUBST(S_ALIS)
AC_SUBST(S_JUPESERV)
AC_SUBST(S_OPERBOT)
AC_SUBST(S_GLOBAL)

AC_OUTPUT(			\
	Makefile		\
        help/Makefile   	\
	src/Makefile		\
)

echo
echo Compiling ratbox-services
echo Installing into: $prefix
echo
echo Nickname Services ........ $cf_enable_nickserv
echo User Services ............ $cf_enable_userserv
echo Channel Services ......... $cf_enable_chanserv
echo Oper Services ............ $cf_enable_operserv
echo Jupe Services ............ $cf_enable_jupe
echo Oper invite/op bot ....... $cf_enable_operbot
echo Global Message Service ... $cf_enable_global
echo List Service ............. $cf_enable_alis
echo
echo Nick Length .............. $ac_cv_nicklen
echo Topic Length ............. $ac_cv_topiclen
echo
