INCLUDE_DIRECTORIES(
	.
	../obdinfo/
)

FILE(GLOB GENERATOR_INCLUDES
	generators/*/CMakeInclude.cmake)
FOREACH(GENERATOR_INCLUDE ${GENERATOR_INCLUDES})
	INCLUDE(${GENERATOR_INCLUDE})
ENDFOREACH(GENERATOR_INCLUDE ${GENERATOR_INCLUDES})


INCLUDE(CheckSymbolExists)
INCLUDE(CheckFunctionExists)

CHECK_FUNCTION_EXISTS(posix_openpt HAVE_POSIX_OPENPT)
IF(HAVE_POSIX_OPENPT)
	ADD_DEFINITIONS(-DHAVE_POSIX_OPENPT)
ENDIF(HAVE_POSIX_OPENPT)

CHECK_FUNCTION_EXISTS(gettimeofday HAVE_GETTIMEOFDAY)
IF(HAVE_GETTIMEOFDAY)
	ADD_DEFINITIONS(-DAVE_GETTIMEOFDAY)
ENDIF(HAVE_GETTIMEOFDAY)

SET(OBD_SIM_DISABLE_BLUEZ false CACHE BOOL "Disable bluetooth support in obdsim")
IF(NOT OBD_SIM_DISABLE_BLUEZ)
	CHECK_SYMBOL_EXISTS(BTPROTO_RFCOMM
		bluetooth/bluetooth.h
			HAVE_BLUETOOTH)
	IF(HAVE_BLUETOOTH)
		MESSAGE(STATUS "Enabling bluetooth obdsim port")
		ADD_DEFINITIONS(-DHAVE_BLUETOOTH)
	ENDIF(HAVE_BLUETOOTH)
ENDIF(NOT OBD_SIM_DISABLE_BLUEZ)

SET(OBD_SIM_DISABLE_SOCKET false CACHE BOOL "Disable socket support in obdsim")
IF(NOT OBD_SIM_DISABLE_SOCKET)
	CHECK_FUNCTION_EXISTS(socket HAVE_SOCKET)
	IF(HAVE_SOCKET)
		MESSAGE(STATUS "Enabling socket obdsim port")
		ADD_DEFINITIONS(-DHAVE_SOCKET)
	ENDIF(HAVE_SOCKET)
ENDIF(NOT OBD_SIM_DISABLE_SOCKET)

SET(OBDSIM_POSIXSRCS
	posixsimport.cc
	posixsimport.h
	fdsimport.cc
	fdsimport.h
)

SET(OBDSIM_WINDOWSSRCS
	windowssimport.cc
	windowssimport.h
)

SET(OBDSIM_BLUETOOTHSRCS
	bluetoothsimport.cc
	bluetoothsimport.h
)

SET(OBDSIM_SOCKETSRCS
	socketsimport.cc
	socketsimport.h
)

SET(OBDSIM_SRCS
	datasource.h
	obdsim.cc
	obdsim.h
	simport.cc
	simport.h
	mainloop.cc
	mainloop.h
)

# For now, assume the choices are "windows" or "posix"
IF("${CMAKE_SYSTEM}" MATCHES "Windows")
	SET(OBDSIM_SRCS ${OBDSIM_SRCS} ${OBDSIM_WINDOWSSRCS})
	SET(OBDSIM_LIBS ${OBDSIM_LIBS} wsock32)
ELSE("${CMAKE_SYSTEM}" MATCHES "Windows")
	SET(OBDSIM_SRCS ${OBDSIM_SRCS} ${OBDSIM_POSIXSRCS})
	SET(OBDSIM_LIBS ${OBDSIM_LIBS} pthread dl)
ENDIF("${CMAKE_SYSTEM}" MATCHES "Windows")

IF(HAVE_BLUETOOTH)
	SET(OBDSIM_SRCS ${OBDSIM_SRCS} ${OBDSIM_BLUETOOTHSRCS})
	SET(OBDSIM_LIBS ${OBDSIM_LIBS} bluetooth)
ENDIF(HAVE_BLUETOOTH)

IF(HAVE_SOCKET)
	SET(OBDSIM_SRCS ${OBDSIM_SRCS} ${OBDSIM_SOCKETSRCS})
ENDIF(HAVE_SOCKET)

SET(OBDSIM_LIBS
	${OBDSIM_LIBS}
	${GENERATOR_LIBS}
	ckobdinfo ckobdconfigfile
)

ADD_EXECUTABLE(obdsim ${OBDSIM_SRCS})

TARGET_LINK_LIBRARIES(obdsim ${OBDSIM_LIBS})

INSTALL(TARGETS obdsim
	RUNTIME DESTINATION bin)

INSTALL(FILES ${OBDGPSLogger_SOURCE_DIR}/man/man1/obdsim.1
	DESTINATION share/man/man1)

