!> @file euler_2d.f90
!> @brief Entry point for the 2D Euler CFD solver (Full 2D Parity P2: two modes).
!!
!! Mirrors app/euler_1d.f90: a positional namelist path runs the CLI solve
!! (behaviour-identical to the single-mode program); `--cortex-socket=<uri>`
!! enters the attached worker mode, serving the Cortex IPC protocol through
!! the dim-aware session (HELLO announces "euler_2d-<build>").
program euler_2d
  use mpi_runtime, only: mpi_runtime_init, mpi_runtime_finalize, parallel_fatal
  use solver_runtime, only: resolve_cli
  use euler_driver_2d, only: run_euler_2d
  use cortex_run_attached_mod, only: cortex_run_attached
  use version_info, only: handle_version_flag
  implicit none

  character(len=512) :: message, nml_file, cortex_uri
  logical :: attached, ok

  call handle_version_flag('euler_2d')

  ! Usage check is pre-init (uniform across all images) so it can remain a
  ! plain `error stop` without orphaning ranks. Worker mode always passes
  ! --cortex-socket=..., so argument_count >= 1 holds in both modes.
  if (command_argument_count() < 1) then
    error stop 'usage: euler_2d <input.nml>  |  euler_2d --cortex-socket=<uri>'
  end if

  call mpi_runtime_init()
  call resolve_cli(nml_file, cortex_uri, attached)

  if (attached) then
    call cortex_run_attached(trim(cortex_uri), worker_name='euler_2d')
  else
    call run_euler_2d(trim(nml_file), ok, message)
    if (.not. ok) call parallel_fatal(trim(message))
  end if

  call mpi_runtime_finalize()

end program euler_2d
