dnl Process this file with autoconf to produce a configure script.
dnl TODO: replace "ChangeLog" with a more unique file name
AC_INIT(ChangeLog)

builtin(include,aclocal/ac_path_generic.m4)dnl
builtin(include,aclocal/check_gnu_make.m4)dnl


AC_PATH_GENERIC(fribidi,0.1.4,[], [ AC_MSG_ERROR(fribidi not found) ])

dnl if version is < 0.10.0 then older interface (1) is used
AC_PATH_GENERIC(fribidi,0.10.0,[
    FRIBIDI_INTERFACE=""
  ], [
    FRIBIDI_INTERFACE="-DFRIBIDI_INTERFACE_1"
  ]
)
AC_SUBST(FRIBIDI_INTERFACE)


AC_ARG_ENABLE(dynamic-fribidi,
  [  --disable-dynamic-fribidi      Link fribidi statically],
  [ case "${enableval}" in
      yes) STATIC_FRIBIDI="" ;;
      no)  STATIC_FRIBIDI="|sed -e 's/-lfribidi/-Wl,-Bstatic & -Wl,-Bdynamic/'" ;;
      *) AC_MSG_ERROR(bad value ${enableval} for --disable-refreshd) ;;
    esac
  ],
  [ STATIC_FRIBIDI=""]
)
AC_SUBST(STATIC_FRIBIDI)


AC_ARG_ENABLE(refreshd,
  [  --disable-refreshd            Disable building of r2l gnome applet ],
    [case "${enableval}" in
      yes) BUILD_REFRESHD=1 ;;
      no)  BUILD_REFRESHD=0 ;;
      *) AC_MSG_ERROR(bad value ${enableval} for --disable-refreshd) ;;
    esac],
  [BUILD_REFRESHD=1]
)
AC_SUBST(BUILD_REFRESHD)

AC_ARG_ENABLE(plugins,
  [  --disable-plugins             Disable building of r2l gnome applet ],
    [case "${enableval}" in
      yes) BUILD_PLUGINS=1 ;;
      no)  BUILD_PLUGINS=0 ;;
      *) AC_MSG_ERROR(bad value ${enableval} for --disable-plugins) ;;
    esac],
  [BUILD_PLUGINS=1]
)
AC_SUBST(BUILD_PLUGINS)

dnl Gnome support is enabled by default
AC_ARG_ENABLE(gnome,
  [  --disable-gnome               Disable building of r2l gnome applet ],
    [case "${enableval}" in
      yes) build_gnome=1 ;;
      no)  build_gnome=0 ;;
      *) AC_MSG_ERROR(bad value ${enableval} for --disable-gnome) ;;
    esac],
  [build_gnome=1]
)

AC_PROG_CC
dnl AC_PROG_MAKE_SET
dnl AC_PROG_RANLIB
dnl AC_PROG_LN_S

dnl Check for X libraries
AC_PATH_X
if test "x${no_x}" = "xyes"; then
  AC_MSG_ERROR(X libraries and/or headers not found)
fi

if test "x${x_includes}" != "x"; then
  X11_CFLAGS="-I$x_includes"
fi

if test "x${x_libraries}" != "x"; then
  X11_LIBS="-L${x_libraries} -lX11"
fi

AC_SUBST(X11_CFLAGS)
AC_SUBST(X11_LIBS)


if test "${build_gnome}" = "1"; then
  AC_PATH_GENERIC(gnome,1.0.0,
    [ 
      if ${GNOME_CONFIG} --cflags applets 2>/dev/null >/dev/null; then
        AC_MSG_RESULT(yes)
      else 
        AC_MSG_WARN(Gnome libraries don't support applets (Gnome 
          configuration taken from $gnome_config). Gnome support disabled)
        build_gnome=0
      fi
    ],
    [
      AC_MSG_WARN(Gnome compilation configuration not found)
      build_gnome=0
    ]
  )
fi

BUILD_GNOME="${build_gnome}"
AC_SUBST(BUILD_GNOME)

dnl Gnome support is enabled by default
AC_ARG_ENABLE(system-r2llib,
  [  --enable-system-r2llib        Use a copy of r2llib from the system ],
    [case "${enableval}" in
      yes) system_r2llib=1 ;;
      no)  system_r2llib=0 ;;
      *) AC_MSG_ERROR(bad value ${enableval} for --disable-gnome) ;;
    esac],
  [system_r2llib=0]
)

