#!/usr/bin/perl

# Copyright (C) 2001-2008 NEC Corporation
# All Rights Reserved.

require "/opt/nec/wbmc/language.pl";
if($mes_language eq "Japanese"){
        require "/opt/nec/wbmc/bin/lang_j.pl";                  # ܸå
} else {
        require "/opt/nec/wbmc/bin/lang_e.pl";                  # Ѹå
}

require "/opt/nec/wbmc/lib/wbmc.pl";
require "/opt/nec/wbmc/adm/proxy/common_roma.pl";

#2008/08 higashi
#ॹѴδؿɤ߹
require "/opt/nec/wbmc/bin/time_conv.pl";

#accesslog.confؤΥѥ
$access_path ="/etc/opt/nec/wbmc/accesslog.conf";
$access_path_lock ="accesslog.conf";

#2008/08 higashi
#ftp_commandؤΥѥ
$ftp_command_path = "/etc/opt/nec/wbmc/ftp_command";
$ftp_command_path_lock = "ftp_command";

#2008/08 higashi
#ftp_commandؤΥѥ
$ftp_command_time_path = "/etc/opt/nec/wbmc/ftp_command_time";
$ftp_command_time_path_lock = "ftp_command_time";

#2008/09 higashi
#ޡƻԤžλ֤žޥ
#򵭽Ҥե(time)ؤΥѥ
$time_path = "/var/log/accesslog_transfer/tmp_time";
$time_path_lock = "tmp_time";

#2008/09 higashi
#send_keep_statؤΥѥ
$send_keep_stat_path = "/var/log/accesslog_transfer/send_keep_stat";
$send_keep_stat_path_lock = "send_keep_stat";

#2008/09 higashi
#žޥ(FTP,smbclient)η̤ϤեؤΥѥ
$send_path = "/var/log/accesslog_transfer/send.log";
$send_path_lock = "send.log";

#2008/09 higashi
#žԤ񤭹եؤΥѥ
$send_fail_log_path="/var/log/accesslog_transfer/send_fail.log";

#2008/09 higashi
#ž͡हեؤΥѥ
$send_true_log_path="/var/log/accesslog_transfer/send_true.log";

#2008/10 higashi
#žΥϤեؤΥѥ
$accesslog_transfer_log_path = "/var/log/accesslog_transfer/accesslog_transfer.log";

#2008/09 higashi
#send_fail_log,accesslog_transfer.logơȤ륵
$send_fail_log_rotate_size=5242880;

#2008/12 higashi
#accesslog_transfer.logơȤ륵
$accesslog_transfer_log_rotate_size=5242880;

#2008/09 higashi
#PUT_ACCESSLOGưwbmc_minute
#ʣưʤPUT_ACCESSLOG
#ʣưν

