#!/bin/sh

# this is GNU-style autoconf script

if [ "X$PREFIX" = "X" ]; then
	PREFIX='/usr/local'
fi
CFLAGS="-I/usr/local/include $CFLAGS"
LIBS="-L/usr/local/lib $LIBS $LDFLAGS $LFLAGS"

show_help=0
while [ "X$1" != "X" ]; do
	case $1 in
	*-help*)
		show_help=1
		;;
	*-prefix)
		shift
		PREFIX=$1
		;;
	*-prefix=*)
		PREFIX=`echo $1 | sed -e 's/^.*-prefix=//'`
		;;
	CC=*)
		CC=`echo $1 | sed -e 's/^CC=//'`
		;;
	CFLAGS=*)
		CFLAGS="$CFLAGS "`echo $1 | sed -e 's/^CFLAGS=//'`
		;;
	LIBS=*)
		LIBS="$LIBS "`echo $1 | sed -e 's/^LIBS=//'`
		;;
	LDFLAGS=*)
		LIBS="$LIBS "`echo $1 | sed -e 's/^LDFLAGS=//'`
		;;
	LFLAGS=*)
		LIBS="$LIBS "`echo $1 | sed -e 's/^LFLAGS=//'`
		;;
	*)
		show_help=2
		;;
	esac
	shift
done

if [ "X$show_help" != "X0" ]; then
	echo "Usage: configure [options]"
	echo "  --help                    print this message"
	echo "  --prefix=foo              specify install prefix"
	if [ "X$show_help" = "X2" ]; then
		exit 1
	fi
	exit 0
fi


echo "Configuring LDAPDNS"

# detect compiler
if [ "X$CC" = "X" ]; then
	CC=cc
fi

echo -n "Testing C compiler: "
cat > test_1.c <<__test_1_eof
main(int argc, char *argv[]) {return 0; }
__test_1_eof

if $CC -c test_1.c -o test_1.o >/dev/null 2>&1; then
	if $CC -o test_1 test_1.o >/dev/null 2>&1; then
		rm -f test_1 test_1.o test_1.c
		if $CC -v 2>&1 | grep gcc >/dev/null 2>&1; then
			if ! $CC -pthreads 2>&1 | grep 'unrecognized option' >/dev/null 2>&1; then
				echo "gcc-pthreads ($CC)"
				CFLAGS="$CFLAGS -pthreads"
			elif ! $CC -pthread 2>&1 | grep 'unrecognized option' >/dev/null 2>&1; then
				echo "gcc-pthread ($CC)"
				CFLAGS="$CFLAGS -pthread"
			else
				echo "gcc ($CC)"
			fi
		else
			echo "$CC"
		fi
	else
		echo "failed!"
		rm -f test_1.o test_1.c
		echo Cannot cannot create executables";" check your linker or \$CC
		exit 1
	fi
else
	echo "failed!"
	rm -f test_1.c
	echo Cannot launch C compiler, or it cannot create objects";" check \$CC
	exit 1
fi

echo -n "Checking for syslog support: "
cat > test_1.c <<__test_1_eof
#include <syslog.h>
int main(int argc, char *argv[]) { openlog("test", 0, 0); return 0; }
__test_1_eof
if $CC -o test_1 -c test_1.c >/dev/null 2>&1; then
	echo "ok"
	rm -f test_1 test_1.c
	CFLAGS="$CFLAGS -DHAVE_SYSLOG"
else
	echo "disabled"
	rm -f test_1.c
fi

echo -n "Checking for memzero: "
cat > test_1.c <<__test_1_eof
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) { memzero(0,0); return 0; }
__test_1_eof
if $CC -o test_1 test_1.c >/dev/null 2>&1; then
	echo "ok"
	rm -f test_1 test_1.c
	CFLAGS="$CFLAGS -DHAVE_MEMZERO"
