PROCEDURE create_ve_deadstart_catalog, crevdc (
  deadstart_catalog, dc: file = $required
  external_vsn, evsn, ev: any of
      string 1..6
      name 1..6
    anyend = $optional
  recorded_vsn, rvsn, rv: any of
      string 1..6
      name 1..6
    anyend = $optional
  type, t: any of
      key
        mt9$1600
        mt9$6250
        mt18$38000
      keyend
    anyend = mt9$6250
  removable_media_group, rmg: (BY_NAME, ADVANCED) any of
      key
        none
      keyend
      name
    anyend = osd$reqmt_removable_media_group, none
  status)

*copyc dst$deadstart_record_lists

  VAR
    beginning_of_file_id: integer
    catalog_path: string
    created_catalogs: list 0 .. $max_list OF file
    created_files: list 0 .. $max_list OF file
    end_of_file_id: integer
    file_id: string 1 .. $max_name
    finished: boolean
    first_file: boolean = TRUE
    ignore_status: status
    index: integer
    info_file: string 1 .. $max_name = $unique
    line: string
    local_file: string 1 .. $max_name = $unique
    local_status: status
    previous_catalog_path: string
    return_file: file
    tape_file: string 1 .. $max_name = $unique
    temp_file_id: string 1 .. 17
  VAREND

  created_catalogs = ()
  created_files = ()

  set_file_attributes f=$fname(info_file) pf=continuous

  catalog_block: BLOCK

    "  Request the labelled deadstart tape.

    IF NOT $specified(external_vsn) AND NOT $specified(recorded_vsn) THEN
      IF removable_media_group = 'NONE' THEN
        local_status = $status(FALSE, 'DS', dse$vsn_required)
        EXIT catalog_block
      IFEND
    IFEND
    IF NOT $specified(external_vsn) AND NOT $specified(recorded_vsn) THEN
      request_magnetic_tape f=$fname(tape_file) r=false ..
            t=type rmg=removable_media_group status=local_status
      EXIT catalog_block WHEN NOT local_status.normal
    ELSEIF NOT $specified(external_vsn) THEN
      request_magnetic_tape f=$fname(tape_file) rvsn=recorded_vsn r=false ..
            t=type rmg=removable_media_group status=local_status
      EXIT catalog_block WHEN NOT local_status.normal
    ELSEIF NOT $specified(recorded_vsn) THEN
      request_magnetic_tape f=$fname(tape_file) evsn=external_vsn r=false ..
            t=type rmg=removable_media_group status=local_status
      EXIT catalog_block WHEN NOT local_status.normal
    ELSE
      request_magnetic_tape f=$fname(tape_file) evsn=external_vsn rvsn=recorded_vsn r=false ..
            t=type rmg=removable_media_group status=local_status
      EXIT catalog_block WHEN NOT local_status.normal
    IFEND
    set_file_attributes f=$fname(tape_file) flt=labelled status=local_status
    EXIT catalog_block WHEN NOT local_status.normal

    "  Create the catalogs to which the files on the deadstart tape will be written.

    create_catalog c=deadstart_catalog status=local_status
    IF NOT local_status.normal THEN
      EXIT catalog_block WHEN $condition(local_status.condition) <> 'PFE$NAME_ALREADY_SUBCATALOG'
    ELSE
      created_catalogs = $add(deadstart_catalog, created_catalogs)
    IFEND

    "  For each file on the deadstart tape, get the file identifier, validate the file with the required
    "  files list, create a file by that name in the deadstart catalog, and copy the file to the deadstart
    "  catalog file according to the site catalog.

    index = 1
    WHILE index <= deadstart_file_count DO

      REPEAT
        IF first_file THEN
          change_tape_label_attributes f=$fname(tape_file) fsp=beginning_of_set rl=no status=local_status
          first_file = FALSE
        ELSE
          change_tape_label_attributes f=$fname(tape_file) fsp=next_file rl=no status=local_status
        IFEND
        EXIT catalog_block WHEN NOT local_status.normal

        IF deadstart_file_list(index).site_catalog = 'CIP' THEN
          set_file_attributes f=$fname(local_file) block_type=SS record_type=U status=local_status
          EXIT catalog_block WHEN NOT local_status.normal
        IFEND;

        copy_file i=$fname(tape_file) o=$fname(local_file) status=local_status
        EXIT catalog_block WHEN NOT local_status.normal

        display_tape_label_attributes f=$fname(tape_file) do=(file_identifier, current_file) ..
              o=$fname(info_file) status=local_status
        EXIT catalog_block WHEN NOT local_status.normal
        accept_line v=line i=$fname(info_file) status=local_status
        EXIT catalog_block WHEN NOT local_status.normal
        beginning_of_file_id = $scan_string(':', line)+3
        file_id = $substr(line, beginning_of_file_id, 18)
        end_of_file_id = $scan_string('''',file_id)-1
        file_id = $substr(file_id, 1, end_of_file_id)
        temp_file_id = $substr(file_id, 1, 17, ' ')
        put_line l=' Moving the file '//temp_file_id//' from the tape to the deadstart catalog.'

        IF index <> deadstart_file_count THEN
          WHILE file_id <> deadstart_file_list(index).tape_name DO
            IF deadstart_file_list(index).site_required OR (index > deadstart_file_count) THEN
              local_status = $status(FALSE, 'DS', dse$not_deadstart_tape)
              EXIT catalog_block
            IFEND
            index = index + 1
          WHILEND
          IF deadstart_file_list(index).site_catalog = ' ' THEN
            catalog_path = $string(deadstart_catalog)
          ELSE
            catalog_path = $string(deadstart_catalog)//'.'//deadstart_file_list(index).site_catalog
            create_catalog c=$fname(catalog_path) status=local_status
            IF NOT local_status.normal THEN
              EXIT catalog_block WHEN $condition(local_status.condition) <> 'PFE$NAME_ALREADY_SUBCATALOG'
            ELSE
              created_catalogs = $add($fname(catalog_path), created_catalogs)
            IFEND
          IFEND
          file_name = catalog_path//'.'//deadstart_file_list(index).tape_name
          previous_catalog_path = catalog_path
          finished = TRUE

        ELSEIF file_id <> last_deadstart_tape_name THEN

          "  File exists on the deadstart tape but is not in the required files list.

          file_name = previous_catalog_path//'.'//file_id
          file_name = $TRIM(file_name ' ' trailing)
          finished = FALSE

        ELSE
          IF deadstart_file_list(index).site_catalog = ' ' THEN
            catalog_path = $string(deadstart_catalog)
          ELSE
            catalog_path = $string(deadstart_catalog)//'.'//deadstart_file_list(index).site_catalog
            create_catalog c=$fname(catalog_path) status=local_status
            IF NOT local_status.normal THEN
              EXIT catalog_block WHEN $condition(local_status.condition) <> 'PFE$NAME_ALREADY_SUBCATALOG'
            ELSE
              created_catalogs = $add($fname(catalog_path), created_catalogs)
            IFEND
          IFEND
          file_name = catalog_path//'.'//deadstart_file_list(index).tape_name
          finished = TRUE
        IFEND

        copy_file i=$fname(local_file) o=$fname(file_name//'.$next') status=local_status
        EXIT catalog_block WHEN NOT local_status.normal
        IF deadstart_file_list(index).object_library_file THEN
          change_file_attributes f=$fname(file_name//'.$high') fc=object fs=library status=local_status
          EXIT catalog_block WHEN NOT local_status.normal
        IFEND
        created_files = $add($fname(file_name//'.$high'), created_files)
        detach_file f=$fname(local_file) status=ignore_status
      UNTIL finished
      index = index + 1
    WHILEND

  BLOCKEND catalog_block

  detach_file f=$fname(tape_file) status=ignore_status
  detach_file f=$fname(info_file) status=ignore_status
  detach_file f=$fname(local_file) status=ignore_status
  IF NOT local_status.normal THEN

    "  Delete any files that were created.

    WHILE NOT $nil(created_files) DO
      return_file = $first(created_files)
      created_files = $rest(created_files)
      delete_file f=return_file status=ignore_status
    WHILEND

    "  Delete any catalogs that were created.

    WHILE NOT $nil(created_catalogs) DO
      return_file = $first(created_catalogs)
      created_catalogs = $rest(created_catalogs)
      delete_catalog c=return_file do=catalog_and_contents status=ignore_status
    WHILEND
  IFEND
  EXIT PROCEDURE WITH local_status WHEN NOT local_status.normal

PROCEND create_ve_deadstart_catalog
