# 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/libtpcrec) 
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/libtpcrec) 
link_directories (${CMAKE_BINARY_DIR}/v1/libtpccurveio) 

# Add executable that is built from the source file(s) 
# The extensions are automatically found. 
add_executable (atnmake atnmake.c) 
add_executable (dft2img dft2img.c) 
add_executable (e63emhdr e63emhdr.c) 
add_executable (e63mdel e63mdel.c) 
add_executable (e63mreg e63mreg.c) 
add_executable (e63to7 ehdrconv.c e63to7.c) 
add_executable (e7emhdr e7emhdr.c) 
add_executable (e7evhdr e7evhdr.c) 
add_executable (e7to63 ehdrconv.c e7to63.c) 
add_executable (ecalibr ecalibr.c) 
add_executable (ecatcat ecatcat.c) 
add_executable (ecatfbp ecatfbp.c) 
add_executable (ecatmrp ecatmrp.c) 
add_executable (ecatnorm ecatnorm.c) 
add_executable (ecattime ecattime.c) 
add_executable (efixplnr efixplnr.c) 
add_executable (eframe eframe.c) 
add_executable (egetstrt egetstrt.c) 
add_executable (esetstrt esetstrt.c) 
add_executable (esplit esplit.c) 
add_executable (img2scn img2scn.c) 
add_executable (lmhdr lmhdr.c) 
add_executable (lmlist lmlist.c) 
add_executable (lshdr lshdr.c) 

# Link the executable to the libraries. 
target_link_libraries (atnmake tpcrec tpcimgio tpcmisc m)
target_link_libraries (dft2img tpccurveio tpcroi tpcimgp tpcimgio tpcmisc m)
target_link_libraries (e63emhdr tpcimgio tpcmisc m)
target_link_libraries (e63mdel tpcimgio tpcmisc m)
target_link_libraries (e63mreg tpcimgio tpcmisc m)
target_link_libraries (e63to7 tpcimgio tpcmisc m)
target_link_libraries (e7emhdr tpcimgio tpcmisc m)
target_link_libraries (e7evhdr tpcimgio tpcmisc m)
target_link_libraries (e7to63 tpcimgio tpcmisc m)
target_link_libraries (ecalibr tpcimgio tpcmisc m)
target_link_libraries (ecatcat tpcimgio tpcmisc m)
target_link_libraries (ecatfbp tpcrec tpcimgio tpcmisc m)
target_link_libraries (ecatmrp tpcrec tpcimgio tpcmisc m)
target_link_libraries (ecatnorm tpcimgio tpcmisc m)
target_link_libraries (ecattime tpcimgio tpcmisc m)
target_link_libraries (efixplnr tpcimgio tpcmisc m)
target_link_libraries (eframe tpcimgp tpcimgio tpcmisc m)
target_link_libraries (egetstrt tpcimgio tpcmisc m)
target_link_libraries (esetstrt tpcimgio tpcmisc m)
target_link_libraries (esplit tpcimgio tpcmisc m)
target_link_libraries (img2scn tpcrec tpcimgio tpcmisc m)
target_link_libraries (lmhdr tpcimgio tpcmisc m)
target_link_libraries (lmlist tpcimgio tpcmisc m)
target_link_libraries (lshdr tpcimgio tpcmisc m)

