PROCEDURE generate_banner_page, genbp (
  input, i: record
      system_file_name: name 19..19
      password: string 1..31
      file: file = $optional
    recend = $required
  output, o: file = $required
  banner_page_count, bpc: integer 1..3 = $required
  banner_placement, bp: key
      (beginning_of_file, bof)
      (beginning_and_end, bae)
      (end_of_file, eof)
    keyend = beginning_of_file
  device_environment_variable, dev: name = $required
  comment_banner, cb: string 0..31 = $required
  data_mode, dm: key
      (coded, c)
      (transparent, t)
    keyend = $required
  routing_banner, rb: string 0..31 = $required
  status)

" PURPOSE:
"   This procedure generates one or more banner pages very similar to
"   those generated by CDCNET software for printers connected to CDCNET.
"
" DESIGN:
"   Calculate the number of lines available for the banner page based on
"  the forms size and the vertical print density of the printer. Conditionally
"  generate the banner text base on the number of lines available on
"  the banner page, and which banner highlight field the printer is defined
"  to display. Reject requests to generate banner pages for transparent
"  files if the TIP type of the printer is not ASYNC or X25_ASYNC. Non-async.
"  devices do not (currently) process transparent files.
"
" NOTE:
"   This function is meant to be a model for site designed procedures
"   which will generate customized banners to replace the CDCNET banners
"   currently generated.

" Constants.
  VAR
    burst_line: string 1...88 = ..
    '   MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWM'
    key_length: integer = 14
    leading_blanks: string 18 = '                  '
  VAREND

  VAR
    banner_block_letters: list of string 0..255
    banner_highlight_field: name
    current_line: integer
    device_type: name
    device_vpd: name
    forms_size : real
    index: integer
    last_line_on_page: integer
    local_status: status
    postscript_device: boolean
    prefix_char: string 0..1
    suffix_chars: string 0..2
    terminal_model: name
    tip_type: name
  VAREND

" Determine if the SCFS/VE that sent the file assignment message to SCF/VE is
" the new 'BOF' version, or if it is the old version which does not send enough
" information to completly initialize the device environment variable. If the
" SCFS/VE is the old version, then certain device-dependent attributes must be
" assumed.

  include_command 'device_type=$device_attributes(dev, device_type)' status=local_status
  IF local_status.normal THEN
    banner_highlight_field = $device_attributes(dev, banner_highlight_field)
    device_vpd = $device_attributes(dev, vertical_print_density)
    forms_size = $device_attributes(dev, forms_size)
    page_width = $device_attributes(dev, page_width)
    terminal_model = $device_attributes(dev, terminal_model)
    tip_type = $device_attributes(dev, tip_type)
  ELSE
    banner_highlight_field = routing_banner
    device_type = printer
    device_vpd = six_only
    forms_size = 11.0
    page_width = 132
    terminal_model = unknown
    tip_type = async
  IFEND

" Determine the prefix and suffix characters for the banner and if this output
" filter should accept the file based on the data mode and the TIP type.

  IF data_mode = coded THEN
    prefix_char = ' '
    suffix_chars = ''
  ELSE
    IF (tip_type <> async) AND (tip_type <> x25_async) THEN
      display_message ..
            m='****GENERATE_BANNER_PAGE can not create a transparent banner for device type '//tip_type
      EXIT_PROC WITH $status(false 'NF' nfe$wrong_file_type)
    IFEND
    prefix_char = ''
    suffix_chars = $char(cr lf)
  IFEND

" Calculate the number of lines that are available for the banner page
" based on the forms size and the vertical print density.

  IF (device_vpd = six_only) OR (device_vpd = six_any) THEN
    IF ($job_output(input.system_file_name, vpd) = eight) THEN
      last_line_on_page = (forms_size * 8)
    ELSE "file vpd is either six, or none.
      last_line_on_page = (forms_size * 6)
    IFEND
  ELSEIF (device_vpd = eight_only) OR (device_vpd = eight_any) THEN
    IF ($job_output(input.system_file_name, vpd) = six) THEN
      last_line_on_page = (forms_size * 6)
    ELSE "file vpd is either eight, or none.
      last_line_on_page = (forms_size * 8)
    IFEND
  IFEND

" Allocate the array needed to hold the banner page text.  Since the array is
" defined as the exact size of one page, it can be written to the output file
" with a single put_line command.

  VAR
    banner_text: ARRAY 1..last_line_on_page OF STRING 0..page_width
  VAREND

  IF data_mode = coded THEN
    create_t_record_file f=output pw=page_width pl=last_line_on_page status=local_status
    EXIT_PROC WITH local_status WHEN NOT local_status.normal
  ELSE
    set_file_attributes file=output pw=page_width pl=last_line_on_page rt=undefined
  IFEND

