#  configure.in version 2.5
#  yudit package - Unicode Editor for the X Window System (and Linux) 
#
#  Author: gaspar@yudit.org (Gaspar Sinai)
#  GNU Copyright (C) 1997-2006  Gaspar Sinai
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License, version 2,
#  dated June 1991, by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
dnl Process this file with autoconf to produce a configure script.
AC_INIT(gui/SYudit.h)

AC_PREFIX_DEFAULT(/usr)

dnl Checks for programs.
AC_PROG_CXX
AC_PROG_CXXCPP
AC_PROG_CC
dnl AC_PROG_INSTALL

dnl Unfortunatelly hpux does not have mmap that is
dnl considered good by configure
dnl AC_FUNC_MMAP

AC_PATH_XTRA

AC_PATH_PROG(RANLIB, ranlib)
if test x$RANLIB = x; then
   RANLIB=":"
fi

LPR_COMMAND=""
LPR_CFLAGS="-DLPR_COMMAND=\\\"/bin/lpr\\\""
AC_PATH_PROG(LPR_COMMAND,lpr)

if test x$LPR_COMMAND = x; then
   LPR_CFLAGS="-DLPR_COMMAND=\\\"/bin/lpr\\\""
else
   LPR_CFLAGS="-DLPR_COMMAND=\\\"${LPR_COMMAND}\\\""
fi

AC_SUBST(LPR_CFLAGS)

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE

dnl Checks for library functions.
AC_CHECK_FUNCS(strtol)

AC_LANG_SAVE
AC_LANG_C

