#!/usr/local/bin/perl -w
# ===========================================================================
#
#                            PUBLIC DOMAIN NOTICE
#               National Center for Biotechnology Information
#
#  This software/database is a "United States Government Work" under the
#  terms of the United States Copyright Act.  It was written as part of
#  the author's official duties as a United States Government employee and
#  thus cannot be copyrighted.  This software/database is freely available
#  to the public for use. The National Library of Medicine and the U.S.
#  Government have not placed any restriction on its use or reproduction.
#
#  Although all reasonable efforts have been taken to ensure the accuracy
#  and reliability of the software and data, the NLM and the U.S.
#  Government do not and cannot warrant the performance or results that
#  may be obtained by using this software or data. The NLM and the U.S.
#  Government disclaim all warranties, express or implied, including
#  warranties of performance, merchantability or fitness for any particular
#  purpose.
#
#  Please cite the author in any work or product based on this material.
#
# ===========================================================================
#
# Author:  Oleg Khovayko
#
# Modified: FEB 2006 by Aurelie Neveol
#
# File Description: eFetch calling example
#

# ---------------------------------------------------------------------------
my $eutils_root  = "http://www.ncbi.nlm.nih.gov/entrez/eutils";
my $eFetch_url = "$eutils_root/efetch.fcgi";

my $db_name = "PubMed";

# ---------------------------------------------------------------------------
use strict;

use LWP::UserAgent;
use LWP::Simple;
use HTTP::Request;
use HTTP::Headers;
use CGI;

# ---------------------------------------------------------------------------
# Read input file into variable $file
# File stream     - first argument $ARGV[0]
# Number of Files - second argument $ARGV[1]

print "$ARGV[1] files in stream $ARGV[0]\n";

my $nbfiles;
my $idfile;

my $outfile=$ARGV[0]."_out.xml";

open OUT,">$outfile" || die "Can't open for write: $!\n";

print OUT "<?xml version=\"1.0\"?>\n".
          "<!DOCTYPE PubmedArticleSet PUBLIC \"-//NLM//DTD PubMedArticle, 1st January 2006//EN\" \"http://www.ncbi.nlm.nih.gov/entrez/query/DTD/pubmed_060101.dtd\">\n".
          "<PubmedArticleSet>\n";

for($nbfiles=1;$nbfiles<=$ARGV[1];$nbfiles++){

  undef $/;  #to load whole file
  
  $idfile=$ARGV[0].$nbfiles.".txt";
  open IN, $idfile || die "Can't open for read: $idfile\n";
  my $file = <IN>;
  close IN;
  print "Loaded file $idfile: [$file]\n";

  # Prepare file - substitute all separators to comma

  $file =~ s/\s+/,/gs;
  print "Prepared file: [$file]\n";

  #Create CGI param line

  my $form_data = "db=$db_name&id=$file&mode=xml&report=citation";

  # ---------------------------------------------------------------------------
  # Create HTTP query

  my $headers = new HTTP::Headers(
	 Accept		=> "xml",
	 Content_Type	=> "application/x-www-form-urlencoded"
  );

  my $query = new HTTP::Request("POST", $eFetch_url, $headers );

  $query->content($form_data);

  # Create the user agent object

  my $ua = new LWP::UserAgent;
  #$ua->agent("ePost/example");
  $ua->agent("eFetch/example");

  # ---------------------------------------------------------------------------
  # send file to eFetch by HTTP

  my $response = $ua->request($query);

  # ---------------------------------------------------------------------------

  print "Response status message: [" . $response->message . "]\n";

  # ---------------------------------------------------------------------------
  # Parse response->content

  $response->content =~
    m|<PubmedArticleSet>(.+)</PubmedArticleSet>|s;

  my $ArticleSet = $1;
  #my $WebEnv   = $2;

  print OUT "$ArticleSet\n";

} # end for

print OUT "</PubmedArticleSet>\n";