" Reserve the last three lines of each page for burst lines.

  last_line_on_page = last_line_on_page - 3

  current_line = 1
  postscript_device = $scan_string('POSTSCRIPT', $translate(ltu, $string(terminal_model))) > 0

  IF current_line <= last_line_on_page THEN
    IF data_mode = coded THEN
      IF postscript_device THEN

" Sidestep a blank sheet of paper between the banner page and the file by
" causing the the first line of the first banner page to overstrike.

        banner_text(current_line) = '+ '//suffix_chars
      ELSE
        banner_text(current_line) = '1 '//suffix_chars
      IFEND;
    ELSE
      banner_text(current_line) = prefix_char//$char(ff)//suffix_chars
    IFEND
    current_line = current_line + 1
  IFEND

" Add padding lines.

  FOR index = 1 TO 5 DO
    IF current_line <= last_line_on_page THEN
      banner_text(current_line) = prefix_char//' '//suffix_chars
      current_line = current_line + 1
    IFEND
  FOREND

" Generate the site information line if appropriate.

  IF current_line <= last_line_on_page THEN
    IF (banner_highlight_field <> site_banner) AND ..
          $strlen($job_output(input.system_file_name, site_information)) > 0 THEN
      banner_text(current_line) = prefix_char//leading_blanks// ..
            $job_output(input.system_file_name, site_information)//suffix_chars
      current_line = current_line + 1
    IFEND
  IFEND

" Produce the appropriate file information lines.
  IF current_line <= last_line_on_page THEN
    banner_text(current_line) = prefix_char//' '//suffix_chars
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    banner_text(current_line) = prefix_char//leading_blanks//..
$substring('PRINTED', 1, key_length, ' ')//'= '//$date(mdy)//' '//$time(hms)//suffix_chars
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    banner_text(current_line) = prefix_char//leading_blanks//..
$substring('CREATED', 1, key_length, ' ')//'= '//$date(mdy, $job_output(input.system_file_name, ost))//' '//..
$time(hms, $job_output(input.system_file_name, ost))//suffix_chars
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    banner_text(current_line) = prefix_char//leading_blanks//..
$substring('FAMILY', 1, key_length, ' ')//'= '//$job_output(input.system_file_name, login_family)//..
suffix_chars
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    IF banner_highlight_field <> user_name THEN
      banner_text(current_line) = prefix_char//leading_blanks//..
$substring('USER NAME', 1, key_length, ' ')//'= '//$job_output(input.system_file_name, login_user)//..
suffix_chars
      current_line = current_line + 1
    IFEND
  IFEND

  IF current_line <= last_line_on_page THEN
    banner_text(current_line) = prefix_char//leading_blanks//..
$substring('USER JOB NAME', 1, key_length, ' ')//'= '//$job_output(input.system_file_name, ujn)//suffix_chars
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    IF banner_highlight_field <> user_file_name THEN
      banner_text(current_line) = prefix_char//leading_blanks//..
$substring('FILE NAME', 1, key_length, ' ')//'= '//$job_output(input.system_file_name, ufn)//suffix_chars
      current_line = current_line + 1
    IFEND
  IFEND

  IF current_line <= last_line_on_page THEN
    banner_text(current_line) = prefix_char//' '//suffix_chars
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    IF banner_highlight_field = comment_banner THEN
      banner_text(current_line) = prefix_char//leading_blanks//routing_banner//suffix_chars
    ELSE
      banner_text(current_line) = prefix_char//leading_blanks//comment_banner//suffix_chars
    IFEND
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    banner_text(current_line) = prefix_char//' '//suffix_chars
    current_line = current_line + 1
  IFEND

  IF current_line <= last_line_on_page THEN
    IF (banner_highlight_field <> comment_banner) AND (banner_highlight_field <> routing_banner) THEN
      banner_text(current_line) = prefix_char//leading_blanks//routing_banner//suffix_chars
    ELSE
      banner_text(current_line) = prefix_char//' '//suffix_chars
    IFEND
    current_line = current_line + 1
  IFEND

" Adding padding lines.

  FOR index = 1 TO 4 DO
    IF current_line <= last_line_on_page THEN
      banner_text(current_line) = prefix_char//' '//suffix_chars
      current_line = current_line + 1
    IFEND
  FOREND

