#!/usr/bin/perl -w
# Copyright (c) 2015, University of Kaiserslautern
# 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. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 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.
# 
# Authors:
#    Matthias Jung
#    Eder F. Zulian
#    Thanh C. Tran
#

use warnings;
use strict;
use Term::ANSIColor;
use Time::localtime;
use Config;

my $pathSelector = shift || "";

if($pathSelector eq "absolute")
{
    chdir("/home/test_dramsys/dram.sys/DRAMSys/tests") || die("chdir");
}

my $timestamp_color = "green";

print color("red"),"  |||\n";
print color("red")," +---+   Microelectronic Systems\n";
print color("red"),"=|   |=  Design Research Group\n";
print color("red"),"=|   |=  ", color("blue"),"University of Kaiserslautern\n";
print color("red")," +---+\n"; 
print color("red"),"  |||    ", color("green"),"DRAMSys Automated Test System\n", color("reset");

# Get number of Cores:
my $numberOfCores;
if ($Config{osname} eq "darwin") 
{
    $numberOfCores = `sysctl -n hw.ncpu`;
}
elsif ($Config{osname} eq "linux") 
{
    $numberOfCores = `cat /proc/cpuinfo | grep processor | wc -l`;
}

# Navigate to Build Folder
chdir("../../") || die("chdir");
`mkdir build >/dev/null 2>&1`;
chdir("build/") || die("chdir");

# Build the Project:
my $starttime = time();
print "\n", color($timestamp_color), timestamp(), color("reset"),"Build Project\t";
`qmake ../DRAMSys/DRAMSys.pro >/dev/null 2>&1`;
`make -j$numberOfCores > /dev/null 2>&1`;
my $took = time() - $starttime;

# Check if Build was sucessful:
if( -e "./simulator/DRAMSys" )
{
    print color("reset"),"[ ", color("green"), "done", color("reset"), " ]\t", "(", $took, " seconds)\n";
}
else
{
    print color("reset"),"[ ", color("red"), "fail", color("reset"), " ]\t", "(", $took, " seconds)\n";
    exit -1;
}

# Find all tests:
# Navigate to Build Folder
chdir("../DRAMSys/tests") || die("chdir");
my @tests = split(/\n/,`find . -iname test.pl`);

# Run Tests:
foreach(@tests)
{
    if($_ =~ /\.\/([\w\d]+)\/test\.pl/)
    {
        my $name = $1;
        chdir("./$name") || die("chdir");
        print color($timestamp_color), timestamp(), color("reset"),"Test: ".$name."\t";
        $starttime = time();
        `perl test.pl`;
        $took = time() - $starttime;

        if( $? == 0 )
        {
            print color("reset"),"[ ", color("green"), "done", color("reset"), " ]\t", "(", $took, " seconds)\n";
        }
        else
        {
            print color("reset"),"[ ", color("red"), "fail", color("reset"), " ]\t", "(", $took, " seconds)\n";
        }
        chdir("../") || die("chdir");
    }
    else
    {
        exit -1;
    }
}

sub timestamp {
  my $t = localtime;
  return sprintf( "[%04d-%02d-%02d %02d:%02d:%02d]\t",
                  $t->year + 1900, $t->mon + 1, $t->mday,
                  $t->hour, $t->min, $t->sec );
}

