#!/usr/bin/env perl
# 
#  Copyright (C) 2023  Smithsonian Astrophysical Observatory
#
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

#
#############################################################
# Regression Test Script Template
#############################################################
# Turn off buffering
select(STDOUT); $| = 1;

my $tool = "pystklib"; # Name of tool to be tested

#----------------------------------------------------------------------
# Find Test Utilities File
#----------------------------------------------------------------------
use File::Basename;
$cwd = dirname($0); # relative to where this script is run from (cwd).
$tutils = "${cwd}/regtest_utils.pl";
if ( ! -e "$tutils" )
{
   print "ERROR: Test Utilities file not found. '${tutils}' \n";
   exit(1);
}
else
{
   require "$tutils";
}

#----------------------------------------------------------------------
# Define base directories
#----------------------------------------------------------------------
#Get test paths with tool specific extensions
($indir, $outdir, $savdir, $logdir) = &get_test_dirs( $tool );

#Create output directories as needed
&gen_test_dirs( $outdir, $logdir );
  
#----------------------------------------------------------------------
# Define Test Suite.
#----------------------------------------------------------------------
my @testlist = define_test_suite();

#----------------------------------------------------------------------
# Standard Test Run.
#----------------------------------------------------------------------
do_standard_test( $tool, $outdir, $logdir, $savdir, \@testlist );



#----------------------------------------------------------------------
# SUBROUTINES
#----------------------------------------------------------------------
# ===================================================================
# define_test_suite( void )
#   Create TestCase objects defining each test.
#
#   Arguments
#     none
#
#   Globals
#     tool    = Tool being tested
#     indir   = Test input directory
#     outdir  = Test output directory
#
#   Returns
#     tests: array of TestCase objects
# --------------------------------------------------------------------
sub define_test_suite
{
  my @testlist = [];
  my $ntests   = 1;   #!! how many test cases

  # pre-size the testslist array
  $endndx = $ntests - 1;
  $#testlist = $endndx;  # this sizes the array

  #!! Find test executable - adjust as needed to support command generation.
  my $texecdir = "${cwd}/${tool}";
  my $testexe = "${texecdir}/test_stkbuild.py";
  if (! -e $testexe )
  {
    print "ERROR: Test executable not found. ${testexe}\n";
  }

  # Find system utilities used
  #!! Add any system utilities used in test command strings
  #my $CP = find_in_path("cp", $ENV{'PATH'}, ':');

  my $comstr = ""; # command string

  for $ii (1 .. $ntests) 
  {
    my $testid = sprintf("test%02d", $ii);
    my $t = TestCase->new( id => $testid, mksubdir => 0 );

    #Setup each TestCase.
    if ( $testid eq "test01" )
    {
      $comstr  = "python ${testexe} > $outdir/${tool}_error.log"; # Build Test Command
    
      $t->desc("pystklib test suite.");  # describe what is being tested
      $t->type("regression");             # flavor "unit" or "regression"
      $t->command( $comstr );       # set command string
      $t->verify('fail', 0);         # validation criteria (for fail test)
    }

    $ndx = $ii - 1;
    $testlist[$ndx] = $t;
  }

  return(@testlist);
}