dnl Configure.in.in for KPilot.
dnl
dnl Copyright (C) 2000,2001 Adriaan de Groot
dnl
dnl This file is released under the terms of the Gnu General Public
dnl Licence (GPL) Version 2.

dnl
dnl Check to see if pisock header and library are available
dnl

dnl
dnl Questions and comments can be sent to groot@kde.org
dnl

dnl Check for the headers first. The first test is the somewhat-
dnl sophisticated one AC uses, and incorporates all the extra-includes
dnl and whatnot stuff.
dnl
dnl We need to explicitly use the --with-extra-includes passed in,
dnl since AC_CHECK_HEADER does not do so.
dnl

AC_DEFUN(KPILOT_CHECK_PISOCK,
[

AC_LANG_PUSH(C++)

kpilot_save_cflags="$CPPFLAGS"
kpilot_save_ldflags="$LDFLAGS"


CPPFLAGS="$CPPFLAGS $all_includes"
LDFLAGS="$LDFLAGS $all_libraries"

HAVE_PISOCK=0
PISOCK_LIB=""
PISOCK_INCLUDE=""
pisock_path=""

dnl Try looking normally
dnl
dnl
AC_CHECK_HEADER(pi-file.h,[HAVE_PISOCK=1], )


dnl If nothing found, try some other places that might have
dnl pilot-link installed. These can be extended for particular
dnl distro's; /vol/kde is where I install stuff on Suns and BSD.
dnl
dnl
if  test "$HAVE_PISOCK" = "0" ; then
	pisock_path=""
	AC_MSG_CHECKING([for pi-file.h alternate])
	for i in /usr/local/pilot/include /usr/local/include /vol/kde/support/include /usr/include/libpisock ; do
		test -f $i/pi-file.h && HAVE_PISOCK=1
		test -f $i/pi-file.h && PISOCK_INCLUDE="-I$i"
		test -f $i/pi-file.h && pisock_path="$i"
	done

	if test "$pisock_path" ; then
		AC_MSG_RESULT([found $pisock_path])
	else
		AC_MSG_RESULT([still not found])
		HAVE_PISOCK="0"
	fi

fi

dnl Next, check the version of pilot-link to make sure it's sufficiently new.
dnl
dnl
if test -z "$pisock_path" ; then
	AC_CHECK_HEADER(pi-version.h,[HAVE_PISOCK=1], )
else
	test -d "$pisock_path" || HAVE_PISOCK="0"
	test -f "$pisock_path/pi-version.h" || HAVE_PISOCK="0"
fi

if test "$HAVE_PISOCK" = "0" ; then
	AC_MSG_WARN([KPILOT: no pilot-link headers found.])
fi


dnl Now we know where the includes are, we need to
dnl check the version more closely.
dnl
dnl
CPPFLAGS="$CPPFLAGS $PISOCK_INCLUDE"
if test "$HAVE_PISOCK" = "1" ; then
	AC_MSG_CHECKING([pilot-link version])
	AC_TRY_RUN([
	#include <pi-version.h>
	int main()
	{ if (PILOT_LINK_VERSION==0) {
	    if ((PILOT_LINK_MAJOR==9) && (PILOT_LINK_MINOR>=5)) return (0);
	    if ((PILOT_LINK_MAJOR==10) || (PILOT_LINK_MAJOR==11)) return (0);
	  }
	  return (1);
	}
	]
	,
	[AC_MSG_RESULT([pilot-link version 0.10 or 0.11 found])],
	[AC_MSG_RESULT([Your version of pilot-link won't work with KPilot])]
	HAVE_PISOCK="0"
	,
	[AC_MSG_RESULT([Cross-compiling KPilot is doomed])
	HAVE_PISOCK="0"
	])
fi



if test "$HAVE_PISOCK" = "1" ; then
	HAVE_PISOCK=0

	AC_CHECK_LIB(pisock,
		pi_accept,
		[PISOCK_LIB="-lpisock"
		HAVE_PISOCK=1
		],
		[
		unset ac_cv_lib_pisock_pi_accept
		AC_CHECK_LIB(pisock,
			pi_accept,
			[PISOCK_LIB="-lpisock -lsocket"
			HAVE_PISOCK=1
			],
			[
			unset ac_cv_lib_pisock_pi_accept
			AC_CHECK_LIB(pisock,
				pi_accept,
				[PISOCK_LIB="-lpisock -lsocket -lxnet"
				HAVE_PISOCK=1
				],
				[HAVE_PISOCK=0],
				[-lsocket -lxnet]
				)
			],
			[-lsocket]
			)
		]
		)

dnl Run through the loop *anyway*, even if we've already found
dnl the library. The inner if makes sure that we only check until
dnl we've found the library once.
dnl
dnl
for i in /usr/local/pilot/lib /usr/local/lib /vol/kde/support/lib ; do
	if test "$HAVE_PISOCK" = "0" ; then
		CPPFLAGS="$kpilot_save_cflags  $all_includes $PISOCK_INCLUDE"
		LDFLAGS="$kpilot_save_ldflags  $all_libraries -L$i -R$i"

		unset ac_cv_lib_pisock_pi_accept
		AC_CHECK_LIB(pisock,pi_accept,
			[PISOCK_LIB="-L$i -R$i -lpisock"
			HAVE_PISOCK=1],[],[])
	fi
done

else
	HAVE_PISOCK="0"
	PISOCK_LIB=""
	PISOCK_INCLUDE=""
fi

if test "$HAVE_PISOCK" = "0" ; then
	AC_MSG_WARN([KPILOT: pisock library not found])
	DO_NOT_COMPILE="$DO_NOT_COMPILE kpilot"
fi

AC_SUBST(PISOCK_LIB)
AC_SUBST(PISOCK_INCLUDE)


CPPFLAGS="$kpilot_save_cflags"
LDFLAGS="$kpilot_save_ldflags"
unset kpilot_save_cflags
unset kpilot_save_ldflags


AC_LANG_POP(C++)

dnl Remainder is for standalone use
AC_HEADER_TIME
AC_CHECK_GETDOMAINNAME

])
KPILOT_CHECK_PISOCK

