#!/usr/bin/perl

# Copyright (C) 2000 NEC Corporation
# All Rights Reserved.

# RCS: $Id: wbmc_webalizer_merge,v 1.1 2006/12/22 05:33:58 shodai Exp $

##010716 v4.10 (ޡ)    haramoto
##010726 v4.10 եɤ߹߽ѹ   murakami

require "/opt/nec/wbmc/lib/wbmc.pl";

#
# ޡޥ
#

$domaindir = "";
$option = $ARGV[0];
$domaindir = $ARGV[1];

#ե(ơȿͤˤĤ".n")
$logdir = "/var/log/httpd/";
$logfile = "access_log." . $domaindir;
$newfile = $logdir . $logfile . ".mrg";
$filenames = "";
$logfile_lock = "logfile";
$flag = 0;
$found = 0;

##010725 .mrg ե
if (-f $newfile) {
    system("/bin/rm -f $newfile");
}

if ($option eq "-n") {
	#
	# ơȥեμ
	#
	chdir($logdir);
##010716 ǿե뤫ɽ
##010726 Ťե뤫ɤ߹
	if (!&readFile("/bin/ls -lt $logfile*|", $logfile_lock, *rotatefile)) {
#	    &showErrorPage("$logfileɤ߹ߤ˼Ԥޤ");
	    exit(1);
	}

	#
	#  եꥹȺ
	#
#	for ($i = 0; $i < @rotatefile; $i++) {
	for ($i = @rotatefile; $i > 0; $i--) {
	    @tmp = split(/\s+/, $rotatefile[$i]);
	    if (($tmp[8] eq $logfile) || ($tmp[8] =~ /^\Q$logfile\E\.\d+$/i)) {
	        $found++;
	        $file = $tmp[8];
	        if ($flag == 0) {
	            $filenames .= "$logdir$file";
	        } else {
				$filenames .= " $logdir$file";
	        }
	        if ($flag eq 0) {
	            $flag = 1;
	        }
	    }
	}

	if (!$found) {
#	    &showResultPage("ե뤬¸ߤޤ","");
	    exit(1);
	}

	#
	# ޥȯ
	#
	if(system("/bin/cat $filenames > $newfile") != 0){
#	    &showResultPage("ѥե($filenames)˼Ԥޤ","");
	    exit(1);
	}

}
