#!/usr/bin/perl

# ====================================================================
# The Vovida Software License, Version 1.0 
# 
# Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. The names "VOCAL", "Vovida Open Communication Application Library",
#    and "Vovida Open Communication Application Library (VOCAL)" must
#    not be used to endorse or promote products derived from this
#    software without prior written permission. For written
#    permission, please contact vocal\@vovida.org.
# 
# 4. Products derived from this software may not be called "VOCAL", nor
#    may "VOCAL" appear in their name, without prior written
#    permission of Vovida Networks, Inc.
# 
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
# NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
# NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
# IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
# 
# ====================================================================
# 
# This software consists of voluntary contributions made by Vovida
# Networks, Inc. and many individuals on behalf of Vovida Networks,
# Inc.  For more information on Vovida Networks, Inc., please see
# <http://www.vovida.org/>.

require "getopts.pl";

&Getopts("c");

# usage:

if($#ARGV != 4) {
    print "usage: $0 [-c] <root-dir> <current-dir> <bin-prefix> <test-program-file> <log-file>\n";
    exit -1;
}

chomp($root_dir = `cd $ARGV[0]; pwd`);
chomp($current_dir = `cd $ARGV[1]; pwd`);
$bin_prefix = $ARGV[2];
$test_file = $ARGV[3];
$log_file = $ARGV[4];

#get test name by taking out everything not in the root directory name

$test_name = $current_dir;
$test_name =~ s/$root_dir//;
$test_name =~ s/^\///;

print "testing $test_name\n";


# if the path to $0 is not implicitly in the exec path, add it to the path

$exec_path = $0;

if($exec_path =~ s/\/[^\/]*$//) {
	$ENV{PATH} = "$exec_path:$ENV{PATH}:.";
}

#get rid of the test.log file

open(LOCALLOG, ">>$bin_prefix/testresults.log" ) || die "can't open for writing";
open(TESTLOG, ">>$bin_prefix/testoutput.log" ) || die "can't open for writing";
$start_time = &get_time();
print TESTLOG ">>>> START $test_name ( $start_time ) ( $bin_prefix )\n";

open(F, $test_file) || die "can't open $test_file for reading";
open(LOGFILE, ">>$log_file") || die "can't open $log_file for writing";
print LOGFILE ">>>> START $test_name ( $start_time ) ( $bin_prefix )\n";
print LOCALLOG ">>>> START $test_name ( $start_time ) ( $bin_prefix )\n";
while(<F>) {
    # run the command line and output it
    chomp;

    if(!/^\#/) {
	($exit_code, $signal, $core, $cmd) = split(/\s+/, $_, 4);
        if(!$opt_c || ($cmd =~ /^test/)) {
		&run_command($exit_code, $signal, $core, $cmd);
	}
    }
}

print LOGFILE "<<<< END $test_name ( $start_time ) ( $bin_prefix )\n";
print LOCALLOG "<<<< END $test_name ( $start_time ) ( $bin_prefix )\n";
print TESTLOG "<<<< END $test_name ( $start_time ) ( $bin_prefix )\n";



sub run_command {
    my($has_exit_code, $has_signal, $has_core, $cmd) = @_;
    my($failed) = 0;
    my($original_cmd);

    $original_cmd = $cmd;

#    $cmd =~ s/^(\w)/\.\/\1/;  # if this is in pwd, append the ./ to make sure it works

    # kill trailing whitespace

    $original_cmd =~ s/\s+$//g;

    print "running $cmd\n";
    open(TEST, "cd $bin_prefix ; $cmd 2>&1 |") || die "can't run program $cmd";
    print TESTLOG "---- running $cmd\n";
    while(<TEST>) {
	# get the output lines, which need to be in legal format
	if(/^PASSED/ || /^FAILED/) {
	    print LOGFILE $_;
	    print LOCALLOG $_;
	    print TESTLOG $_;
	} else {
	    print TESTLOG $_;
	}
    }
    close(TEST);

    $exit_code = ($? >> 8);
    $signal = ($? & 127);
    $core_dumped = ($? & 128);
    
    if( ( $exit_code != $has_exit_code ) ) {
	$failed = 1;
    }
    
    if( ( $signal && !$has_signal) ||
	( !$signal && $has_signal)) {
	$failed = 1;
    }
    
    if( ($core_dumped && !$has_core) ||
	(!$core_dumped && $has_core)) {
	$failed = 1;
    }
    
    
    if($failed) {
	print LOGFILE "FAILED: ${original_cmd}:: ";
	print LOCALLOG "FAILED: ${original_cmd}:: ";
	print TESTLOG "FAILED: ${original_cmd}:: ";
    } else {
	print LOGFILE "PASSED: ${original_cmd}:: ";
	print LOCALLOG "PASSED: ${original_cmd}:: ";
	print TESTLOG "PASSED: ${original_cmd}:: ";
    }
    
    print LOGFILE "(exit code: $exit_code, signal: $signal, core: $core_dumped)\n";
    print LOCALLOG "(exit code: $exit_code, signal: $signal, core: $core_dumped)\n";
    print TESTLOG "(exit code: $exit_code, signal: $signal, core: $core_dumped)\n";
}


sub get_time {
    my(@time_array);
    my($time);
    @time_array = localtime();

    $time = sprintf("%4.4d-%2.2d-%2.2d-%2.2d-%2.2d-%2.2d", 
		    $time_array[5] + 1900, 
		    $time_array[4] + 1,
		    $time_array[3],
		    $time_array[2],
		    $time_array[1],
		    $time_array[0]);

    return $time;
}
