#
# Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
#
# This file is part of Orfeo Toolbox
#
#     https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Current informations:
# Version 1.0B released on May, 2005.
# Download site : http://6s.ltdri.org/
#

set(OTB3P_INSTALL_EXPORT_NAME "${OTB6S-targets}")
set(OTB3P_INSTALL_INCLUDE_DIR "${OTB6S_INSTALL_INCLUDE_DIR}")
set(OTB3P_INSTALL_RUNTIME_DIR "${OTB6S_INSTALL_RUNTIME_DIR}")
set(OTB3P_INSTALL_LIBRARY_DIR "${OTB6S_INSTALL_LIBRARY_DIR}")
set(OTB3P_INSTALL_ARCHIVE_DIR "${OTB6S_INSTALL_ARCHIVE_DIR}")

# Inform header files they are being included from a source inside the
# otb_6S library.
add_definitions(-DOTB_6S_SRC)
add_definitions(-DNON_POSIX_STDIO)

# On Microsoft Visual
if(WIN32)
  if(NOT BORLAND)
    if(NOT CYGWIN)
      if(NOT MINGW)
#        if(NOT ITK_ENABLE_VISUAL_STUDIO_DEPRECATED_C_WARNINGS)
          add_definitions(
            -DUSE_CLOCK
            -DMSDOS
            -DNO_ONEXIT
            )
#        endif()
      else()
          add_definitions(
            -DUSE_CLOCK
            -DMSDOS
            -DNO_TRUNCATE  #For generate endfile.c
            )
      endif()
    endif()
  endif()
endif()


file(GLOB OTB6S_SRCS "6SV1.0B/*.c" )
file(GLOB LIBF2C_SRCS "libf2c/*.c" )
list(REMOVE_ITEM LIBF2C_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/libf2c/arithchk.c" )
list(REMOVE_ITEM LIBF2C_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/libf2c/main.c" )
list(REMOVE_ITEM LIBF2C_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/libf2c/getarg_.c" )
list(REMOVE_ITEM LIBF2C_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/libf2c/iargc_.c" )

list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/aeroprof_com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/multorder_com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_del__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_ier__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/num_quad__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_aer__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_disc__com.c" )

list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_planesim__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/mie_in__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_atm__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_ffu__com.c" )
list(REMOVE_ITEM OTB6S_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/6SV1.0B/sixs_test__com.c" )

# Main code source
set(OTB6S_MAIN_SRCS
  otb_main6S_function.c                # otb 6S main function
  main.c                               # ORIGINAL 6S main
  otb_main6S_program_call_function.c   # OTB 6S main (which call otb_main6S_function function)
  aeroprof_com.c                       # others *.c fils generated by f2c
  multorder_com.c
  sixs_del__com.c
  sixs_ier__com.c
  num_quad__com.c
  sixs_aer__com.c
  sixs_disc__com.c
  sixs_planesim__com.c
  mie_in__com.c
  sixs_atm__com.c
  sixs_ffu__com.c
  sixs_test__com.c
  )

add_library(otb6S ${LIBF2C_SRCS} ${OTB6S_SRCS} ${OTB6S_MAIN_SRCS})
if(UNIX)
    target_link_libraries(otb6S m)
endif()
if(OTB_LIBRARY_PROPERTIES)
  set_target_properties(otb6S PROPERTIES ${OTB_LIBRARY_PROPERTIES})
endif()

# Below "extra" compile flags are needed for proper functioning of 6S
# library. Without these flags especially /Oy- OpticalCalibration tests
# are failing. the flags are automatially added by VS generator.
# But due to time constraints and processing power available on testing VMs,
# using Visual Studio generator is not possible.
if(MSVC_VERSION GREATER 1800)
  set_target_properties(otb6S PROPERTIES COMPILE_FLAGS "/Ob1 /Oy- /GS")
endif()

install(TARGETS otb6S
  EXPORT ${OTB3P_INSTALL_EXPORT_NAME}
  RUNTIME DESTINATION ${OTB3P_INSTALL_RUNTIME_DIR} COMPONENT RuntimeLibraries
  LIBRARY DESTINATION ${OTB3P_INSTALL_LIBRARY_DIR} COMPONENT RuntimeLibraries
  ARCHIVE DESTINATION ${OTB3P_INSTALL_ARCHIVE_DIR} COMPONENT Development
  )

otb_module_target( otb6S NO_INSTALL )
