# Copyright (C) 2020, 2021 |Meso|Star> (contact@meso-star.com)
# Copyright (C) 2015, 2016 CNRS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.1)
enable_testing()
project(schiff C)

set(SCHIFF_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src/)

################################################################################
# Check dependencies
################################################################################
get_filename_component(_current_source_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
set(LibYAML_DIR ${_current_source_dir}/)

find_package(LibYAML REQUIRED)
find_package(RCMake 0.4 REQUIRED)
find_package(RSys 0.8 REQUIRED)
find_package(Star3D 0.8 REQUIRED)
find_package(StarSchiff 0.4.1 REQUIRED)
find_package(StarSP 0.12 REQUIRED)

include_directories(
  ${LibYAML_INCLUDE_DIR}
  ${RSys_INCLUDE_DIR}
  ${Star3D_INCLUDE_DIR}
  ${StarSchiff_INCLUDE_DIR}
  ${StarSP_INCLUDE_DIR}
  ${CMAKE_CURRENT_BINARY_DIR})

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
include(rcmake)
include(rcmake_runtime)

################################################################################
# Configure and define the targets
################################################################################
set(VERSION_MAJOR 0)
set(VERSION_MINOR 4)
set(VERSION_PATCH 2)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})

set(SCHIFF_ARGS_DEFAULT_NINSAMPLES "100")
set(SCHIFF_ARGS_DEFAULT_NREALISATIONS "10000")
set(SCHIFF_ARGS_DEFAULT_NANGLES "1000")
set(SCHIFF_ARGS_DEFAULT_NANGLES_INV "2000")

configure_file(${SCHIFF_SOURCE_DIR}/schiff_version.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/schiff_version.h @ONLY)
configure_file(${SCHIFF_SOURCE_DIR}/schiff_args.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/schiff_args.h @ONLY)
configure_file(${PROJECT_SOURCE_DIR}/../doc/schiff.1.in
  ${CMAKE_CURRENT_BINARY_DIR}/schiff.1 @ONLY)

set(SCHIFF_FILES_SRC
  schiff.c
  schiff_args.c
  schiff_geometry.c
  schiff_mesh.c
  schiff_optical_properties.c)
set(SCHIFF_FILES_INC
  schiff_geometry.h
  schiff_mesh.h
  schiff_optical_properties.h
  schiff_streamline.h)
set(SCHIFF_FILES_DOC COPYING README.md)

# Prepend each file in the `SCHIFF_FILES_<SRC|INC|DOC>' list by the absolute
# path of the directory in which they lie
rcmake_prepend_path(SCHIFF_FILES_SRC ${SCHIFF_SOURCE_DIR})
rcmake_prepend_path(SCHIFF_FILES_INC ${SCHIFF_SOURCE_DIR})
rcmake_prepend_path(SCHIFF_FILES_DOC ${PROJECT_SOURCE_DIR}/../)

set(SCHIFF_FILES_MAN1
  ${CMAKE_CURRENT_BINARY_DIR}/schiff.1)
set(SCHIFF_FILES_MAN5
  ${PROJECT_SOURCE_DIR}/../doc/schiff-geometry.5
  ${PROJECT_SOURCE_DIR}/../doc/schiff-output.5)

if(CMAKE_COMPILER_IS_GNUCC)
  set(MATH_LIB m)
endif()

add_executable(schiff ${SCHIFF_FILES_SRC} ${SCHIFF_FILES_INC})
target_link_libraries(schiff LibYAML RSys Star3D StarSchiff StarSP ${MATH_LIB})
set_target_properties(schiff PROPERTIES
  VERSION ${VERSION}
  SOVERSION ${VERSION_MAJOR})
rcmake_copy_runtime_libraries(schiff)

################################################################################
# Tests
################################################################################
# Check that the following command exists
find_program(_bash bash)
find_program(_bc bc)
find_program(_csplit csplit)
find_program(_env env)

if(NOT _bash OR NOT _bc OR NOT _csplit OR NOT _env)
  message(WARNING "Cannot setup the bash test script")
else()
  add_test(
    NAME test_schiff_cylinder
    COMMAND
      ${_env} PATH=$ENV{PATH}:$<TARGET_FILE_DIR:schiff>
      ${_bash} ${SCHIFF_SOURCE_DIR}/test_schiff_cylinder.sh)
  add_test(
    NAME test_schiff_sphere
    COMMAND
      ${_env} PATH=$ENV{PATH}:$<TARGET_FILE_DIR:schiff>
      ${_bash} ${SCHIFF_SOURCE_DIR}/test_schiff_sphere.sh
    WORKING_DIRECTORY ${_test_sphere_path})
endif()

################################################################################
# Define output & install directories
################################################################################
install(TARGETS schiff
  ARCHIVE DESTINATION bin
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin)
install(FILES ${SCHIFF_FILES_DOC} DESTINATION share/doc/schiff)
install(FILES ${SCHIFF_FILES_MAN1} DESTINATION share/man/man1)
install(FILES ${SCHIFF_FILES_MAN5} DESTINATION share/man/man5)
rcmake_install_runtime_libraries(schiff)

