cmake_minimum_required(VERSION 2.8.6)
project(Cora CXX)

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-std=c++11" COMPILER_SUPPORTS_CXX11)
if(COMPILER_SUPPORTS_CXX11)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
	check_cxx_compiler_flag("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
	if(COMPILER_SUPPORTS_CXX0X)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
	else()
		message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
	endif()
endif()

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I.")
# set (CMAKE_SHARED_LINKER_FLAGS "-rdynamic")

SET(CMAKE_BUILD_TYPE DEBUG)
SET(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0")

IF(NOT DEFINED INSTALL_DIR)
#   IF (NOT DEFINED CMAKE_INSTALL_DIR)
    set(INSTALL_DIR "/usr/local")
#   ELSE()
#     set(INSTALL_DIR "${CMAKE_INSTALL_DIR}")
#   ENDIF()
ENDIF()

find_package(Curses)
if("${CURSES_FOUND}")
	add_executable(cora cora.cc)
	target_link_libraries(cora dl)
	target_link_libraries(cora awalidyn)
	target_link_libraries(cora ncurses)
	
	add_executable(__cora_autocompletion autocompletion.cc)
	target_link_libraries(__cora_autocompletion dl)
	target_link_libraries(__cora_autocompletion awalidyn)
	include_directories("..")
	
	if (TARGET check)
	else()
	  add_custom_target(check)
	endif()
	
	add_subdirectory(extras)
	add_subdirectory(tests)
	
	###############################################################################
	##                           INSTALLATION COMMANDS                           ##
	###############################################################################
	install( TARGETS cora DESTINATION "${INSTALL_DIR}/bin" )
	install( TARGETS __cora_autocompletion DESTINATION "${INSTALL_DIR}/bin" )
	
	## Trying to install autocompletion files for cora ############################
	
  if (NOT DEFINED INSTALL_BASH_COMPL_DIR)
    find_path( INSTALL_BASH_COMPL_DIR
               NAMES "bash_completion.d"
               HINTS "/" "/opt" "/opt/local"  
               PATH_SUFFIXES "etc" )
  ENDIF()
	if ( "${INSTALL_BASH_COMPL_DIR}" STREQUAL "INSTALL_BASH_COMPL_DIR-NOTFOUND" )
	  message( WARNING "Could not find 'bash_completion.d' directory. Bash-completion support for cora will not be installed. Use option -DINSTALL_BASH_COMPL_DIR=<path> to provide custom path." )
	else()
	  install( FILES "${CMAKE_SOURCE_DIR}/cora/autocompletion-cora.bash" DESTINATION "${INSTALL_BASH_COMPL_DIR}/bash_completion.d" )
	endif ()
  
  if (NOT DEFINED INSTALL_ZSH_COMPL_DIR)
    find_path( INSTALL_ZSH_COMPL_DIR
              NAMES "site-functions"
              HINTS "/etc" "/usr/local/share" "/usr/share" "/usr" "/opt" "/opt/local"
              PATH_SUFFIXES "zsh" )
  ENDIF()

	if ( "${INSTALL_ZSH_COMPL_DIR}" STREQUAL "INSTALL ZSH_COMPL_DIR-NOTFOUND" )
	  message( WARNING "Could not find 'site-functions' directory.  Zsh-completion support for cora will not be installed. Use option -DINSTALL_ZSH_COMPL_DIR=<path> to provide custom path." )
	else()
	#  install( FILES "${CMAKE_SOURCE_DIR}/cora/autocompletion-cora.zsh"
	#           DESTINATION "${ZSH_COMPL_DIR}/site-functions"
	#           RENAME _cora )
	  install(CODE "execute_process(COMMAND ${CMAKE_COMMAND}
	      -D \"SOURCE=${CMAKE_SOURCE_DIR}/cora/autocompletion-cora.zsh\"
	      -D \"DESTINATION=${INSTALL_ZSH_COMPL_DIR}/site-functions\"
	      -D \"MANIFEST=${CMAKE_CURRENT_BINARY_DIR}/tmp.txt\"
	      -P \"${CMAKE_CURRENT_SOURCE_DIR}/CMakeScripts/install_zsh_comp.cmake\")")
	
	  install(CODE "file(READ \"${CMAKE_CURRENT_BINARY_DIR}/tmp.txt\" CONTENT)")
	  install(CODE "SET (CMAKE_INSTALL_MANIFEST_FILES \"\${CMAKE_INSTALL_MANIFEST_FILES};\${CONTENT}\")")
	endif ()
else()
  message(WARNING "NCurses not found. Cora won't be compiled.")
endif()


###############################################################################
##                          UNINSTALLATION COMMANDS                          ##
###############################################################################

if(TARGET uninstall)
else()
  add_custom_target ( uninstall
    COMMAND ${CMAKE_COMMAND}
        -D "DATA_TO_REMOVE=${CMAKE_BINARY_DIR}/install_manifest.txt"
        -P "${CMAKE_CURRENT_SOURCE_DIR}/../CMakeScripts/remove_data.cmake"
  )
endif()
