#!/usr/athena/bin/perl

# set some variables
$writeIt = 1;
$noWrites = 0;
$recordStart = "^ Rec";
$recordEnd   = "^._____";
$home = $ENV{"HOME"};


# prompt for a save file
system("clear");
print "Welcome to the MIT Connection to FirstSearch.\n\n";
print "You may automatically save records if you wish.\n";
print "If you do not wish to save records,enter carriage return.\n";
print "file for records:";
$inFile = <STDIN>;
chop($inFile);

# what did he decide?

if ($inFile eq "") {
	print "will not save records!\n";
	exec("tfirst");
}

#looks like he wants to save records
$inFile = "$home/$inFile";
$outFile = "$inFile.fsch";

# do the searching
system("tfirstP","$inFile");

#manipulate the file

print "saving records in $outFile\n";

print "$inFile\n";
open(TEEFILE,"$inFile") || die "cannot open tfirst file $inFile";

open(OUTFILE,">$outFile") || die "can't open $outFile";

$writeFlag = $noWrites;

while (<TEEFILE>) {
	if ($_ =~ /$recordStart/) {
		$writeFlag = $writeIt;
	}
	if ($writeFlag == $writeIt)  {
		print OUTFILE $_;
		if ($_ =~ /$recordEnd/) {
			$writeFlag = $noWrites;
		}
	}
}

close(OUTFILE);
close(TEEFILE);
unlink("$inFile");
print "You can find your records in $outFile"
	
	
