{ *************************************************************************** }
MODULE mod_init_auxi ;
{ Created    9-JUL-1989 MICHIGAN STATE UNIVERSITY,  TRIGGER CONTROL SOFTWARE }
{ *************************************************************************** }
INCLUDE
  mod_common_global_flags,
  mod_common_soft_conn,
  mod_handle_console,
  mod_handle_tracing ;
{ *************************************************************************** }
EXPORT
  init_auxi ;       {PROCEDURE read and execute auxiliary initialization file }
{ *************************************************************************** }
IMPORT
  status_type,                        {from module MOD_COMMON_GLOBAL_FLAGS    }
  ok, not_found, io_failure,          {from module MOD_COMMON_GLOBAL_FLAGS    }
  boot_directory_name,                {from module MOD_COMMON_GLOBAL_FLAGS    }

  replymsg, reqstmsg,                 {from module MOD_COMMON_SOFT_CONN       }
  con_served,                         {from module MOD_COMMON_SOFT_CONN       }

  display_console,                    {from module MOD_HANDLE_CONSOLE         }
  esc, save_cursor, restore_cursor,   {from module MOD_HANDLE_CONSOLE         }
  bold, standard,                     {from module MOD_HANDLE_CONSOLE         }
  inline_show_channel_bold,           {from module MOD_HANDLE_CONSOLE         }
  inline_show_channel_dim,            {from module MOD_HANDLE_CONSOLE         }

 {state_type,}  ON, OFF,              {from module MOD_HANDLE_TRACING         }
  handle_trc_sys,                     {from module MOD_HANDLE_TRACING         }
  handle_trc_sta,                     {from module MOD_HANDLE_TRACING         }
  handle_trc_err,                     {from module MOD_HANDLE_TRACING         }
  modify_trace_inf,                   {from module MOD_HANDLE_TRACING         }
  modify_trace_wrn,                   {from module MOD_HANDLE_TRACING         }
  modify_trace_err,                   {from module MOD_HANDLE_TRACING         }
  modify_io_tracing,                  {from module MOD_HANDLE_TRACING         }
  modify_db_tracing,                  {from module MOD_HANDLE_TRACING         }
  save_tracing_status,                {from module MOD_HANDLE_TRACING         }
  restore_tracing_status ;            {from module MOD_HANDLE_TRACING         }

{ *************************************************************************** }

  %INCLUDE 'SITE_DEPENDENT.CST/LIST'

TYPE

  com_file_type = FILE OF VARYING_STRING(132) ;

VAR  

  tag             : VARYING_STRING(8) := 'INI/FIL%' ;
  con_served_save : INTEGER ;
  re_entry_depth  : INTEGER := 0 ;

{ *************************************************************************** }
PROCEDURE soft_conn_disp ; EXTERNAL ;{from mod_soft_conn_dispatch}
{ *************************************************************************** }
PROCEDURE init_auxi ( tagext : VARYING_STRING(16) := '' ;
                    filename : VARYING_STRING(80) := '' ;
             VAR error_count :[OPTIONAL] INTEGER ;
                  VAR status :[OPTIONAL] status_type ) ;

VAR 

  com_file   : com_file_type ;
  ini_status : INTEGER ;                          {general use status buffer}
  fail_count : INTEGER ;

