# 
# anyRemote - a bluetooth remote for your PC.
#
# Copyright (C) 2007-2018 Mikhail Fedotov <anyremote@mail.ru>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# 

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

AC_PREREQ(2.63)

AC_INIT([anyremote],[6.7.1])
AC_CONFIG_SRCDIR([src/main.c])
AM_INIT_AUTOMAKE

# Checks for programs.
AC_PROG_AWK
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT

CFLAGS="$CFLAGS -I/usr/local/include -Wall -D_REENTRANT -O2 -g"

AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/ioctl.h sys/time.h termios.h unistd.h glib.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_TIME
#AC_CHECK_HEADER_STDBOOL
AC_TYPE_UID_T
AC_C_INLINE
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_TYPE_UINT32_T
AC_TYPE_UINT8_T

# Checks for library functions.
AC_FUNC_MEMCMP
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_CHOWN
AC_FUNC_FORK
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([bzero gethostbyname gettimeofday inet_ntoa isascii memset mkdir regcomp select socket strchr strdup strerror strrchr strstr strtol])

AC_PATH_PROG(PKG_CONFIG, pkg-config, no) 

#AC_CONFIG_FILES([Makefile
#                 src/Makefile])


AC_CANONICAL_HOST

AC_ARG_ENABLE(bluetooth,
	[  --disable-bluetooth     disable bluetooth support])

case "$host_os" in
linux*)
	if test x"$enable_bluetooth" != xno; then
		AC_CHECK_HEADERS([bluetooth/bluetooth.h], [], [echo "Can not find bluetooth.h ! Install bluez-dev package or use --disable-bluetooth";exit])
		AR_EXTRAFLAGS="$AR_EXTRAFLAGS -DUSE_BLUEZ=1"
		AR_EXTRALIBS="$AR_EXTRALIBS -lbluetooth"
	fi
	;;
freebsd*)
	if test x"$enable_bluetooth" != xno; then
		AC_CHECK_HEADERS([bluetooth.h], [], [echo "Can not find bluetooth.h ! Install it or use --disable-bluetooth";exit])
		AR_EXTRAFLAGS="$AR_EXTRAFLAGS -DUSE_BT_FBSD=1"
		AR_EXTRALIBS="$AR_EXTRALIBS -lbluetooth -lsdp"
	fi
	;;
esac

AC_ARG_ENABLE(xtest,
  [  --disable-xtest         disable xtest support])

if test x"$enable_xtest" != xno; then
  AC_CHECK_HEADERS([X11/extensions/XTest.h], [], [echo "Can not find XTest.h ! Install it or use --disable-xtest";exit])
  AR_EXTRAFLAGS="$AR_EXTRAFLAGS -DUSE_XTEST=1"
  AR_EXTRALIBS="$AR_EXTRALIBS -lX11 -lXtst"
fi

AC_ARG_ENABLE(iconv,
	[  --disable-iconv         disable iconv support])

if test x"$enable_iconv" != xno; then
  AC_CHECK_HEADERS([iconv.h], [], [echo "Can not find iconv.h ! Install it or use --disable-iconv";exit])
  AR_EXTRAFLAGS="$AR_EXTRAFLAGS -DUSE_ICONV=1"
fi

AC_ARG_ENABLE(glib,
  [  --disable-glib          disable glib support, impiles disable-dbus])

if test x"$enable_glib" != xno; then

  AC_CHECK_LIB(gthread-2.0, g_thread_init)
  
  PKG_CHECK_MODULES([GLIB], [glib-2.0], [], [echo "Can not find glib !";exit])
  AM_PATH_GLIB_2_0([],[],[],[gthread])

  AR_EXTRAFLAGS="$AR_EXTRAFLAGS $GLIB_CFLAGS -DUSE_GLIB=1"
  AR_EXTRALIBS="$AR_EXTRALIBS $GLIB_LIBS"
else
  enable_dbus=no
  AR_EXTRALIBS="$AR_EXTRALIBS -lpthread"
fi

AC_ARG_ENABLE(dbus,
  [  --disable-dbus          disable dbus support, impiles disable-avahi])

if test x"$enable_dbus" != xno; then
  PKG_CHECK_MODULES([DBUS],      [dbus-1 >= 0.61], [], [echo "Can not find dbus-1 ! Install it or use --disable-dbus";exit])
  PKG_CHECK_MODULES([DBUS_GLIB], [dbus-glib-1],    [], [echo "Can not find dbus-glib-1 ! Install it or use --disable-dbus";exit])
  
  DBUS_CFLAGS="`$PKG_CONFIG --cflags dbus-1`" 
  DBUS_LIBS="`$PKG_CONFIG --libs dbus-1`" 
   
  DBUSG_CFLAGS="`$PKG_CONFIG --cflags dbus-glib-1`" 
  # anyRemote do not needs gobject-2.0, but dbus-glib does
  #DBUSG_LIBS="-ldbus-glib-1"
  DBUSG_LIBS="`$PKG_CONFIG --libs dbus-glib-1`"
  
  AR_EXTRAFLAGS="$AR_EXTRAFLAGS $DBUS_CFLAGS $DBUSG_CFLAGS -DUSE_DBUS=1"
  AR_EXTRALIBS="$AR_EXTRALIBS $DBUS_LIBS $DBUSG_LIBS"
else
  enable_avahi=no
fi

AC_ARG_ENABLE(avahi,
	[  --disable-avahi         disable avahi support])

if test x"$enable_avahi" != xno; then
  PKG_CHECK_MODULES(AVAHI, [avahi-client >= 0.6], [], [echo "Can not find avahi ! Install avahi headers or use --disable-avahi";exit])
  AR_EXTRAFLAGS="$AR_EXTRAFLAGS $AVAHI_CFLAGS -DUSE_AVAHI=1"
  AR_EXTRALIBS="$AR_EXTRALIBS $AVAHI_LIBS"
fi

AR_EXTRAFLAGS="$AR_EXTRAFLAGS -DDATADIR=\\\"${datadir}\\\""

AC_SUBST(AR_EXTRAFLAGS)        
AC_SUBST(AR_EXTRALIBS)        

AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT
