#!/bin/csh -f
if (! -e $1) then
   echo "Directory $1 does not exist \!"
   exit 0
else
   cd $1
endif

/bin/echo ""
printf '%-40s %-8s %-8s %-8s\n' "TEST NAME"  "RTL" "WORST" "BEST"
/bin/echo "---------------------------------------------------------------------------------------"

if (-f ../rtl_regression.sh) then
   set num_rtl = `grep -c irun ../rtl_regression.sh`
else
   set num_rtl = 0
endif
set num_rtl_run = 0
set rtl_pass = 0
set rtl_fail = 0
set rtl_to = 0
set rtl_vlce = 0
set rtl_unknown = 0

if (-f ../best_regression.sh) then
   set num_best = `grep -c irun ../best_regression.sh`
else
   set num_best = 0
endif
set num_best_run = 0
set best_pass = 0
set best_fail = 0
set best_to = 0
set best_vlce = 0
set best_unknown = 0

if (-f ../worst_regression.sh) then
   set num_worst = `grep -c irun ../worst_regression.sh`
else
   set num_worst = 0
endif
set num_worst_run = 0
set worst_pass = 0
set worst_fail = 0
set worst_to = 0
set worst_vlce = 0
set worst_unknown = 0

foreach log_file ( *.log )

set test_temp = $log_file:r

set test_mode = $test_temp:e

# if (( ${test_mode} == worst) | ( ${test_mode} == best)) then
#   set test_temp = $test_temp:r
# endif
 
set test = $test_temp:r

if ( ${test_mode} == rtl ) then
  set run = 1
else
  if (( ${test_mode} == worst ) & (! -e ${test}.rtl.log )) then
    set run = 1
  else
    if ((${test_mode} == best) & (! -e ${test}.rtl.log) & (! -e ${test}.worst.log)) then
      set run = 1
    else
      set run = 0
    endif
  endif
endif


set rtl_status = UNKNOWN
set best_status = UNKNOWN
set worst_status = UNKNOWN

if (-e ${test}.rtl.log & $run) then
  tail -1000 ${test}.rtl.log > temp
  set grep_result = `grep -l "Simulation Completed Successfully" temp`
  if ($grep_result == temp) then
    set rtl_status = OK
  else
    set grep_result = `grep -l "simulation timed out in Stingray testbench" temp`
    if ($grep_result == temp) then
      set rtl_status = TOut
    else
      set grep_result = `grep -l "TIME OUT" temp`
      if ($grep_result == temp) then
        set rtl_status = TOut
      else
        set grep_result = `grep -l "Simulation Completed with Errors" temp`
        if ($grep_result == temp) then
          set rtl_status = FAIL
        else
          set grep_result = `grep -l "Simulation Completed with Warnings" temp`
          if ($grep_result == temp) then
            set rtl_status = OK
          else
            set grep_result = `grep -l "irun: \*E," temp`
            if ($grep_result == temp) then
              set rtl_status = VLCE
            endif # verilog compile check
          endif # combined warn check
        endif # combined fail check
      endif # time out check
    endif # time out check
  endif # combined pass check
else
  set rtl_status = NOTRUN
endif


if (-e ${test}.best.log & $run) then
  tail -1000 ${test}.best.log > temp
  set grep_result = `grep -l "Simulation Completed Successfully" temp`
  if ($grep_result == temp) then
    set best_status = OK
  else
    set grep_result = `grep -l "simulation timed out in Stingray testbench" temp`
    if ($grep_result == temp) then
      set best_status = TOut
    else
      set grep_result = `grep -l "TIME OUT" temp`
      if ($grep_result == temp) then
        set best_status = TOut
      else
        set grep_result = `grep -l "Simulation Completed with Errors" temp`
        if ($grep_result == temp) then
          set best_status = FAIL
        else
          set grep_result = `grep -l "Simulation Completed with Warnings" temp`
          if ($grep_result == temp) then
            set best_status = OK
          else
            set grep_result = `grep -l "irun: \*E," temp`
            if ($grep_result == temp) then
              set best_status = VLCE
            endif # verilog compile check
          endif # combined warn check
        endif # combined fail check
      endif # time out check
    endif # time out check
  endif # combined pass check
