dnl configure.in for Waimea

AC_INIT(src/main.cc, 0.5.0)
AM_INIT_AUTOMAKE(waimea, 0.5.0)

AM_CONFIG_HEADER(config.h)

AC_PROG_CXX
AC_PROG_INSTALL

AC_PATH_X
AC_PATH_XTRA

CFLAGS="$CFLAGS $X_CFLAGS"
CXXFLAGS="$CXXFLAGS $X_CFLAGS"
LIBS="$LIBS $X_LIBS"
LDFLAGS="$LDFLAGS $LIBS $X_PRE_LIBS"

AC_CHECK_PROGS(regex_cmd, sed)
if test x$regex_cmd = "x"; then
  AC_MSG_ERROR([error. sed is required to build the default style file.])
fi

AC_CHECK_LIB(X11, XOpenDisplay,
  LIBS="$LIBS -lX11",
  AC_MSG_ERROR([Could not find XOpenDisplay in -lX11.])
)

use_interlacing="no"
use_orderedpseudo="no"
use_shape="no"
use_xinerama="no"
use_randr="no"
use_render="no"
use_xft="no"
use_pixmap="no"

AC_LANG_SAVE
AC_LANG_CPLUSPLUS

AC_HEADER_STDC
AC_CHECK_HEADERS(ctype.h libgen.h signal.h stdio.h time.h unistd.h sys/select.h sys/signal.h sys/stat.h sys/time.h sys/types.h sys/wait.h regex.h)

dnl Check for Interlacing
INTERLACE=""
enableval="yes"
AC_MSG_CHECKING([whether to include interlacing image code])
AC_ARG_ENABLE(
  interlace, [  --enable-interlace      include code for image interlacing [default=yes]])

: ${enableval="yes"}
if test x$enableval = "xyes"; then
  AC_MSG_RESULT([yes])
  use_interlacing="yes"
  INTERLACE="-DINTERLACE"
else
  AC_MSG_RESULT([no])
fi
AC_SUBST(INTERLACE)

dnl Check for ordered 8bpp dithering
ORDEREDPSEUDO=""
enableval="yes"
AC_MSG_CHECKING([whether to include Pseudocolor ordered dithering code])
AC_ARG_ENABLE(ordered-pseudo,
[  --enable-ordered-pseudo include code for ordered pseudocolor (8bpp)
                          dithering [default=no]],
  if test x$enableval = "xyes"; then
    AC_MSG_RESULT([yes])
    use_orderedpseudo="yes"
    ORDEREDPSEUDO="-DORDEREDPSEUDO"
  else
    AC_MSG_RESULT([no])
  fi,
  AC_MSG_RESULT([no])
)
AC_SUBST(ORDEREDPSEUDO)

dnl Check for XShape extension support and proper library files
SHAPE=""
enableval="yes"
AC_MSG_CHECKING([whether to build support for the Xshape extension])
AC_ARG_ENABLE(
  shape, [  --enable-shape          enable support for the Xshape extension [default=yes]])

: ${enableval="yes"}
if test x$enableval = "xyes"; then
  AC_MSG_RESULT([yes])
  AC_CHECK_LIB(Xext, XShapeCombineShape,
    AC_MSG_CHECKING([for X11/extensions/shape.h])
    AC_TRY_COMPILE(
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/shape.h>
      , long foo = ShapeSet,
      AC_MSG_RESULT([yes])
      use_shape="yes"
      SHAPE="-DSHAPE"; Xext_lib="-lXext",
      AC_MSG_RESULT([no])
      AC_MSG_RESULT([** WARNING: X11/extensions/shape.h is missing])
      AC_MSG_RESULT([** WARNING: Disabled Xshape extension support])
    ),
    AC_MSG_RESULT([** WARNING: Could not find XShapeCombineShape in -lXshape])
    AC_MSG_RESULT([** WARNING: Disabled Xshape extension support])
  )
else
  AC_MSG_RESULT([no])
fi
AC_SUBST(SHAPE)

LIBS="$LIBS $Xext_lib"

dnl Check for Xinerama extension support and proper library files.
XINERAMA=""
enableval="yes"
AC_MSG_CHECKING([whether to build support for the Xinerama extension])
AC_ARG_ENABLE(
  xinerama, [  --enable-xinerama       enable support for the Xinerama extension [default=yes]])

: ${enableval="yes"}
if test x$enableval = "xyes"; then
   AC_MSG_RESULT([yes])
   AC_CHECK_LIB(Xinerama, XineramaQueryScreens,
     AC_MSG_CHECKING([for X11/extensions/Xinerama.h])
     AC_TRY_LINK(
#include <X11/Xlib.h>
#include <X11/extensions/Xinerama.h>
, XineramaScreenInfo foo,
      AC_MSG_RESULT([yes])
      use_xinerama="yes"
      XINERAMA="-DXINERAMA"; Xinerama_lib="-lXinerama",
      AC_MSG_RESULT([no])
      AC_MSG_RESULT([** WARNING: X11/extensions/Xinerama.h is missing])
      AC_MSG_RESULT([** WARNING: Disabled Xinerama extension support])
    ),
    AC_MSG_RESULT([** WARNING: Could not find XineramaQueryScreens in -lXinerama])
    AC_MSG_RESULT([** WARNING: Disabled Xinerama extension support])
  )