dnl Disabled test for system r2llib. The failure might scare people...
if test "${system_r2llib}" = '1'; then
  AC_PATH_GENERIC(r2llib,0.32.0)
else
  R2LLIB_CONFIG=""
  AC_SUBST(R2LLIB_CONFIG)
fi


AC_MSG_CHECKING(for dock applets library (libdockapp))

dnl This value of '1' will be set to '0' if either headers or libraries are
dnl not given
dockapp_headers_libs=1
AC_ARG_WITH(libdockapp-prefix,
  [  --with-libdockapp-prefix=DIR   Look for libdockapp under DIR],
  [ 
    libdockapp_headers="${withval}/include" 
    libdockapp_libs="${withval}/lib" 
  ],
  [ dockapp_headers_libs=0 ]
)


AC_ARG_WITH(libdockapp-headers,
  [  --with-libdockapp-headers=DIR  Take libdocoapp's headers from DIR],
  [ libdockapp_headers="${withval}" ],
  [ dockapp_headers_libs=0 ]
)
AC_ARG_WITH(libdockapp-libs,
  [  --with-libdockapp-libs=DIR  Take libdocoapp's headers from DIR],
  [ libdockapp_libs="${withval}" ],
  [ dockapp_headers_libs=0 ]
)

AC_ARG_ENABLE(dynamic-libdockapp,
  [  --enable-dynamic-libdockapp    Link libdockapp dynamically],
  [ libdockapp_lib="-ldockapp"],
  [ libdockapp_lib="-Wl,-Bstatic -ldockapp -Wl,-Bdynamic"]
)


if test -r "${libdockapp_libs}/libdockapp.a" &&
   test -r "${libdockapp_headers}/dockapp.h"; then
  DOCKAPP_CFLAGS="-I${libdockapp_headers}"
  DOCKAPP_LIBS="-L${libdockapp_libs} ${libdockapp_lib}"
  build_docpapp=1
else
  libdockapp_libs="/usr/local/lib" 
  libdockapp_headers="/usr/local/include" 
  if test -r "${libdockapp_libs}/libdockapp.a" &&
     test -r "${libdockapp_headers}/dockapp.h"; then
    DOCKAPP_CFLAGS="-I${libdockapp_headers}"
    DOCKAPP_LIBS="-L${libdockapp_libs} ${libdockapp_lib}"
    build_docpapp=1
  else
    libdockapp_libs="/usr/X11R6/lib" ; 
    libdockapp_headers="/usr/X11R6/include/X11" 
    if test -r "${libdockapp_libs}/libdockapp.a" &&
       test -r "${libdockapp_headers}/dockapp.h"; then
      DOCKAPP_CFLAGS="-I${libdockapp_headers}"
      DOCKAPP_LIBS="-L${libdockapp_libs} ${libdockapp_lib}"
      build_docpapp=1
    else
      libdockapp_libs="/usr/lib" ; 
      libdockapp_headers="/usr/include" 
      if test -r "${libdockapp_libs}/libdockapp.a" &&
         test -r "${libdockapp_headers}/dockapp.h"; then
        DOCKAPP_CFLAGS="-I${libdockapp_headers}"
        DOCKAPP_LIBS="-L${libdockapp_libs} ${libdockapp_lib}"
        build_docpapp=1
      else
        build_docpapp=0
      fi
    fi
  fi
fi

if test "$build_docpapp" = 1; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi

AC_SUBST(DOCKAPP_LIBS)
AC_SUBST(DOCKAPP_CFLAGS)

BUILD_DOCKAPP="${build_docpapp}"
AC_SUBST(BUILD_DOCKAPP)


dnl The following macro sets the variable INSTALL
AC_PROG_INSTALL

dnl Checks for programs.
CHECK_GNU_MAKE

if test "x${_cv_gnu_make_command}" = "x"; then
  # gnu make not found. abort
  AC_MSG_ERROR(GNU make is required)
else
  GMAKE="${_cv_gnu_make_command}"
  AC_SUBST(GMAKE)
fi


AC_CHECK_LIB(socket,bind,[SOCKET_LIBS="-lsocket"],[SOCKET_LIBS=""])
AC_SUBST(SOCKET_LIBS)


wish_found=0

default_wish_program="/usr/local/bin/wish"

AC_ARG_WITH(tcltk_program,
  [  --with-tcltk-program=PROG      Use PROG as the tcl/tk interpeter (wish)],
  [ 
    wish_program_tmp="${withval}"
    wish_found=1
  ]
)

dnl Check for a TCL/TK interpeter:
dnl this test does not give me exatly what I want, so I have to go around a
dnl bit
if test "${wish_found}" = "0"; then
  # tcl/tk interpeter location was not given in the command--line
  AC_PATH_PROGS(wish_program_tmp, 
    wish wish8.4 wish8.3 wish8.2 wish8.1 wish8.0 wish4.2 wish4.1 wish4.0, 
    wish_not_found
  )
  if test "${wish_program_tmp}" = "wish_not_found"; then
    wish_program_tmp="${default_wish_program}"
  else
    wish_found=1
  fi
fi
WISH_PROGRAM=${wish_program_tmp}
AC_SUBST(WISH_PROGRAM)

dnl dnl Checks for libraries.
dnl AC_CHECK_LIB(X11, XDrawString)
dnl AC_CHECK_LIB(dl, dlopen)
dnl dnl This checks for a system copy of r2llib
dnl AC_CHECK_LIB(r2l, r2l_enable)


dnl dnl for the dock applet:
dnl AC_CHECK_LIB(dockapp, buttonPressCallback)
dnl I'll skip those for the moment. One test for Xlib is enough hopefully
dnl AC_CHECK_LIB(Xext, main)
dnl Replace `main' with a function in -lXpm:
dnl AC_CHECK_LIB(Xpm, main)

dnl Checks for header files.
dnl AC_HEADER_STDC
dnl AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h)


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

dnl Checks for library functions.
AC_CHECK_FUNCS(gettimeofday getcwd select strdup)

AC_MSG_CHECKING(linux version >= 2.2)
if test "`uname -s`" = "Linux" &&
  # asume linux major and minor version numbers are one digit!!!
  test "`uname -r |cut -c 1,3`" -ge 22; then
  AC_MSG_RESULT(yes)
  LINUX_22=1
  SOCK_CFLAGS="-DABSTRACT_UNIX_SOCKET_ADDRESSES"
else
  AC_MSG_RESULT(no)
  LINUX_22=0
  SOCK_CFLAGS=""
fi
AC_SUBST(SOCK_CFLAGS)


AC_ARG_ENABLE(debug-refreshd,
  [  --enable-debug-refreshd        Enable refreshd debugging (-DNOISY) ],
    [case "${enableval}" in
      yes) REFRESHD_DEBUG="-DNOISY" ;;
      no)  REFRESHD_DEBUG="" ;;
      *) AC_MSG_ERROR(bad value ${enableval} for --disable-debug-refreshd) ;;
    esac],
  [REFRESHD_DEBUG=""]
)
AC_SUBST(REFRESHD_DEBUG)

dnl
dnl print results summary
dnl

echo "================================================"
echo "============== Configure Results ==============="
echo "================================================"
echo "* Using fribidi configured from ${FRIBIDI_CONFIG}"

echo "* Using X11 libs with: cflags=${X11_CFLAGS} , libs=${X11_LIBS}"

if test "$BUILD_GNOME" = "1"; then 
  echo "* Building gnome plug-in."
  echo "  Gnome configured from from ${GNOME_CONFIG}"
fi

if test "$BUILD_DOCKAPP" = "1"; then 
  echo "* Building dock applet plug-in."
  echo "  libdockapp from ${libdockapp_headers}, ${libdockapp_libs}"
fi

if test "x${R2LLIB_CONFIG}" != "x"; then 
  echo "* Using system copy of r2llib."
  echo "  r2llib configured from ${R2LLIB_CONFIG}"
fi

if test "$LINUX_22" = "1"; then 
  echo "* Using Linux >=2.2 Abstract Socket"
fi

if test "x${prefix}" != 'xNONE'; then
  echo ""
  echo "NOTE: It seems that you have set a value to --prefix ."
  echo "      This value is currently ignored. use 'make PREFIX=${prefix}'"
fi

if test "${wish_found}" = "1"; then
  echo "* tcl/tk interpeter: ${WISH_PROGRAM}"
else
  echo "* tcl/tk interpeter not found."
  echo "  Using the default ${default_wish_program}."
  echo "  This only affects the script r2ltcl."
fi

if test "x${REFRESHD_DEBUG}" != "x"; then
  echo "* with refreshd debugging flag (${REFRESHD_DEBUG})"
fi

echo "================================================"
echo "================================================"

AC_OUTPUT(
  make.defs
  src/r2llib/make.defs 
  src/r2llib/r2ltcl
  src/biditext/make.defs 
  src/r2l-plugins/make.defs 
  src/refreshd/src/make.defs
)
