#!/usr/bin/perl
#

$url = "http://risa.is.tokushima-u.ac.jp/~tetsushi/achievements";

sub printhead {
	print F "\\begin{center}\n";
	print F "\\begin{tabular}{";
	$t = "c";
	$head = "Year &";
	for $x (@KindAbb){ $t .= "c";  $head .= "$x &"; }
	$t .= "c";
	$head .= "Total\\\\";
	print F "$t}\n $head\n";
}

$file = "current.csv";
$body = "achieve";
$output  = "$body.tex";
$outputbib = "$body.bib";


($sec, $min, $hour, $mday, $mmm, $thisyear, $wday, $yday, $isdst) 
	= localtime(time);

@Month = ("0", "Jan.", "Feb.", "Mar.", "Apr.", "May", "June", "July",
"Aug.", "Sep.", "Oct.", "Nov.", "Dec." );

@col = ("blue", "red", "red", "black", "magenta", "black", "black", "black");

@Kind = (
	"Book",
	"Academic Paper",
	"Academic Letter",
	"Review, Commentary",
	"Proceeding of International Conference",
	"Proceeding of Domestic Conference",
	"Et cetera, Workshop",
	"Reports"
);

@KindAbb = ( "BK", "PR", "LT", "RC", "IC", "DC", "WS", "RE" );

open F, "<$file" or die "cannot open $file\n";

while (<F>){
	@list = split('\t');

	$kind = $list[2];

	$kind =~ s/学術論文/Academic Paper/g;
	$kind =~ s/その他.*研究会/Et cetera, Workshop/g;
	$kind =~ s/学術レター/Academic Letter/g;
	$kind =~ s/国際会議/Proceeding of International Conference/g;
	$kind =~ s/国内講演発表/Proceeding of Domestic Conference/g;
	$kind =~ s/総説.*解説/Review, Commentary/g;
	$kind =~ s/著書/Book/g;
	$kind =~ s/報告書/Reports/g;

	$kindx = 0;
	for $x (@Kind){
		$kindx++;
		if ($kind eq $x){ last; }
	}

	#print "list20 = ------>$list[20]<---------\n";

	$year = substr($list[20], 0, 4);
	$month = substr($list[20], 4, 2);

	if ($month >= 1 || $month <= 12){ $mon = $Month[$month]; } 
	else { $mon = ""; } 

	$eid = $list[0];
	$authlist = $list[9];
	$authlist =~ s/\(.*?\)//g;
	$authlist =~ s/上田 哲史/\\underline{上田 哲史}/g;
	$authlist =~ s/Tetsushi Ueta/\\underline{Tetsushi Ueta}/g;

	$title = $list[10];
	$subtitle = $list[11];

	$abst = $list[12];


	$publisher = $list[14];
	$publisher =~ s/&/\\&/g;
	$journal = $list[15];
	$journal =~ s/&/\\&/g;
	$volume = $list[16];
	$number = $list[17];
	$pages = $list[18];
	$place = $list[19];
	
	if ($abst ne ""){
		$line = "$authlist, \\htmladdnormallink{$title}{$url/abst/abst$eid.html}, ";
	} else {
		$line = "$authlist, $title, ";
	}
	$data = "$authlist, $title, ";

	if ($subtitle ne "") {$line .= "$subtitle, "; $data .= "$subtitle"; }
	if ($kind ne $Kind[2] && $publisher ne "") { 
		$line .= "$publisher, "; $data .= "$publisher, ";} 
	if ($journal ne "") { $line .= "$journal, "; $data .= "$journal, "; } 
	if ($volume ne "") { $line .= "$volume, "; $data .= "$volume, "; } 
	if ($number ne "") { $line .= "$number, "; $data .= "$number, "; } 
	if ($pages ne "") { $line .= "$pages, "; $data .= "$pages, "; } 
	if ($place ne "") { $line .= "$place, "; $data .= "$place, "; } 
	if ($mon ne "") { $line .= "$mon"; $data .= "$mon"; } 
	$line .= "$year.\n"; $data .= "$year.\n";

	if ($month >= 4 && $month <= 12) { $syear = $year; } 
	else { $syear = $year - 1; }

	if ($abst ne ""){
		open AB, "> ./abst/abst$eid.tex" or die "cannot open abstfile for $eid\n";
$doc = <<END;
		\\documentclass{article}
		\\begin{document}
END
		print AB "$doc\n";
		print AB "{\\LARGE\\bf $kind }\\\\\n";
		print AB "$data\n";
		print AB "{\\bf Abstract: } $abst\n";
		print AB "\\end{document}\n";
		close AB;
		# system "/usr/local/bin/latex2thml abst$eid.tex";
	}

# 科研用スクリプト
#	$kaken = "$year & 上田哲史 & ``$title,''" ."$authlist, ";
#	if ($journal ne "") { $kaken .= "$journal, ";}
#	if ($kind ne $Kind[2] && $publisher ne "") { $kaken .= "$publisher, ";}
#	if ($volume ne "") { $kaken .= "$volume, ";}
#	if ($number ne "") { $kaken .= "$number, ";}
#	if ($pages ne "") { $kaken .= "$pages, ";}
#	if ($year ne "") { $kaken .= "($year)";}
#	if ($year >= 2002 && $kind eq $Kind[0]) {print "$kaken\\\\\n";}
#	if ($year >= 2002 && $kind eq $Kind[1]) {print "$kaken\\\\\n";}
#	if ($year >= 2002 && $kind eq $Kind[2]) {print "$kaken\\\\\n";}
#	if ($year >= 2002 && $kind eq $Kind[4]) {print "$kaken\\\\\n";}

	if ($line eq "") { $line = "none\."; }
	$databycateg{$kind} .= "\\item ".$line;
	$key = $kindx.$kind;
	$databyyear{$year}{$key} .= 
		"\\item "."\\textcolor{$col[$kindx-1]}{[$kindx $kind]}~".$line;
	$databysyear{$syear}{$key} .= 
		"\\item "."\\textcolor{$col[$kindx-1]}{[$kindx $kind]}~"
		.$line."($syear 年度 )";
	$biblist .= "\\bibitem{$eid}".
			"\\textcolor{$col[$kindx-1]}{[\\bf $kindx $kind]}~".$line.
			"EID=\\htmladdnormallink{$eid}{https://web.db.tokushima-u.ac.jp/cgi-bin/edb_browse?EID=$eid}\n\n";
	$numbycateg{$kind}++;
	$numbyyearwithkind{$year}{$kind}++;
	$numbyyear{$year}++;
	$numbysyear{$syear}++;
	$numtotal++;

	if ($bibbyyear{$year}{$kind} eq ""){ 
		$bibbyyear{$year}{$kind} = "\\begin{tabular}{cc}";
	}

	# trim two entries within a cell.
	$bibbyyear{$year}{$kind} .= "\\cite{$eid}";
	if ($numbyyearwithkind{$year}{$kind} % 2 == 0) { 
		$bibbyyear{$year}{$kind} .= "\\\\";
	}

}

