#!/usr/bin/perl

$num=1;
$css_path="http://www.akita-nct.ac.jp/yamamoto/stylesheet.css";
$body='</BODY>';
$google_analytics='
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2363198-1";
urchinTracker();
</script>
</BODY>
';

#------ check a filen name ---------------------
$ARGV[0]=~/.tex$/ or die "error input file. input file:\"$ARGV[0]\"";

printf "================================================================\n";
printf " processing of platex has started.\n";
printf "================================================================\n";

exec_command("rm -rf html");
exec_command("platex $ARGV[0]");

printf "================================================================\n";
printf " processing of latex2html has started.\n";
printf "================================================================\n";

exec_command("jlatex2html -no_transparent -style $css_path $ARGV[0]");

printf "================================================================\n";
printf " Processing of making a pdf has started.\n";
printf "================================================================\n";

$dvi_file=$ARGV[0];
$dvi_file=~s/\.tex$/\.dvi/;
exec_command("dvipdfmx -d5 $dvi_file");

printf "================================================================\n";
printf " Processing of making html files have started.\n";
printf "================================================================\n";


opendir(DIR,"html") or die "$dir can not opened. $dir: $!";
chdir('html');

while ($file=readdir(DIR)){
    if ($file=~ /.html$/ ){
	print "$num\t$file\n";
	open(IN,$file) or die "IN file can not be opened.";
	open(OUT,">changed.html") or die "OUT file can not be opened.";
	while(<IN>){
	    s/$body/$google_analytics/;
	    print OUT;
	}
	close(IN);
	close(OUT);
	unlink($file);
	rename("changed.html",$file);
	$num ++;
    }
}


#=========================================================================
# subroutine exec unix command
#=========================================================================
sub exec_command
{
    print "$_[0]\n";
    system $_[0];
}