#2008/08 higashi
#Υå
if($ARGV[0] eq ""){
	if(!&writeLog_rotate("[Error] Not argument",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
		system("/bin/echo $accesslog_transfer_log_path cannot write");
	}
	exit(1);
}
$log_file_name=$ARGV[0];

#2008/11 higashi
#accesslog_transferǥ쥯ȥκ
if(!(-d "/var/log/accesslog_transfer")){
	system("/bin/mkdir /var/log/accesslog_transfer");
}

($err,$WbMCVersion) = &get_WbMCVersion();
if($err){
	exit(1);
}

#400h2꡼Ƚե饰
$CS400h2_flag = "";
if($WbMCVersion >= 8){
        $CS400h2_flag = "yes";
}

#2008/08 higashi
#ȥ饤
#ȥ饤塼0פꤵƤȥ饤ϹԤʤ
do{

	#2008/11 higashi
	#꤬ĤäƤʤɤ߹

	#žեξ
	$send_stat = "SEND";

	#žΥե륵Фǰֿե
	$send_file_name = "";

	#žΥե륵ФǰֿեΥॹ
	$send_time = "";

	#žԤä
	$log_save_interval_old              = 1;
	$log_trance_type_old                = 0;
	$log_trance_machine_old             = "";
	$log_workgroup_old                  = "";
	$log_common_old                     = "";
	$log_user_old                       = "";

	@file_send_keep_read = ();
	if(-e $send_keep_stat_path){
		if (!&readFile("$send_keep_stat_path", $send_keep_stat_path_lock, *file_send_keep_read) ) {
			system("/bin/echo $send_keep_stat_path cannot read");
			if(!&writeLog_rotate("[Warning] $send_keep_stat_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
			exit(1);
		}else{
			for($i= 0;$i < 	@file_send_keep_read;$i++){
				#žեξ
				if($file_send_keep_read[$i] =~ /^\s*SEND_STAT\s*=\s*(.*)\s*$/){
					$send_stat = $1;
				}

				#žǰֿե̾
				if($file_send_keep_read[$i] =~ /^\s*SEND_FILE_NAME\s*=\s*(.*)\s*$/){
					$send_file_name = $1;
				}

				#žǰֿեΥॹ
				if($file_send_keep_read[$i] =~ /^\s*SEND_TIME\s*=\s*(.*)\s*$/){
					$send_time = $1;
				}
				# 
				if ($file_send_keep_read[$i] =~ /^\s*LOG_SAVE_INTERVAL\s*=\s*(.*)\s*$/) {
					$log_save_interval_old = $1;
				}
				# ž
				if ($file_send_keep_read[$i] =~ /^\s*LOG_TRANCE_TYPE\s*=\s*(.*)\s*$/) {
					$log_trance_type_old = $1;
				} 
				# žޥ̾ 
				if ($file_send_keep_read[$i] =~ /^\s*LOG_TRANCE_MACHINE\s*=\s*(.*)\s*$/) {
					$log_trance_machine_old = $1;
				}
				# 롼̾ 
				if ($file_send_keep_read[$i] =~ /^\s*LOG_WORKGROUP\s*=\s*(.*)\s*$/) {
					if ("$1" ne "") {
						$log_workgroup_old = "-W $1";
					}
				}
				# ͭ̾
				if ($file_send_keep_read[$i] =~ /^\s*LOG_COMMON\s*=\s*(.*)\s*$/) {
					$log_common_old = $1;
				}
				# 桼̾
				if ($file_send_keep_read[$i] =~ /^\s*LOG_USER\s*=\s*(.*)\s*$/) {
					$log_user_old = $1;
				}
			}
		}
	}

	# accesslog.ocnfեɹ
	if (!&readFile("$access_path", $access_path_lock, *file) ) {
		system("/bin/echo $access_path cannot read");
		if(!&writeLog_rotate("[Error] $access_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}
		exit(1);
	}

	# եƤɤ߽Ф
	$log_save_interval              = 0;
	$log_get_generation_stat        = "OFF";
	$log_trance_type                = 0;
	$log_trance_machine             = "";
	$log_workgroup                  = "";
	$log_common                     = "";
	$log_user                       = "";
	$log_passwd                     = "";

	#2008/12 higashi
	#FTPž⡼ɲ
	$log_ftp_trance_mode	= 0;

	#2008/8 higashi
	#᡼εǽɲ
	$log_mail_stat                  = 0;
	$log_mail_server                = "";
	$log_from_mail_addr             = "";
	$log_to_mail_addr               = "";

	#2008/8 higashi
	#ȥ饤ǽɲ
	$log_max_que                    = 0;
	$log_timeout                    = 15 * 60;
	$log_retry_interval             = 10 * 60;

	#ݳǧб 2012/07 higashi
	$log_size_chk                   = 1;
	#CS400h2ʹߤΥǥեȤ
	if($CS400h2_flag eq "yes"){
		$log_size_chk               = 0;
	}

	for ($i = 0; $i < @file; $i++) {

		# ȤʤɤФ
		if ($file[$i] =~ /\s*#/) {
			next;
		}
		# 
		if ($file[$i] =~ /^\s*LOG_SAVE_INTERVAL\s*=\s*(.*)\s*$/) {
			$log_save_interval = $1;
		}
		#2008/11 higashi
		# Фॹפ
		if ($file[$i] =~ /^\s*LOG_GET_GENERATION_STAT\s*=\s*(.*)\s*$/) {
			$log_get_generation_stat = $1;
		}
		# ž
		if ($file[$i] =~ /^\s*LOG_TRANCE_TYPE\s*=\s*(.*)\s*$/) {
		$log_trance_type = $1;
		}
		#2008/12 higashi
		# FTPž⡼
		if ($file[$i] =~ /^\s*LOG_FTP_TRANCE_MODE\s*=\s*(.*)\s*$/) {
			$log_ftp_trance_mode = $1;
		}
		# žޥ̾ 
		if ($file[$i] =~ /^\s*LOG_TRANCE_MACHINE\s*=\s*(.*)\s*$/) {
			$log_trance_machine = $1;
		}
		# 롼̾ 
		if ($file[$i] =~ /^\s*LOG_WORKGROUP\s*=\s*(.*)\s*$/) {
			if ("$1" ne "") {
				$log_workgroup = "-W $1";
			}
		}
		# ͭ̾
		if ($file[$i] =~ /^\s*LOG_COMMON\s*=\s*(.*)\s*$/) {
			$log_common = $1;
		}
		# 桼̾
		if ($file[$i] =~ /^\s*LOG_USER\s*=\s*(.*)\s*$/) {
			$log_user = $1;
		}
		# ѥ
		if ($file[$i] =~ /^\s*LOG_PASSWD\s*=\s*(.*)\s*$/) {
			$log_passwd = $1;
		}
		#  
		if ($file[$i] =~ /^\s*LOG_GENERATION\s*=\s*(.*)\s*$/) {
			$log_generation = $1;
		}
		# ե̾
		if ($file[$i] =~ /^\s*LOG_METHOD\s*=\s*(.*)\s*$/) {
			$log_method = $1;
		}
		#2008/8 higashi
		# ȥ饤塼
		if ($file[$i] =~ /^\s*LOG_MAX_QUE\s*=\s*(.*)\s*$/) {
			$log_max_que = $1;
		}
		#2008/8 higashi
		# ॢȻ
		if ($file[$i] =~ /^\s*LOG_TIMEOUT\s*=\s*(.*)\s*$/) {
			$log_timeout = $1 * 60;
		}
		#2008/8 higashi
		# ȥ饤ֳ
		if ($file[$i] =~ /^\s*LOG_RETRY_INTERVAL\s*=\s*(.*)\s*$/) {
			$log_retry_interval = $1 * 60;
		}
		#2008/8 higashi
		# ԤؤΥž̤
		if ($file[$i] =~ /^\s*LOG_MAIL_STAT\s*=\s*(.*)\s*$/) {
			$log_mail_stat = $1;
		}
		#2008/8 higashi
		# ᡼륵
		if ($file[$i] =~ /^\s*LOG_MAIL_SERVER\s*=\s*(.*)\s*$/) {
			$log_mail_server = $1;
		}
		#2008/8 higashi
		# ԤΥ᡼륢ɥ쥹
		if ($file[$i] =~ /^\s*LOG_FROM_MAIL_ADDR\s*=\s*(.*)\s*$/) {
			$log_from_mail_addr = $1;
		}
		#2008/8 higashi
		# ԤΥ᡼륢ɥ쥹
		if ($file[$i] =~ /^\s*LOG_TO_MAIL_ADDR\s*=\s*(.*)\s*$/) {
			$log_to_mail_addr = $1;
		}
		#ݳǧб 2012/07 higashi
		if ($file[$i] =~ /^\s*LOG_SIZE_CHK\s*=\s*(.*)\s*$/) {
			$log_size_chk = $1;
		}
	}

	#2012/08 higashi
	#åBINARYˤ
	if($log_size_chk eq "0"){
		$log_ftp_trance_mode = 1;
	}

	#2008/11 higashi
	#ȥ饤ʤΤǧ
	if(-e $send_keep_stat_path){
		#,ž褬ѤäƤʤХȥ饤Ȥ
		if(($send_stat ne "SEND") && ($log_save_interval ne $log_save_interval_old || $log_trance_type ne $log_trance_type_old || $log_trance_machine ne $log_trance_machine_old || $log_workgroup ne $log_workgroup_old || $log_common ne $log_common_old || $log_user ne $log_user_old)){
			$send_stat = "RETRY";
		}else{
			$send_stat = "SEND";
		}
	}

	#2008/09 higashi
	#žѤ̾˥͡
	if($log_generation eq ""){
		$log_generation = "1";
	}

	#2008/09 higashi
	#ե̾Ǽ
	$log_file_name_new = "";

	#2008/09 higashi
	#եꥹȼ̥ե饰
	$log_access_flg    = 0;

	#2008/10 higashi
	#ž˼Ԥ̤Ǽ
	@send_fail_log_work=();

	if($log_method eq "date"){
		#2008/09 higashi
		#ॹפղäե̾
		use POSIX 'strftime';
		$log_file_name_new=sprintf("/var/log/roma/access%010d.log",strftime("%y%m%d%H%M",localtime((stat("$log_file_name"))[9])));
		$log_access_flg=1;
	}else{
		#2008/09 higashi
		#žե륵ФꥹȤη̥ॹפUTCѴ
		#ꥹȤ
		if($log_get_generation_stat eq "ON"){
			if($log_trance_type eq "0"){
				#žFTPξ

				#lsޥɤե륵Ф˼¹Ԥ륳ޥɺ
				@ftp_command_time_file=();
				push(@ftp_command_time_file,"user $log_user $log_passwd\n");
				if($log_common ne ""){
					push(@ftp_command_time_file,"cd $log_common\n");
				}
				push(@ftp_command_time_file,"ls access*.log\n");
				push(@ftp_command_time_file,"quit\n");
				if(&writeFile($ftp_command_time_path, $ftp_command_time_path_lock, @ftp_command_time_file)){
					system("ftp -i -n -d $log_trance_machine < $ftp_command_time_path >$send_path 2>&1");
					if(&readFile($send_path,$send_path_lock,*log_send_file)){
						$ls_flg=0;
						@send_fail_log_work=@log_send_file;
						for($i=0;$i < @log_send_file;$i++){
							if ($log_send_file[$i] =~ /LIST\s*access\*\.log/) {
								$ls_flg=1;
								next;
							}
							if($log_send_file[$i] =~ /QUIT/ && $ls_flg eq "1"){
								$log_access_flg = 1;
								last;
							}
						}
					}else{
						system("/bin/echo $send_path cannot read");
						if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
							system("/bin/echo $accesslog_transfer_log_path cannot write");
						}
					}
				}else{
					system("/bin/echo $ftp_command_time_path cannot write");
					if(!&writeLog_rotate("[Error] $ftp_command_time_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						system("/bin/echo $accesslog_transfer_log_path cannot write");
					}
				}
			}else{
				#žSAMBAξ
				system("/usr/bin/smbclient //$log_trance_machine/$log_common $log_passwd -U $log_user $log_workgroup -c 'ls access*.log' 2>$send_path 1>&2");
				if(&readFile($send_path,$send_path_lock,*log_send_file)){
					@send_fail_log_work=@log_send_file;
					for($i=0;$i < @log_send_file;$i++){
						if ($log_send_file[$i] =~ /\s*Domain=\[.*\]\s*OS=\[.*\]\s*Server=\[.*\]\s*/i) {
							$i++;
							if($log_send_file[$i] =~ /access\d+\.log|NT_STATUS_NO_SUCH_FILE/i){
							$log_access_flg = 1;
							last;
							}
						}
					}
				}else{
					system("/bin/echo $send_path cannot read");
					if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						system("/bin/echo $accesslog_transfer_log_path cannot write");
					}
				}
			}

			$log_conv_flg      = 0;
			if($log_access_flg eq "1"){
				#2008/11 higashi
				#եꥹȤμΤǥ˽
				if(!&writeLog_rotate("[Success] The file list of the file server was acquired",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}

				#2008/11 higashi
				#lsޥɤη̤access().logιԤΤ
				#ॹפUTCѴФ
				@accesslog_list=();
				$cnt=0;
				$j=0;
				for($i=0;$i < @log_send_file;$i++){
					if ($log_send_file[$i] =~ /\s*access(\d\d?)\.log\s*/) {
						$j++;
						#2008/11 higashi
						#ĶƤ륢ϥॹѴоݳ
						if($1 <= $log_save_interval){
							@ret_time=&time_conv($log_send_file[$i]);
							if($ret_time[0] > 0){
								$accesslog_list[$cnt][0]=$ret_time[0];
								$accesslog_list[$cnt][1]=$ret_time[1];
								$accesslog_list[$cnt][2]=$1;
								$log_conv_flg = 1;
								$cnt++;
							}elsif($ret_time[0] eq "-1"){
								$log_conv_flg = 0;
								if(!&writeLog_rotate("[Warning] It failed in the conversion of the time stamp",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
									system("/bin/echo $accesslog_transfer_log_path cannot write");
								}
								last;
							}
						}
					}
				}

				#2008/11 higashi
				#ž褬ξ祿ॹפѴƤ뤳Ȥˤ
				if($j eq "0"){
					$log_conv_flg = 1;
					$send_time = 0;
				}
				
				if(($log_conv_flg eq "1") && (@accesslog_list > 0) && ($j ne "0")){
					#2008/11 higashi
					#ֿեΥॹסե̾

					#ե륵Фǿե̾Ǽ
					$send_time_work = "";

					#ե륵ФǿեΥॹ
					$send_file_name_work = "";

					#ॹפ˥
					@accesslog_list = sort{@$b[0]<=>@$a[0]}@accesslog_list;

					#ֿॹפʣʤå
					if(@accesslog_list eq "1"){
						$send_time_work = $accesslog_list[0][0];
						$send_file_name_work = $accesslog_list[0][1];
					}elsif($accesslog_list[0][0] > $accesslog_list[1][0]){
						$send_time_work = $accesslog_list[0][0];
						$send_file_name_work = $accesslog_list[0][1];
					}else{
						#ֿॹפʣν
						$generation = 0;
						$j = $accesslog_list[0][2];

						#ॹפֿΤ˺ʤǧ
						for($i=1;$i < @accesslog_list && $accesslog_list[0][0] <= $accesslog_list[$i][0];$i++){
							if(($j < $accesslog_list[$i][2])  && ($accesslog_list[$i][2] ne $log_save_interval) && ($j ne $log_save_interval)){
								$j = $accesslog_list[$i][2];
							}elsif($accesslog_list[$i][2] eq $log_save_interval){
								$j = $accesslog_list[$i][2];
							}
						}

						#鸽߲ܤǧ
						if($i < @accesslog_list){
							if($j eq $log_save_interval){
								$j++;
								if($j > $log_save_interval){
									$j=1;
								}
								for($cnt=0;$cnt < $i;$cnt++){
									if($accesslog_list[$cnt][2] eq $j){
										$cnt = 0;
										$j++;
									}
								}
								$j--;
								if($j eq "0"){
									$j = $log_save_interval;
								}
							}
							$generation = $j;
						}

						#եΥॹפƱեѤ
						#ֿե򸡺
						if($generation eq "0"){
							$j = $log_generation - 1;
							$time_flg = 0;
							while(($j ne $log_generation) && ($time_flg eq "0")){
								if($j eq "0"){
									$j = $log_save_interval;
								}
								for($cnt=0;$cnt < @accesslog_list;$cnt++){
									if($accesslog_list[$cnt][2] eq $j){
										$generation = $j;
										$time_flg = 1;
										last;
									}
								}
								$j--;
							}
						}

						#ֿե̾ȥॹפ
						for($i=0;$i < @accesslog_list;$i++){
							if($accesslog_list[$i][2] eq $generation){
								$send_time_work = $accesslog_list[$i][0];
								$send_file_name_work = $accesslog_list[$i][1];
								last;
							}
						}
					}
					#2008/11 higashi
					#ե̾ȥॹפǤԤ
					if(($send_time_work ne "") && ($send_file_name_work ne "")){
						if(($send_time eq "")  && ($send_file_name eq "")) {
							#2008/10 higashi
							#ǽžλžե륵Фΰֿॹסե̾
							$send_time = $send_time_work;
							$send_file_name = $send_file_name_work;
						}elsif($send_time ne ""){
							#2008/10 higashi
							#ȥ饤ʤžڤƤե뤬ʤǧ
							if($send_time < $send_time_work){
								#žΥեȯ
								$log_file_name_new = sprintf("/var/log/roma/%s",$send_file_name_work);
							}
						}
					}
				}
			}else{
				#2008/11 higashi
				#ꥹȤμԻΥĤ
				if(!&writeLog_rotate("[Warning] Cannot get file list of file server ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
				unshift(@send_fail_log_work,`date +%Y/%m/%d%t%H:%M:%S`);
				if(!&writeLog_accesslog($send_fail_log_path,$send_fail_log_rotate_size,@send_fail_log_work)){
					system("/bin/echo $send_fail_log_path cannot write");
					if(!&writeLog_rotate("[Error] $send_fail_log_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						system("/bin/echo $accesslog_transfer_log_path cannot write");
					}
				}
				#񤭹Ȥޤ魯
				@send_fail_log_work = ();
			}

			#2008/09 higashi
			#ĶƤʤå
			$generation=0;

			if($log_file_name_new eq ""){
				if( (@accesslog_list >= $log_save_interval) && ($log_save_interval ne "1") && ($log_conv_flg eq "1")){
					#ॹפŤ˥
					@accesslog_list = sort{@$a[0]<=>@$b[0]}@accesslog_list;

					#ָŤॹפʣʤå
					if(@accesslog_list eq "1"){
						$generation = $accesslog_list[0][2];					
					}elsif($accesslog_list[0][0] < $accesslog_list[1][0]){
						$generation = $accesslog_list[0][2];
					}else{
						#ָŤॹפʣν
						$j = $accesslog_list[0][2];
						for($i=1;$i < @accesslog_list && $accesslog_list[0][0] >= $accesslog_list[$i][0];$i++){
							if(($j > $accesslog_list[$i][2])  && ($accesslog_list[$i][2] ne $log_save_interval) && ($j ne $log_save_interval)){
								$j = $accesslog_list[$i][2];
							}elsif($accesslog_list[$i][2] eq $log_save_interval){
								$j = $accesslog_list[$i][2];
							}
						}

						if($i < @accesslog_list){
							#夫ָŤե򸡺
							if($j eq $log_save_interval){
								$j--;
								for($cnt=0;$cnt < $i;$cnt++){
									if($accesslog_list[$cnt][2] eq $j){
										$cnt = 0;
										$j--;
									}
								}
								$generation = $j + 1;
							}else{
								#Ťॹפˤʤ
								#־夬ŤեȤʤ
								$generation = $j;
							}
						}
						#եΥॹפƱξե
						if($generation eq "0"){
							$generation = $log_generation;
						}
					}
				}elsif($log_conv_flg eq "1"){
					#ե¤Ӥꤢ,žΥե륵Фξֻν
					#Ƥΰּ㤤ֹ
					if($log_save_interval ne "1"){
						for($i=1;$i <= $log_save_interval;$i++){
							$list_flg=0;
							for($j=0;$j < @accesslog_list;$j++){
								if($accesslog_list[$j][2] eq $i){
									$list_flg = 1;
									last;
								}
							}
							if($list_flg eq "0"){
								$generation=$i;
								last;
							}
						}
					}else{
						$generation = 1;
					}
				}else{
					#2008/09 higashi
					#ॹפӤǤʤեͤ
					$generation = $log_generation;
				}
				$log_file_name_new = sprintf("/var/log/roma/access%d.log",$generation);
			}else{
				#ե륵ФˤžΥե̾Ѥ
				$log_file_name_new =~ /access(\d\d?)\.log/;
				$generation = $1;
			}
			$log_generation = $generation;
		}else{
			$log_file_name_new = sprintf("/var/log/roma/access%d.log",$log_generation);
		}
	}

	#2008/09 higashi
	#žѤΥե̾˥͡
	if(($log_file_name_new ne "") && ($log_file_name ne $log_file_name_new)){
		system("/bin/mv $log_file_name $log_file_name_new");
		$log_file_name = $log_file_name_new;
	}

	#2008/08 higashi
	#žȽѥե饰
	#:1 :0
	$log_send_flg=0;

	#2008/08 higashi	
	#ž̤Ǽ
	@log_send_file=();

	#2008/08 higashi
	#žžλ֤žޥɤ˳Ǽ
	$time_out=`date +%s` + $log_timeout;
	@log_time=("FILE_NAME=$log_file_name\n","END_TIME=$time_out\n");
	if($log_trance_type eq "0"){
	  	push(@log_time,"PROCESS=ftp -i -n -d $log_trance_machine\n");
	}else{
		$log_file_name =~ /(access\d+\.log)/;
		push(@log_time,"PROCESS=/usr/bin/smbclient //$log_trance_machine/$log_common $log_passwd -U $log_user $log_workgroup -c lcd /var/log/roma;put $1");
	}

	#2008/11 higashi
	#ƥƵưžԤ褦ͤݻ
	@file_send_keep = ();
	push(@file_send_keep,"SEND_STAT=$send_stat\n");
	push(@file_send_keep,"FILE_NAME=$log_file_name\n");
	push(@file_send_keep,"SEND_FILENAME=$send_file_name\n");
	push(@file_send_keep,"SEND_TIME=$send_time\n");
	push(@file_send_keep,"LOG_SAVE_INTERVAL=$log_save_interval\n");
	push(@file_send_keep,"LOG_TRANCETYPE=$log_trance_type\n");
	push(@file_send_keep,"LOG_MACHINE=$log_trance_machine\n");
	push(@file_send_keep,"LOG_COMMON=$log_common\n");
	push(@file_send_keep,"LOG_WORKGROUP=$log_workgroup\n");
	push(@file_send_keep,"LOG_USER=$log_user\n");
	if(!&writeFile($send_keep_stat_path,$send_keep_stat_path_lock,@file_send_keep)){
		system("/bin/echo $send_keep_stat_path cannot write");
		if(!&writeLog_rotate("[Warning] $send_keep_stat_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}
	}

	#2008/09 higashi
	#ޡƻԤtimeե
	if(&writeFile($time_path,$time_path_lock,@log_time)){
		#FTPΤȤ
		if($log_trance_type eq "0"){
			#2008/08 higashi
			#FtpФ˥¹Ԥ륳ޥɤ
			@ftp_command_file=();
			push(@ftp_command_file,"user $log_user $log_passwd\n");
			push(@ftp_command_file,"lcd /var/log/roma\n");
			if($log_common ne ""){
				push(@ftp_command_file,"cd $log_common\n");
			}
			#2008/12 higashi
			#FTPž⡼ɤΥޥɤɲ
			if($log_ftp_trance_mode eq "1"){
				#Хʥ⡼ɤž
				push(@ftp_command_file,"bin\n");
			}
			$log_file_name =~/^\/var\/log\/roma\/(.*)$/;
			$log_send_file_name = $1;
			push(@ftp_command_file,"put $log_send_file_name\n");
			push(@ftp_command_file,"quit");

			#2008/09 higashi
			#Ftpޥɤե˽񤭹
			if(&writeFile($ftp_command_path,$ftp_command_path_lock,@ftp_command_file)){
				#2008/09 higashi
				#եžԤ
				system("ftp -i -n -d -v $log_trance_machine < $ftp_command_path > $send_path 2>&1");

				#2008/08 higashi
				#ž̤ΥåԤ
				if(&readFile($send_path,$send_path_lock,*log_send_file)){
					@send_fail_log_work=@log_send_file;
					$stor_flg = 0;
					for($i=0;$i < @log_send_file;$i++){
						if($log_send_file[$i] =~ /STOR\s*$log_send_file_name\s*$/){
							$stor_flg = 1;
							next;
						}
						if(($stor_flg eq "1") && ($log_send_file[$i] =~ /^\s*226\s+/)){
							$log_send_flg=1;
							last;
						}
					}
					# 2012/06 higashi
					# žǧ
					if(($log_size_chk eq "0") && ($log_send_flg eq "1")){
						sleep(10);
						$log_send_flg = 0;
						@ftp_command_file=();
						push(@ftp_command_file,"user $log_user $log_passwd\n");
						push(@ftp_command_file,"lcd /var/log/roma\n");
						if($log_common ne ""){
							push(@ftp_command_file,"cd $log_common\n");
						}
						push(@ftp_command_file,"ls $log_send_file_name\n");
						push(@ftp_command_file,"quit");
						if(&writeFile($ftp_command_path,$ftp_command_path_lock,@ftp_command_file)){
							system("ftp -i -n -d $log_trance_machine < $ftp_command_path > $send_path 2>&1");
							if(&readFile($send_path,$send_path_lock,*log_send_file)){
								@send_fail_log_work=@log_send_file;
								for($i=0;$i < @log_send_file;$i++){
									if ($log_send_file[$i] =~ /LIST\s*$log_send_file_name\s*/) {
										if($log_send_file[$i+1] =~ /\s*$log_send_file_name\s*/){
											if(($ftp_size = &ftp_filesize_conv($log_send_file[$i+1])) == -1){
												$log_send_flg = 1;
											}else{
												$local_ls_line="";
												@local_ls_line_split = ();
												open(IN, "/bin/ls -l /var/log/roma/$log_send_file_name |");
												$local_ls_line=<IN>;
												close(IN);
												@local_ls_line_split = split(/\s+/, $local_ls_line);
												$local_ls_line_split[4] =~ s/^\s*//;
												$local_ls_line_split[4] =~ s/\s*$//;
												if($ftp_size == $local_ls_line_split[4]){
													$log_send_flg = 1;
												}else{
													system("/bin/echo $send_path cannot read");
													if(!&writeLog_rotate("[Error] $log_send_file_name size is different.",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
														system("/bin/echo $accesslog_transfer_log_path cannot write");
													}
												}
											}
										}
										last;
									}
								}
							}else{
								system("/bin/echo $send_path cannot read");
								if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
									system("/bin/echo $accesslog_transfer_log_path cannot write");
								}
							}
						}else{
							system("/bin/echo $ftp_command_path cannot write");
							if(!&writeLog_rotate("[Error] $ftp_command_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
								system("/bin/echo $accesslog_transfer_log_path cannot write");
							}
						}
					}
				}else{
					#2008/11 higashi
					#send.logɤ߹ߤ˼Ԥ
					system("/bin/echo $send_path cannot read");
					if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						system("/bin/echo $accesslog_transfer_log_path cannot write");
					}
				}
			}else{
				#2008/11 higashi
				#ftp_commandν񤭹ߤ˼Ԥ
				system("/bin/echo $ftp_command_path cannot write");
				if(!&writeLog_rotate("[Error] $ftp_command_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}
		#SAMBAΤȤ
		else{
			#2008/09 higashi
			#եžԤ
			$log_file_name =~/^\/var\/log\/roma\/(.*)$/;
			$log_send_file_name = $1;
			system("/usr/bin/smbclient //$log_trance_machine/$log_common $log_passwd -U $log_user $log_workgroup -c 'lcd /var/log/roma;put $log_send_file_name' 2>$send_path 1>&2");

			#2008/08 higashi
			#ž̤ΥåԤ
			if(&readFile($send_path,$send_path_lock,*log_send_file)){
				@send_fail_log_work=@log_send_file;
				for($i=0;$i < @log_send_file;$i++){
					if($log_send_file[$i] =~ /putting\s*file\s*${log_send_file_name}\s*as\s*\\${log_send_file_name}\s*\(.*\s*.*\/s\)\s*\(average\s*.*\s*.*\/s\)/){
						$log_send_flg=1;
						last;
					}
				}
				# 2012/07 higashi
				# žǧ
				if(($log_size_chk eq "0") && ($log_send_flg eq "1")){
					sleep(10);
					$log_send_flg = 0;
					system("/usr/bin/smbclient //$log_trance_machine/$log_common $log_passwd -U $log_user $log_workgroup -c 'ls $log_send_file_name' 2>$send_path 1>&2");
					if(&readFile($send_path,$send_path_lock,*log_send_file)){
						@send_fail_log_work=@log_send_file;
						for($i=0;$i < @log_send_file;$i++){
							if ($log_send_file[$i] =~ /\s*Domain=\[.*\]\s*OS=\[.*\]\s*Server=\[.*\]\s*/i) {
								$i++;
								if($log_send_file[$i] =~ /$log_send_file_name/i){
									if(($smb_size = &smb_filesize_conv($log_send_file[$i])) == -1){
										$log_send_flg = 1;
									}else{
										$local_ls_line="";
										@local_ls_line_split = ();
										open(IN, "/bin/ls -l /var/log/roma/$log_send_file_name |");
										$local_ls_line=<IN>;
										close(IN);
										@local_ls_line_split = split(/\s+/, $local_ls_line);
										$local_ls_line_split[4] =~ s/^\s*//;
										$local_ls_line_split[4] =~ s/\s*$//;
										if($smb_size == $local_ls_line_split[4]){
											$log_send_flg = 1;
										}else{
											system("/bin/echo $send_path cannot read");
											if(!&writeLog_rotate("[Error] $log_send_file_name size is different.",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
												system("/bin/echo $accesslog_transfer_log_path cannot write");
											}
										}
									}
									last;
								}
							}
						}
					}else{
						system("/bin/echo $send_path cannot read");
						if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
							system("/bin/echo $accesslog_transfer_log_path cannot write");
						}
					}
				}
			}else{
				#2008/11 higashi
				#send.logɤ߹ߤ˼Ԥ
				system("/bin/echo $send_path cannot read");
				if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}
	}else{
		#2008/11 higashi
		#tmp_timeν񤭹ߤ˼Ԥ
		system("/bin/echo $time_path cannot write");
		if(!&writeLog_rotate("[Error] $time_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}
	}

	#ޡƻΥե
	if(-e $time_path){
		system("/bin/rm -r $time_path > /dev/null 2>&1");
	}

	if($log_send_flg eq "1"){
		#2008/08 higashi
		#žν
		
		#2008/11 higashi
		#ž
		$log_file_name =~ /(access\d+\.log)/;
		if(!&writeLog_rotate("[Success] $1 transfer success",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}

		#2008/09 higashi
		#ž̤Υե͡
		system("/bin/mv $send_path $send_true_log_path");

		#2008/09 higashi
		#žեκ
		system("/bin/rm -r $log_file_name > /dev/null 2>&1");

		#2008/11 higashi
		#ž᡼
		if($log_mail_stat eq "2"){
			#᡼Τץŵưʤ褦ץkill
			open(IN, "/bin/ps -efw |");
			while(<IN>) {
				if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
					@wk = split(/\s+/, $_);
					system("/bin/kill $wk[1]");
					sleep(1);
					if(!&writeLog_rotate("[Warning] wbmc_mail process kill. ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						system("/bin/echo $accesslog_transfer_log_path cannot write");
					}
				}
			}
			close(IN);
			system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 1 &");
			sleep(1);
		}

		#2008/10 higashi
		#ݻƤ֤
		if(-e $send_keep_stat_path){
			system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
		}

		#
		#LOG_METHOD=generationΤȤѹ
		#
    		if($log_method eq "generation"){
			#䤷ۤ飱᤹
			++$log_generation;
			if($log_generation > $log_save_interval){
				$log_generation=1;
			}

			for($i=0;$i<@file;$i++){
				if($file[$i] =~ /^LOG_GENERATION/){
					$file[$i]="LOG_GENERATION=$log_generation\n";
					last; 
				}
			}
		}

		#եξ
		if (!&writeFile($access_path, $access_path_lock, @file)) {
			system("/bin/echo $access_path cannot write");
			if(!&writeLog_rotate("[Error] $access_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
		}else{
			if(!&writeLog_rotate("[Success] $access_path write success",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
		}
	}else{
		#2008/08 higashi
		#žԸν

		#2008/08 higashi
		#žԤĤ
		if(@send_fail_log_work > 0){
			unshift(@send_fail_log_work,`date +%Y/%m/%d%t%H:%M:%S`);
			if(!&writeLog_accesslog($send_fail_log_path,$send_fail_log_rotate_size,@send_fail_log_work)){
				system("/bin/echo $send_fail_log_path cannot write");
				if(!&writeLog_rotate("[Error] $send_fail_log_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}
		if(!&writeLog_rotate("[Warning] $log_file_name transfer failed.",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}

		#Ū¸Ƥž̤Υ
		if(-e $send_path){
			system("/bin/rm -r $send_path > /dev/null 2>&1");
		}

		#2008/11 higashi
		#ž˼Ԥ᡼
		if(($log_mail_stat eq "1") || (($send_stat eq "SEND") && ($log_mail_stat eq "2"))){
			#᡼Τץŵưʤ褦ץkill
			open(IN, "/bin/ps -efw |");
			while(<IN>) {
				if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
					@wk = split(/\s+/, $_);
					system("/bin/kill $wk[1]");
                                        if(!&writeLog_rotate("[Warning] wbmc_mail process kill. ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				            system("/bin/echo $accesslog_transfer_log_path cannot write");
			                }
					sleep(1);
				}
			}
			close(IN);
			system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 2 &");
                        sleep(1);
		}

		#2008/11 higashi
		#ȥ饤˰ܤäȤե˻Ĥ
		if(($log_max_que ne "0") && ($send_stat eq "SEND")){
			$send_stat = "RETRY";
			for($i=0;$i < @file_send_keep;$i++){
				if($file_send_keep[$i] =~ /^\s*SEND_STAT\s*=.*\s*/){
					$file_send_keep[$i] = "SEND_STAT=$send_stat\n";
				}
			}
			if(!&writeFile($send_keep_stat_path,$send_keep_stat_path_lock,@file_send_keep)){
				system("/bin/echo $send_keep_stat_path cannot write");
			}
		}elsif($log_max_que eq "0" && -e $send_keep_stat_path){
			#ȥ饤ʤΤݻƤ
			system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
		}

		#2008/09 higashi
		#ȥ饤塼0ξȥ饤Ԥʤ
		if($log_max_que eq "0"){
			if(-e $log_file_name){
				system("/bin/rm -r $log_file_name > /dev/null 2>&1");
			}
			if(($log_mail_stat eq "1") || ($log_mail_stat eq "2")){
				#᡼Τץŵưʤ褦ץkill
				open(IN, "/bin/ps -efw |");
				while(<IN>) {
					if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
						@wk = split(/\s+/, $_);
						system("/bin/kill $wk[1]");
						sleep(1);
					}
				}
				close(IN);
				system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 3 &");
			}
			if(!&writeLog_rotate("[Fatal] $log_file_name deleted,because retry-queu 0",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
			$log_send_flg=1;
		}else{
			#2008/08 higashi
			#ȥ饤ֳʬƤžƳ
			while(($log_retry_interval > 0) && (-e $log_file_name)){
				if($log_retry_interval > 300){
					sleep 300;
					$log_retry_interval -= 300;
				}else{
					sleep $log_retry_interval;
					$log_retry_interval = 0;
				}
			}
			if(!(-e $log_file_name)){
				#2008/09 higashi
				#žե뤬줿Τžλ
				if(-e $send_keep_stat_path){
					system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
				}
				if(($log_mail_stat eq "1") || ($log_mail_stat eq "2")){
					#᡼Τץŵưʤ褦ץkill
					open(IN, "/bin/ps -efw |");
					while(<IN>) {
						if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
							@wk = split(/\s+/, $_);
							system("/bin/kill $wk[1]");
							if(!&writeLog_rotate("[Warning] wbmc_mail process kill. ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
								system("/bin/echo $accesslog_transfer_log_path cannot write");
							}
							sleep(1);
						}
					}
					close(IN);
					system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 3 &");
                                        sleep(1);
					#ݻƤ֤
					if(-e $send_keep_stat_path){
						system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
					}
				}
				$log_send_flg=1;
			}
		}
	}
}while($log_send_flg eq "0");

#2009/1 higashi
#᡼Τǥ쥤Ԥ
if($log_mail_stat ne "0"){
	for($i=0;$i < 300;$i++){
		$flg_mail = 0;
		open(IN, "/bin/ps -efw |");
		while(<IN>) {
			if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
				$flg_mail = 1;
				sleep 1;
				last;
			}
		}
		if($flg_mail eq "0"){
			last;
		}
	}
}
exit(0);