" Generate the wrapped block text for the banner highlight field.
" Truncate the excess lines if there are not enough lines remaining
" on the banner page.

  IF current_line <= (last_line_on_page - 13) THEN
    IF banner_highlight_field = user_name THEN
      banner_block_letters = $wrapped_block_text($string($job_output(input.system_file_name, login_user)), ..
            page_width, (last_line_on_page- current_line))
    ELSEIF banner_highlight_field = comment_banner THEN
      banner_block_letters = $wrapped_block_text(comment_banner, page_width, (last_line_on_page- ..
            current_line))
    ELSEIF banner_highlight_field = routing_banner THEN
      banner_block_letters = $wrapped_block_text(routing_banner, page_width, (last_line_on_page- ..
            current_line))
    ELSEIF banner_highlight_field = site_banner THEN
      banner_block_letters = $wrapped_block_text($job_output(input.system_file_name, site_information), ..
            page_width, (last_line_on_page- current_line))
    ELSEIF banner_highlight_field = user_file_name THEN
      banner_block_letters = $wrapped_block_text(..
            $string($job_output(input.system_file_name, user_file_name)), page_width, (last_line_on_page- ..
            current_line))
    IFEND
    FOR index = 1 TO $size(banner_block_letters) DO
      banner_text(current_line) = prefix_char//banner_block_letters(index)//suffix_chars
      current_line = current_line + 1
    FOREND
  IFEND

" Add padding lines.

  FOR index = current_line TO last_line_on_page DO
    banner_text(index) = prefix_char//' '//suffix_chars
  FOREND

" Add the three burst lines that print on the same page as the other banner
" text.

  banner_text(last_line_on_page + 1) = prefix_char//burst_line//suffix_chars
  banner_text(last_line_on_page + 2) = prefix_char//burst_line//suffix_chars
  banner_text(last_line_on_page + 3) = prefix_char//burst_line//suffix_chars

" Copy either the queue file or the input file to the output file if the banner
" is to be printed after the file.

  IF (banner_placement = end_of_file) THEN
    IF $field(input, file, initialized) THEN
      copy_file i=input.file o=output.$eoi
    ELSE
      copy_output_file n=input.system_file_name o=output.$eoi
    IFEND
  IFEND

" Insert the banner page(s) into the output file.

  FOR index = 1 TO banner_page_count DO
    IF (index = 2) THEN
      banner_text(1) = prefix_char//burst_line//suffix_chars
      banner_text(2) = prefix_char//burst_line//suffix_chars
      banner_text(3) = prefix_char//burst_line//suffix_chars
    IFEND
    put_line l=banner_text o=output.$eoi
  FOREND

" Insert the three burst lines that trail onto the first page
" after the banners.

  IF NOT postscript_device THEN
    put_line l=prefix_char//burst_line//suffix_chars o=output.$eoi
    put_line l=prefix_char//burst_line//suffix_chars o=output.$eoi
    put_line l=prefix_char//burst_line//suffix_chars o=output.$eoi
  IFEND

" Append either the queue file or the input file to the file which
" contains the banner page(s).

  IF (banner_placement = beginning_of_file) OR (banner_placement = beginning_and_end) THEN
    IF $field(input, file, initialized) THEN
      copy_file i=input.file o=output.$eoi
    ELSE
      copy_output_file n=input.system_file_name o=output.$eoi
    IFEND
  IFEND

" Insert the banner pages at the end of the file, but do not waste an
" extra page of paper by appending three more burst lines.

  IF (banner_placement = beginning_and_end) THEN
    IF data_mode = coded THEN
      IF postscript_device THEN
        banner_text(1) = prefix_char//' '//suffix_chars
      ELSE
        banner_text(1) = '1 '//suffix_chars
      IFEND
      banner_text(2) = prefix_char//' '//suffix_chars
      banner_text(3) = prefix_char//' '//suffix_chars
    ELSE
      banner_text(1) = prefix_char//$char(ff)//suffix_chars
      banner_text(2) = prefix_char//' '//suffix_chars
      banner_text(3) = prefix_char//' '//suffix_chars
      current_line = current_line + 1
    IFEND
    FOR index = 1 TO banner_page_count DO
      IF (index = 2) THEN
        banner_text(1) = prefix_char//burst_line//suffix_chars
        banner_text(2) = prefix_char//burst_line//suffix_chars
        banner_text(3) = prefix_char//burst_line//suffix_chars
      IFEND
      put_line l=banner_text o=output.$eoi
    FOREND
  IFEND

  EXIT_PROC WITH local_status WHEN NOT local_status.normal

PROCEND generate_banner_page
