PROCEDURE jmp$backup_job_files (
  control_family, control_families, cf: list of name = $optional
  control_user, control_users, cu: list of name = $optional
  job_category_name, job_category_names, jcn: list of name = $optional
  job_class, job_classes, jc: list of name = $optional
  job_deferred_by_operator, jdbo: boolean = $optional
  job_deferred_by_user, jdbu: boolean = $optional
  job_qualifier, job_qualifiers, jq: list of name = $optional
  job_state, job_states, js: any of
      key
        all
      keyend
      list of key
        (deferred, d)
        (queued, q)
      keyend
    anyend = $optional
  login_account, login_accounts, la: list of name = $optional
  login_family, login_families, lf: list of name = $optional
  login_project, login_projects, lp: list of name = $optional
  login_user, login_users, lu: list of name = $optional
  name, names, n: list of name = $optional
  site_information, si: list of string 0..256 = $optional
  user_information, ui: list of string 0..256 = $optional
  maximum_selection, maxs: any of
      key
        all
      keyend
      integer 1..65535
    anyend = all
  vsn, vsns, v: list of any of
      name 1..6
      string 1..6
    anyend = $optional
  vsn_prefix, vp: any of
      name 1..5
      string 1..5
    anyend = $optional
  vsn_count, vc: integer 1..11881376 = 15
  increment_scheme, is: key
      (alphabetic, a)
      (decimal, d)
    keyend = decimal
  type, t: key
      mt9$800, mt9$1600, mt9$6250, mt18$38000
    keyend = mt9$1600
  delete_file, delete_files, df: boolean = true
  excluded_file_list, efl: (VAR) list 0..$max_list of name = $optional
  backup_file,bf : file = $optional
  output, o: file = $OUTPUT
  errors, e: file = $ERRORS
  unload_volume, uv: boolean = TRUE
  status)

" Confirm that we are in the system_operator_utility with the system_administration and system_operation
" capabilities active.

  WHEN COMMAND_FAULT DO
    exit procedure with $status(FALSE 'OF' ofe$sou_not_active 'system_administration and system_operation')
  WHENEND

  VAR
    active_capabilities: file = $unique($local)
    ignore_status: status
    lines: list 0..$max_list of string
  VAREND

  system_operator_utility.display_active_capabilities output=active_capabilities
  get_line variable=lines input=active_capabilities
  detach_file file=active_capabilities status=ignore_status
  IF $nil($select_wild_card_strings(lines '*system_administration*' basic)) OR ..
     $nil($select_wild_card_strings(lines '*system_operation*' basic)) THEN
    exit procedure with $status(FALSE 'OF' ofe$sou_not_active 'system_administration and system_operation')
  IFEND

  CANCEL COMMAND_FAULT
  VAR
    constructed_vsn_list: list 1..$max_list of string 6
    queue_backup: file = $unique($local)
    vsn_list: list 1..$max_list of string 1..6
  VAREND

  " Construct the list of VSNs for the backup tape set.

  IF $specified(vsn) THEN
    IF $specified(vsn_prefix) THEN
      exit procedure with $status(FALSE 'JM' jme$incompatible_vsn_params '')
    IFEND
    IF $nil(vsn) THEN
      exit procedure with $status(FALSE 'JM' jme$empty_vsn_list '')
    IFEND
    vsn_list = $string(vsn)

  ELSEIF NOT $specified(vsn_prefix) THEN
    IF NOT $specified(backup_file) THEN
      exit procedure with $status(FALSE 'JM' jme$vsn_or_vsnp_or_bf_required '')
    IFEND
  ELSE
    pup$construct_volume_list vsn_count=vsn_count vsn_prefix=vsn_prefix increment_scheme=increment_scheme ..
          volume_list=constructed_vsn_list
    vsn_list = constructed_vsn_list
  IFEND
  $system.MANAGE_JOBS
    change_list_options output=output errors=errors

" Construct a list of system job names of the jobs to back up.

"$FORMAT=OFF"
    VAR
      all_known_jobs: list 0..$max_list of name
      catalog_paths: list 0..$max_list of string 2..512
      chosen_states: list of key
          (deferred, d)
          (queued, q)
          keyend = (deferred queued)
      command_file: file = $unique(:$local)
      desired_jobs: list 0..$max_list of name
      desired_paths: list 0..$max_list of file
      excluded_jobs: list 0..$max_list of name
      excluded_paths: list 0..$max_list of file
    VAREND

