!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
module cubetemplate_language
  use cubetools_parameters
  use cubetools_structure
  use cubetemplate_messaging
  !
  use cubetemplate_format2format
  use cubetemplate_fullcube2fullcube
  use cubetemplate_image2image
  use cubetemplate_image2visi
  use cubetemplate_list
  use cubetemplate_spectrum2spectrum
  use cubetemplate_spectrum2spectrum_onechan
  use cubetemplate_subcube2subcube
  use cubetemplate_table2cube
  use cubetemplate_visi2visi
  !
  public :: cubetemplate_register_language
  private
  !
  integer(kind=lang_k) :: langid
  !
contains
  !
  subroutine cubetemplate_register_language(error)
    !----------------------------------------------------------------------
    ! Register the CUBE\ language
    !----------------------------------------------------------------------
    logical, intent(inout) :: error
    !
    call cubetools_register_language(&
         'TEMPLATE',&
         'J.Pety, S.Bardeau, V.deSouzaMagalhaes',&
         'COMMAND templates',&
         'TEMPLATE provides developer scaffolds for CUBE command implementations.'//strg_bl//&
         'Behavior:'//strg_cr//&
         '- Commands illustrate the standard register/parse/main command flow.'//strg_cr//&
         '- Template variants cover multiple input/output data granularities.'//strg_cr//&
         '- Execution follows interface timing and finish wrappers used in production.'//strg_cr//&
         '- LIST demonstrates typed list and session-structure utilities.'//strg_bl//&
         'Typical workflow:'//strg_cr//&
         'CUBE> TEMPLATE\LIST'//strg_cr//&
         'CUBE> TEMPLATE\IMAGE2IMAGE cube_in'//strg_cr//&
         'CUBE> TEMPLATE\SPEC2SPEC cube_in'//strg_bl//&
         'Warning:'//strg_cr//&
         'TEMPLATE commands are development examples, not production processing tools.',&
         'gag_doc:hlp/cube-help-template.hlp',&
         cubetemplate_execute_command,&
         langid,&
         error)
    if (error) return
    !----------------------------------------------------------------------
    !
    call format2format%register(error)
    if (error) return
    call fullcube2fullcube%register(error)
    if (error) return
    call image2image%register(error)
    if (error) return
    call image2visi%register(error)
    if (error) return
    call list%register(error)
    if (error) return
    call spectrum2spectrum%register(error)
    if (error) return
    call spectrum2spectrum_onechan%register(error)
    if (error) return
    call subcube2subcube%register(error)
    if (error) return
    call table2cube%register(error)
    if (error) return
    call visi2visi%register(error)
    if (error) return
    !
    call cubetools_register_dict(error)
    if (error) return
  end subroutine cubetemplate_register_language
  !
  subroutine cubetemplate_execute_command(line,comm,error)
    use cubeadm_opened
    use cubeadm_timing
    !----------------------------------------------------------------------
    ! Execute a command of the CUBE\ language
    !----------------------------------------------------------------------
    character(len=*), intent(in)    :: line
    character(len=*), intent(in)    :: comm
    logical,          intent(inout) :: error
    !
    character(len=*), parameter :: rname='EXECUTE>COMMAND'
    !
    error = .false.
    if (comm.eq.'TEMPLATE?') then
       call cubetools_list_language_commands(langid,error)
       if (error) return
    else
       call cubeadm_timing_init()
       call cubetools_execute_command(line,langid,comm,error)
       if (error) continue ! To ensure error recovery in the call to cubeadm_finish_all
       call cubeadm_finish_all(comm,line,error)
       if (error) continue ! To ensure error recovery in timing
       call cubeadm_timing_final(comm)
    endif
  end subroutine cubetemplate_execute_command
end module cubetemplate_language
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
