# Standalone project that builds the legacy PARMA C++ code and generates the
# regression reference in tests/reference/.
#
#   cmake -S tests/legacy -B build-legacy -G Ninja -DCMAKE_BUILD_TYPE=Release
#   cmake --build build-legacy --target legacy_reference
#
# The legacy code reads "input/..." relative to the working directory, so data/
# is copied to <build>/run/input before running.
cmake_minimum_required(VERSION 3.16)
project(ParmaLegacy CXX)

set(CMAKE_CXX_STANDARD 17)
set(PARMA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
set(REF_DIR ${PARMA_ROOT}/tests/reference)
set(RUN_DIR ${CMAKE_BINARY_DIR}/run)

add_executable(legacy_dump legacy_dump.cpp subroutines_legacy.cpp)
add_executable(legacy_generator main_generator_legacy.cpp subroutines_legacy.cpp)

# Variants of main-generator with other conditions, made at configure time by
# replacing setting lines of the verbatim copy:  legacy_variant(name old1 new1 old2 new2 ...)
file(READ main_generator_legacy.cpp GEN_SRC)
function(legacy_variant name)
  set(src "${GEN_SRC}")
  set(i 1)
  while(i LESS ARGC)
    math(EXPR j "${i} + 1")
    string(FIND "${src}" "${ARGV${i}}" pos)
    if(pos EQUAL -1)
      message(FATAL_ERROR "legacy_variant ${name}: '${ARGV${i}}' not found")
    endif()
    string(REPLACE "${ARGV${i}}" "${ARGV${j}}" src "${src}")
    math(EXPR i "${i} + 2")
  endwhile()
  file(WRITE ${CMAKE_BINARY_DIR}/main_generator_${name}.cpp "${src}")
  add_executable(legacy_generator_${name} ${CMAKE_BINARY_DIR}/main_generator_${name}.cpp subroutines_legacy.cpp)
endfunction()
# photon, 0.01-100 MeV: includes the 511 keV annihilation line
legacy_variant(photon
  "int ip = 1;" "int ip = 33;"
  "double emin = 1.0e0;" "double emin = 1.0e-2;"
  "double emax = 1.0e5;" "double emax = 1.0e2;")
# neutron from 1e-9 MeV (raised to 1e-8 MeV), downward only (cos 0-1)
legacy_variant(neutron
  "int ip = 1;" "int ip = 0;"
  "double emin = 1.0e0;" "double emin = 1.0e-9;"
  "double amin = -1.0;" "double amin = 0.0;")

# runs a generator in RUN_DIR and copies its output to tests/reference/<sub>
set(RUN_COMMANDS)
foreach(pair "legacy_generator;generator" "legacy_generator_photon;generator-photon"
             "legacy_generator_neutron;generator-neutron")
  list(GET pair 0 exe)
  list(GET pair 1 sub)
  list(APPEND RUN_COMMANDS
    COMMAND ${CMAKE_COMMAND} -E make_directory ${RUN_DIR}/GeneOut
    COMMAND ${CMAKE_COMMAND} -E chdir ${RUN_DIR} $<TARGET_FILE:${exe}>
    COMMAND ${CMAKE_COMMAND} -E make_directory ${REF_DIR}/${sub}
    COMMAND ${CMAKE_COMMAND} -E copy ${RUN_DIR}/GeneOut/generation.out ${REF_DIR}/${sub}/generation.out
    COMMAND ${CMAKE_COMMAND} -E copy ${RUN_DIR}/GeneOut/flux.out ${REF_DIR}/${sub}/flux.out
    COMMAND ${CMAKE_COMMAND} -E rm -rf ${RUN_DIR}/GeneOut)
endforeach()

add_custom_target(legacy_reference
  COMMAND ${CMAKE_COMMAND} -E rm -rf ${RUN_DIR}
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${PARMA_ROOT}/data ${RUN_DIR}/input
  COMMAND ${CMAKE_COMMAND} -E chdir ${RUN_DIR} $<TARGET_FILE:legacy_dump> legacy_values.txt
  COMMAND ${CMAKE_COMMAND} -E copy ${RUN_DIR}/legacy_values.txt ${REF_DIR}/legacy_values.txt
  ${RUN_COMMANDS}
  DEPENDS legacy_dump legacy_generator legacy_generator_photon legacy_generator_neutron
  COMMENT "Generating regression reference from the legacy code")