else
	rm -f test_1.c
	cat > test_1.c <<__test_1_eof
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) { bzero(0,0); return 0; }
__test_1_eof
	if $CC -o test_1 test_1.c >/dev/null 2>&1; then
		echo "bzero"
		rm -f test_1 test_1.c
		CFLAGS="$CFLAGS -DHAVE_BZERO"
	else
		rm -f test_1.c
		cat > test_1.c <<__test_1_eof
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) { memset(0,0,0); return 0; }
__test_1_eof
		if $CC -o test_1 test_1.c >/dev/null 2>&1; then
			echo "memset"
			rm -f test_1 test_1.c
			CFLAGS="$CFLAGS -DHAVE_MEMSET"
		else
			rm -f test_1.c
			echo "emulated"
		fi
	fi
fi

echo -n "Checking for memcpy: "
cat > test_1.c <<__test_1_eof
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) { memcpy(0,0,0); return 0; }
__test_1_eof
if $CC -o test_1 test_1.c >/dev/null 2>&1; then
	echo "ok"
	rm -f test_1 test_1.c
	CFLAGS="$CFLAGS -DHAVE_MEMCPY"
else
	rm -f test_1.c
	cat > test_1.c <<__test_1_eof
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) { bcopy(0,0,0); return 0; }
__test_1_eof
	if $CC -o test_1 test_1.c >/dev/null 2>&1; then
		echo "bcopy"
		rm -f test_1 test_1.c
		CFLAGS="$CFLAGS -DHAVE_BCOPY"
	else
		rm -f test_1.c
		echo "emulated"
	fi
fi

echo -n "Checking for IPV6 support: "
cat > test_1.c <<__test_1_eof
#include <sys/types.h>
#include <netinet/in.h>
int main(int argc, char *argv[]) { struct in6_addr foo; return 0; }
__test_1_eof
if $CC -o test_1 -c test_1.c >/dev/null 2>&1; then
	echo "ok"
	rm -f test_1 test_1.c
	CFLAGS="$CFLAGS -DHAVE_IPV6"
else
	echo "none"
	rm -f test_1.c
fi

echo -n "Checking for setsid() support: "
cat > test_1.c <<__test_1_eof
#include <unistd.h>
int main(int argc, char *argv[]) { setsid(); return 0; }
__test_1_eof
if $CC -o test_1 test_1.c >/dev/null 2>&1; then
	echo "ok"
	rm -f test_1 test_1.c
	CFLAGS="$CFLAGS -DHAVE_SETSID"
else
	echo "disabled"
	rm -f test_1.c
fi

echo -n "Checking for poll() support: "
cat > test_1.c <<__test_1_eof
#include <sys/poll.h>
int main(int argc, char *argv[]) { struct pollfd p; poll(&p, 0, 0); return 0; }
__test_1_eof
if $CC -o test_1 test_1.c >/dev/null 2>&1; then
	echo "ok"
	rm -f test_1 test_1.c
	CFLAGS="$CFLAGS -DHAVE_POLL"
else
	echo "using select()"
	rm -f test_1.c
fi

echo -n "Checking for waitpid() support: "
cat > test_1.c <<__test_1_eof
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char *argv[]) { waitpid(0,0,0); return 0; }
__test_1_eof
if $CC -o test_1 test_1.c >/dev/null 2>&1; then
	echo "ok"
	rm -f test_1 test_1.c
	CFLAGS="$CFLAGS -DHAVE_WAITPID"
else
	echo "failed"
	rm -f test_1.c

	echo -n "Checking for wait4() support: "
	cat > test_1.c <<__test_1_eof
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(int argc, char *argv[]) { wait4(0,0,0,0); return 0; }
__test_1_eof
	if $CC -o test_1 -c test_1.c >/dev/null 2>&1; then
		rm -f test_1 test_1.c
		CFLAGS="$CFLAGS -DHAVE_WAIT4"
	else
		rm -f test_1.c
	fi
fi

echo -n "Checking for POSIX threads: "
cat > test_1.c <<__test_1_eof
#include <pthread.h>
int main(int argc, char *argv[]) { pthread_create(0,0,0,0); return 0; }
__test_1_eof
if $CC $LIBS $CFLAGS -o test_1 test_1.c >/dev/null 2>&1; then
	echo "ok"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lc_r >/dev/null 2>&1; then
	echo "-lc_r"
	LIBS="$LIBS -lc_r"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lpthread >/dev/null 2>&1; then
	echo "-lpthread"
	LIBS="$LIBS -lpthread"
