# Run cprnc on each of the output files against the target
# Note: we mix @ and $ notation, since at configure time we only
#       resolve @ variables. The remaning ones will be resolve by
#       cmake when the script is run with 'cmake -P CprncCxxVsF90.cmake'
FOREACH (NC_FILE_NAME @NC_OUTPUT_FILES@)
  EXECUTE_PROCESS (COMMAND  @CPRNC_BINARY@ @F90_DIR@/movies/${NC_FILE_NAME} @CXX_DIR@/movies/${NC_FILE_NAME}
                   RESULT_VARIABLE ERROR_CODE
                   OUTPUT_VARIABLE cprnc_stdout
                   ERROR_VARIABLE cprnc_stderr)

  # Search stdout for "IDENTICAL". -1 means it does not exist. Use REVERSE on
  # the off chance that makes it faster, since "IDENTICAL", if it exists, is
  # near the end of the output.
  string (FIND "${cprnc_stdout}" "IDENTICAL" identical_pos REVERSE)

  # If cprnc does not return 0, then the test failed
  IF (identical_pos EQUAL -1)
    MESSAGE ("${cprnc_stdout}")
    MESSAGE (FATAL_ERROR "Test did not succeed. Netcdf outputs '${NC_FILE_NAME}' differ.")
  ENDIF()
ENDFOREACH()

MESSAGE ("CXX and F90 netcdf outputs match.\n")

#stdout part: -----------------------------------------------------------------------------

STRING(FIND @CXX_TEST_NAME@ "preqx" preqxtest)
#preqxtest = -1 meants the test is not preqx test
#for preqx, comparisons are not run since cxx runs do not have push to F from cxx code,
#so in preqx, cxx output is meaningless.
IF(preqxtest EQUAL -1)

  SET(GREP_ARGS "  u     =")
  FOREACH(s
    #"  u     ="
    "  v     ="
    "  w     ="
    "  T     ="
    "  mu    ="
    "  vTh_dp="
    "  dz(m) ="
    "  dp    ="
    "    fu  ="
    "    fv  ="
    "    ft  ="
    "    fq1 ="
    "min dz/w"
    " qv(  1)="
    "    TBOT="
    "      ps="
    "      M ="
    "  dry M ="
    #"dKE/dt"
    #"dIE/dt"
    #"dPE/dt"
    "dQ1/dt"
    "Q  1,Q"
    #"(E-E0)"
    "(Q-Q0)"
    "pmax_")

    SET(GREP_ARGS "${GREP_ARGS}\\|${s}")
  ENDFOREACH()

  MESSAGE("grep_args are ${GREP_ARGS} -----------------------------------")

  EXECUTE_PROCESS (COMMAND grep ${GREP_ARGS} @F90_DIR@/@F90_TEST_NAME@_1.out
                   RESULT_VARIABLE ERROR_CODE
                   OUTPUT_VARIABLE grep_f90_stdout
                   ERROR_VARIABLE  grep_f90_stderr)
  EXECUTE_PROCESS (COMMAND grep ${GREP_ARGS} @CXX_DIR@/@CXX_TEST_NAME@_1.out
                   RESULT_VARIABLE ERROR_CODE
                   OUTPUT_VARIABLE grep_cxx_stdout
                   ERROR_VARIABLE  grep_cxx_stderr)

  IF (NOT "${grep_f90_stdout}" STREQUAL "${grep_cxx_stdout}")
    MESSAGE("Test did not succeed. CXX and F90 diagnostics outputs differ:\nf90:\n${grep_f90_stdout}\ncxx:\n${grep_cxx_stdout}")
    MESSAGE (FATAL_ERROR "Test did not succeed. Stdout outputs differ.")
  ELSE ()
    MESSAGE ("CXX and F90 diagnostics outputs match.\n")
  ENDIF()

ENDIF() # not preqx test
