AC_DEFUN([AC_PATH_QT3],
[
#
# This macros has been written by Kornilov Matwey
# Copyright (c) 2004, Kornilov Matwey
#
AC_ARG_WITH([qtdir], AC_HELP_STRING([--with-qtdir=DIR],[Qt3 installation directory (default=$QTDIR)]), QTDIR=$withval)

AC_MSG_CHECKING([where QT3])

if test -z $QTDIR; then
	for I in `ls -d /usr/l*/* | grep -i qt`
	do
		if test -d $I ; then
			QTDIR=$I
		fi
	done
fi

if test -z $QTDIR ; then
	AC_MSG_ERROR([QT3 libs not found])
fi

AC_MSG_RESULT([$QTDIR])

AC_MSG_CHECKING([where QT3 includes path])

if test -d "$QTDIR/include" ; then
	QT_INCLUDE="$QTDIR/include"
else
	AC_MSG_ERROR([QT3 includes not found])
fi

AC_MSG_RESULT([$QT_INCLUDE])

AC_MSG_CHECKING([for QT3 version])

QT_VER_MAJ=`grep -m1 QT_VERSION_STR $QT_INCLUDE/qglobal.h | cut -d"\"" -f 2 | cut -d. -f 1`
QT_VER_MIN=`grep -m1 QT_VERSION_STR $QT_INCLUDE/qglobal.h | cut -d"\"" -f 2 | cut -d. -f 2`
QT_VER_MOD=`grep -m1 QT_VERSION_STR $QT_INCLUDE/qglobal.h | cut -d"\"" -f 2 | cut -d. -f 3`

AC_MSG_RESULT([$QT_VER_MAJ.$QT_VER_MIN.$QT_VER_MOD])

if test -n $2 ; then
	QT_NVER_MAJ=`echo $2 | cut -d. -f 1`
	QT_NVER_MIN=`echo $2 | cut -d. -f 2`
	QT_NVER_MOD=`echo $2 | cut -d. -f 3`

	if test $QT_VER_MAJ -lt $QT_NVER_MAJ ; then
		AC_MSG_ERROR([You need QT3 libs $2 or latest])
	fi
	if test $QT_VER_MAJ -eq $QT_NVER_MAJ ; then
		if test $QT_VER_MIN -lt $QT_NVER_MIN ; then
			AC_MSG_ERROR([You need QT3 libs $2 or latest])
		fi
		if test $QT_VER_MIN -eq $QT_NVER_MIN ; then
			if test $QT_VER_MOD -lt $QT_NVER_MOD ; then
				AC_MSG_ERROR([You need QT3 libs $2 or latest])
			fi
		fi
	fi
fi

AC_MSG_CHECKING([where QT3 libs path])

if test -d "$QTDIR/lib" ; then
	QT_LIB="$QTDIR/lib"
else
	AC_MSG_ERROR([QT3 libs not found])
fi

AC_MSG_RESULT([$QT_LIB])

AC_MSG_CHECKING([for QT3 libs])

if test -e "$QT_LIB/libqt.so" ; then
	QT_L=qt
fi
if test -e "$QT_LIB/libqt-mt.so" ; then
	QT_LMT=qt-mt
fi
if test -e "$QT_LIB/libqte.so" ; then
	QT_LE=qte
fi
if test -e "$QT_LIB/libqte-mt.so" ; then
	QT_LEMT=qte-mt
fi

AC_MSG_RESULT([$QT_L $QT_LMT $QT_LE $QT_LEMT])

if test -n $1 ; then
	case $1 in
	qt)
		if test -z $QT_L ; then
			AC_MSG_ERROR([QT3 qt not found])
		fi
	;;
	qt-mt)
		if test -z $QT_LMT ; then
			AC_MSG_ERROR([QT3 qt-mt not found])
		fi
	;;
	qte)
		if test -z $QT_LE ; then
			AC_MSG_ERROR([QT3 qte not found])
		fi
	;;
	qte-mt)
		if test -z $QT_LEMT ; then
			AC_MSG_ERROR([QT3 qte-mt not found])
		fi
	;;
	esac
fi

AC_MSG_CHECKING([where QT3 uic])

if test -x $QTDIR/bin/uic ; then
	QT_UIC="$QTDIR/bin/uic";
else
	AC_MSG_ERROR([QT3 uic not found])
fi

AC_MSG_RESULT([$QT_UIC])

AC_MSG_CHECKING([where QT3 moc])

if test -x $QTDIR/bin/moc ; then
	QT_MOC="$QTDIR/bin/moc";
else
	AC_MSG_ERROR([QT3 moc not found])
fi

AC_MSG_RESULT([$QT_MOC])

QT_CXXFLAGS=" -I$QT_INCLUDE -I$QTDIR/mkspecs/default"
QT_LIBS=" -L$QT_LIB"

AC_SUBST(QTDIR)
AC_SUBST(QT_CXXFLAGS)
AC_SUBST(QT_LIBS)
AC_SUBST(QT_VER_MAJ)

])

