#
# combine.pl
# Combine XML files into one
#
# 2004.11.14 Ver. 0.9
# Copyright (C) 2004 by Masashi Suzuto. All rights reserved.
#

$header_num = 3;

if (@ARGV < 1) {
  die "Set the directory \n";
}

$dir = $ARGV[0];
@filelist = &get_filelist($dir) ;

open(FILE, ">MEDLINE.xml") or die;

# XML header
print FILE "<?xml version=\"1.0\"?>\n";
print FILE "<!DOCTYPE PubmedArticleSet PUBLIC \"-//NLM//DTD PubMedArticle, 1st November 2003//EN\" \"http://www.ncbi.nlm.nih.gov/entrez/query/DTD/pubmed_031101.dtd\">\n";
print FILE "<PubmedArticleSet>\n";

for ($i = 2; $i < $#filelist+1; $i++) {
  $filelist[$i] = $dir . $filelist[$i];

  open(INPUT, "<$filelist[$i]") or die "Cannot open file";
  @xmldata = <INPUT>;
  chomp(@xmldata);

  for ($j = $header_num; $j < $#xmldata; $j++) {
    print FILE $xmldata[$j] . "\n";
  }
  close(INPUT);
}

# XML footer
print FILE "</PubmedArticleSet>\n";

close(FILE);

exit 0;

sub get_filelist {
  my($dir) = $_[0];
  opendir (DIR , $dir) || die "Cannot open dir $dir" ;
  my @filelist = readdir(DIR);
  closedir(DIR) ;
  return @filelist;
}