# Install the executable(s)
INSTALL(
  PROGRAMS
  ${CMAKE_CURRENT_BINARY_DIR}/atnmake${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/dft2img${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/e63emhdr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/e63mdel${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/e63mreg${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/e63to7${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/e7emhdr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/e7evhdr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/e7to63${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ecalibr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ecatcat${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ecatfbp${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ecatmrp${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ecatnorm${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/ecattime${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/efixplnr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/eframe${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/egetstrt${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/esetstrt${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/esplit${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/img2scn${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/lmhdr${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/lmlist${CMAKE_EXECUTABLE_SUFFIX}
  ${CMAKE_CURRENT_BINARY_DIR}/lshdr${CMAKE_EXECUTABLE_SUFFIX}
  DESTINATION bin
  COMPONENT applications
)

# Copy test data folder
add_custom_target(ecattests 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(atnmakeUsage atnmake "--help")
add_test(dft2imgUsage dft2img "--help")
add_test(e63emhdrUsage e63emhdr "--help")
add_test(e63mdelUsage e63mdel "--help")
add_test(e63mregUsage e63mdel "--help")
add_test(e63to7Usage e63to7 "--help")
add_test(e7emhdrUsage e7emhdr "--help")
add_test(e7evhdrUsage e7evhdr "--help")
add_test(e7to63Usage e7to63 "--help")
add_test(ecalibrUsage ecalibr "--help")
add_test(ecatcatUsage ecatcat "--help")
add_test(ecatfbpUsage ecatfbp "--help")
add_test(ecatmrpUsage ecatmrp "--help")
add_test(ecatnormUsage ecatnorm "--help")
add_test(ecattimeUsage ecattime "--help")
add_test(efixplnrUsage efixplnr "--help")
add_test(eframeUsage eframe "--help")
add_test(egetstrtUsage egetstrt "--help")
add_test(esetstrtUsage esetstrt "--help")
add_test(esplitUsage esplit "--help")
add_test(img2scnUsage img2scn "--help")
add_test(lmhdrUsage lmhdr "--help")
add_test(lmlistUsage lmlist "--help")
add_test(lshdrUsage lshdr "--help")
set_tests_properties(
  atnmakeUsage dft2imgUsage e63emhdrUsage e63mdelUsage e63mregUsage e63to7Usage e7emhdrUsage
  e7evhdrUsage e7to63Usage ecalibrUsage ecatcatUsage ecatfbpUsage ecatmrpUsage ecatnormUsage
  ecattimeUsage efixplnrUsage eframeUsage egetstrtUsage esetstrtUsage esplitUsage img2scnUsage
  lmhdrUsage lmlistUsage lshdrUsage
  PROPERTIES PASS_REGULAR_EXPRESSION "Usage: *"
)


#
# Run test scripts
# 
add_test (
  NAME atnmakeTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/atnmake
  COMMAND bash "./test_atnmake.sh"
)
set_tests_properties(atnmakeTests PROPERTIES LABELS slow)
add_test (
  NAME dft2imgTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/dft2img
  COMMAND bash "./test_dft2img.sh"
)
add_test (
  NAME e63emhdrTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/e63emhdr
  COMMAND bash "./test_e63emhdr.sh"
)
add_test (
  NAME e63mdelTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/e63mdel
  COMMAND bash "./test_e63mdel.sh"
)
add_test (
  NAME e63mregTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/e63mreg
  COMMAND bash "./test_e63mreg.sh"
)
add_test (
  NAME e63to7Tests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/e63to7
  COMMAND bash "./test_e63to7.sh"
)
add_test (
  NAME e7emhdrTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/e7emhdr
  COMMAND bash "./test_e7emhdr.sh"
)
add_test (
  NAME e7evhdrTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/e7evhdr
  COMMAND bash "./test_e7evhdr.sh"
)
add_test (
  NAME e7to63Tests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/e7to63
  COMMAND bash "./test_e7to63.sh"
)
add_test (
  NAME ecalibrTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ecalibr
  COMMAND bash "./test_ecalibr.sh"
)
add_test (
  NAME ecatcatTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ecatcat
  COMMAND bash "./test_ecatcat.sh"
)
add_test (
  NAME ecatfbpTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ecatfbp
  COMMAND bash "./test_ecatfbp.sh"
)
add_test (
  NAME ecatmrpTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ecatmrp
  COMMAND bash "./test_ecatmrp.sh"
)
set_tests_properties(ecatmrpTests PROPERTIES LABELS slow)
add_test (
  NAME ecatnormTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ecatnorm
  COMMAND bash "./test_ecatnorm.sh"
)
add_test (
  NAME ecattimeTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/ecattime
  COMMAND bash "./test_ecattime.sh"
)
add_test (
  NAME eframeTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/eframe
  COMMAND bash "./test_eframe.sh"
)
add_test (
  NAME egetstrtTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/egetstrt
  COMMAND bash "./test_egetstrt.sh"
)
add_test (
  NAME esetstrtTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/esetstrt
  COMMAND bash "./test_esetstrt.sh"
)
add_test (
  NAME img2scnTests
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/img2scn
  COMMAND bash "./test_img2scn.sh"
)
