#! /usr/bin/perl
$CODE_PAR = ' ';
#! //G/perl/Perl
# This script runs one abinis built-in test in background mode while
# periodically displaying the status file till the end of the process.

# Copyright (C) 1999-2005 ABINIT group (LSi,XG,LSi)
# This file is distributed under the terms of the
# GNU General Public License, see ~ABINIT/Infos/copyright
# or http://www.gnu.org/copyleft/gpl.txt .
# For the initials of contributors, see ~ABINIT/Infos/contributors .
#
# Usage :
# This script is intended to be called from Makefile (unix) or make.bat
# (DOS/Windows). Execute "make help" for details. It can also be called
# directly:
# unix shell: Run [ 1 | 2 | 3 | 4 | 5 | 6 ]
# Windows DOS box: Run.pl [ 1 | 2 | 3 | 4 | 5 | 6 ]
#
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator
#
$DELAY = 3;		# delay in seconds between 2 verifications of the STATUS file
$MAXLOOP = '50';	# maximum count for STATUS file checking loop
#
$TestN = $ARGV[0];
$debug = 0;		# Debug level
# make sure "files" file exist
if (! -r "test$TestN.files") {
	print "Invalid option: $TestN";
	print "Usage is: Run [ 1 | 2 | 3 | 4 | 5 | 6 ]";
	exit (8);
	}

$TMPfile = 'Test_in.tmp';	# multi usage temporary file
$UNIXSLASH = '/';	# unix subdirectory delimitor in file paths (octal 057)
$DOSSLASH = '\\';	# subdirectory delimitor in DOS file paths (\ escaped)

# try unix 'uname' command in the Bourne shell manner
$OStype = $ENV{'OSTYPE'};	# OSTYPE environment on unix systems (HP/UX excluded
if ($OStype eq 'OPENVMS')
  {
      $unamerc = system("pipe uname > $TMPfile 2>&1");
      }
else
  {
      $unamerc = system("uname > $TMPfile 2>&1");
      }
unlink ("$TMPfile");
# Check for Windows NT (DOS box or PGI Workstation)
# Although PGI Workstation is a unix shell, it behaves as a DOS one on some aspects
if ($ENV{'OS'} eq 'Windows_NT') {	# check OS environmental variable

# When 'uname' command is unknown, the return code is non zero. Under Windows it may
# be 1 or 256 depending of the Perl version and the command interpretor; 
	if ($unamerc == 1 || $unamerc == 256 || $OStype ne 'cygwin32') {
		$OStype = $ENV{'OS'};
# since unlink <file*> fails on some versions of perl for DOS, let's use del
		$ERASE_COMMAND = 'del /q';	# DOS delete file command
		$TYPE_COMMAND = 'type';		# DOS type file command
		}
	else {
#	$SLASH = $UNIXSLASH;		# subdirectory delimitor in file paths
		$ERASE_COMMAND = 'rm -f';	# unix delete file command
		$TYPE_COMMAND = 'cat';		# unix type file command
		}
# Define the DOS conventions to call the dotest perl script by means of the "system" 
# function. Since perl for Windows NT is not a PGI Workstation command (cygwin32) but a
# DOS module, these conventions are also valid in this environment.
	$SLASH = $DOSSLASH;	# subdirectory delimitor in DOS file paths
	$SUFXstyle = 'DOS';		# use DOS-style suffixes for binaries, ...
	$PLSUFX = '.pl';		# DOS suffix for perl script
	$PRLPFX = 'start /min perl ';	# DOS command to start perl script in background
	$BGSUFX = '';			# no special DOS suffix for new task
# NOTE: perl.exe MUST be accessible through the DOS PATH
#
# Check the presence of pstat command in PATH ;
# this is a ps-like command that can be found in Windows NT Resource kit.
# Since new windows started by a perl system function under PGI workstation don't
# appear in the output of a ps command under PGI Workstation, the completion of the
# the test will also be handled in the Windows NT rather than unix fashion.
	unlink ("$TMPfile") if (-e $TMPfile);
	$rc = system("pstat > $TMPfile 2>&1");
# some command interpretors create an empty file even the command is unknown
	if ($rc == 1 || $rc == 256 || -z $TMPfile) {
		$ENDtest = $MAXLOOP;	# loop limit in case dotest fails early
		print 'Warning ! pstat command not found';
		print "File test$TestN.end will be checked to test end of abinis execution";
		print "The wait loop of this script is limited to $ENDtest cycles";
		}
	else {
		$ENDtest = 'pstat';	# pstat command will be used to check end of test
		}
	unlink ("$TMPfile") if (-e $TMPfile);
	}
