#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT
AC_CONFIG_SRCDIR([src/main.c])
AM_INIT_AUTOMAKE(fcitx, 3.2.1)

dnl Find out what type of system we are
AC_CANONICAL_HOST
AC_CONFIG_HEADERS(config.h)

# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_RANLIB

# Checks for libraries.
AM_ICONV

# Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS([limits.h malloc.h malloc/malloc.h stdlib.h string.h sys/param.h unistd.h wait.h sys/wait.h machine/endian.h])

# x11
AC_PATH_X
#if test "x$x_includes" = "xNONE" ; then
#    AC_MSG_ERROR([You must install XFree86 or xorg first!])
#fi
#AC_PATH_XTRA
test -n "$x_libraries" && X_LIBS="-L$x_libraries"
test -n "$x_includes" && X_CFLAGS="-I$x_includes"
#X_LIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
AC_SUBST(X_LIBS)
AC_SUBST(X_CFLAGS)

# Option to enable the Xft library for Freetype font support.
AC_ARG_ENABLE(xft,
[  --enable-xft            Enable Xft for Freetype font support],
[case "${enableval}" in
  yes)  enable_xft=yes ;;
  no)   enable_xft=no ;;
  auto) enable_xft=auto ;;
  *) AC_MSG_ERROR([bad value "${enableval}" for --enable-xft, use "yes" or "no" or "auto"(default).]) ;;
esac],[])

if test "x$enable_xft" = "xno"; then
  # --enable-xft=no
  :
else
  # Checks for Xft/XRender
  PKG_CHECK_MODULES(XFT, xft >= 0.0.0, have_xft=true, :)

  if test "x$have_xft" = "xtrue"; then
    AC_SUBST(XFT_LIBS)
    AC_SUBST(XFT_CFLAGS)
    :
  else
    if test "x$enable_xft" = "xyes"; then
      # Must use xft, raise a error.
      AC_MSG_ERROR([No xft found, configure for FreeType and Xft failed!])
    else
      AC_MSG_WARN([No xft found, skipping configure for FreeType and Xft])
    fi
  fi
fi
AM_CONDITIONAL(HAVE_XFT, $have_xft)

if test "x$have_xft" = "xtrue"; then
  LIBS="$LIBS $X_LIBS $XFT_LIBS -lX11 $LIBICONV"
  CFLAGS="$CFLAGS $X_CFLAGS $XFT_CFLAGS -D_USE_XFT"
else
  LIBS="$LIBS $X_LIBS -lX11 $LIBICONV"
  CFLAGS="$CFLAGS $X_CFLAGS"
fi

#AC_MSG_CHECKING(for OS-dependent information)
#case "$host" in
#  *-linux* )
#    AC_MSG_RESULT([Linux ($host) found.])
#    ;;
#  *-darwin* )
#    AC_MSG_RESULT([Darwin (Mac OS X) ($host) found.])
#    AC_DEFINE(DARWIN)
#    LIBS="$LIBS -liconv"
#    ;;
#  *)
#    AC_MSG_RESULT([Unknown system type $host found.])
#    ;;
#esac

CFLAGS="$CFLAGS -Wall"

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_PID_T
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_FORK
AC_FUNC_MALLOC
AC_TYPE_SIGNAL
AC_CHECK_FUNCS([memmove memset mkdir setlocale strcasecmp strstr strtol])

AC_CONFIG_FILES([
           Makefile
           fcitx.spec
           doc/Makefile
           data/Makefile
           debian/Makefile
           xpm/Makefile
           lib/Makefile
           src/version.h
           src/Makefile
           tools/Makefile
])
AC_OUTPUT