BEGIN

  IF PRESENT(status) THEN status := ok ;

  IF ( con_served <> 0 ) 
  THEN re_entry_depth := 0 
  ELSE re_entry_depth := re_entry_depth + 1 ;

  IF ( re_entry_depth > 0 ) 
  THEN BEGIN

    tagext := tagext + CONVERT(STRING, re_entry_depth) + '%' ;

    handle_trc_sys ( TAG := tag + tagext,
                 MESSAGE := ' Recursive Entry in the Com File code with '
                          + ' Depth = ' + CONVERT(STRING, re_entry_depth ) ) ;

    IF ( re_entry_depth > 8 ) 
    THEN BEGIN
      handle_trc_err ( TAG := tag + tagext,
                MESSAGE := ' Recursive Entry in Com File code limit exceeded' );
      {make the calling auxi_init procedure notice the error}
      replymsg[0]^.replystat := '     BAD' ;
      GOTO quit_init_auxi ;
    END ;

  END ;

  {get auxiliary file and system ready for reading                            }
  IF ( filename = '' ) THEN filename := boot_directory_name + aux_init_filename ;
  open_auxi_file ( TAGEXT := tagext, 
                 COM_FILE := com_file,
                 FILENAME := filename,
                   STATUS := ini_status ) ;
  IF ( ini_status <> 1 ) THEN BEGIN
    IF PRESENT(status) THEN status := not_found ;
    GOTO quit_init_auxi ;
  END ;

  IF ( re_entry_depth = 0 ) 
  THEN BEGIN

    {this inititialization routine can be called from an LENCON Channel       }
    {(e.g. coor init) do not lose its context                                 }
    con_served_save := con_served ;
    con_served := 0 ; {0 is not a real elncon channels, reserved for this task}
  
    {allocate memory for messages                                             }
    NEW ( reqstmsg[0] ) ;
    NEW ( replymsg[0] ) ;
  
    {advertize this fake connection on screen status window                   }
    inline_show_channel_bold ( 0 ) ;

    save_tracing_status ;
  
    modify_trace_inf ( CONSOLE := OFF, LOGFILE :=  ON, REPORT := FALSE ) ;
    modify_trace_wrn ( CONSOLE := OFF, LOGFILE := OFF, REPORT := FALSE ) ;
    modify_db_tracing (  STATE :=  ON, REPORT := FALSE ) ;
    modify_io_tracing (  STATE := OFF, REPORT := FALSE ) ;
    modify_trace_err ( CONSOLE := OFF, LOGFILE :=  ON, REPORT := FALSE ) ;

  END ;  

  fail_count := 0 ;

  WHILE ( EOF( com_file ) = FALSE )
  DO BEGIN

    {prepare for next request                                          **** }
    reqstmsg[0]^::STRING(reqst_msg_len) := com_file^ ;
    replymsg[0]^.replystat := '      OK' ;

    {dispatch and execute request                                      **** }
    IF ( FIND_NONMEMBER(reqstmsg[0]^.reqstid,['0'..'9','A'..'F']) = 0 ) 
    THEN soft_conn_disp ;

    {verify sucess                                                     **** }
    IF ( replymsg[0]^.replystat <> '      OK' )
    THEN fail_count := fail_count + 1 ;

    {read next file record                                               **** }
    GET ( com_file ) ;

  END ;


  IF ( re_entry_depth = 0 ) 
  THEN BEGIN

    {advertize task completion in screen status window                          }
    inline_show_channel_dim ( 0 ) ;
  
    {release memory                                                             }
    DISPOSE ( reqstmsg[0] ) ;
    DISPOSE ( replymsg[0] ) ;
  
    {restore connection context                                                 }
    con_served := con_served_save ;

    restore_tracing_status ( REPORT := FALSE ) ;

  END ;

  close_auxi_file ( TAGEXT := tagext,
                  COM_FILE := com_file,
                 FILENAME := filename ) ;

  IF ( fail_count  <> 0 )
  THEN BEGIN
    handle_trc_err ( TAG := tag + tagext,
                 MESSAGE := ' Failure Count Executing Command File is '
                          + CONVERT(STRING,fail_count)
                          + ', Check Log File '  ) ;
    IF PRESENT(status) THEN status := io_failure ;
  END ;

quit_init_auxi :

  re_entry_depth := re_entry_depth - 1 ;

  IF PRESENT(error_count) THEN error_count := fail_count ;

END ;
{ *************************************************************************** }
{ *************************************************************************** }
PROCEDURE open_auxi_file ( tagext : VARYING_STRING(16) := '' ;
                     VAR com_file : com_file_type ;
                         filename : VARYING_STRING(80) := '' ;
                       VAR status : INTEGER ) ;

BEGIN

  OPEN ( com_file, FILE_NAME := filename,
                     HISTORY := HISTORY$READONLY,
                      STATUS := status ) ;

  IF ( ( status MOD 8 ) = 1 )
  THEN BEGIN

    handle_trc_sys ( TAG := tag + tagext,
                 MESSAGE := ' Command File Opened '
                          + filename ) ;

    {simplify return status                                                   }
    status := 1 ;

    {set inspection mode and fetch first record                               }
    RESET ( com_file ) ;

  END

  ELSE BEGIN
    handle_trc_sys ( TAG := tag + tagext,
                 MESSAGE := ' No Command File Found '
                          + filename ) ;
    handle_trc_sta ( TAG := tag, STATUS := status ) ;

  END ;

END ;
{ *************************************************************************** }
{ *************************************************************************** }
PROCEDURE close_auxi_file ( tagext : VARYING_STRING(16) := '' ;
                      VAR com_file : com_file_type ;
                          filename : VARYING_STRING(80) := '' ) ;

BEGIN

  CLOSE ( com_file ) ;

  handle_trc_sys ( TAG := tag + tagext,
               MESSAGE := ' Command File Closed ' 
                          + filename ) ;

END ;
{ *************************************************************************** }
{ *************************************************************************** }
END.
