PROCEDURE activate_xtf (
  transfer_retry_delay, trd: integer 30..3600 = 600
  log_option, lo: key
      (startup, s)
      (startup_and_execution, sae)
      (rts_only, ro)
    keyend = startup
  enable_password_checking, epc: boolean = FALSE
  job_class, jc: name = $job_default(job_class, batch)
  unknown_mta_connections, umc: integer 0..10 = 0
  maximum_restart_attempts, maxra: (BY_NAME, ADVANCED) integer 0..65535 = 10
  notify_after_aborting, naa: (BY_NAME, ADVANCED) boolean = FALSE
  status)

" PURPOSE: This procedure activates the system job for the X.400 Transfer Facility.

" Define constants local to this activation procedure.

  "$FORMAT=OFF"
  VAR
    default_log_option_value: (READ) name = startup
    default_password_checking_value: (READ) boolean = false
    default_xfer_retry_delay_value: (READ) integer = 600
    osiam_binary_log: (READ) file = :$system.$system.xtf.osiam_binary_log
    xtf_active_configuration: (READ) file = :$system.$system.xtf.active_configuration
    xtf_base_configuration: (READ) file = :$system.$system.xtf.base_configuration
    xtf_job_output: (READ) file = $fname('$system.xtf.xtf_output_'//$mainframe(id))
    xtf_product_library: (READ) file = :$system.$system.xtf.bound_product
    xtf_starting_procedure: (READ) name = nfp$rts_mailve_main_task_entry
    xtf_user_job_name: (READ) name = $name('XTF'//$mainframe(id))
  VAREND
  "$FORMAT=ON"

" Declare variables local to this activation procedure.

  "$FORMAT=OFF"
  VAR
    editing_directives: file
    local_status: status
    replace_text_command: string
    task_parameter_list: string
    xtf_system_job_name: name
  VAREND
  "$FORMAT=ON"

  editing_directives = $unique($local)

" Verify the procedure has been called by a system operator job.

  IF NOT $job_validation(system_operation) THEN
    EXIT procedure WITH $status(false, 'RA', 0, 'XTF can only be activated by a system operator.')
  IFEND

" Verify the XTF bound product file is installed.

  IF (NOT $first($file_attributes(xtf_product_library, registered)).registered) THEN
    EXIT procedure WITH $status(false, 'RA', 0, 'The XTF product is not installed.')
  IFEND

" Verify the XTF bound product file has the correct ring attributes.

  IF (($first($file_attributes(xtf_product_library, ring_attributes)).ring_attributes.r1 <> 6) OR ($first(..
        $file_attributes(xtf_product_library, ring_attributes)).ring_attributes.r2 <> 6) OR ($first(..
        $file_attributes(xtf_product_library, ring_attributes)).ring_attributes.r3 <> 13)) THEN
    EXIT procedure WITH $status(false, 'RA', 0, 'The XTF product has improper ring attributes.')
  IFEND

" Verify that the XTF configuration file exists.

  IF (NOT $first($file_attributes(xtf_base_configuration, registered)).registered) THEN
    EXIT procedure WITH $status(false, 'RA', 0, $string(xtf_base_configuration)//' does not exist.')
  IFEND

" Check for the presence of an existing XTF job.

  MANAGE_JOBS
    select_jobs job_state=(deferred, queued, initiated) name=xtf_user_job_name status=local_status
    IF local_status.normal THEN
      IF $size(jmv$selected_jobs) > 0 THEN
        EXIT procedure WITH $status(false, 'RA', 0 ..
              'An identical XTF job was found. XTF will not be activated')
      IFEND
    ELSE
      EXIT procedure WITH local_status
    IFEND
  QUIT

" Create the active configuration file.

  TASK ring=4
    copy_file input=xtf_base_configuration output=xtf_active_configuration
    change_file_attribute file=xtf_active_configuration ring_attributes=(11, 11, 11)
  TASKEND

  IF $specified(log_option) AND (log_option <> default_log_option_value) THEN
    IF (log_option = rts_only) THEN

" Define the command to change the log level for the RTS User task, task 15 only.

      replace_text_command = ..
            'replace_text text=''log_level=0'' new_text=''log_level=8''  lines=current..last number=1'
    ELSE

" Define the command to change the log level for all tasks.

      replace_text_command = ..
            'replace_text text=''log_level=0'' new_text=''log_level=8''  lines=current..last number=all'
    IFEND

" Generate the editing directives to adjust the log option value in the active configuration file.

COLLECT_TEXT output=editing_directives until='///END_EDIT_DIRECTIVES\\\' substitution_mark='?'
  locate_text text='Logging options' lines=all number=2
  ?replace_text_command?

  locate_text text='debug off' lines=all number=1
  replace_text text='debug off' new_text='debug on' line=current
///END_EDIT_DIRECTIVES\\\
  IFEND

  IF $specified(transfer_retry_delay) AND (transfer_retry_delay <> default_xfer_retry_delay_value) THEN

" Generate the editing directives to adjust the transfer retry delay in the active configuration file.

COLLECT_TEXT output=editing_directives.$eoi until='///END_EDIT_DIRECTIVES\\\'
  locate_text text='transfer_retry_delay' lines=all number=1
  replace_text text=$string(default_xfer_retry_delay_value) new_text=$string(transfer_retry_delay) ..
        line=current
///END_EDIT_DIRECTIVES\\\
  IFEND

  IF $specified(enable_password_checking) AND enable_password_checking <> default_password_checking_value THEN

" Generate the editing directives to adjust the password checking value in the active configuration file.

COLLECT_TEXT output=editing_directives.$eoi until='///END_EDIT_DIRECTIVES\\\'
  locate_text text='enable_password_checking' lines=all number=1
  replace_text text='false' new_text='true' line=current
///END_EDIT_DIRECTIVES\\\
  IFEND

" Generate the editing directives to process the UNKNOWN_MTA_CONNECTIONS parameter, the commands which
" revise the base configuration commands for compatibility with OSIAM version 2.4B and above, and the
" commands which enable trace information generation for all SAPs and entities.

COLLECT_TEXT output=editing_directives.$eoi until='///END_EDIT_DIRECTIVES\\\' sm='?'
  VAR
    editing_directives_status: status
  VAREND

" Find the line in the active configuration which would proceed the UNKNOWN entry.

  position_cursor text='Remote MTA(s)' l=all

  position_cursor text='UNKNOWN' status=editing_directives_status
  IF editing_directives_status.normal THEN
    replace_line text='mcc=0 msc='..
          new_text='       mcc=0 msc=?unknown_mta_connections?  client_connection_idle_timeout=180'' ..'
  ELSEIF ?unknown_mta_connections? <> 0 THEN

" The UNKNOWN entry should be inserted at the end of remote MTA list; advance to the end of the list.

    position_cursor text='-----' number=2

    insert_lines placement=before until='///END_INSL_TEXT\\\'
 send_command 'define_mta mta_name=''UNKNOWN'' password='''' ..
        mcc=0 msc=?unknown_mta_connections? client_connection_idle_timeout=180' ..
        job_number=0 task_number=0 module_number=15
///END_INSL_TEXT\\\
  IFEND

" In OSIAM version 2.4B and beyond, session version 1 is selected via RTS entity flags. Disable the
" configuration file commands which alter the session entity directly as was required by previous OSIAM
" versions.

  replace_text text=' send_command ''vary entity 50 flag 0 0 off' ..
        new_text='"send_command ''vary entity 50 flag 0 0 off' ..
        lines=all status="ignore" editing_directives_status

  replace_text text=' send_command ''vary entity 50 flag 0 1 on' ..
        new_text='"send_command ''vary entity 50 flag 0 1 on' ..
        lines=all status="ignore" editing_directives_status

  locate_text text='Block_size' lines=all number=1

  insert_lines placement=before until='///END_INSL_TEXT\\\'

" Increase the number of connections in the session and RTS layers.

 send_command 'define entity 50 200 64 '  ..
       job_number=0 task_number=0 module_number=2

 send_command 'define entity 60 200 64 '  ..
       job_number=0 task_number=0 module_number=2

" Select session version 1 using RTS entity flags.

 send_command 'vary entity 60 flag 0 6 on' ..
       job_number=0 task_number=0 module_number=2

///END_INSL_TEXT\\\

" For OSIAM versions 2.4B and greater, the block size of 4K should be specified. Replace the configuration
" file commands which specify the 8K block size which was appropriate for previous OSIAM versions.

   replace_text text='vary entity 60 parm 3 8'  new_text='vary entity 60 parm 3 4' ..
       lines=all status="ignore" editing_directives_status

" For OSIAM versions 2.4B and greater, the offline configuration contains all necessary SAP definitions. Delete the
" configuration file commands which defined intermediate SAPS for previous OSIAM versions.

  locate_text text='define sap 20 17' lines=all number=1
  delete_text number=2

" Enable trace for all entities.

  locate_text text='debug' lines=all number=1
  insert_lines placement=before until='///END_INSL_TEXT\\\'
 send_command 'var sap 19 trace on'           job_number=0 task_number=0 module_number=2
 send_command 'var sap 20 trace on'           job_number=0 task_number=0 module_number=2
 send_command 'var sap 21 trace on'           job_number=0 task_number=0 module_number=2
 send_command 'var sap 17 trace on'           job_number=0 task_number=0 module_number=2
 send_command 'var ent 53 trace 0 on'         job_number=0 task_number=0 module_number=2
///END_INSL_TEXT\\\

///END_EDIT_DIRECTIVES\\\

  IF $first($file_attributes(editing_directives, size)).size > 0 THEN

" Customize the active configuration file using EDIT_FILE. A seperate task is created to bypass problems
" which could result if this procedure is called from within the EDIT_FILE utility.

    TASK
      $system.edit_file file=xtf_active_configuration input=editing_directives output=$null prolog=$null
    TASKEND
  IFEND

  delete_file file=editing_directives status="ignore" local_status

  task_parameter_list = 'command_file='//xtf_active_configuration
  task_parameter_list = task_parameter_list//' log_file='//osiam_binary_log
  task_parameter_list = task_parameter_list//' unknown_mta_connections='//unknown_mta_connections
  task_parameter_list = $quote(task_parameter_list)

  JOB user_job_name=xtf_user_job_name job_abort_disposition=terminate job_class=job_class ..
        job_recovery_disposition=terminate output_disposition=xtf_job_output substitution_mark='?' ..
        system_job_name=xtf_system_job_name status=local_status

    VAR
      abort_file: file = $unique($local)
      binary_log_file: name
      date_time_stamp: string
      local_status: status
      maximum_restart_attempts: integer = ?maximum_restart_attempts?
      number_of_restarts: integer = 0
      nfv$notify_after_aborting: boolean = ?notify_after_aborting?
      status_string_value: list of string
    VAREND

    WHEN exit DO
        change_file_attributes f=?osiam_binary_log? ra=(11, 11, 11) status="ignore" local_status
    WHENEND

    change_message_level il=full status="ignore" local_status
    change_working_catalog c=$system.xtf

    SYSTEM_OPERATOR_UTILITY
      TASK ring=6

        set_debug_ring ring=6
        put_line 'display_calls do=ac' output=abort_file
        set_program_attributes preset_value=zero abort_file=abort_file

        delete_file file=?osiam_binary_log? status="ignore" local_status

        REPEAT
          IF number_of_restarts > 0 THEN
            display_message message=' ' to=job
            display_message message='***The X.400 Transfer Facility has aborted with the following status:' ..
                  to=job

            status_string_value = $string(local_status)

            FOR EACH status_string_list_element IN status_string_value DO
              display_message message=status_string_list_element to=job
            FOREND

" Regardless of the value specified for the MAXIMUM_RESTART_ATTEMPTS parameter, capture no more than ten
" binary log files.

            IF (number_of_restarts <= 10) AND ?(log_option <> startup)? THEN

" Obtain the current date and time in order to produce a distinct file name for the OSIAM binary log. Distinct
" names are used so the next abort will not overwrite the information from the previous abort.

              date_time_stamp = $date('Y2M2D2')//'_'//$time('H24MMSS')

              IF $first($file_attributes(?osiam_binary_log?, registered)).registered THEN

" XTF/VE executed long enough to generate an OSIAM binary log. Alter the name of the OSIAM binary log to
" contain the date time stamp for this abort.

                binary_log_file = $name('osiam_binary_log_'//date_time_stamp)

                change_catalog_entry file=?osiam_binary_log? new_file_name=binary_log_file status=local_status
                IF local_status.normal THEN
                  change_file_attributes f=$fname($path(?osiam_binary_log?, catalog)//'.'//binary_log_file) ..
                        ra=(11, 11, 11) status="ignore" local_status
                ELSE
                  display_message ..
                        message='***Attempt to capture OSIAM binary log failed with the following status:' ..
                        to=job

                  status_string_value = $string(local_status)

                  FOR EACH status_string_list_element IN status_string_value DO
                    display_message message=status_string_list_element to=job
                  FOREND
                IFEND
              IFEND
            IFEND

            put_line line=' '
            put_line line='***Attempting restart #'//$string(number_of_restarts)

            display_message message='***Attempting restart #'//$string(number_of_restarts) to=(job, ..
                  job_message)
            display_message message=' ' to=job

            display_log o=?xtf_job_output? do=all
            change_file_attributes f=?xtf_job_output? ra=(11, 11, 11)


" A small (7 1/2 second) delay is forced before the next restart attempt. The delay will reduce the impact
" on the system should the X.400 transfer facility continuously abort immediately after beginning execution.

            wait time=0-0-0.00:00:07.500
          IFEND

          execute_task parameters=?task_parameter_list? library=?xtf_product_library? ..
                starting_procedure=?xtf_starting_procedure? load_map=$null load_map_options=none pv=zero ..
                abort_file=abort_file termination_error_level=error status=local_status

          number_of_restarts = number_of_restarts + 1

        UNTIL (number_of_restarts > maximum_restart_attempts)

      TASKEND
    END_SYSTEM_OPERATOR_UTILITY

    IF ($variable(nfv$notify_after_aborting, defined)) AND ..
          ($job_status($job(system_job_name), job_state) <> terminated) THEN
      IF nfv$notify_after_aborting THEN
        send_operator_message m='X.400 Transfer Facility failed, see ?xtf_job_output?' oc=system_operator
      IFEND
    IFEND
  JOBEND

  IF local_status.normal THEN
    put_line line=' XTF has been activated as job  '//..
$string(xtf_user_job_name)//' ('//$string(xtf_system_job_name)//').' o=$response
  ELSE
    EXIT procedure WITH local_status
  IFEND

PROCEND activate_xtf