else
	echo "no idea"
	exit 1
fi

echo -n "Checking for non-portable pthreads extensions: "
cat > test_1.c <<__test_1_eof
#include <pthread.h>
int main(int argc, char *argv[]) { pthread_kill_other_threads_np(); return 0; }
__test_1_eof
if $CC $LIBS $CFLAGS -o test_1 test_1.c >/dev/null 2>&1; then
	echo "pthread_kill_other_threads_np"
	CFLAGS="$CFLAGS -DHAVE_pthread_kill_other_threads_np"
else
	echo "none"
fi


LIBS="$LIBS -lldap_r -llber"
echo -n "Checking OpenLDAP dependencies: "
cat > test_1.c <<__test_1_eof
#include <ldap.h>
#include <lber.h>
int main(int argc, char *argv[]) { LDAP *x = ldap_open(0,0); bind(0,0,0); connect(0,0,0); return 0; }
__test_1_eof
if $CC $LIBS $CFLAGS -o test_1 test_1.c >/dev/null 2>&1; then
	echo "ok"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lsocket >/dev/null 2>&1; then
	echo "-lsocket"
	LIBS="$LIBS -lsocket"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lsocket -lnsl -lssl >/dev/null 2>&1; then
	echo "-lsocket -lnsl"
	LIBS="$LIBS -lsocket -lnsl"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lsocket -lnsl >/dev/null 2>&1; then
	echo "-lsocket -lnsl"
	LIBS="$LIBS -lsocket -lnsl"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lresolv >/dev/null 2>&1; then
	echo "-lresolv"
	LIBS="$LIBS -lresolv"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lrt -lresolv >/dev/null 2>&1; then
	echo "-lrt -lresolv"
	LIBS="$LIBS -lrt -lresolv"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lrt -lresolv -lssl >/dev/null 2>&1; then
	echo "-lrt -lresolv"
	LIBS="$LIBS -lrt -lresolv"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lsocket -lresolv >/dev/null 2>&1; then
	echo "-lsocket -lresolv"
	LIBS="$LIBS -lsocket -resolv"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lsocket -lnsl -lresolv >/dev/null 2>&1; then
	echo "-lsocket -lnsl -lresolv"
	LIBS="$LIBS -lsocket -lnsl -resolv"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lsocket -lnsl -lresolv -lssl >/dev/null 2>&1; then
	echo "-lsocket -lnsl -lresolv"
	LIBS="$LIBS -lsocket -lnsl -resolv"
elif $CC $LIBS $CFLAGS -o test_1 test_1.c -lcrypto -lssl -lrt -lresolv -lsocket -lnsl >/dev/null 2>&1; then
	echo "-lcrypto -lssl -lrt -lresolv -lsocket -lnsl"
	LIBS="$LIBS -lcrypto -lssl -lrt -lresolv -lsocket -lnsl"
else
	echo "no idea"
	exit 1
fi
rm -f test_1.c test_1

echo -n "Checking for OpenLDAP < 2.1.8: "
cat > test_1.c <<__test_1_eof
#include <ldap.h>
#include <lber.h>
int main(int argc, char *argv[]) { ldap_enable_cache(0, 1, 0); }
__test_1_eof
if $CC $LIBS $CFLAGS -o test_1 test_1.c >/dev/null 2>&1; then
	echo "yes"
	CFLAGS="$CFLAGS -DACCELERATE_CACHE"
else
	echo "no"
fi


echo -n "Writing Makefile.config: "
echo '# this file is automatically generated: do not edit' > Makefile.config
echo CC=$CC >> Makefile.config
echo CFLAGS="$CFLAGS" >> Makefile.config
echo LFLAGS="$LIBS" >> Makefile.config
echo "done"
echo ""

# adjust timestamps
touch *.c *.h

exit 0
