#!/usr/bin/perl

$num=1;

$stylesheet_url='<LINK REL="STYLESHEET" HREF="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>
';

printf "================================================================\n";
printf " change css path and add google analytics script.\n";
printf "================================================================\n";


opendir(DIR,$ARGV[0]) or die "$ARGV[0] can not be opened. $!";
chdir($ARGV[0]);

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/<LINK\s+REL\s*=\s*"STYLESHEET"\s+HREF\s*=\s*.*>$/$stylesheet_url/i;
	    s/<\/BODY>/$google_analytics/i;
	    print OUT;
	}
	close(IN);
	close(OUT);
	unlink($file);
	rename("changed.html",$file);
	$num ++;
    }
}