AC_MSG_CHECKING(long long )
HAVE_LONG_LONG=
AC_TRY_COMPILE([#include <sys/types.h>],
	[ u_int64_t a; ],
	[HAVE_LONG_LONG=-DHAVE_LONG_LONG])

AC_TRY_COMPILE([#include <stdio.h>],
	[ unsigned long long  a; ],
	[HAVE_LONG_LONG=-DHAVE_LONG_LONG])

if test x$HAVE_LONG_LONG = x ; then
	AC_MSG_RESULT(no)
else
	AC_MSG_RESULT(yes)
fi

AC_MSG_CHECKING(memmove )
NO_MEMMOVE="-DNO_MEMMOVE"
AC_TRY_LINK([#include <string.h>],
	[ char*a; char*b; memmove(a,b,1); ],
	[NO_MEMMOVE=])

if test x$NO_MEMMOVE = x ; then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
fi

CPPFLAGS="$HAVE_LONG_LONG $NO_MEMMOVE $CPPFLAGS"

AC_LANG_RESTORE

AC_SUBST(HAVE_LONG_LONG)

dnl--------------------
dnl Doe we need -ldl ?
dnl--------------------

DL_FLAGS=-ldl

AC_TRY_LINK([#include <dlfcn.h>],
	[ dlopen("", 0);] , 
	[ DL_FLAGS= ])

dnl 
dnl check if it is real
dnl
if test x$DL_FLAGS = x-ldl
then

SAVELD=$LIBS
LIBS="$SAVELD -ldl"

AC_TRY_LINK([#include <dlfcn.h>],
	[ dlopen("", 0);] , 
	[ DL_FLAGS=-ldl ], [ DL_FLAGS= ])

LIBS="$SAVELD $DL_FLAGS"

fi

dnl
dnl solaris has this library outside libc
dnl

SOCKET_FLAGS=-lsocket

AC_TRY_LINK([#include <sys/socket.h>],
	[ socket(1,1,1);] , 
	[ SOCKET_FLAGS= ])

dnl 
dnl check if it is real
dnl
if test x$SOCKET_FLAGS = x-lsocket
then

SAVELD=$LIBS
LIBS="$SAVELD -lsocket"

AC_TRY_LINK([#include <sys/socket.h>],
	[ sockett(1,1,1);] , 
	[ SOCKET_FLAGS=-lsocket ], [ SOCKET_FLAGS= ])

LIBS="$SAVELD $SOCKET_FLAGS"

fi

AC_MSG_CHECKING(libsocket )
if test x$SOCKET_FLAGS = x-lsocket
then
	AC_MSG_RESULT(needed)
else
	AC_MSG_RESULT(not needed)
fi

dnl
dnl solaris has this library outside libc
dnl
AC_MSG_CHECKING(nsl )

NSL_FLAGS=-lnsl

AC_TRY_LINK([#include <netdb.h>],
	[ gethostbyname("localhost");] , 
	[ NSL_FLAGS= ])

dnl 
dnl check if it is real
dnl
if test x$NSL_FLAGS = x-nsl
then

SAVELD=$LIBS
LIBS="$SAVELD -lnsl"

AC_TRY_LINK([#include <netdb.h>],
	[ gethostbyname("localhost");] , 
	[ NSL_FLAGS=-lnsl ], [ NSL_FLAGS= ])

LIBS="$SAVELD $NSL_FLAGS"

fi

if test x$NSL_FLAGS = x-nsl
then
	AC_MSG_RESULT(needed)
else
	AC_MSG_RESULT(not needed)
fi

dnl
dnl People complained that gettext distribution does not have it.
dnl
MSGMERGE=
AC_PATH_PROG(MSGMERGE, msgmerge)
if test x$MSGMERGE = x; then
   MSGMERGE=":"
fi

dnl
dnl finding xgettext to find the libs
dnl version > 2.4  does not need it.
dnl 
GETTEXT_CFLAGS=
GETTEXT_LFLAGS=

TOPDIR=`pwd`
AC_SUBST(TOPDIR)
AC_SUBST(MSGMERGE)
AC_SUBST(GETTEXT_CFLAGS)
AC_SUBST(GETTEXT_LFLAGS)

AC_TRY_LINK([#include <X11/Xlocale.h>],
        [ setlocale(LC_ALL,"C"); ] ,
        [ HAVE_LOCALE=-DHAVE_LOCALE ],
        [ HAVE_LOCALE= ])

SAVELD=$LIBS
SAVECFLAGS=$CLFAGS

LIBS="$X_EXTRA_LIBS $X_LIBS -lX11 $LIBS"
CLFAGS="$X_CFLAGS $CLFAGS"

AC_TRY_LINK([#include <X11/Xlib.h> 
extern char *
XSetIMValues(
#if NeedVarargsPrototypes
    XIM /* im */, ...
#endif
);
],
        [ XSetIMValues (0, XNDestroyCallback , 0, 0); ] ,
        [ HAVE_SET_IM_VALUES=-DHAVE_SET_IM_VALUES; NEED_SET_IM_VALUES_PROTO=-DNEED_SET_IM_VALUES_PROTO ],
        [ NEED_SET_IM_VALUES_PROTO= ])

if test x$HAVE_SET_IM_VALUES = x ; then
AC_TRY_LINK([ #include <X11/Xlib.h> ],
        [ XSetIMValues (0, XNDestroyCallback , 0, 0); ] ,
        [ HAVE_SET_IM_VALUES=-DHAVE_SET_IM_VALUES ],
        [ HAVE_SET_IM_VALUES= ])

fi

LIBS=$SAVELD
CLFAGS=$SAVECFLAGS

AC_SUBST(HAVE_LOCALE)
AC_SUBST(HAVE_SET_IM_VALUES)
AC_SUBST(NEED_SET_IM_VALUES_PROTO)

AC_MSG_CHECKING(locale )

if test x$HAVE_LOCALE = x ; then
	AC_MSG_RESULT(no)
else
	AC_MSG_RESULT(yes)
fi

if test x$no_x != xyes
then
  AC_MSG_CHECKING(XSetIMValues )
  if test x$HAVE_SET_IM_VALUES = x ; then
  	AC_MSG_RESULT(no)
  else
    AC_MSG_RESULT(yes)
  fi
fi

#
# Sorry guys this autoconf program is a monster - useful features are missing 
#
cat > /tmp/gaspar$$.sh <<+
prefix=${prefix}
if test  ${prefix} = NONE
then
  prefix=${ac_default_prefix}
fi
echo ${datarootdir}/yudit
+

YUDIT_DATA=`sh /tmp/gaspar$$.sh`

/bin/rm -f /tmp/gaspar$$.sh 

#
# Check if I can compile with cc
#
CAN_USE_CC="0"
cat > /tmp/gaspar$$.cpp <<+
#include <stdio.h>

class A{
 public:
    A(void) {};
    ~A() {};
    void print(void) {
      fprintf (stderr, "Hi\n");
    }
};

int
main(int argc, char* argv[])
{
  A a;
  a.print();
  // gcc 3.xx does not handle it without c++
  A* aa = new A();
  aa->print();
  delete aa;
  try { throw A(); } catch (A& ea) {}
  return (0);
}
+

AC_MSG_CHECKING(c++ or c compiler to use )
${CC} -o /tmp/gaspar$$ /tmp/gaspar$$.cpp 2> /dev/null 1> /dev/null

if test $? = 0 
then
  CAN_USE_CC="1"
  AC_MSG_RESULT($CC)
  CXX=$CC
else
  AC_MSG_RESULT($CXX)
fi

/bin/rm -f  /tmp/gaspar$$ /tmp/gaspar$$.cpp


AC_SUBST(YUDIT_DATA)

if test x$no_x = xyes
then
  USE_X11=-DUSE_X11=0
  X11_LIB=
else
  USE_X11=-DUSE_X11=1
  X11_LIB=-lX11
fi

AC_SUBST(USE_X11)
AC_SUBST(X11_LIB)

AC_OUTPUT(Makefile.conf uniconv/uniconv.1 uniprint/uniprint.1 yudit.properties doc/FAQ.TXT doc/vi/FAQ.TXT doc/de/FAQ.TXT doc/hu/FAQ.TXT doc/ko/FAQ.TXT doc/ja/FAQ.TXT doc/yi/FAQ.TXT doc/es/FAQ.TXT doc/fi/FAQ.TXT doc/ru/FAQ.TXT doc/te/FAQ.TXT doc/sr/FAQ.TXT doc/zh/FAQ.TXT doc/cs/FAQ.TXT doc/bg/FAQ.TXT doc/it/FAQ.TXT Makefile)
