MODULE global_sub;
LIST TITLE 'Global Substitution across Files';
!++
!
! ABSTRACT:
!
!	This module performs global substitutions
!	on sets of files based on a wildcard specifications.
!
! AUTHORS:
!
!    David K. Ream
!
! CREATION DATE: 1/11/1991
!
! MODIFICATION HISTORY:
!
!    [-tbs-]
!--

INCLUDE FILE 'comp_include:lib$find_file.scn_inc';
INCLUDE FILE 'comp_include:$rmsdef.scn_inc';

DECLARE count_1,count_2: INTEGER;

TOKEN findee_1 CASELESS { 'ENCYCLOPEDIA' };
TOKEN findee_2 CASELESS { 'Department of Duplication' };

MACRO find_1 TRIGGER { findee_1 };
	ANSWER 'HANDBOOK';
	count_1 = count_1 + 1;
	END MACRO; /* find_1

MACRO find_2 TRIGGER { findee_2 };
	count_2 = count_2 + 1;
	ANSWER 'Division of Redundancy';
	END MACRO; /* find_2

PROCEDURE nihil;

! this empty procedure will just throw away lines
! when they don't need to be saved

END PROCEDURE /* nihil */;


PROCEDURE subber MAIN OF INTEGER;

    DECLARE file_spec,file_name: STRING;
    DECLARE file_count,new_file_count,context,status: INTEGER;
    DECLARE oc: FIXED STRING(1);

    !
    !	prompt for a file specification with/out wildcards and
    !	read through the specified file(s)
    !

    WRITE ' ';
next_file_spec:
    READ PROMPT ('Enter files to process: ') file_spec;
    IF file_spec = ''
    THEN
	GOTO no_more_files;
    END IF;

    context = 0;
    status=lib$find_file(file_spec,file_name,context,*,*);
    IF status = rms$_fnf
    THEN
	WRITE ('no files found');
	GOTO next_file_spec;
    END IF;
! report other error statuses
    IF status <> rms$_normal
    THEN
	RETURN status;
    END IF;


    WHILE status <> rms$_nmf;
! strip off the version number
	file_name = file_name[ 1 .. INDEX(file_name,';')-1 ];
	WRITE 'DOING: ', file_name;
	file_count = file_count + 1;
	count_1 = 0;
	count_2 = 0;
! make a pass to see if any substitutions will occur
	START SCAN
	    INPUT FILE file_name
	    INPUT WIDTH 255
	    OUTPUT PROCEDURE nihil;
! don't create new versions of the file unless there are 
! substitutions to be made
	IF count_1+count_2 > 0 THEN
	    new_file_count = new_file_count + 1;
	    WRITE '1: ', count_1, '   2: ', count_2;
	    START SCAN
		INPUT FILE file_name
		INPUT WIDTH 255
		OUTPUT FILE file_name;
	END IF;
	status=lib$find_file(file_spec,file_name,context,*,*);
    END WHILE; /* find_file

    status=lib$find_file_end(context);
    GOTO next_file_spec;
no_more_files:
    WRITE ' ';
    WRITE file_count, ' files scanned';
    WRITE new_file_count, ' files changed';
    WRITE ' ';
    RETURN rms$_normal;

END PROCEDURE; /* subber

END MODULE; /* global_sub
