#!/usr/bin/perl -w

use Bio;


sub ask_user {
  print "$_[0] [$_[1]]: ";
  my $rc = <>;
  chomp $rc;
  if($rc eq "") { $rc = $_[1]; }
  return $rc;
}

# ---------------------------------------------------------------------------
# Define library for the 'get' function used in the next section.
# $utils contains route for the utilities.
# $db, $query, and $report may be supplied by the user when prompted; 
# if not answered, default values, will be assigned as shown below.

use LWP::Simple;

my $utils = "http://www.ncbi.nlm.nih.gov/entrez/eutils";


# db=[pubmed|pmc|journals|omim]
my $db     = ask_user("Database", "pmc");

# query={searchterm}
my $query  = ask_user("Query",    "breast cancer nuclear hormone receptor drug treatment");

# Retrieval Mode:
#    xml (not journals)
#    html
#    text
#    asn.1 (not journals) 
# with pmc -> only xml
my $retmode = ask_user("Retrieval Mode",   "xml");

#Retrieval Type:
#    uilist
#    abstract (not omim)
#    citation (not omim)
#    medline (not omim)
#    full  (journals and omim) 
my $rettype = ask_user("Retrieval Type",   "full");

my $fetch = ask_user("Fetch",	  "no");




my $esearch = Bio::DB::EUtilities->new(
					-eutil      => 'esearch',
					-db         => $db,
					-term       => $query,
					-usehistory => 'n');

print "Count = " . @esearch;

# ---------------------------------------------------------------------------
# this area defines a loop which will display $retmax citation results from 
# Efetch each time the the Enter Key is pressed, after a prompt.

if ($fetch eq "yes") {	
	for($i = 0; $i < @esearch; $i = $i +1) {
		my $efetch = Bio::DB::EUtilities->new (
					-cookie       => $esearch->next_cookie,
                                        -retmax       => 10,
                                        -rettype      => 'full');
		open(FILE, ">pmc_articles/myNCBI_$i.txt");    # Datei zum Schreiben öffnen
		flock (FILE, 2);
		print FILE $efetch->get_response->content;
		close(FILE);
	}
}