else
  set best_status = NOTRUN
endif


if (-e ${test}.worst.log & $run) then
  tail -1000 ${test}.worst.log > temp
  set grep_result = `grep -l "Simulation Completed Successfully" temp`
  if ($grep_result == temp) then
    set worst_status = OK
  else
    set grep_result = `grep -l "simulation timed out in Stingray testbench" temp`
    if ($grep_result == temp) then
      set worst_status = TOut
    else
      set grep_result = `grep -l "TIME OUT" temp`
      if ($grep_result == temp) then
        set worst_status = TOut
      else
        set grep_result = `grep -l "Simulation Completed with Errors" temp`
        if ($grep_result == temp) then
          set worst_status = FAIL
        else
          set grep_result = `grep -l "Simulation Completed with Warnings" temp`
          if ($grep_result == temp) then
            set worst_status = OK
          else
            set grep_result = `grep -l "irun: \*E," temp`
            if ($grep_result == temp) then
              set worst_status = VLCE
            endif # verilog compile check
          endif # combined warn check
        endif # combined fail check
      endif # time out check
    endif # time out check
  endif # combined pass check
else
  set worst_status = NOTRUN
endif


if ($rtl_status != NOTRUN) then
    @ num_rtl_run = $num_rtl_run + 1
endif

switch ($rtl_status)
    case OK: 
      @ rtl_pass = $rtl_pass + 1
      breaksw
    case FAIL:
      @ rtl_fail = $rtl_fail + 1
      breaksw
    case TOut: 
      @ rtl_to = $rtl_to + 1
      breaksw
    case VLCE: 
      @ rtl_vlce = $rtl_vlce + 1
      breaksw
    case UNKNOWN:
      @ rtl_unknown = $rtl_unknown + 1
      breaksw
    default:
      breaksw
endsw


if ($best_status != NOTRUN) then
    @ num_best_run = $num_best_run + 1
endif

switch ($best_status)
    case OK: 
      @ best_pass = $best_pass + 1
      breaksw
    case FAIL:
      @ best_fail = $best_fail + 1
      breaksw
    case TOut: 
      @ best_to = $best_to + 1
      breaksw
    case VLCE: 
      @ best_vlce = $best_vlce + 1
      breaksw
    case UNKNOWN:
      @ best_unknown = $best_unknown + 1
      breaksw
    default:
      breaksw
endsw


if ($worst_status != NOTRUN) then
    @ num_worst_run = $num_worst_run + 1
endif

switch ($worst_status)
    case OK: 
      @ worst_pass = $worst_pass + 1
      breaksw
    case FAIL:
      @ worst_fail = $worst_fail + 1
      breaksw
    case TOut: 
      @ worst_to = $worst_to + 1
      breaksw
    case VLCE: 
      @ worst_vlce = $worst_vlce + 1
      breaksw
    case UNKNOWN:
      @ worst_unknown = $worst_unknown + 1
      breaksw
    default:
      breaksw
endsw


if ($run) then
  printf '%-40s %-8s %-8s %-8s\n' $test $rtl_status $worst_status $best_status
endif

end

/bin/echo
/bin/echo "KEY:"
/bin/echo "  OK:      Test Passed."
/bin/echo "  FAIL:    Either combined test was fail, or FAILURE was detected."
/bin/echo "  TOut:    Timed out."
/bin/echo "  VLCE:    Verilog compile Error."
/bin/echo "  NOTRUN:  Test Results were not found."
/bin/echo "  UNKNOWN: None of the above."
/bin/echo

if ($num_rtl_run > 0) then
    @ rtl_run_percent  = (100 * $num_rtl_run) / $num_rtl
    @ rtl_pass_percent = (100 * $rtl_pass) / $num_rtl_run
    @ rtl_vlce_percent = (100 * $rtl_vlce) / $num_rtl_run
    @ rtl_fail_percent = (100 * $rtl_fail) / $num_rtl_run
    @ rtl_to_percent   = (100 * $rtl_to) / $num_rtl_run
    @ rtl_run_dgt = $rtl_run_percent / 2
    @ rtl_notrun_dgt = 50 - $rtl_run_dgt
