MODULE seminar_prep;

!
! this module peruses the file containing the files comprising
! a seminar and prepares them by
!
!   titling the first page with the file name,
!   adjusting the left margin
!   optionally numbering the lines
!
! it does not try to prep files that are on the PC
!
! it also creates a list of print commands
!

CONSTANT bias = 100;

DECLARE line_count, page_count : INTEGER;
DECLARE file_in : STRING;
DECLARE numbering : BOOLEAN;

TOKEN start_of_line { S'SOS' | S'EOL' };
! don't number lines with formfeeds
TOKEN form_feed_line { S'EOL' S'FF' };
! don't number empty lines at the end of files
TOKEN last_empty_line { S'EOL' S'EOS' };

MACRO line_to_prep TRIGGER { byte: start_of_line };
    DECLARE rj_count : STRING;

    line_count = line_count + 1;

! right justify the count
    rj_count = STRING( line_count );
    WHILE LENGTH( rj_count ) < 3;
	rj_count = ' ' & rj_count;
    END WHILE;

    ANSWER byte;

! note the filename on the first page
    IF line_count = 1
    THEN
	ANSWER S'HT', S'HT', S'HT', '>>> ', file_in, ' <<<',
	    S'EOL', S'EOL';
    END IF;

! shift right 4 spaces for a larger left margin
    ANSWER '    ';

! add a line count if requested
    IF numbering 
    THEN
	ANSWER rj_count, '  ';
    END IF;

END MACRO /* line_to_prep */;

MACRO find_ff TRIGGER { ffl: form_feed_line };

    ANSWER ffl;
    page_count = page_count + 1;

END MACRO /* find_ff */;

PROCEDURE seminar_prep MAIN;
    DECLARE line_in, file_out : STRING;
    DECLARE i, file_in_count, pc_file_count, file_out_count : INTEGER;
    DECLARE prep_list, print_list : FILE;

    file_out_count = bias;

! lines in the order file have this format
!   column 1	    y or n to determine whether line counts should be added
!   column 2	    ignored
!   column 3+	    filename for VAX or PC files

    OPEN FILE( prep_list ) AS 'seminar.order' FOR INPUT;
    OPEN FILE( print_list ) AS 'print.com' FOR OUTPUT;

    WRITE ' ';

    READ FILE( prep_list ) line_in;
    WHILE NOT ENDFILE( prep_list );

! get past the numbering flag
	file_in = line_in[ 3.. ];

	file_in_count = file_in_count + 1;

! don't process PC chart files
	IF INDEX( line_in, '.' ) = 0
	THEN
	    WRITE file_in, ' -> PC';
	    pc_file_count = pc_file_count + 1;
	ELSE
! output file name is number.prepped to maintain the printing order
	    file_out_count = file_out_count + 1;
	    file_out = STRING( file_out_count ) & '.prepped';
! create file to generate printouts
	    WRITE FILE( print_list ) '$tallet ', file_out;
! log which files in become which files out
	    WRITE file_in, ' -> ', file_out;

! set whether to number or not
	    numbering = LOWER( line_in[ 1 ] ) = 'y';

	    line_count = 0;	    ! reset the count for each file !

	    START SCAN
		INPUT FILE file_in
		OUTPUT FILE file_out;

	END IF;

	READ FILE( prep_list ) line_in;
    END WHILE;


    CLOSE FILE( prep_list );
    CLOSE FILE( print_list );

    WRITE ' ';
    WRITE file_in_count, ' files in';
    WRITE file_out_count - bias, ' files out';
    WRITE ' ';
    WRITE pc_file_count, ' PC files';
    WRITE ' ';
    WRITE page_count + file_in_count, ' pages total';
    WRITE ' ';

END PROCEDURE /* seminar_prep */;

END MODULE /* seminar_prep */;
