# Make sure the compiler can find include files from our libraries. 
include_directories (${CMAKE_SOURCE_DIR}/v2/libtpcextensions) 
include_directories (${CMAKE_SOURCE_DIR}/v2/libtpcisotope) 
include_directories (${CMAKE_SOURCE_DIR}/v2/libtpcift) 
include_directories (${CMAKE_SOURCE_DIR}/v2/libtpcfileutil) 
include_directories (${CMAKE_SOURCE_DIR}/v2/libtpccsv) 
include_directories (${CMAKE_SOURCE_DIR}/v2/libtpctac) 

# Make sure the linker can find the libraries once they are built. 
link_directories (${CMAKE_BINARY_DIR}/v2/libtpcextensions) 
link_directories (${CMAKE_BINARY_DIR}/v2/libtpcisotope) 
link_directories (${CMAKE_BINARY_DIR}/v2/libtpcift) 
link_directories (${CMAKE_BINARY_DIR}/v2/libtpcfileutil) 
link_directories (${CMAKE_BINARY_DIR}/v2/libtpccsv) 
link_directories (${CMAKE_BINARY_DIR}/v2/libtpctac) 

# Add executable that is built from the source file(s) 
# The extensions are automatically found. 
add_executable (dcftime dcftime.c) 
add_executable (halflife halflife.c) 
add_executable (tacdecay tacdecay.c) 

# Link the executable to the libraries. 
target_link_libraries (dcftime tpcisotope tpcextensions m)
target_link_libraries (halflife tpcisotope tpcextensions m)
target_link_libraries (tacdecay tpctac tpcift tpccsv tpcisotope tpcextensions m)

# Install the executable(s)
install (
  PROGRAMS 
  ${CMAKE_CURRENT_BINARY_DIR}/dcftime${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/halflife${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/tacdecay${CMAKE_EXECUTABLE_SUFFIX}
  DESTINATION bin
  COMPONENT applications
)

# Copy test data folder
add_custom_target(halflifetests ALL
  COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/test
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/test ${CMAKE_CURRENT_BINARY_DIR}/test/
)
# And delete test data folder with clean
set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${CMAKE_CURRENT_BINARY_DIR}/test")

#
# Simple tests
#

# Does does the usage message work
add_test (dcftimeUsage dcftime "--help")
add_test (halflifeUsage halflife "--help")
add_test (tacdecayUsage tacdecay "--help")
set_tests_properties(
  dcftimeUsage halflifeUsage tacdecayUsage
  PROPERTIES PASS_REGULAR_EXPRESSION "Usage: *"
)

# Invalid option or argument must return an error
add_test(halflifeWrongArgument halflife "StupidArg")
set_tests_properties(halflifeWrongArgument PROPERTIES WILL_FAIL TRUE)

#
# Run test scripts
#
add_test (
  NAME dcftimeTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/dcftime
  COMMAND bash "./test_dcftime.sh"
)
add_test (
  NAME halflifeTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/halflife
  COMMAND bash "./test_halflife.sh"
)
add_test (
  NAME tacdecayTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/tacdecay
  COMMAND bash "./test_tacdecay.sh"
)
