project (csctapi)
if (CMAKE_COMPILER_IS_GNUCC)
	execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
	string(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION})
	list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
	list(GET GCC_VERSION_COMPONENTS 0 GCC_MINOR)
	add_definitions ("-W -Wall ")
	set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
	set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O2")
	set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -ggdb")
	set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -ggdb")
endif (CMAKE_COMPILER_IS_GNUCC)
# Mac extra removes to avoid ranlib warnings in some situations
if (OSCamOperatingSystem MATCHES "Mac OS X" AND NOT HAVE_LIBUSB AND HAVE_PCSC)
set (mac_usbpcsc "ifd_smartreader*")
elseif(OSCamOperatingSystem MATCHES "Mac OS X" AND NOT HAVE_LIBUSB AND NOT HAVE_PCSC)
set (mac_usbpcsc "ifd_smartreader*" "ifd_pcsc*")
elseif (OSCamOperatingSystem MATCHES "Mac OS X" AND HAVE_LIBUSB AND NOT HAVE_PCSC)
set (mac_usbpcsc "ifd_pcsc*")
endif(OSCamOperatingSystem MATCHES "Mac OS X" AND NOT HAVE_LIBUSB AND HAVE_PCSC)
if (OSCamOperatingSystem MATCHES "Mac OS X")
file (GLOB csctapi_mac "ifd_azbox*" "ifd_cool*" "ifd_stapi*" ${mac_usbpcsc})
file (GLOB csctapi_srcs "*.c")
list(REMOVE_ITEM csctapi_srcs ${csctapi_mac})
file (GLOB csctapi_hdrs "*.h")
list(REMOVE_ITEM csctapi_hdrs ${csctapi_mac})
else (OSCamOperatingSystem MATCHES "Mac OS X")
file (GLOB csctapi_srcs "*.c")
file (GLOB csctapi_hdrs "*.h")
endif (OSCamOperatingSystem MATCHES "Mac OS X")

if (NOT OSCamOperatingSystem MATCHES "Mac OS X")
	if (LIBRTDIR)
		check_include_file ("${LIBRTDIR}/include/sys/time.h" HAVE_LIBRT_STATIC)
		if (HAVE_LIBRT_STATIC)
			if (EXISTS ${LIBRTDIR}/lib/librt.a)
		message(STATUS "  static librt found : ${LIBRTDIR}/lib/librt.a (needed by libusb).")
				add_definitions ("-I${LIBRTDIR}/include/")
			else (EXISTS ${LIBRTDIR}/lib/librt.a)
		message(STATUS " static librt not found (needed by libusb). No smartreader support)")
				set (HAVE_LIBRT_STATIC False)
			endif (EXISTS ${LIBRTDIR}/lib/librt.a)
		elseif (HAVE_LIBRT_STATIC)
		message(STATUS " static librt not found (needed by libusb). No smartreader support)")
			set (HAVE_LIBRT_STATIC False)
		endif (HAVE_LIBRT_STATIC)
	else (LIBRTDIR)
		check_include_file ("time.h" HAVE_LIBRT)
		if (HAVE_LIBRT)
		message(STATUS "  librt found (needed by libusb).")
			set (HAVE_LIBRT True)
		else (HAVE_LIBRT)
		message(STATUS "  no librt found (needed by libusb). No smartreader support")
			set (HAVE_LIBRT False)
		endif (HAVE_LIBRT)
	endif (LIBRTDIR)
else (NOT OSCamOperatingSystem MATCHES "Mac OS X")
	set (HAVE_LIBRT True)
endif (NOT OSCamOperatingSystem MATCHES "Mac OS X")

check_include_file ("${LIBUSBDIR}/include/libusb-1.0/libusb.h" HAVE_LIBUSB)
if (HAVE_LIBUSB AND HAVE_PTHREAD AND HAVE_LIBRT_STATIC)
	message(STATUS "  static libusb 1.0 found (${LIBUSB_STATIC_LIBRARIES}). Adding smartreader support ")
	add_definitions ("-DWITH_LIBUSB")
else (HAVE_LIBUSB AND HAVE_PTHREAD AND HAVE_LIBRT_STATIC)
	message(STATUS "  static libusb 1.0 not found. No smartreader support ")
endif (HAVE_LIBUSB AND HAVE_PTHREAD AND HAVE_LIBRT_STATIC)

set (lib_name "csctapi")
add_library (${lib_name} STATIC ${csctapi_srcs} ${csctapi_hdrs})