else
    set rtl_run_percent  = 0
    set rtl_pass_percent = "-"
    set rtl_vlce_percent = "-"
    set rtl_fail_percent = "-"
    set rtl_to_percent   = "-"
    @ rtl_run_dgt = 0
    @ rtl_notrun_dgt = 50 - $rtl_run_dgt
endif

if ($num_worst_run > 0) then
    @ worst_run_percent  = (100 * $num_worst_run) / $num_worst
    @ worst_pass_percent = (100 * $worst_pass) / $num_worst_run
    @ worst_vlce_percent = (100 * $worst_vlce) / $num_worst_run
    @ worst_fail_percent = (100 * $worst_fail) / $num_worst_run
    @ worst_to_percent   = (100 * $worst_to) / $num_worst_run
    @ worst_run_dgt = $worst_run_percent / 2
    @ worst_notrun_dgt = 50 - $worst_run_dgt
else
    set worst_run_percent  = 0
    set worst_pass_percent = "-"
    set worst_vlce_percent = "-"
    set worst_fail_percent = "-"
    set worst_to_percent   = "-"
    @ worst_run_dgt = 0
    @ worst_notrun_dgt = 50 - $worst_run_dgt
endif

if ($num_best_run > 0) then
    @ best_run_percent  = (100 * $num_best_run) / $num_best
    @ best_pass_percent = (100 * $best_pass) / $num_best_run
    @ best_vlce_percent = (100 * $best_vlce) / $num_best_run
    @ best_fail_percent = (100 * $best_fail) / $num_best_run
    @ best_to_percent   = (100 * $best_to) / $num_best_run
    @ best_run_dgt = $best_run_percent / 2
    @ best_notrun_dgt = 50 - $best_run_dgt
else
    set best_run_percent  = 0
    set best_pass_percent = "-"
    set best_vlce_percent = "-"
    set best_fail_percent = "-"
    set best_to_percent   = "-"
    @ best_run_dgt = 0
    @ best_notrun_dgt = 50 - $best_run_dgt
endif

/bin/echo "======================================================================================="
/bin/echo ""

# rtl
# ---------------------------------------------------------------------------------------
printf "\033[1;7mTotal number of rtl tests:                  %4s      \033[m\n" $num_rtl
printf "Total number of rtl tests run:              %4s(%3s%%)\n" ${num_rtl_run} ${rtl_run_percent}
if (($rtl_pass_percent == 100) | ($rtl_run_percent == 0)) then
   printf "\033[1mTotal number of rtl tests passed:           %4s(%3s%%)\033[m\n" ${rtl_pass} ${rtl_pass_percent}
else
   printf "\033[31;1mTotal number of rtl tests passed:           %4s(%3s%%)\033[m\n" ${rtl_pass} ${rtl_pass_percent}
endif
printf "Total number of rtl tests fail:             %4s(%3s%%)\n" ${rtl_fail} ${rtl_fail_percent}
printf "Total number of rtl tests didn't compile:   %4s(%3s%%)\n" ${rtl_vlce} ${rtl_vlce_percent}
printf "Total number of rtl tests timed out:        %4s(%3s%%)\n" ${rtl_to} ${rtl_to_percent}
/bin/echo ""


# worst
# ---------------------------------------------------------------------------------------
printf "\033[1;7mTotal number of worst tests:                %4s      \033[m\n" $num_worst
printf "Total number of worst tests run:            %4s(%3s%%)\n" ${num_worst_run} ${worst_run_percent}
if (($worst_pass_percent == 100) | ($worst_run_percent == 0)) then
   printf "\033[1mTotal number of worst tests passed:         %4s(%3s%%)\033[m\n" ${worst_pass} ${worst_pass_percent}
