cmake_minimum_required(VERSION 3.2.2 FATAL_ERROR)
###############################################################################################################
# This script will build and run the examples from a folder
# Execute from a command line:
#     ctest -S H4H5_Examples.cmake,OPTION=VALUE -C Release -VV -O test.log
###############################################################################################################

set(CTEST_CMAKE_GENERATOR "@CMAKE_GENERATOR@")
if("@CMAKE_GENERATOR_TOOLSET@")
  set(CMAKE_GENERATOR_TOOLSET "@CMAKE_GENERATOR_TOOLSET@")
endif()
set(CTEST_DASHBOARD_ROOT ${CTEST_SCRIPT_DIRECTORY})

# handle input parameters to script.
#INSTALLDIR - H4H5 root folder
#CTEST_CONFIGURATION_TYPE - Release, Debug, RelWithDebInfo
#CTEST_SOURCE_NAME - name of source folder; H4H5Examples
#STATIC_ONLY - Default is Yes
if(DEFINED CTEST_SCRIPT_ARG)
    # transform ctest script arguments of the form
    # script.ctest,var1=value1,var2=value2
    # to variables with the respective names set to the respective values
    string(REPLACE "," ";" script_args "${CTEST_SCRIPT_ARG}")
    foreach(current_var ${script_args})
        if ("${current_var}" MATCHES "^([^=]+)=(.+)$")
            set("${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}")
        endif()
    endforeach()
endif()

if(NOT DEFINED INSTALLDIR)
    set(INSTALLDIR "@CMAKE_INSTALL_PREFIX@")
endif()
if(NOT DEFINED HDFINSTALLDIR)
  set(HDFINSTALLDIR "${CTEST_DASHBOARD_ROOT}/HDF_Group/HDF/4.2.13")
endif()
if(NOT DEFINED HDF5INSTALLDIR)
  set(HDF5INSTALLDIR "${CTEST_DASHBOARD_ROOT}/HDF_Group/HDF5/1.11")
endif()

set(CTEST_CONFIGURATION_TYPE "$ENV{CMAKE_CONFIG_TYPE}")
if(NOT DEFINED CTEST_CONFIGURATION_TYPE)
    set(CTEST_CONFIGURATION_TYPE "Release")
endif()

if(NOT DEFINED CTEST_SOURCE_NAME)
    set(CTEST_SOURCE_NAME "H4H5Examples")
endif()

if(NOT DEFINED STATIC_ONLY)
    set(STATICLIBRARIES "YES")
else(NOT DEFINED STATIC_ONLY)
    set(STATICLIBRARIES "NO")
endif()

if(NOT DEFINED HDF_LOCAL)
    set(CDASH_LOCAL "NO")
else()
    set(CDASH_LOCAL "YES")
endif()
if(NOT DEFINED CTEST_SITE)
    set(CTEST_SITE "local")
endif()
if(NOT DEFINED CTEST_BUILD_NAME)
    set(CTEST_BUILD_NAME "examples")
endif()
set(ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DSITE:STRING=${CTEST_SITE} -DBUILDNAME:STRING=${CTEST_BUILD_NAME}")

#TAR_SOURCE - name of tarfile
#if(NOT DEFINED TAR_SOURCE)
#  set(CTEST_USE_TAR_SOURCE "H4H5Examples-0.1.1-Source")
#endif()

###############################################################################################################
#     Adjust the following SET Commands as needed
###############################################################################################################
if(WIN32)
  if(${STATICLIBRARIES})
    set(ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DBUILD_SHARED_LIBS:BOOL=ON -DUSE_SHARED_LIBS:BOOL=ON")
  endif()
  set(SITE_OS_NAME "Windows")
  set(CTEST_BINARY_NAME ${CTEST_SOURCE_NAME}\\build)
  set(CTEST_SOURCE_DIRECTORY "${CTEST_DASHBOARD_ROOT}\\${CTEST_SOURCE_NAME}")
  set(CTEST_BINARY_DIRECTORY "${CTEST_DASHBOARD_ROOT}\\${CTEST_BINARY_NAME}")
  set(ENV{H4H5_DIR} "${INSTALLDIR}/cmake")
  set(ENV{HDF4_DIR} "${HDFINSTALLDIR}/cmake")
  set(ENV{HDF5_DIR} "${HDF5INSTALLDIR}/cmake")
  set(ENV{PATH} "$ENV{PATH};${HDFINSTALLDIR};${HDF5INSTALLDIR}")
else()
  if(${STATICLIBRARIES})
    set(ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DBUILD_SHARED_LIBS:BOOL=ON -DUSE_SHARED_LIBS:BOOL=ON -DCMAKE_ANSI_CFLAGS:STRING=-fPIC")
  endif()
  set(ENV{H4H5_DIR} "${INSTALLDIR}/share/cmake")
  set(ENV{HDF4_DIR} "${HDFINSTALLDIR}/share/cmake")
  set(ENV{HDF5_DIR} "${HDF5INSTALLDIR}/share/cmake")
  set(ENV{LD_LIBRARY_PATH} "${INSTALLDIR}/lib:${HDFINSTALLDIR}/lib:${HDF5INSTALLDIR}/lib")
  set(CTEST_BINARY_NAME ${CTEST_SOURCE_NAME}/build)
  set(CTEST_SOURCE_DIRECTORY "${CTEST_DASHBOARD_ROOT}/${CTEST_SOURCE_NAME}")
  set(CTEST_BINARY_DIRECTORY "${CTEST_DASHBOARD_ROOT}/${CTEST_BINARY_NAME}")
endif()
if(${CDASH_LOCAL})
  set(ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DCDASH_LOCAL:BOOL=ON")
endif()
set(ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DH4H5_PACKAGE_NAME:STRING=@H4H5_PACKAGE@@HDF_PACKAGE_EXT@")
if(win32)
  set(ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DCTEST_CONFIGURATION_TYPE:STRING=${CTEST_CONFIGURATION_TYPE}")
endif()
##################################################################

###############################################################################################################
# For any comments please contact cdashhelp@hdfgroup.org
#
###############################################################################################################

if(WIN32)
  include(${CTEST_SCRIPT_DIRECTORY}\\H4H5_Examples_options.cmake)
  include(${CTEST_SCRIPT_DIRECTORY}\\CTestScript.cmake)
else()
  include(${CTEST_SCRIPT_DIRECTORY}/H4H5_Examples_options.cmake)
  include(${CTEST_SCRIPT_DIRECTORY}/CTestScript.cmake)
endif()
