?? RIGHT := 110 ??
*copyc osd$default_pragmats
MODULE ram$build_replacement_sl;
?? PUSH (LISTEXT := ON) ??
*copyc clt$file_reference
*copyc pft$application_info
*copyc pft$group
*copyc pft$permit_selections
*copyc pft$share_requirements
*copyc rat$file_values
*copyc rat$match_decks
*copyc rat$open_file_list
*copyc rat$write_scl_commands
?? POP ??
*copyc amp$close
*copyc amp$open
*copyc amp$put_next
*copyc amp$return
*copyc clp$scan_command_line
*copyc pfp$delete_permit
*copyc pfp$permit
*copyc pmp$get_unique_name
*copyc pmp$get_user_identification

*copyc rah$build_replacement_sl

  PROCEDURE [XDCL] rap$build_replacement_sl (source_file: rat$file_values;
        decks_ok: ^array [1 .. * ] OF ost$name;
        replace_sl: ost$name;
    VAR status: ost$status);

    VAR
      application_info: pft$application_info,
      ba: amt$file_byte_address,
      command: ^array [1 .. * ] of rat$write_scl_commands,
      command_fid: amt$file_identifier,
      command_file: ost$name,
      found: boolean,
      group: pft$group,
      i: rat$deck_index,
      identification: ost$user_identification,
      ignore_status: ost$status,
      j: rat$deck_index,
      k: integer,
      permit_selections: pft$permit_selections,
      permit_status: ost$status,
      rav$open_file_list: [STATIC, XREF] rat$open_file_list,
      share_requirements: pft$share_requirements,
      size: integer,
      text: string (osc$max_string_size);

    pmp$get_unique_name (command_file, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    amp$open (command_file, amc$record, NIL, command_fid, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    IF decks_ok = NIL THEN
      PUSH command: [1 .. 3];
    ELSE
      PUSH command: [1 .. UPPERBOUND (decks_ok^) + 3];
    IFEND;

    STRINGREP (command^ [1].command, command^ [1].size, ' scu');
    STRINGREP (command^ [2].command, command^ [2].size, ' use_library b=',
          source_file.ref.path_name (1, source_file.ref.path_name_size), ' r=', replace_sl);
    j := 3;
    IF decks_ok <> NIL THEN
      FOR i := 1 TO UPPERBOUND (decks_ok^) DO
        STRINGREP (command^ [j].command, command^ [j].size, '   delete_deck ', decks_ok^ [i]);
        j := j + 1;
      FOREND;
    IFEND;
    STRINGREP (command^ [j].command, command^ [j].size, ' quit wl=true');

    FOR i := 1 TO j DO
      amp$put_next (command_fid, ^command^ [i].command, command^ [i].size, ba, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;
    FOREND;

    amp$close (command_fid, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;


{    In order to successfully run SCU a permit must be built giving the user all authority }
{  on access mode and an application information value of 'I4'.                            }
{    The permit is deleted as soon as possible.  The source library used is from the       }
{  installation catalog and so the assumption that a permit does'nt already exist can      }
{  be made. }


    pmp$get_user_identification (identification, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    group.group_type := pfc$user;
    group.user_description.family := identification.family;
    group.user_description.user := identification.user;

    application_info := 'I4';
    permit_selections := $pft$permit_selections [pfc$read, pfc$shorten, pfc$append, pfc$modify,
          pfc$execute, pfc$cycle, pfc$control];
    share_requirements := $pft$share_requirements [];

    pfp$permit (source_file.path^, group, permit_selections, share_requirements,
          application_info, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    amp$return (source_file.lfn, ignore_status);
    k := 1;
    found := FALSE;
    WHILE (k <= UPPERBOUND (rav$open_file_list)) AND NOT found DO
      IF rav$open_file_list [k].name = source_file.lfn THEN
        rav$open_file_list [k].attached := FALSE;
        found := TRUE;
      IFEND;
      k := k + 1;
    WHILEND;

    STRINGREP (text, size, ' include_file f=', command_file);
    clp$scan_command_line (text (1, size), status);
    pfp$delete_permit (source_file.path^, group, permit_status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    IF NOT permit_status.normal THEN
      status := permit_status;
      RETURN;
    IFEND;

    amp$return (command_file, ignore_status);

  PROCEND rap$build_replacement_sl;
MODEND ram$build_replacement_sl;