AC_DEFUN([AC_PATH_RS485],
[
#
# This macros has been written by Kornilov Matwey
# Copyright (c) 2004, Kornilov Matwey
#
AC_ARG_WITH([rs485],AC_HELP_STRING([--with-rs485=DIR],[Rs485 installation directory (default=/usr/src/rs485)]), RS485DIR=$withval)

AC_MSG_CHECKING([where RS485 driver])

if test -z $RS485DIR; then
	for I in `ls -d /usr/src/* | grep -i rs485`
	do
		if test -d $I ; then
			RS485DIR=$I
			break
		fi
	done
fi

AC_MSG_RESULT([$RS485DIR])

AC_MSG_CHECKING([where RS485 includes path])

if test -d "$RS485DIR/include" ; then
	RS485_INCLUDE="$RS485DIR/include"
	RS485_CFLAGS=" -I$RS485_INCLUDE"
else
	AC_MSG_WARN([RS485 includes not found])
fi

AC_MSG_RESULT([$RS485_INCLUDE])

AC_MSG_CHECKING([for RS485 driver version])

if test -f $RS485_INCLUDE/version.h ; then

RS485_VER_MAJ=`grep -m1 RS485_UTS_RELEASE $RS485_INCLUDE/version.h | cut -d"\"" -f 2 | cut -d. -f 1`
RS485_VER_MOD=`grep -m1 RS485_UTS_RELEASE $RS485_INCLUDE/version.h | cut -d"\"" -f 2 | cut -d. -f 3`

AC_MSG_RESULT([$RS485_VER_MAJ.x.$RS485_VER_MOD])

if test -n $1 ; then
	RS485_NVER_MAJ=`echo $1 | cut -d. -f 1`
	RS485_NVER_MOD=`echo $1 | cut -d. -f 3`

	if test $RS485_VER_MAJ -lt $RS485_NVER_MAJ ; then
		AC_MSG_ERROR([You need RS485 driver $1 or latest])
	fi
	if test $RS485_VER_MAJ -eq $RS485_NVER_MAJ ; then
		if test $RS485_VER_MOD -lt $RS485_NVER_MOD ; then
			AC_MSG_ERROR([You need RS485 driver $1 or latest])
		fi
	fi
fi

else

AC_MSG_RESULT([])

fi

AC_SUBST(RS485DIR)
AC_SUBST(RS485_CFLAGS)

])

AC_DEFUN([AC_CHECK_EXTRAS],
[
#
# This macros has been written by Kornilov Matwey
# Copyright (c) 2004, Kornilov Matwey
#
AC_ARG_ENABLE([playback],AC_HELP_STRING([--enable-playback],[Enable playback mode, default=no]), PLAYBACK=$enableval)
AC_ARG_ENABLE([counter],AC_HELP_STRING([--enable-counter],[Enable counter mode, default=no]), COUNTER=$enableval)
AC_ARG_ENABLE([motor],AC_HELP_STRING([--enable-motor],[Enable motor mode, default=no]), MOTOR=$enableval)
AC_ARG_ENABLE([scanmotor],AC_HELP_STRING([--enable-scanmotor],[Enable scanmotor mode, default=no]), SCANMOTOR=$enableval)

if test "$QT_VER_MAJ" = "3" ; then
	EXTRA_CFLAGS=$EXTRA_CFLAGS" -D__QT3__ -I\$(QTATTIC)"
	EXTRA_T_OBJ="qttableview.o"
	EXTRA_T_MOCOBJ="moc_qttableview.o"
	EXTRA_T_MOCCPP="moc_qttableview.cpp"
fi

if test "$PLAYBACK" = "yes" ; then
	EXTRA_CFLAGS=$EXTRA_CFLAGS" -D__WITH_PLAYBACK__"
fi

if test "$COUNTER" = "yes" ; then
	EXTRA_CFLAGS=$EXTRA_CFLAGS" -D__COUNTER_BUTTON__"
fi

if (test "$MOTOR" = "yes") && (test "$SCANMOTOR" = "yes") ; then
	AC_MSG_ERROR([Imposible enable motor and scanmotor modes together])
fi

if test "$MOTOR" = "yes" ; then
	EXTRA_CFLAGS=$EXTRA_CFLAGS" -D__MOTOR__"
fi

if test "$SCANMOTOR" = "yes" ; then
	EXTRA_CFLAGS=$EXTRA_CFLAGS" -D__SCANMOTOR__"
fi

AC_SUBST(EXTRA_CFLAGS)
AC_SUBST(EXTRA_T_OBJ)
AC_SUBST(EXTRA_T_MOCOBJ)
AC_SUBST(EXTRA_T_MOCCPP)

])

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

AC_PREREQ(2.57)
AC_INIT(Turbina, 2.054, kolja@sai.msu.ru)
AC_PREFIX_DEFAULT(/opt/turbina)
AC_CONFIG_SRCDIR([src/trbscenario.h])

AC_CANONICAL_TARGET

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_INSTALL
#AC_CHECK_TOOL(LIBTOOL,[libtool])
AC_CHECK_TOOL(AR,[ar])

# Checks for libraries.
AC_PATH_QT3([qt-mt],[2.3.0])
AC_PATH_XTRA
AC_PATH_RS485([3.x.0])

AC_CHECK_LIB([m], [main])
AC_CHECK_LIB([pthread], [main])
AC_CHECK_LIB([rt], [main])

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([arpa/inet.h fcntl.h limits.h netinet/in.h stdint.h stdlib.h string.h sys/ioctl.h sys/socket.h sys/time.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_TYPE_UID_T
AC_C_INLINE
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

# Checks for library functions.
AC_FUNC_CHOWN
AC_FUNC_ERROR_AT_LINE
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MALLOC
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_STRTOD
AC_CHECK_FUNCS([atexit floor gettimeofday inet_ntoa pow rint select socket sqrt strcasecmp strchr strdup strerror strrchr strstr strtol strtoul])

#
AC_CHECK_EXTRAS

AC_CONFIG_FILES([Makefile src/Makefile src/science/Makefile])
AC_OUTPUT