else
  AC_MSG_RESULT([no])
fi
AC_SUBST(XINERAMA)

LIBS="$LIBS $Xinerama_lib"

dnl Checks for Xrender extension support
RENDER=""
enableval="yes"
have_xrender="false"
AC_MSG_CHECKING([whether to build support for the Xrender extension])
AC_ARG_ENABLE(
  render, [  --enable-render         enable support for the Xrender extension [default=yes]])
: ${enableval="yes"}

if test x$enableval = "xyes"; then
  AC_MSG_RESULT([yes])
  AC_CHECK_LIB(Xrender, XRenderFindFormat,
    AC_MSG_CHECKING([for X11/extensions/Xrender.h])
    AC_TRY_COMPILE(
#include <X11/Xlib.h>
#include <X11/extensions/Xrender.h>
      ,,
      AC_MSG_RESULT([yes])
      use_render="yes"
      RENDER="-DRENDER"; XRender_lib="-lXrender"; have_xrender="true",
      AC_MSG_RESULT([no])
      AC_MSG_RESULT([** WARNING: X11/extensions/Xrender.h is missing])
      AC_MSG_RESULT([** WARNING: Disabled Xrender extension support])
    ),
  AC_MSG_RESULT([** WARNING: Could not find XRenderFindFormat in -lXrender])
  AC_MSG_RESULT([** WARNING: Disabled Xrender extension support]))
else
  AC_MSG_RESULT([no])
fi
AC_SUBST(RENDER)

LIBS="$LIBS $XRender_lib"

dnl Check for Xrandr extension support and proper library files.
RANDR=""
enableval="yes"
AC_MSG_CHECKING([whether to build support for the Xrandr extension])
AC_ARG_ENABLE(
  randr, [  --enable-randr          enable support for the Xrandr extension [default=yes]])

: ${enableval="yes"}
if test x$enableval = "xyes"; then
  if test x$have_xrender = "xtrue" ; then
     AC_MSG_RESULT([yes])
     AC_CHECK_LIB(Xrandr, XRRSelectInput,
       AC_MSG_CHECKING([for X11/extensions/Xrandr.h])
       AC_TRY_LINK(
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
, XRRScreenChangeNotifyEvent foo,
         AC_MSG_RESULT([yes])
         use_randr="yes"
         RANDR="-DRANDR"; Xrandr_lib="-lXrandr",
         AC_MSG_RESULT([no])
         AC_MSG_RESULT([** WARNING: X11/extensions/Xrandr.h is missing])
         AC_MSG_RESULT([** WARNING: Disabled Xrandr extension support]))
       ,
       AC_MSG_RESULT([** WARNING: Could not find XRRSelectInput in -lXrandr])
       AC_MSG_RESULT([** WARNING: Disabled Xrandr extension support]))
  else
      AC_MSG_RESULT([no])
      AC_MSG_RESULT([** WARNING: Requires Xrender extension support])
      AC_MSG_RESULT([** WARNING: Disabled Xrandr extension support])
  fi
else
  AC_MSG_RESULT([no])
fi
AC_SUBST(RANDR)

LIBS="$LIBS $Xrandr_lib"

dnl Checks for Xft library support
enableval="yes"
XFT=""
AC_MSG_CHECKING([whether to build support for the Xft library])
AC_ARG_ENABLE(
  xft, [  --enable-xft            enable support for Xft library [default=yes]])
