# Scripts to assist developers
# Mostly these are calling the test suite driver and validator classes in Java

# TODO More Test Targets
# Rebasing
# com.kitware.physiology.utilities.testing.Rebase [from to (config|ALL)]
#
# Ploting
# com.kitware.physiology.utilities.csv.plots.PlotDriver
#
# Reconfigure the config file (in memory) so we can test serialization
# com.kitware.physiology.utilities.testing.Reconfiguration

find_package(Java REQUIRED)
include(UseJava)
if(_JAVA_HOME)
  # Overwrite Java if the env variable is set
  
  message(STATUS "Using JAVA_HOME as my Java executable (set to : ${_JAVA_HOME})")
  set(Java_JAVA_EXECUTABLE ${_JAVA_HOME}/bin/java)
endif()
#  You tell me what JDK/JRE you want to use
#set(Java_JAVA_EXECUTABLE "/the_jdk_I_want/bin/java)

set(JAVA_CLASSPATH Pulse.jar
        "@CMAKE_SOURCE_DIR@/jar/javassist-3.16.1-GA.jar"
        "@CMAKE_SOURCE_DIR@/jar/jcommon-1.0.16.jar"
        "@CMAKE_SOURCE_DIR@/jar/jdom-2.0.2.jar"
        "@CMAKE_SOURCE_DIR@/jar/jfreechart-1.0.13.jar"
        "@CMAKE_SOURCE_DIR@/jar/gson-2.8.5.jar"
        "@CMAKE_SOURCE_DIR@/jar/guava-26.0-jre.jar"
        "@CMAKE_SOURCE_DIR@/jar/log4j-1.2.17.jar"
        "@CMAKE_SOURCE_DIR@/jar/poi-3.13-20150929.jar"
        "@CMAKE_SOURCE_DIR@/jar/poi-ooxml-3.13-20150929.jar"
        "@CMAKE_SOURCE_DIR@/jar/poi-ooxml-schemas-3.13-20150929.jar"
        "@CMAKE_SOURCE_DIR@/jar/pdfbox-2.0.0-RC3.jar"
        "@CMAKE_SOURCE_DIR@/jar/reflections-0.9.9-RC1-uberjar.jar"
        "@CMAKE_SOURCE_DIR@/jar/xmlbeans-2.6.0.jar"
        "@CMAKE_SOURCE_DIR@/jar/zip4j-1.3.1.jar"
        "@CMAKE_SOURCE_DIR@/jar/org.eclipse.jgit-4.11.0.201803080745-r.jar"
        "@CMAKE_SOURCE_DIR@/jar/slf4j-api-1.7.25.jar"
        "@CMAKE_SOURCE_DIR@/jar/slf4j-simple-1.7.25.jar"
        "@CMAKE_SOURCE_DIR@/jar/error_prone_annotations-2.3.2.jar")

if(NOT WIN32)
  string(REPLACE ";" ":" JAVA_CLASSPATH "${JAVA_CLASSPATH}")
endif()

if(TYPE STREQUAL "tests" OR TYPE STREQUAL "SystemValidation" OR TYPE STREQUAL "PatientValidation")
  if(TYPE STREQUAL "tests")
    execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SETestDriver CDMUnitTests.config)
    execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SETestDriver EngineUnitTests.config)
    execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SETestDriver ScenarioVerification.config)
  endif()
  if(TYPE STREQUAL "SystemValidation" OR TYPE STREQUAL "tests")
    execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SETestDriver SystemVerification.config)
    execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.validation.SystemValidation )
  endif()
  if(TYPE STREQUAL "PatientValidation" OR TYPE STREQUAL "tests")
    execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SETestDriver PatientVerification.config)
    execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.validation.PatientValidation)
  endif()
elseif(TYPE STREQUAL "genData")
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.pulse.dataset.DataSetReader)
elseif(TYPE STREQUAL "genStates")
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SETestDriver InitialPatientStates.config)
elseif(TYPE STREQUAL "doxygen")
  file(MAKE_DIRECTORY  "./docs/html")
  file(MAKE_DIRECTORY  "./docs/markdown")
  file(GLOB_RECURSE DOCS "./docs/html/*.*" "./docs/markdown/*.*")
  if(DOCS)
    file(REMOVE ${DOCS})
  endif()
  file(COPY "@CMAKE_SOURCE_DIR@/docs/Doxygen/bootstrap" DESTINATION ./docs/html)
  file(COPY "@CMAKE_SOURCE_DIR@/docs/Doxygen/font-awesome-4.3.0" DESTINATION ./docs/html)
  file(COPY "@CMAKE_SOURCE_DIR@/docs/Images" DESTINATION ./docs/html)
  # These are run above
  #execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.validation.SystemValidation)
  #execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.validation.PatientValidation)
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.datamodel.doxygen.DoxygenPreprocessor "@CMAKE_SOURCE_DIR@/docs/Markdown" ./docs/markdown)
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.datamodel.doxygen.DoxygenPreprocessor "@CMAKE_SOURCE_DIR@/docs/Methodology" ./docs/markdown ./test_results/tables)
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.datamodel.doxygen.Proto2Doxygen "@CMAKE_SOURCE_DIR@/schema/proto" ./docs/markdown)
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.datamodel.doxygen.CDM2MD ./docs/markdown)
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.utilities.csv.plots.PlotDriver PlotRun.config)
  execute_process(COMMAND doxygen ./docs/full.doxy)
elseif(TYPE STREQUAL "protoc")
  execute_process(COMMAND ${CMAKE_COMMAND} -DSCHEMA_SRC:STRING=@SCHEMA_SRC@ -DSCHEMA_DST:STRING=@SCHEMA_DST@ -P "@CMAKE_SOURCE_DIR@/schema/GenerateCDMBindings.cmake")
elseif(TYPE STREQUAL "updateBaselines")
  execute_process(COMMAND ${CMAKE_COMMAND} --build "@CMAKE_CURRENT_BINARY_DIR@" --target UpdateVerification)
elseif(TYPE STREQUAL "rebase")
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.Rebase)
elseif(TYPE STREQUAL "FullReport")
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SEReportResults)
else()
  execute_process(COMMAND "${Java_JAVA_EXECUTABLE}" -Xmx900m -classpath "${JAVA_CLASSPATH}" com.kitware.physiology.testing.SETestDriver ${TYPE}.config)
endif()
