dnl This file writen by Devrim SERAL for tac_plus daemon

AC_INIT()

dnl Checks for programs.
AC_PROG_MAKE_SET
AC_PROG_CC

dnl Check for Host information
dnl AC_CANONICAL_HOST()
AC_CANONICAL_SYSTEM()

case $host_os in 
	*linux-gnu)
	OS="-DLINUX -DGLIBC"
	;;
	*solaris*)
	OS="-DSOLARIS"
	;;
	*freebsd*)
	OS="-DFREEBSD"
	;;
	*hpux)
	OS="-DHPUX"
	;;
	*aix)
	OS="-DAIX"
	;;
	*)
	;;
esac
	
dnl Checks for libraries.
dnl Replace `main' with a function in -lnsl:
AC_CHECK_LIB(nsl, main)
dnl Replace `main' with a function in -log:
AC_CHECK_LIB(og, main)
dnl Replace `main' with a function in -lsocket:
AC_CHECK_LIB(socket, main)
dnl Check for Crypt function
AC_CHECK_LIB(crypt, crypt)
AC_CHECK_LIB(c,printf)


dnl Devrim Added 
AC_CONFIG_HEADER(config.h)

dnl For PAM support
AC_MSG_CHECKING(for PAM support:)
echo
AC_ARG_WITH(pam,
	[  --with-pam		With PAM Support   ],,)
if test "x$with_pam" = "xyes";then
	AC_CHECK_LIB(dl, dlopen)
	AC_CHECK_LIB(pam, pam_start)
	DEFINES="-DUSE_PAM $DEFINES";
        AC_MSG_RESULT(Pam support... yes)
else
        AC_MSG_RESULT(Pam support... no)
fi

dnl For LDAP Support
AC_MSG_CHECKING(for LDAP support)
echo
AC_ARG_WITH(ldap,
        [  --with-ldap		With LDAP Support   ],,)

if test "x$with_ldap" = "xyes";then
	AC_CHECK_LIB(ldap, main)
	AC_CHECK_LIB(lber, main)
   	AC_CHECK_LIB(ldap, ldap_simple_bind_s)
   	AC_CHECK_LIB(ldap, ldap_init)
 
   DEFINES="-DUSE_LDAP $DEFINES"
        AC_MSG_RESULT(LDAP support... yes)
else
        AC_MSG_RESULT(LDAP support... no)
fi

dnl For DB Support
AC_MSG_CHECKING(for DB support)
echo 
AC_ARG_WITH(db,
        [  --with-db		For DB Support   ],,)
if test "x$with_db" = "xyes";then
	DB="$DB -DDB -DDB_NULL"	
	AC_MSG_RESULT(DB support... yes)
else
	AC_MSG_RESULT(DB support... no)
fi

dnl For MySQL support
if test "x$with_db" = "xyes";then

echo "Check for MySQL support:"

AC_ARG_WITH(mysql,
	[  --with-mysql		With MySQL Support   ],,)

AC_ARG_WITH(mysql-include-dir,
        [  --with-mysql-include-dir=PREFIX  Mysql include path [default=/usr/include/mysql]],
	MYSQL_INCLUDE_DIR=$withval,
	MYSQL_INCLUDE_DIR=/usr/include/mysql
)

AC_ARG_WITH(mysql-lib-dir,
        [  --with-mysql-lib-dir=PREFIX  Mysql library path [default=/usr/lib/mysql]],
	MYSQL_LIB_DIR=$withval,
	MYSQL_LIB_DIR=/usr/lib/mysql
)


if test "x$with_mysql" = "xyes";then
	
	LDFLAGS="-L$MYSQL_LIB_DIR $LDFLAGS"
        LDFLAGS="-I$MYSQL_INCLUDE_DIR $LDFLAGS"
        AC_CHECK_LIB(mysqlclient, mysql_init,
                        LIBS="-lmysqlclient -lm $LIBS",
                        AC_MSG_ERROR(*** couldn't find libmysqlclient),
                        -lm)

	DB="$DB -DDB_MYSQL";
        AC_MSG_RESULT(Mysql support... yes)
else
        AC_MSG_RESULT(Mysql support... no)
fi

fi

dnl For PgSQL support
if test "x$with_db" = "xyes";then

echo "Check for PgSQL support:"

AC_ARG_WITH(pgsql,
	[  --with-pgsql		With PgSQL Support   ],,)

AC_ARG_WITH(pgsql-include-dir,
        [  --with-pgsql-include-dir=PREFIX  PgSQL include path [default=/usr/include/pgsql]],
	PGSQL_INCLUDE_DIR=$withval,
	PGSQL_INCLUDE_DIR=/usr/include/pgsql
)

AC_ARG_WITH(pgsql-lib-dir,
        [  --with-pgsql-lib-dir=PREFIX  PgSQL library path [default=/usr/lib/pgsql]],
	PGSQL_LIB_DIR=$withval,
	PGSQL_LIB=/usr/lib/pgsql
)


if test "x$with_pgsql" = "xyes";then
	
	LDFLAGS="-I$PGSQL_INCLUDE_DIR $LDFLAGS"
        LDFLAGS="-L$PGSQL_LIB_DIR $LDFLAGS"
        AC_CHECK_LIB(pq,PQconnectdb ,
                        LIBS="-lpq $LIBS",
                        AC_MSG_ERROR(*** couldn't find libpq))

	DB="$DB -DDB_PGSQL";
        AC_MSG_RESULT(Pgsql support... yes)
else
        AC_MSG_RESULT(Pgsql support... no)
fi

fi

dnl Tacuid & tac guid 

AC_ARG_WITH(tacuid,
		[  --with-tacuid=ID	If you want to run tac_plus other than root user (no default value) ],,)
AC_ARG_WITH(tacgid,
 		[  --with-tacgid=GID	If you want to run tac_plus other than root group(no default value) ],,)


if (test "x$with_tacuid" != "x" && test "x$with_tacgid" != "x" && test "x$with_tacuid" != "xyes" && test "x$with_tacgid" != "xyes");then

 	DEFINES="-DTACPLUS_USERID=$with_tacuid -DTACPLUS_GROUPID=$with_tacgid $DEFINES";	
	AC_MSG_RESULT(tacacs+ work with given user and group id) 
fi

AC_MSG_CHECKING(whether to enable the maxsess feature)
AC_ARG_ENABLE(maxsess,
              [  --enable-maxsess	Enable maxsess feature ],
[
if test "$enableval" = "yes";then
 	DEFINES="-DMAXSESS $DEFINES";
	AC_MSG_RESULT(yes)
else 
	AC_MSG_RESULT(no)
fi
],
[
	AC_MSG_RESULT(no)
])

dnl Enable tacacs.pid file directory 

AC_ARG_WITH(tacplus_pid,
        [  --with-tacplus_pid=PREFIX  Tac_plus pid file location [default=/var/run] ],
        PIDFILE="-DTACPLUS_PIDFILE=\\\"$withval/tac_plus.pid\\\"",
        PIDFILE="-DTACPLUS_PIDFILE=\\\"/var/run/tac_plus.pid\\\""
)

dnl For libwrap check
AC_MSG_CHECKING(whether to enable the libwrap feture)

AC_ARG_WITH(libwrap,
[  --with-libwrap[=PATH]   Compile in libwrap (tcp_wrappers) support.],
[ case "$withval" in
  no)
    AC_MSG_RESULT(no)
    ;;
  yes)
    AC_MSG_RESULT(yes)
    AC_CHECK_LIB(wrap, request_init, [
        LIBS="-lwrap $LIBS"
        DEFINES="-DTCPWRAPPER $DEFINES"])
    ;;
  *)
    AC_MSG_RESULT(yes)
    if test -d "$withval"; then
        LDFLAGS="-L$withval $LDFLAGS"
    	DEFINES="-DTCPWRAPPER $DEFINES"
    fi
    AC_TRY_LINK([ int allow_severity; int deny_severity; ],
                [ hosts_access(); ],
                [],
                [ AC_MSG_ERROR(Could not find the $withval library.  You must first install tcp_wrappers.) ])
    ;;
  esac ],
  AC_MSG_RESULT(no)
)

dnl Maxsess finger check
if test "x$enable_maxsess" = "xyes"; then

AC_ARG_ENABLE(finger,
        [  --enable-finger	Enable finger at maxsess check ],
	AC_MSG_RESULT("Enable whether finger at maxsess check: yes");
	DEFINES="-DENABLE_FINGER $DEFINES",
	AC_MSG_RESULT("Enable whether finger at maxsess check: no");
)
fi

dnl insert defines to Makefile 
AC_SUBST(DEFINES)
AC_SUBST(PIDFILE)
AC_SUBST(DB)
AC_SUBST(OS)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h malloc.h strings.h sys/file.h sys/ioctl.h sys/time.h syslog.h unistd.h)
AC_CHECK_HEADERS(shadow.h,[
		if test -f /etc/shadow ; then
		  AC_DEFINE(SHADOW_PASSWORDS)		
		fi 
		],)
dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_HEADER_TIME

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_SETPGRP
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_FUNC_WAIT3
AC_CHECK_FUNCS(regcomp select socket strcspn strdup strtol)

AC_OUTPUT(Makefile,echo timestamp > stamp-h)