: ${enableval="yes"}
if test x$enableval = "xyes"; then
   if test x$have_xrender = "xtrue" ; then
     AC_MSG_RESULT([yes])
     xftsucceeded=no
	  AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
     if test "x$PKG_CONFIG" != "xno" ; then
       AC_MSG_CHECKING(for Xft)
       if $PKG_CONFIG --exists "xft" ; then
            AC_MSG_RESULT(yes)
            AC_MSG_CHECKING(XFT_CFLAGS)
            Xft_flags=`$PKG_CONFIG --cflags "xft"`
            AC_MSG_RESULT($Xft_flags)
            AC_MSG_CHECKING(XFT_LIBS)
            Xft_lib=`$PKG_CONFIG --libs "xft"`
            AC_MSG_RESULT($Xft_lib)
            xftsucceeded=yes
            use_xft="yes"
            XFT="-DXFT"
            CPPFLAGS="$CPPFLAGS $Xft_flags"
       else
            AC_MSG_RESULT(no)
       fi
     fi

     if test $xftsucceeded = no; then
       dnl Checks for freetype/freetype.h
       have_freetype="false"
       AC_PATH_PROG(FREETYPE_CONFIG, freetype-config, no)
       if test "x$FREETYPE_CONFIG" != "xno" ; then
         FREETYPE_CFLAGS=`$FREETYPE_CONFIG --cflags`
         save_cppflags="$CPPFLAGS"
         CPPFLAGS="$CPPFLAGS $FREETYPE_CFLAGS"
       fi
       AC_MSG_CHECKING([for freetype/freetype.h])
       AC_TRY_COMPILE(
#include <freetype/freetype.h>
        ,,
        AC_MSG_RESULT([yes])
        dnl Checks for Xft

        AC_CHECK_LIB(Xft, XftFontOpen,
          AC_MSG_CHECKING([for X11/Xft/Xft.h])
          AC_TRY_COMPILE(
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
          ,,
          AC_MSG_RESULT([yes])
          use_xft="yes"
          XFT="-DXFT"; Xft_lib="-lXft",
          AC_MSG_RESULT([no])
          AC_MSG_RESULT([** WARNING: X11/Xft/Xft.h is missing])
         AC_MSG_RESULT([** WARNING: Disabled Xft library support])
        ),
        AC_MSG_RESULT([** WARNING: Could not find XftFontOpen in -lXft])
        AC_MSG_RESULT([** WARNING: Disabled Xft library support]))
        ,
        AC_MSG_RESULT([no])
        AC_MSG_RESULT([** WARNING: freetype/freetype.h is missing])
        AC_MSG_RESULT([** WARNING: Disabled Xft library support])
        CPPFLAGS="$save_cppflags")
     fi
   else
      AC_MSG_RESULT([no])
      AC_MSG_RESULT([** WARNING: Requires Xrender extension support])
      AC_MSG_RESULT([** WARNING: Disabled Xft library support])
   fi
else
  AC_MSG_RESULT([no])
fi
AC_SUBST(XFT)

LIBS="$LIBS $Xft_lib"

save_cppflags="$CPPFLAGS"
save_ldflags="$LDFLAGS"
dnl Check for pixmap support
enableval="yes"
PIXMAP=""
AC_MSG_CHECKING([whether to include pixmap support])
AC_ARG_ENABLE(
  pixmap, [  --enable-pixmap         include code for pixmap textures [default=yes]])

: ${enableval="yes"}
if test x$enableval = "xyes"; then
  AC_MSG_RESULT([yes])
  AC_ARG_WITH(imlib2dir,
   [  --with-imlib2dir[=DIR]    Imlib2 in DIR],,withval=no)

  if test "x$withval" != "xno" && test "x$withval" != "xyes"; then 
     CPPFLAGS="$CPPFLAGS -I${withval}/include"
     LDFLAGS="$LDFLAGS -L${withval}/lib"
  fi
else
   AC_MSG_RESULT([no])
fi

if test x$enableval = "xyes"; then
  AC_CHECK_LIB(Imlib2, imlib_create_image,
    AC_MSG_CHECKING([for Imlib2.h])
    AC_TRY_COMPILE(
#include <X11/Xlib.h>
#include <Imlib2.h>
      ,,
      AC_MSG_RESULT([yes])
      use_pixmap="yes"
      PIXMAP="-DPIXMAP"; Imlib2_lib="-lImlib2",
      AC_MSG_RESULT([no])
      AC_MSG_RESULT([** WARNING: Imlib2.h is missing])
      AC_MSG_RESULT([** WARNING: Disabled pixmap support])
      CPPFLAGS="$save_cppflags"
      LDFLAGS="$save_ldflags"),
    AC_MSG_RESULT([** WARNING: Could not find imlib_create_imaget in -lImlib2])
    AC_MSG_RESULT([** WARNING: Disabled pixmap support])
    CPPFLAGS="$save_cppflags"
    LDFLAGS="$save_ldflags")
fi

LIBS="$LIBS $Imlib2_lib"

AC_SUBST(PIXMAP)

AC_OUTPUT([Makefile 
src/Makefile
data/Makefile
data/actions/Makefile
data/styles/Makefile
data/scripts/Makefile
data/backgrounds/Makefile
doc/Makefile])

echo ""
echo "Finished configure:"

if [[ $use_interlacing = "yes" ]] ; then
   echo "  with    interlace";
else
   echo "  without interlace";
fi

if [[ $use_orderedpseudo = "yes" ]] ; then
   echo "  with    ordered-pseudo";
else
   echo "  without ordered-pseudo";
fi

if [[ $use_shape = "yes" ]] ; then
   echo "  with    shape";
else
   echo "  without shape";
fi

if [[ $use_xinerama = "yes" ]] ; then
   echo "  with    xinerama";
else
   echo "  without xinerama";
fi

if [[ $use_randr = "yes" ]] ; then
   echo "  with    randr";
else
   echo "  without randr";
fi

if [[ $use_render = "yes" ]] ; then
   echo "  with    render";
else
   echo "  without render";
fi

if [[ $use_xft = "yes" ]] ; then
   echo "  with    xft";
else
   echo "  without xft";
fi

if [[ $use_pixmap = "yes" ]] ; then
   echo "  with    pixmap";
else
   echo "  without pixmap";
fi

echo ""
echo "type 'make' to build Waimea."