else
   printf "\033[31;1mTotal number of worst tests passed:         %4s(%3s%%)\033[m\n" ${worst_pass} ${worst_pass_percent}
endif
printf "Total number of worst tests fail:           %4s(%3s%%)\n" ${worst_fail} ${worst_fail_percent}
printf "Total number of worst tests didn't compile: %4s(%3s%%)\n" ${worst_vlce} ${worst_vlce_percent}
printf "Total number of worst tests timed out:      %4s(%3s%%)\n" ${worst_to} ${worst_to_percent}
/bin/echo ""

# best
# ---------------------------------------------------------------------------------------
printf "\033[1;7mTotal number of best tests:                 %4s      \033[m\n" $num_best
printf "Total number of best tests run:             %4s(%3s%%)\n" ${num_best_run} ${best_run_percent}
if (($best_pass_percent == 100) | ($best_run_percent == 0)) then
   printf "\033[1mTotal number of best tests passed:          %4s(%3s%%)\033[m\n" ${best_pass} ${best_pass_percent}
else
   printf "\033[31;1mTotal number of best tests passed:          %4s(%3s%%)\033[m\n" ${best_pass} ${best_pass_percent}
endif
printf "Total number of best tests fail:            %4s(%3s%%)\n" ${best_fail} ${best_fail_percent}
printf "Total number of best tests didn't compile:  %4s(%3s%%)\n" ${best_vlce} ${best_vlce_percent}
printf "Total number of best tests timed out:       %4s(%3s%%)\n" ${best_to} ${best_to_percent}
/bin/echo ""

if ((($num_rtl_run == $num_rtl) | ($num_rtl_run == 0)) & (($num_worst_run == $num_worst) | ($num_worst_run == 0)) & (($num_best_run == $num_best) | ($num_best_run == 0))) then
   if (($rtl_pass == $num_rtl_run) & ($worst_pass == $num_worst_run) & ($best_pass == $num_best_run)) then
   printf "\033[32m   =================================\n"
   printf "   =                               =\n"
   printf "   =       REGRESSION PASSED       =\n"
   printf "   =                               =\n"
   printf "   =================================\033[m\n"
   /bin/echo ""
   /bin/echo ""
   /bin/echo ""
   else
   printf "\033[31;1m   *********************************\n"
   printf "   *                               *\n"
   printf "   *  \!\!\!  REGRESSION FAILED  \!\!\!  *\n"
   printf "   *                               *\n"
   printf "   *********************************\033[m\n"
   /bin/echo ""
   /bin/echo ""
   /bin/echo ""
   endif
else
   printf "\033[34m   =================================\n"
   printf "   =                               =\n"
   printf "   =      Regression Progress      =\n"
   printf "   =                               =\n"
   printf "   =================================\n"

   printf "   RTL   regression progress (%3s%%) ["  ${rtl_run_percent}
   while ($rtl_run_dgt>0)
      printf "\033[34;7m \033[m"
      @ rtl_run_dgt = $rtl_run_dgt - 1
   end
   while ($rtl_notrun_dgt>0)
      printf "\033[34m_"
      @ rtl_notrun_dgt = $rtl_notrun_dgt - 1
   end
   printf "\033[34m]\n"
   printf "   WORST regression progress (%3s%%) ["  ${worst_run_percent}
   while ($worst_run_dgt>0)
      printf "\033[34;7m \033[m"
      @ worst_run_dgt = $worst_run_dgt - 1
   end
   while ($worst_notrun_dgt>0)
      printf "\033[34m_"
      @ worst_notrun_dgt = $worst_notrun_dgt - 1
   end
   printf "\033[34m]\n"
   printf "   BEST  regression progress (%3s%%) ["  ${best_run_percent}
   while ($best_run_dgt>0)
      printf "\033[34;7m \033[m"
      @ best_run_dgt = $best_run_dgt - 1
   end
   while ($best_notrun_dgt>0)
      printf "\033[34m_"
      @ best_notrun_dgt = $best_notrun_dgt - 1
   end
   printf "\033[34m]\033[m\n"
endif

/bin/rm temp
exit

