sub eutilities_getdata {

my $this = shift;
my $file1 = shift;
my $adminemail = shift;
my $gi_number_protein = shift;

#open file to write genbank records to
open (OUT, '>', $$file1) || die "Can't open file:$!";

#initial number of records to post
my $number = scalar(@$gi_number_protein);
print "Initial number of records of array before sub dividing ".$number."\n";

#starting and ending index of which records to post from the original array
my $min = 0;
my $max = 100;
my $step = $max - $min;

#subarray to hold the segment of records specified by min and max
my @subarray;

	while ($min < $number){

		if ($max < $number){
			print "start and end subarray index positions: $min     $max\n";
			@subarray = @$gi_number_protein[$min...$max];
		}	
		else{
			@subarray = @$gi_number_protein[$min...($number-1)];
			print "end of array last post, index positions: $min    ".($number-1)."\n";
		}
		
	print "subarray had this many elements ".scalar(@subarray)."\n";
	
	#retry counter to handle the eval block of post and fetch
	my $retry = 0;
	my $count = scalar(@subarray);
	print "number of records in sub array: $count\n";
	my $factory;
		REDO_POST_FETCH:
		eval{
			my $factory = Bio::DB::EUtilities->new(-eutil => 'epost',
	                                       -email => $$adminemail,
	                                       -db => 'protein',
	                                       -id => \@subarray,
	                                       -verbose => 2,);
	 

			# get history from queue
			my $hist = $factory->next_History || die 'No history data returned';
			print "History returned\n";
			
			# note db carries over from above
			$factory->set_parameters(-eutil => 'efetch',
	                             	-rettype => 'gp',
	                     	    	-history => $hist,
	                     	    	-verbose => 2,);

		};
		if ($@) {
			die "Server error on post and fetch: $@.  Try again later" if $retry == 5;
		    print STDERR "$@\n";
		    print STDERR "Server error on post and fetch, redo #$retry\n";
		    $retry++;
		    sleep(5);
		    goto REDO_POST_FETCH;
		}
	
	#increment the sub index pointers
	$min = ($max +1);
	$max += $step;
	
		
	my $retry = 0;
	my ($retmax, $retstart) = (500,0);
 
		RETRIEVE_SEQS:
		while ($retstart < $count) {
		
	    	$factory->set_parameters(-retmax => $retmax,
	                            	-retstart => $retstart,
	                            	-verbose => 2,);
	    		eval{
	        		$factory->get_Response(-cb => sub {my ($data) = @_; print OUT $data} );
	    		};
	    		if ($@) {
	        		die "Server error: $@.  Try again later" if $retry == 5;
	        		print STDERR "Server error, redo #$retry\n";
	        		$retry++;
	        		sleep(5);	
					redo RETRIEVE_SEQS;
	    		}
			print "Retrieved $retstart\n";
	    	$retstart += $retmax;
		}

	}
#end of while loop of $min < $number

close OUT;
}