# This is provided as an alternative to using git submodules for
# including TOAST in a project, for example in limited networking
# environments. GIT_REPOSITORY may be replaced with URL pointing
# to a local file; here git is only used as a transport mechanism
set(TOAST_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/TOAST-source")

# Reenable TOAST_LOCAL_INSTALL_DIR once `make install` works for
# TOAST and disable it below. This allows for a local installation
# of TOAST under ./build with a predicatable directory structure

# set(TOAST_LOCAL_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/TOAST-artifacts")

## Note: "<INSTALL_DIR>" is interpolated within ExternalProject_Add to
# TOAST_LOCAL_INSTALL_DIR

# list(APPEND TOAST_CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>")

ExternalProject_Add(
    TOAST_external
    # Note: Use URL and URL_HASH [SHA512|SHA256|MD5]=4A54C0DE... to
    # download and checksum an archive. Note that URL may refer to a
    # local file, allowing this to work without net access.
#    GIT_REPOSITORY         git@github.com:thomasms/toast.git
    GIT_REPOSITORY         https://github.com/thomasms/toast.git
    GIT_TAG                c1e313deaf21a4323b7e4e5b5686217b3d398efb
    SOURCE_DIR             "${TOAST_SOURCE_DIR}"
    # Enable after installation works
#    INSTALL_DIR            "${TOAST_LOCAL_INSTALL_DIR}"
    # Skip installation (not currently working). Disable
    # INSTALL_COMMAND argument once INSTALL_DIR works; CMake will
    # install TOAST to TOAST_LOCAL_INSTALL_DIR which makes
    # allows us to directly set TOAST_LIBRARY_DIR, TOAST_MODULE_DIR,
    # and TOAST_MODULE_FILE
    INSTALL_COMMAND        echo "Skipping install step for TOAST"
    CMAKE_ARGS             ${TOAST_CMAKE_ARGS}
    LOG_BUILD              YES
    USES_TERMINAL_DOWNLOAD YES
    USES_TERMINAL_UPDATE   YES
)

# TOAST fails to install; use subdirectories under TOAST-build for
# library and include directories. Alternately, use
# add_subdirectory(TOAST_SOURCE_DIR) and incorporate TOAST target into
# test app add_custom_target() configs
# TODO: Reduce the number of variables; relocate these to above call
# to ExternalProject_Add()
set(TOAST_LOCAL_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/TOAST_external-prefix/src/TOAST_external-build")
set(TOAST_ROOT_DIR "${TOAST_LOCAL_INSTALL_DIR}")
set(TOAST_LOCAL_LIBRARY_DIR "${TOAST_LOCAL_INSTALL_DIR}/lib")
set(TOAST_LOCAL_INCLUDE_DIR "${TOAST_LOCAL_INSTALL_DIR}/include")

##### Set Output Variables #####

# Set the following:
# - TOAST_LIB_NAME
# - TOAST_LIBRARY_DIR
# - TOAST_MODULE_DIR
# - TOAST_MODULE_FILE

# These variables are set to be compatible with the naming scheme used
# in original TOAST example CMake setup; see
# build/TOAST-source/examples/example1/CMakeLists.txt
set(TOAST_LIB_NAME toast)
set(TOAST_LIBRARY_DIR "${TOAST_LOCAL_LIBRARY_DIR}")
set(TOAST_MODULE_DIR "${TOAST_LOCAL_INCLUDE_DIR}")

# Note: This needs to be manually added to the list of source files
# required for unit tests (maybe...?)
set(TOAST_MODULE_FILE "${TOAST_LOCAL_INCLUDE_DIR}/toast.mod")

# Let the world know TOAST_MODULE_FILE is generated by TOAST_external
# The TOAST_external target should be added as a dependency to every unit
# test executable to ensure TOAST is downloaded and built before the unit
# test is compiled
add_custom_command(
    OUTPUT "${TOAST_MODULE_FILE}"
    COMMAND echo "Manually associating ${TOAST_MODULE_FILE} with TOAST_external"
    DEPENDS TOAST_external
)

# Signal that TOAST is available
set(TOAST_FOUND ON)
# __END__