# if not Windows NT check return code of uname command:
elsif ($unamerc == 0) {
# Since unknown commands yield a return code of 0 under the Win9x command interpretor,
# a special environment variable, that may be set by make.bat, will be checked:
	if ($ENV{'PLATFORM_FOR_ABINIS'} eq 'DOS/Windows') {
		$OStype = 'DOS/Windows';
		$SLASH = $DOSSLASH;	# subdirectory delimitor in DOS file paths
# since unlink <file*> fails on some versions of perl for DOS, let's use del
		$ERASE_COMMAND = 'del /q';	# DOS delete file command
		$TYPE_COMMAND = 'type';		# DOS type file command
		$SUFXstyle = 'DOS';		# use DOS-style suffixes for binaries, ...
		$PLSUFX = '.pl';		# DOS suffix for perl script
		$PRLPFX = 'start /min perl ';	# DOS command to start perl script in background
		$BGSUFX = '';			# no special DOS suffix for new task
		$ENDtest = $MAXLOOP;		# loop limit in case dotest fails early	
		print "File test$TestN.end will be checked to test end of abinis execution";
		print "The wait loop of this script is limited to $ENDtest cycles";
		}
	else {
	    if ($ENV{'PLATFORM_FOR_ABINIS'} eq 'OPENVMS') {
		$SLASH = $UNIXSLASH;		# subdirectory delimitor in file paths
		$ERASE_COMMAND = 'delete ';	# OpenVMS delete file command
		$TYPE_COMMAND = 'type ';		# OpenVMS type file command
# for perl under normal unix systems:
		$SUFXstyle = 'vms';	# use unix-style suffixes for binaries, ...
		$PLSUFX = '.pl';		# no special suffix for perl script under unix
		$PRLPFX = 'perl ';		# perl path defined in first line of script
		$BGSUFX = '';		# special suffix for background task
		$ENDtest = 'vms';	# use gnv ps command to check end of test
		}
	    else {
		$SLASH = $UNIXSLASH;		# subdirectory delimitor in file paths
		$ERASE_COMMAND = 'rm -f';	# unix delete file command
		$TYPE_COMMAND = 'cat';		# unix type file command
# for perl under normal unix systems:
		$SUFXstyle = 'unix';	# use unix-style suffixes for binaries, ...
		$PLSUFX = '';		# no special suffix for perl script under unix
		$PRLPFX = './';		# perl path defined in first line of script
		$BGSUFX = '&';		# special suffix for background task
		$ENDtest = 'ps';	# use unix ps command to check end of test
		}
		}
	}
else {
	print "unrecognized Operating System $OStype";
	exit (99);
	}
#
print "Built-in test $TestN will be run through dotest$PLSUFX script";
# suppress old files
unlink("test$TestN.end") if (-e "test$TestN.end");
unlink("test$TestN.log") if (-e "test$TestN.log");
unlink("test$TestN.out") if (-e "test$TestN.out");
unlink("test$TestN".'.o_WFK') if (-e "test$TestN".'.o_WFK');
if ($OStype eq 'OPENVMS')
  {
      $statfile = "test$TestN".'_STATUS.dat';	# name of STATUS file
      }
else
  {
      $statfile = "test$TestN".'_STATUS';	# name of STATUS file
      }
unlink($statfile) if (-e $statfile);
# abinis will be started through a dotest perl script
# this is necessary under DOS/Windows because of file redirections
$cmd = $PRLPFX."dotest$PLSUFX $TestN $ENDtest $SLASH $SUFXstyle $BGSUFX";
print $cmd if ($debug > 0);
$rc = system($cmd);
die "Error $rc starting dotest" if ($rc != 0);
# loop waiting end of abinis execution
while (1) {
	sleep($DELAY);		# take a few seconds rest

	if ($ENDtest eq 'pstat') {
# check abinis process using pstat from Windows NT Ressource Kit
		$cmd = 'pstat | findstr /R "^pid.*abinis.exe" > '.$TMPfile;
		print $cmd if ($debug > 0);
		$rc = system($cmd);	
# findstr returns 0 if regular expression is found; 1 otherwise
		last if ($rc == 1 || $rc == 256);	# no abinis process was found
		}
	elsif ($ENDtest eq 'ps') {
# check abinis process using unix ps
		$cmd = 'ps | grep abinis | grep -v grep > '.$TMPfile;
		print $cmd if ($debug > 0);
		$rc = system($cmd);
		last if (-z $TMPfile);	# no abinis process was found
		}
	elsif ($ENDtest eq 'vms') {
# always end for VMS
                last;
		}
	else {
# check presence of file "test$TestN.end"  (like test1.end)
		last if (-e "test$TestN.end");	# dotest has completed
		$ENDtest --;	# decrement loop counter
		if ($ENDtest <= 0) {
			print 'Wait loop limit reached, dotest not yet finished';
			exit;		# loop limit reached
			}
		}
	$cmd = "$TYPE_COMMAND $statfile";
	system($cmd) if (-e $statfile);	# type status file if it exists
	}
#
	$cmd = "$TYPE_COMMAND $statfile";
	system($cmd);		# type status file
	unlink ("$TMPfile") if (-e $TMPfile);
exit (0);


