#!/usr/bin/perl -s --

require 5.004;


sub inputTitle
{
    if ( $title eq "" ){
	print "ƥȥȥ:";
	$testtitle = <STDIN>;
    } else {
	$testtitle=$title;
    }
}

#
# Ķѿ򸡺
#
sub checkEnv
{
    my $ip;
    my $path;

    $exportenv=" export TERM=dumb; ";

    if ( !(exists $ENV{'CT_TARGET_HOST'}) ){
	print "åȥܡɤIPɥ쥹:";
	$ip = <STDIN>;
	chop $ip;
	$exportenv = $exportenv." export CT_TARGET_HOST=$ip;";
	$ENV{'CT_TARGET_HOST'}=$ip;
    }
    if ( !(exists $ENV{'CT_CMD_PATH'}) ){
	print "CTޥɷΥǥ쥯ȥ[/home/ct/bin]:";
	$path = <STDIN>;
	chop $path;
	if ( $path ne "" ){
	    $exportenv = $exportenv." export CT_CMD_PATH=$path;" ;
	    $ENV{'CT_CMD_PATH'}=$path;
	} else {
	    $ENV{'CT_CMD_PATH'}="/home/ct/bin";
	}
    }
}




sub getTestList 
{
    my ($dir, $pattern) = @_;
    my @filelist;

    open(FLIST, "cd $dir ; /bin/ls $pattern |") || die "Can't execute 'ls'";

    while(<FLIST>){
	chop;
	push @filelist, $_;
    }

    return @filelist;
}

sub getLogFileName 
{
    my $timeZone;
    my @times;

    @times = localtime(time);
    $today = sprintf("%04d%02d%02d",$times[5]+1900,$times[4]+1,$times[3]);
    $now = sprintf("%02d%02d%02d",$times[2],$times[1],$times[0]);

    return ( "ct_".$today."_".$now.".log", "ct_".$today."_".$now.".result" );
}

sub do_test
{
    my ($expfile, $retry) = @_;
    my ($ok, $testcount, $success, $fail, $skip) = (0, 0, 0, 0, 0);

    open(EXE, "$exportenv /usr/bin/expect $expfile|") || die "Can't exe script\n";

    while(<EXE>){
	print LOGFILE $_;
	if ( /CABI_FILE-END/ ){
	    $ok = 1;
	} elsif ( /CABI_TEST-START/ ){
	    $testcount++;
	} elsif ( /CABI_TEST-SUCCESS/ ){
	    print RESFILE $_;
	    $success++;
	} elsif ( /CABI_TEST-FAIL/ ){
	    print RESFILE $_;
	    $fail++;
	} elsif ( /CABI_TEST-SKIP/ ){
	    print RESFILE $_;
	    $skip++;
	} 
    }

    if ( !$retry || $ok ){
	$total_testcount += $testcount;
	$total_success += $success;
	$total_fail += $fail;
	$total_skip += $skip;
    }
    
    close(EXE);
    return $ok;
}

sub usage
{
    print <<EOF;
Usage: $0 [options...]

Option:
  -help                Υåɽ

  -title=<title>       ̥եƬ˽񤭹ʸ
                       άϤ䤤碌Ԥ

  -pattern=<pattern>   ƥȤscript̾Υ磻ɥ
                       ά:t*.exp

  -stress              ȥ쥹ƥȤ¹Ԥ
                       ά:ȥ쥹ƥȤ¹Ԥʤ

  -nodumb              ƥȼ¹ɽԤʤ
                       ά:ƥȼ¹ɽԤ

¹
  $0           ȥ쥹ƥȤ t*.exp ٤Ƥ¹
  $0 -stress   ȥ쥹ƥȤޤ t*.exp ٤Ƥ¹
  $0 -pattern='t01.01.*.exp' 
               ȥ쥹ƥȤ t01.01.*.exp ٤Ƥ¹
  $0 -pattern='t01.01.*.exp t02.01.*.exp' 
               ȥ쥹ƥȤ t01.01.*.exp  t02.01.*.exp ٤Ƥ¹
EOF
exit;
}

usage() if ( $help );

inputTitle();
checkEnv();

if ( $pattern eq "" ){
    $pattern = "t*.exp";
}
@tmplist = getTestList("./", $pattern);

# ȥ쥹ƥȤμ¹Ԥ
foreach $i ( @tmplist ){
    if ( $i =~ /^[a-zA-Z]05\./ ){
	if ( $stress ne "" ){
	    push @filelist, $i;
	}
    } else {
	push @filelist, $i;
    }
}

($logfile, $resultfile ) = getLogFileName();

# LOGFILE - script ϤǼ
# RESFILE - ̤Τ߳Ǽ
open(LOGFILE, ">$logfile") || die "Can't open log file\n";
open(RESFILE, ">$resultfile") || die "Can't open result file\n";

$commonLogHead="$testtitle\n\n".
    "TARGET IP ADDRESS:".$ENV{'CT_TARGET_HOST'}."\n".
    "COMMAND PATH:".$ENV{'CT_CMD_PATH'}."\n\n";

print LOGFILE $commonLogHead;
print RESFILE $commonLogHead;

print LOGFILE "TEST SCRIPT LIST\n";
foreach $i ( @filelist ){
    print LOGFILE " ".$i."\n";
}
print LOGFILE "END OF TEST SCRIPT LIST\n";

$| = 1;

# ¹Գϻ
@times = localtime(time);
$starttime = sprintf("%04d/%02d/%02d %02d:%02d:%02d",
		     $times[5]+1900,$times[4]+1,$times[3],$times[2],$times[1],$times[0]);



# ƥȤμ¹
$count=0;
$total=@filelist;
foreach $i ( @filelist ){
    $count++;

    $name = $i;
    $name =~ s/^([a-zA-Z][0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\.exp$/\1/;
    $name = "CABI TEST ".$name;
    $name = "$name ($count/$total)";
    print $name;
    
    $ok = 0;
    $retry = 3;
    while (!$ok && $retry > 0 ){
	print LOGFILE "RETRY\n" if ( $retry != 3 );
	$retry--;
	$ok = do_test($i, $retry);
    }

    if ( !$nodumb ) {
	if ( $ok ){
	    print " SUCCESS\n";
	} else {
	    print " FAIL\n";
	}
    } else {
	$l = length($name);
	for $j ( 1..$l ) {
	    print "\b";
	}
	for $j ( 1..$l ) {
	    print " ";
	}
	for $j ( 1..$l ) {
	    print "\b";
	}
    }
}

# λ
@times = localtime(time);
$endtime = sprintf("%04d/%02d/%02d %02d:%02d:%02d",
		     $times[5]+1900,$times[4]+1,$times[3],$times[2],$times[1],$times[0]);

print "\n\n";

print "START TIME ....... $starttime\n";
print "END TIME ......... $endtime\n";
print "\n";
print "TOTAL TEST FILE .. $total\n";
print "TOTAL TEST ....... $total_testcount\n";
print "TOTAL SUCCESS .... $total_success\n";
print "TOTAL FAIL ....... $total_fail\n";
print "TOTAL SKIP ....... $total_skip\n" if ( $total_skip != 0 );
print "\n";
print "RESULT FILE ...... $resultfile\n";
print "LOG FILE ......... $logfile\n";
print "\n";

close(LOGFILE);
