# Make sure the compiler can find include files from our libraries. 
include_directories (${CMAKE_SOURCE_DIR}/v1/libtpcmisc) 
include_directories (${CMAKE_SOURCE_DIR}/v1/libtpcimgio) 
include_directories (${CMAKE_SOURCE_DIR}/v1/libtpcimgp) 
include_directories (${CMAKE_SOURCE_DIR}/v1/libtpcroi) 
include_directories (${CMAKE_SOURCE_DIR}/v1/libtpccurveio) 

# Make sure the linker can find the libraries once they are built. 
link_directories (${CMAKE_BINARY_DIR}/v1/libtpcmisc) 
link_directories (${CMAKE_BINARY_DIR}/v1/libtpcimgio) 
link_directories (${CMAKE_BINARY_DIR}/v1/libtpcimgp) 
link_directories (${CMAKE_BINARY_DIR}/v1/libtpcroi) 
link_directories (${CMAKE_BINARY_DIR}/v1/libtpccurveio) 

# Add executable that is built from the source file(s) 
# The extensions are automatically found. 
add_executable (ana_lhdr ana_lhdr.c) 
add_executable (ana_ehdr ana_ehdr.c) 
add_executable (anabyteo anabyteo.c) 
add_executable (ecat2ana ecat2ana.c) 
add_executable (ana2ecat ana2ecat.c) 

# Link the executable to the libraries. 
target_link_libraries (ana_lhdr tpcimgio tpcmisc m)
target_link_libraries (ana_ehdr tpcimgio tpcmisc m)
target_link_libraries (anabyteo tpcimgio tpcmisc m)
target_link_libraries (ecat2ana tpcimgp tpcimgio tpcmisc m)
target_link_libraries (ana2ecat tpcimgp tpcimgio tpcmisc m)

# Install the executable(s)
INSTALL(
  PROGRAMS
  ${CMAKE_CURRENT_BINARY_DIR}/ana_lhdr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ana_ehdr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/anabyteo${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ecat2ana${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ana2ecat${CMAKE_EXECUTABLE_SUFFIX}
  DESTINATION bin
  COMPONENT applications
)

# Copy test data folder
add_custom_target(analyzetests 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(ana_lhdrUsage ana_lhdr "--help")
add_test(ana_ehdrUsage ana_ehdr "--help")
add_test(anabyteoUsage anabyteo "--help")
add_test(ecat2anaUsage ecat2ana "--help")
add_test(ana2ecatUsage ana2ecat "--help")
set_tests_properties(
  ana_lhdrUsage ana_ehdrUsage anabyteoUsage ecat2anaUsage ana2ecatUsage
  PROPERTIES PASS_REGULAR_EXPRESSION "Usage: *"
)


#
# Run test scripts
# 
add_test (
  NAME ana_lhdrTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ana_lhdr
  COMMAND bash "./test_ana_lhdr.sh"
)
add_test (
  NAME ana_ehdrTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ana_ehdr
  COMMAND bash "./test_ana_ehdr.sh"
)
add_test (
  NAME ecat2anaTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ecat2ana
  COMMAND bash "./test_ecat2ana.sh"
)
add_test (
  NAME ana2ecatTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ana2ecat
  COMMAND bash "./test_ana2ecat.sh"
)
