cmake_minimum_required(VERSION 3.1)

get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
SET(PROJECT_NAME Lars${PROJECT_NAME})
project(${PROJECT_NAME})

FILE(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/include/lars/*.h")
get_directory_property(has_parent PARENT_DIRECTORY)

IF(LARS_LIBRARY)
  SET(lars_headers ${lars_headers} ${headers} PARENT_SCOPE)
  SET(lars_include_dirs ${lars_include_dirs} "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE)
ELSEIF(has_parent)
  SET(include_dirs ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/bundled/include)
  SET(${PROJECT_NAME}_INCLUDE_DIRS ${include_dirs} CACHE INTERNAL "")
ELSE()
  SET(LARS_BUILD_EXAMPLES TRUE)
  SET(LARS_BUILD_TESTS TRUE)
ENDIF()

IF(LARS_BUILD_EXAMPLES)
  SET(include_dirs ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/bundled/include)
  include_directories(${include_dirs})
  set(CMAKE_CXX_STANDARD 11)
  file( GLOB example_sources ${CMAKE_CURRENT_SOURCE_DIR}/examples/*.cpp )
  foreach( examplesourcefile ${example_sources} )
    get_filename_component(filename ${examplesourcefile} NAME)
    string( REPLACE ".cpp" "" examplename ${filename} )
    add_executable( ${examplename} ${examplesourcefile} ${headers})
  endforeach()
ENDIF()

IF(LARS_BUILD_TESTS)
  enable_testing()
  SET(include_dirs ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/bundled/include)
  include_directories(${include_dirs})
  set(CMAKE_CXX_STANDARD 11)
  file( GLOB test_sources ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp )
  foreach( testsourcefile ${test_sources} )
    get_filename_component(filename ${testsourcefile} NAME)
    string( REPLACE ".cpp" "" testname ${filename} )
    add_executable(${testname} ${testsourcefile} ${headers})
    add_test(NAME ${testname} COMMAND ${testname})
  endforeach()
ENDIF()
