#!/usr/local/bin/perl

#
############################################################################
#
#  Author:	E. Ashburn
#
#  Date:	03/13/00
#
#
#  Description:	Replaces string characters
#		(specific - WIDTH=30 with WIDTH=40 & HEIGHT=30 with HEIGHT=40)
#		(also, adds in the NIST back logo by subsitution)
#
#
#
#  Inputs:	Arguments from the command line (i.e., filename)
#
#
#  Outputs:	filename.r
#
#
#  Comments:	Enlarges TREC logo so that it is in proportion with the NIST logo
#		Inserts the NIST logo to meet Identity Guidelines for NIST
#
#
#
###########################################################################

#
use File::Basename;
#Call CHK_CMD_LINE
&CHK_CMD_LINE;
#sets chapter and page variables
        $chapter = "App5A";
        $page = "296";
for $arg (@ARGV) {
	&OPEN_FILES;
#prints message to STDOUT "Processing file arg"
	print( STDOUT "Processing file $arg  \n");
        while ($line=<INPUT>) {
		chomp ($line);
		if ($line =~ "<IMG SRC=\"text.gif\" BORDER=0 ALT=\"Text\"></A>") { 
                	print OUTPUT "<A HREF=\"../../cranv2_text/" . $chapter . "_" . $page . ".txt\"><IMG SRC=\"text.gif\" BORDER=0 ALT=\"Text\"></A>\n";
		$page ++	
		} else {
			print OUTPUT "$line\n";
		}
	}

&RENAME_FILES;
&CLOSE_FILES;
}
#

###########################################################################
#
#  Function Name:	CHK_CMD_LINE 
#
#  Processing:		Routine which checks the command line for number 
#			of arguments (i.e., filename) in addition to the 
#			executable name (replace.pl).  If there is no 
#			filename specified, a usage message is printed to 
#			STDOUT and the program is exited.
#
#  Inputs:		Arguments from command line.
#
#  Outputs:		Usage message to STDOUT if applicable.
#
#  Speical Notes:	None.
#
#
############################################################################
sub CHK_CMD_LINE {
	if ( $#ARGV < 0 )
	{
    	print "Usage: ", $0, " inputfiles \n";
	exit 0;
	}
}

#
###########################################################################
#
#  Function Name:	OPEN_FILES
#
#  Processing:		Routine which opens the input file named as the
#			initial arguments for reading.
#
#  Inputs:		None.
#
#  Outputs:		Error message to STDOUT if unable to open.
#
#  Speical Notes:	None.
#
#
############################################################################
sub OPEN_FILES {
	my $filename, $path, $dir, @path;
	($filename, $path) = fileparse($arg);
	@path=split('/', $path);
	$dir=pop(@path);
	open( INPUT, $arg ) or die "Cannot open file for reading: $arg. ";
	open( OUTPUT, "> $arg.buffer" ) or die "Cannot open file for writing: $arg. ";
}


###########################################################################
#
#  Function Name:       RENAME_FILES
#
#  Processing:          Routine which renames the files which the script
#			was writing to from $arg.buffer to $arg. 
#
#  Inputs:              Arguments from command line.
#
#  Outputs:             Usage message to STDOUT if applicable.
#
#  Speical Notes:       None.
#
#
############################################################################
sub RENAME_FILES {
	`mv "$arg".buffer "$arg".r`;
}

###########################################################################
#
#  Function Name:	CLOSE_FILES
#
#  Processing:		Routine which closes the input file for reading
#			and output file for writing or an error message to 
#			STDOUT if unable to close input/output file.
#
#  Inputs:		None.
#
#  Outputs:		None.
#
#  Speical Notes:	None.
#
#
#
############################################################################
sub CLOSE_FILES {
        close( INPUT );
        close( OUTPUT );
}