# terminator.
#
foreach $i (sort keys %bibbyyear){
	foreach $j (sort keys %{$bibbyyear{$i}}){
		$bibbyyear{$i}{$j} .= "\\end{tabular}";
	}
}


#=============================================================================


open F, ">$output" or die "cannot open $output\n";

$doc = <<END;
\\documentclass{article}
\\textwidth=180mm
\\textheight=240mm
\\oddsidemargin=-5mm
\\topmargin=-10mm
\\usepackage{html}
\\usepackage[dvips]{color}
\\title{Achievements}
\\author{\\htmladdnormallink
	{Tetsushi Ueta}{http://risa.is.tokushima-u.ac.jp}}
\\begin{document}
\\maketitle

Totally \\textcolor{red}{$numtotal} items are recorded.
%Below lists are automatically obtained from 
%\\htmladdnormallink{EDB}{http://web.db.tokushima-u.ac.jp}
%with a Perl script and \\LaTeX2HTML. 
END

print F $doc;


print F "\\section{Summary}\n";
#---------------------------------------------
print F "\\subsection{Legends}\n";
@KX = @Kind;
print F "\\begin{description}\n";
for $i (@KindAbb){ $tmp = shift(@KX); print F "\\item[$i] $tmp\n"; }
print F "\\end{description}\n";

#---------------------------------------------
print F "\\subsection{Numbers of publications}\n";

printhead();

foreach $y (sort keys %numbyyearwithkind)
{
	print F "$y \\pageref{subsec:$y}&";
	for $K (@Kind){ printf F ("%d &", $numbyyearwithkind{$y}{$K}); }
	print F "$numbyyear{$y}\\\\\n";
}
print F "Total &";
for $K (@Kind){ print F "$numbycateg{$K} &"; }
print F "$numtotal\\\\\n";
print F "\\end{tabular}\n";
print F "\\end{center}\n";

#---------------------------------------------
print F "\\subsection{Table with reference numbers}\n";

print F "{\\footnotesize ";
printhead();

foreach $y (sort keys %numbyyearwithkind)
{
	print F "$y \\pageref{subsec:$y}&";
	for $K (@Kind){ printf F "$bibbyyear{$y}{$K} &"; }
	print F "$numbyyear{$y}\\\\\n";
}
print F "Total &";
for $K (@Kind){ print F "$numbycateg{$K} &"; }
print F "$numtotal\\\\\n";
print F "\\end{tabular}\n";
print F "\\end{center}\n";
print F "}";

#---------------------------------------------

print F "\\section{List by category}\n";
foreach $k (@Kind)
{
	print F "\\subsection{ $k ($numbycateg{$k} entries)}\n";
	print F "\\label{subsec:$k}\n";
	print F "\\begin{enumerate}\n";
	print F $databycateg{$k};
	print F "\\end{enumerate}\n";
}

#---------------------------------------------

print F "\\section{List by year}\n";
foreach $y (sort keys %databyyear)
{
	print F "\\subsection{$y, $numbyyear{$y} entries}\n";
	print F "\\label{subsec:$y}\n";
	print F "\\begin{enumerate}\n";
	for $x (sort keys %{$databyyear{$y}}){
		print "$x\n";
		print F $databyyear{$y}{$x};
	}
	print F "\\end{enumerate}\n";
}
print F "\\section{List by school year (from April to March}\n";
foreach $y (sort keys %databysyear)
{
	$age = $y - 1967;
	print F "\\subsection{$y school year ($age years old) $numbysyear{$y} entries}\n";
	print F "\\label{subsec:$y}\n";
	print F "\\begin{enumerate}\n";
	for $x (sort keys %{$databysyear{$y}}){
		#print "$x\n";
		print F $databysyear{$y}{$x};
	}
	print F "\\end{enumerate}\n";
}

#---------------------------------------------

print F "\\begin{thebibliography}{999}\n";
print F "$biblist\n";
print F "\\end{thebibliography}\n";
print F "\\end{document}\n";
close F;

open F, ">$body.bib" or die "cannot open bib file\n";
print F "$biblist\n";
close F;

system "nkf $output >> xxx";
system "mv xxx $output";
