#!/usr/bin/perl

use strict;
use warnings;
use File::Path;
use File::Spec;
use Fcntl;
use File::Find;
use File::Basename;


#=============================================================================================
# メイン
#=============================================================================================

my $top_dir = "lecture_new";
my ($base, $dir, $ext);
my $file;
my $num=1;

our $style_sheet_apath = q(/home/yamamoto/www_server_yamamoto/www/stylesheet.css);
our $end_process_apath = q(/home/yamamoto/www_server_yamamoto/www/end_process.php);
our $home_page_apath   = q(/home/yamamoto/www_server_yamamoto/www/index.html);
our $self_intro_apath  = q(/home/yamamoto/www_server_yamamoto/www/home/yamamoto_masashi/index.php);

our $style_sheet_rpath;
our $end_process_rpath;
our $home_page_rpath;
our $self_intro_rpath;

our ($HTML_old,       $HTML_new);
our ($HEAD_old,       $HEAD_new);
our ($STYLESHEET_old, $STYLESHEET_new);
our ($mail_old,       $mail_new);
our ($home_page_old,  $home_page_new);
our ($author_old,     $author_new);
our ($BODY_old,       $BODY_new);


&mk_old_line;
find(\&file_process, $top_dir);


#=============================================================================================
# 修正すべき行
#=============================================================================================
sub mk_old_line{
    $HTML_old       = '<HTML>';
    $HEAD_old       = '<HEAD>';
    $STYLESHEET_old = 'http:\/\/www\.akita-nct\.ac\.jp\/~?yamamoto\/stylesheet\.css';
    $mail_old       = 'yamamoto@akita-nct\.ac\.jp';
    $home_page_old  = 'http:\/\/www\.akita-nct\.ac\.jp\/~?yamamoto\/';
    $author_old     = 'http:\/\/akita-nct\.jp\/~?yamamoto\/home\/member\/yamamoto_masashi\/index.php';
    $BODY_old       = '<\/BODY>';
}

#=============================================================================================
# 修正後
#=============================================================================================
sub mk_new_line{

    $HTML_new = '<HTML lang="ja">';
    $HEAD_new = '
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
';
    $STYLESHEET_new = $style_sheet_rpath;
    $mail_new = 'ymm10m34@khaki.plala.or.jp';
    $home_page_new = $home_page_rpath;
    $author_new = $self_intro_rpath;
    $BODY_new       = '
<?PHP
 $show_counter    = FALSE;
 $show_update     = FALSE;
 $isuse_side_main = FALSE;
 include "'.$end_process_rpath.'";
?>

</BODY> 
';

}

#=============================================================================================
# ファイル毎の処理
#=============================================================================================
sub file_process{
    my $in_google_analystics = 0;
    my $in_javascript = 0;
    my $temp_file = "changed131130.html";
    my $cor_file;
    my $command;

    $file = $File::Find::name;                           # ファイル名の取得
    ($base, $dir, $ext) = fileparse($file, qr/\..+$/);   #　ファイル名, ディレクトリー, 拡張子

    $cor_file = $base.$ext;

    if($ext eq ".html" || $ext eq ".HTML"){
	print $num,"\t", $file, "\n";
	$num++;

	&get_rel_path($dir);
	&mk_new_line;

	open(IN, $cor_file) or die "IN file can not be opened.";
	open(OUT,">$temp_file") or die "OUT file can not be opened.";

	while(<IN>){

	    #  --- goole-analytics の内外のチェック -----------
	    if(/<script src="http:\/\/www\.google-analytics\.com\/urchin\.js"/){
		$in_google_analystics = 1;
		next;
	    }

	    if($in_google_analystics && /<\/script>/){
		$in_google_analystics = 0;
		next;
	    }


	    #  --- javascript の内外のチェック -----------
	    if(/<script type="text\/javascript">/){
		$in_javascript = 1;
		next;
	    }

	    if($in_javascript && /<\/script>/){
		$in_javascript = 0;
		next;
	    }


	    if($in_google_analystics || $in_javascript){
		next;
	    }

	    s/$HTML_old/$HTML_new/i;
	    s/$HEAD_old/$HEAD_new/i;
	    s/$STYLESHEET_old/$STYLESHEET_new/i;
	    s/$mail_old/$mail_new/i;
	    s/$home_page_old/$home_page_new/i;
	    s/$author_old/$author_new/i;
	    s/$BODY_old/$BODY_new/i;

	    print OUT;

	}

	close(IN);
	close(OUT);

	unlink($cor_file);
	rename("$temp_file",$cor_file);
	$command = sprintf("chmod 644 %s", $cor_file);
	system($command);

    }
}


#=============================================================================================
# 相対パスの取得
#=============================================================================================
sub get_rel_path{

    $style_sheet_rpath = File::Spec->abs2rel($style_sheet_apath);
    $end_process_rpath = File::Spec->abs2rel($end_process_apath);
    $home_page_rpath   = File::Spec->abs2rel($home_page_apath);
    $self_intro_rpath  = File::Spec->abs2rel($self_intro_apath);

}
