dnl Process this file with autoconf to produce a configure script.
dnl ---------------------------------------------------------------------------
dnl Copyright 2012,2013 Thomas E. Dickey
dnl
dnl Permission is hereby granted, free of charge, to any person obtaining a
dnl copy of this software and associated documentation files (the "Software"),
dnl to deal in the Software without restriction, including without limitation
dnl the rights to use, copy, modify, merge, publish, distribute, distribute
dnl with modifications, sublicense, and/or sell copies of the Software, and to
dnl permit persons to whom the Software is furnished to do so, subject to the
dnl following conditions:
dnl
dnl The above copyright notice and this permission notice shall be included in
dnl all copies or substantial portions of the Software.
dnl
dnl THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dnl IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dnl FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
dnl THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dnl LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dnl FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dnl DEALINGS IN THE SOFTWARE.
dnl
dnl Except as contained in this notice, the name(s) of the above copyright
dnl holders shall not be used in advertising or otherwise to promote the sale,
dnl use or other dealings in this Software without prior written authorization.
dnl ---------------------------------------------------------------------------
AC_REVISION($Revision: 1.15 $)
AC_PREREQ(2.13.20030927)
rm -f config.cache

dnl $Id: configure.in,v 1.15 2013/07/14 18:33:35 tom Exp $

AC_INIT(Cdk.xs)
AC_PREFIX_DEFAULT(/usr)

AC_MSG_CHECKING(for version used by Cdk.pm)
VERSION=`egrep '[[$]]VERSION[[ 	]][[ 	]]*=' $srcdir/Cdk.pm | sed -e 's/^[[^"]]*"//' -e 's/".*//'`
AC_MSG_RESULT($VERSION)
AC_SUBST(VERSION)

CF_PROG_CC
CF_PROG_AR
AC_CHECK_TOOL(LD,ld,'$(CC)')
AC_PATH_TOOL(FULL_AR,ar,'$(AR)')
AC_PROG_RANLIB

CF_PROG_EXT
CF_LIB_SUFFIX($LIB_MODEL, DFT_LIB_SUFFIX, DFT_DEP_SUFFIX)
AC_SUBST(DFT_LIB_SUFFIX)

CF_XOPEN_SOURCE
CF_WITH_WARNINGS
CF_DISABLE_LEAKS

AC_CHECK_PROGS(PERL,[perl perl5],none)
if test "$PERL" = none
then
	AC_MSG_ERROR(no perl found)
fi

AC_MSG_CHECKING(if you want to use cdk5-config for C flags)
AC_ARG_WITH(cdk,
	[  --with-cdk              use cdk5-config for C flags],
	[with_cdk="cdk5-config"],
	[with_cdk=no])
AC_MSG_RESULT($with_cdk)

AC_MSG_CHECKING(if you want to use cdkw5-config for C flags)
AC_ARG_WITH(cdkw,
	[  --with-cdkw             use cdkw5-config for C flags],
	[with_cdk="cdkw5-config"],
	[with_cdk=no])
AC_MSG_RESULT($with_cdk)

if test $with_cdk = no
then
	AC_CHECK_HEADER(cdk.h,,[
		cf_save_CPPFLAGS="$CPPFLAGS"
		CPPFLAGS="$CPPFLAGS -I/usr/include/cdk"
		AC_CHECK_HEADER(cdk/cdk.h,[
			AC_DEFINE(HAVE_CDK_CDK_H)
		],[
			AC_MSG_ERROR(cannot find cdk header file)
		])
	])
	AC_CHECK_LIB(cdk,initCDKScreen,[LIBS="-lcdk $LIBS"],[
	AC_CHECK_LIB(cdkw,initCDKScreen,[LIBS="-lcdkw $LIBS"],[
	AC_MSG_ERROR(cannot find cdk library)])])

elif $with_cdk --version 2>/dev/null >/dev/null
then
	CFLAGS=`$with_cdk --cflags`
	LIBS=`$with_cdk --libs`
else
	AC_MSG_ERROR(cannot use $cdk_config script)
fi

AC_SUBST(LIBS)
AC_SUBST(CPPFLAGS)

###	output makefile script
AC_OUTPUT(Makefile.PL,[

# move confdefs.h aside while using perl, since it gets confused
test -f "confdefs.h" && mv confdefs.h confdefs.hdr
$PERL Makefile.PL
test -f "confdefs.hdr" && mv confdefs.hdr confdefs.h

# add to the distclean rule to cleanup files created by the configure script
cat >>Makefile <<CF_EOF
distclean ::
	rm -f Makefile.PL config.status config.cache config.log
CF_EOF

# Perl's MakeMaker treats $(PREFIX) and $(DESTDIR) inconsistently.  One easy
# workaround for test-builds and packaging is to make it work with our variable
# $(BUILDDIR)
if fgrep '$(BUILDDIR)' Makefile
then
	echo '?? name-pollution interference'
else
	sed \
		-e 's%^\(SITELIBEXP = \)/%\1$(BUILDDIR)/%' \
		-e 's%^\(SITEARCHEXP = \)/%\1$(BUILDDIR)/%' \
		-e 's%^\(PERL_LIB = \)/%\1$(BUILDDIR)/%' \
		-e 's%\($(PERL_ARCHLIB)/\)%$(BUILDDIR)\1%' \
		-e '/$(MV) $(FIRST_MAKEFILE)/d' \
		Makefile >Makefile.old
	echo "** updated Makefile to allow in-tree test-builds"
	diff Makefile Makefile.old 
	mv Makefile.old Makefile
fi
],[
PERL="$PERL"
])
