set(GCC34 0)
if(CMAKE_COMPILER_IS_GNUCC)
	# Use the maximum optimization level for release builds
	foreach(var CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO)
		if(${var} MATCHES "-O2")
			string(REGEX REPLACE "-O2" "-O3" ${var} "${${var}}")
		endif()
	endforeach()
	execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
		OUTPUT_VARIABLE GCC_VERSION)

	# NOTE: Setting GCC34=1 enables MMX code in Xorg.  We can't enable it on
	# Solaris because the version of GCC that ships with Solaris 10 doesn't put
	# mmintrin.h in a sane place.
	if((GCC_VERSION VERSION_GREATER 3.4 OR GCC_VERSION VERSION_EQUAL 3.4)
		AND NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS")
		set(GCC34 1)
	endif()
endif()

if("${CMAKE_C_COMPILER_ID}" MATCHES SunPro)
	# Use the maximum optimization level for release builds
	foreach(var CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO)
		if(${var} MATCHES "-xO3")
			string(REGEX REPLACE "-xO3" "-xO5" ${var} "${${var}}")
		endif()
		if(${var} MATCHES "-xO2")
			string(REGEX REPLACE "-xO2" "-xO5" ${var} "${${var}}")
		endif()
	endforeach()
endif()

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
	set(CMAKE_LIBRARY_PATH /usr/lib/${CPU_TYPE}-linux-gnu;/usr/lib${BITS};/usr/lib)
endif()

find_package(X11 REQUIRED)

message(STATUS "X11_X11_LIB = ${X11_X11_LIB}")

set(DEFAULT_TVNC_MANDIR ${CMAKE_INSTALL_PREFIX}/man)
set(TVNC_MANDIR ${DEFAULT_TVNC_MANDIR} CACHE PATH
	"Directory into which man pages should be installed (default: ${DEFAULT_TVNC_MANDIR})")
message(STATUS "TVNC_MANDIR = ${TVNC_MANDIR}")

include_directories(include)


#
# Viewer build
#

if(TVNC_BUILDNATIVE)
	add_subdirectory(vncviewer)
endif()


#
# Server build
#

if(TVNC_BUILDSERVER)

add_subdirectory(${CMAKE_SOURCE_DIR}/common ${CMAKE_BINARY_DIR}/common)
add_subdirectory(libvncauth)
add_subdirectory(tvncconfig)
add_subdirectory(vncconnect)
add_subdirectory(vncpasswd)
add_subdirectory(Xvnc)

string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UC)

set(EFFECTIVE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UC}}")

message(STATUS "Compiler flags = ${EFFECTIVE_C_FLAGS}")
message(STATUS "Linker flags = ${CMAKE_EXE_LINKER_FLAGS}")

set(DEFAULT_TVNC_CONFDIR ${CMAKE_INSTALL_PREFIX}/etc)
set(TVNC_CONFDIR ${DEFAULT_TVNC_CONFDIR} CACHE PATH
	"Directory into which configuration files should be installed (default: ${DEFAULT_TVNC_CONFDIR})")
message(STATUS "TVNC_CONFDIR = ${TVNC_CONFDIR}")

configure_file(vncserver.in ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/vncserver @ONLY)
execute_process(COMMAND chmod +x vncserver
	WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

install(PROGRAMS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/vncserver
	DESTINATION ${TVNC_BINDIR})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/vncserver.man
	DESTINATION ${TVNC_MANDIR}/man1 RENAME vncserver.1)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/turbovncserver.conf
	DESTINATION ${TVNC_CONFDIR})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/turbovncserver-security.conf
	DESTINATION ${TVNC_CONFDIR})

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
	configure_file(vncserver.init.in vncserver.init @ONLY)
	install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/vncserver.init
		DESTINATION ${TVNC_CONFDIR}/init.d RENAME tvncserver)
	install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/tvncservers
		DESTINATION ${TVNC_CONFDIR}/sysconfig)
endif()

endif() # TVNC_BUILDSERVER
