#!/usr/bin/perl
#/usr/local/bin/perl
#
# Program to open the password file, read it in,
# print it, and close it again.

# Firstly, open the file for our input.

use Switch;

$inputfile = $ARGV[0];
$outputfile = $ARGV[1];

open(DATA, $inputfile); # Open for input
open(INFO, ">>$outputfile");	# Open for output

#open(INFO, $file);	# Open for input
#open(INFO, ">$file");	# Open for output
#open(INFO, ">>$file");	# Open for appending
#open(INFO, "<$file");	# Also open for input

@inputLines = <DATA>;

# Next, we test for each lines. If the information stand in this line is what we need we print this line to our output file.
# Otherwise, we go to the next line and test again.

my $auth_counter;
#my $auth = "";
#my $sec = "";
my $output = "";
my $jid = 0;
my $sec_num = 0;

my ($auth, $title, $journal, $year, $vi, $number, $page, $month);

foreach my $inputLine (@inputLines) 
{
	$inputLine =~ s/^\s*//;
	$inputLine =~ s/\s*$//;
	@wordinputLine = split /\s+/, $inputLine;
	#Author
	#Title
	#Journal
	#Year
	#Volume
	#Number
	#Pages
	#Month

	
	switch (@wordinputLine[0]) {
		case /^PMID.*/	{
						 	if ($sec_num > 0){
								$output = $jid."\@Article\{pmid$pmid,\n"."   Author=\"$auth\",\n"."   Title=\"\{$title\}\",\n"."   Journal:\"$journal\".\n"."   Year=\"$year\",\n"."   Volume=\"$vi\",\n"."   Number=\"$number\",\n"."   Pages=\"$page\",\n"."   Month=\"$month\",\n"."\}\n";
							}
							
							print INFO $output; 
							$auth = "";
							$jid = "";
							#$sec = "";
							$title = "";
							$journal = "";
							$year = "";
							$vi = "";
							$number = "";
							$page = "";
							$month = "";							
							
							$pmid = @wordinputLine[1];
						 	print "\@Article\{pmid$pmid,\n";
							#$sec = $sec."\@Article\{pmid$pmid,\n";
							$auth_counter = 0; 
							
							
		}
					 					 
	    case /^VI/		{ 
						  	$vi = @wordinputLine[2];
		 				  	print "   Volume=\"$vi\",\n";
							#$sec = $sec."   Volume=\"$vi\",\n";
		}
						
		case /^IP/		{
						  	$number = @wordinputLine[2];
						 	print "   Number=\"$number\",\n";
							#$sec = $sec."   Number=\"$number\",\n";
		}
						
		case /^DP/      {
							
							$year = @wordinputLine[2];
							$month = @wordinputLine[3];
							print "   Year=\"$year\",\n";
							#$sec = $sec."   Year=\"$year\",\n";
							print "   Month=\"$month\",\n";
							#$sec = $sec."   Month=\"$month\",\n";
		}
						
		case /^TI/      { 	
							$title = "";
							my $i;
							for($i=2; $i < $#wordinputLine; $i++)
							{
								$title = $title.@wordinputLine[$i]." ";
							}
							$title = $title.@wordinputLine[$i];
							print "   Title=\"\{$title\}\",\n";
							#$sec =  $sec."   Title=\"\{$title\}\",\n";
		}
		
		case /^PG/      {
							$page = @wordinputLine[2];
							print "   Pages=\"$page\",\n";
							#$sec = $sec."   Pages=\"$page\",\n";
		}
		case /^FAU/     {
							$auth_counter = $auth_counter + 1;
							#print " $auth_counter \n";
							if($auth_counter > 1){$auth = $auth." and ";}
							my $i;
							for($i=2; $i < $#wordinputLine; $i++)
							{
								$auth = $auth.@wordinputLine[$i]." ";
							}
							$auth = $auth.@wordinputLine[$i];
							#print "   Author=\"$auth\",\n";
		}	
		case /^TA/		{
							print "   Author=\"$auth\",\n";
							#$sec = $sec."   Author=\"$auth\",\n";
							
							my $i;
							for($i=2; $i < $#wordinputLine; $i++)
							{
								$journal = $journal.@wordinputLine[$i]." ";
							}
							$journal = $journal.@wordinputLine[$i];
							print "   Journal:\"$journal\".\n";
							#$sec = $sec."   Journal:\"$journal\".\n";
		}
		case /^JID/		{
							my $this_jid = @wordinputLine[2];
							print "\n\% $this_jid (JID)\n";
							$jid = "\n\% $this_jid (JID)\n";
							$sec_num = $sec_num + 1;
			
		}
		else 			{print "";}
	}
	
	  
}

$output = $jid."\@Article\{pmid$pmid,\n"."   Author=\"$auth\",\n"."   Title=\"\{$title\}\",\n"."   Journal:\"$journal\".\n"."   Year=\"$year\",\n"."   Volume=\"$vi\",\n"."   Number=\"$number\",\n"."   Pages=\"$page\",\n"."   Month=\"$month\",\n"."\}\n";

print INFO $output;

#print INFO "This line goes to the file.\n";


