AC_INIT()
AC_CONFIG_HEADER(config.h)
AC_PROG_CC
AC_AIX
AC_PROG_INSTALL
AC_PREFIX_DEFAULT(/usr)

LIBS=""

AC_CHECK_LIB(c, getusershell, result=yes, result=no)
if test "$result" = "yes"; then
  AC_DEFINE(HAVE_GETUSERSHELL)
fi

AC_CHECK_LIB(c, inet_ntoa, result=yes, result=no)
if test "$result" = "no"; then
AC_CHECK_LIB(nsl, inet_ntoa, result=yes, result=no)
if test "$result" = "yes"; then
  LIBS="$LIBS -lnsl"
fi
fi
AC_CHECK_LIB(socket, socket, result=yes, result=no)
if test "$result" = "yes"; then
  LIBS="$LIBS -lsocket"
else
  AC_CHECK_LIB(socket, connect, result=yes, result=no)
  if test "$result" = "yes"; then
    LIBS="$LIBS -lsocket"
  fi
fi

AC_CHECK_LIB(crypt, crypt, result=yes, result=no)
if test "$result" = "yes"; then
  LIBS="$LIBS -lcrypt"
fi

AC_HEADER_DIRENT
AC_CHECK_HEADERS(paths.h)
AC_CHECK_HEADERS(netinet/in.h)
AC_CHECK_HEADERS(sys/time.h)
AC_CHECK_HEADERS(time.h)
AC_CHECK_HEADERS(sys/types.h)
AC_CHECK_HEADERS(sys/stat.h)
AC_CHECK_HEADERS(sys/socket.h)
AC_CHECK_HEADERS(asm/socket.h)
AC_CHECK_HEADERS(wait.h)
AC_CHECK_HEADERS(sys/wait.h)
AC_CHECK_HEADERS(shadow.h)
AC_CHECK_HEADERS(arpa/inet.h)
AC_CHECK_HEADERS(syslog.h)
AC_CHECK_HEADERS(crypt.h)
AC_CHECK_HEADERS(utmp.h)
AC_CHECK_HEADERS(sys/sendfile.h)



define(ARG_ENABLE_BFTPD, [
AC_MSG_CHECKING(whether to enable $1)
AC_ARG_ENABLE($1, [$2], result=yes, result=no)
if test "$result" = "yes"; then
  $3
fi
AC_MSG_RESULT($result)
])

ARG_ENABLE_BFTPD(pam, [  --enable-pam            Enable PAM (pluggable authentication modules) support], AC_DEFINE(WANT_PAM))
if test "$result" = "yes"; then
  LIBS="$LIBS -lpam -ldl"
  AC_CHECK_HEADERS(security/pam_appl.h, result=yes, result=no)
  if test "$result" = "no"; then
    echo "Error: PAM header files not found. Install PAM or don't compile"
    echo "       bftpd with PAM support."
    exit 1
  fi
fi

ARG_ENABLE_BFTPD(libz, [  --enable-libz           Link against libz so that gzip on the fly is supported],true)
if test "$result" = "yes"; then
    AC_CHECK_HEADERS(zlib.h, result=yes, result=no)
    if test "$result" = "yes"; then
        AC_DEFINE(WANT_GZIP)
        LIBS="$LIBS -lz"
    else
        echo "Error: zlib header file not found. Install it or don't compile"
        echo "       bftpd with zlib support."
        exit 1
    fi
fi

AC_ARG_ENABLE(pax, [  --enable-pax            Specify path to PAX sources], result=$enableval, result="")
if test ! "$result" = ""; then
    if test -d $result; then
        DIRPAX=$result
        AC_SUBST(DIRPAX)
        PAX='$(DIRPAX)/append.o $(DIRPAX)/buffer.o $(DIRPAX)/cpio.o $(DIRPAX)/create.o $(DIRPAX)/extract.o $(DIRPAX)/fileio.o $(DIRPAX)/link.o $(DIRPAX)/list.o $(DIRPAX)/mem.o $(DIRPAX)/namelist.o $(DIRPAX)/names.o $(DIRPAX)/pass.o $(DIRPAX)/pathname.o $(DIRPAX)/pax.o $(DIRPAX)/port.o $(DIRPAX)/regexp.o $(DIRPAX)/replace.o $(DIRPAX)/tar.o $(DIRPAX)/ttyio.o $(DIRPAX)/warn.o $(DIRPAX)/wildmat.o'
        AC_SUBST(PAX)
    	AC_DEFINE(WANT_TAR)
    else
        echo "Error: The PAX path you specified was not found."
        exit 1
    fi
fi

ARG_ENABLE_BFTPD(debug, [  --enable-debug          Enable debugging],true)
if test "$result" = "yes"; then
	DEBUG="-DDEBUG=1"
	AC_SUBST(DEBUG)
fi

AC_OUTPUT(Makefile)