"$FORMAT=ON"

    IF $specified(job_state) THEN
      IF $generic_type(job_state) <> KEY THEN
        chosen_states = job_state
      IFEND
    IFEND
    select_jobs job_state=(deferred queued) maximum_selection=all job_selection_list=all_known_jobs
    select_jobs control_family=control_family control_user=control_user job_category_name=job_category_name ..
          job_class=job_class job_deferred_by_operator=job_deferred_by_operator ..
          job_deferred_by_user=job_deferred_by_user job_qualifier=job_qualifier job_state=chosen_states ..
          login_account=login_account login_family=login_family login_project=login_project ..
          login_user=login_user name=name site_information=site_information ..
          user_information=user_information maximum_selection=maximum_selection ..
          job_selection_list=desired_jobs
    FOR each JOB in desired_jobs DO
      change_input_attributes names=JOB job_deferred_by_operator=true status=ignore_status
      IF NOT ignore_status.normal THEN
        desired_jobs=$difference(desired_jobs job)
      IFEND
    FOREND
    display_input_attributes names=desired_jobs display_option=all output=output

" Generate a list of the unique catalog paths associated with the desired
" jobs.

    desired_paths = $queued_job_path(desired_jobs)
    catalog_count = 0
    find_unique_catalog_paths: ..
      FOR EACH file_path IN desired_paths DO
        FOR catalog_index = 1 TO catalog_count DO
          CYCLE find_unique_catalog_paths WHEN $path(file_path, catalog) = catalog_paths(catalog_index)
        FOREND
        catalog_count = catalog_count + 1
        IF catalog_count = 1 THEN
          catalog_paths = $path(file_path, catalog)
        ELSE
          put_line line='catalog_paths=(..' o=command_file
          FOR index = 1 TO catalog_count-1 DO
            put_line line=''''//catalog_paths(index)//''',..' o=command_file.$eoi
          FOREND
          put_line line=''''//$path(file_path, catalog)//'''..' o=command_file.$eoi
          put_line line=')' o=command_file.$eoi
          include_file file=command_file
        IFEND
      FOREND find_unique_catalog_paths

" Generate a list of the jobs that are not to be backed up.

      excluded_jobs = $difference(all_known_jobs desired_jobs)
      IF NOT $nil(excluded_jobs) THEN
        excluded_paths = $queued_job_path(excluded_jobs)
      IFEND

" Backup the job command files to tape. "

      WHEN any_fault DO
        change_input_attributes names=desired_jobs job_deferred_by_operator=false
        IF NOT $SPECIFIED(BACKUP_FILE) THEN
          detach_file file=queue_backup unload_volume=unload_volume status=ignore_status
        IFEND
        EXIT_PROC
      WHENEND

      TASK ring=3
        IF NOT $SPECIFIED(BACKUP_FILE) THEN
          request_magnetic_tape queue_backup recorded_vsn=vsn_list ring=true type=type
        ELSE
          QUEUE_BACKUP=BACKUP_FILE
        IFEND
        BACKUP_PERMANENT_FILES backup_file=queue_backup list=output.$eoi
          IF NOT $nil(excluded_paths) THEN
            FOR EACH file_path IN excluded_paths DO
              exclude_file file=file_path
            FOREND
          IFEND
          FOR catalog_index = 1 TO catalog_count DO
            backup_catalog catalog=$fname(catalog_paths(catalog_index))
          FOREND
        QUIT " backup_permanent_files "
        IF NOT $SPECIFIED(BACKUP_FILE) THEN
          detach_file file=queue_backup unload_volume=unload_volume
        IFEND
      TASKEND

" Terminate or reenable the jobs for initiation. "

      IF delete_files THEN
        terminate_jobs names=desired_jobs reason=operator_backup
      ELSE
        change_input_attributes names=desired_jobs job_deferred_by_operator=false
      IFEND

" Return a list of known jobs that were not backed up as the excluded_file_list value.

    IF $specified(excluded_file_list) THEN
      excluded_file_list = excluded_jobs
    IFEND

  QUIT " manage_jobs "

PROCEND jmp$backup_job_files
