#!/usr/local/bin/perl

# Test driver for the Make test suite

# Usage:  run_make_tests  [testname] 
#                         [-debug] 
#                         [-help] 
#                         [-verbose]
#                         [-make]
#                        (and others)

require "test_driver.pl";

sub valid_option
{
   local($option) = @_;
   if ($option =~ /^-make(_path)?$/)
   {
      $make_path = shift @argv;
      if (!-f $make_path)
      {
	 print "$option $make_path: Not found.\n";
	 exit 0;
      }
      return 1;
   }
   else
   {
      return 0;
   }
}

sub run_make_with_options
{
   local ($filename,$options,$logname,$expected_code) = @_;
   local($code);
   local($command) = $make_path;

   if ($filename)
   {
      $command .= " -f $filename";
   }

   if ($options)
   {
      $command .= " $options";
   }

   $code = &run_command_with_output($logname,$command);
   if ($code != $expected_code)
   {
      print "Error running $make_path ($code): $command\n"; 
      $test_passed = 0;
      return 0;
   }  

   if ($profile & $vos)
   {
      system "add_profile $make_path";
   }
1;
}

sub print_usage
{
   &print_standard_usage ("run_make_tests", "[-make_path make_pathname]");
}

sub print_help
{
   &print_standard_help ("-make_path",
          "\tYou may specify the pathname of the copy of make to run.");
}

sub set_defaults
{
   # $profile = 1;
   $testee = "GNU make";
   $make_path = "make";
   $tmpfilesuffix = "mk";
   if ($vos)
   {
      $delete_command = "delete_file";
      $pwd = `++(current_dir)`;
   }
   else
   {
      $delete_command = "rm";
      chop ($pwd = `pwd`);
   }
}

sub set_more_defaults
{
   local($string);
   local($index);

   $string = `$make_path -v -f /dev/null 2> /dev/null`;

   $index = index ($string, ",");
   if ($index)
   {
      $string = substr ($string, 0, $index) . "\n";
   }

   $testee_version = $string;
   if ($make_path =~ /$pathsep([^\n$pathsep]*)$/)
   {
      $make_name = $1;
   }
   else
   {
      $make_name = $make_path;
   }

   # prepend pwd if this is a relative path (ie, does not
   # start with a slash, but contains one).  Thanks for the
   # clue, Roland.

   if (index ($make_path, "/") > 0)
   {
      $mkpath = "$pwd$pathsep$make_path";
   }
   else
   {
      $mkpath = $make_path;
   }
}

sub setup_for_test
{
   $makefile = &get_tmpfile;
   if (-f $makefile)
   { 
      unlink $makefile;
   }
}

exit !&toplevel;
