#! perl -w

##
## Name:
##
## cfx5stop
##
## Purpose:
##
## Stop a running ANSYS CFX Solver
##
## Synopsis:
#S
#S cfx5stop -directory <directory> [-verbose]
#S cfx5stop -help
#S
## []  denotes an optional argument. 
## |   separates mutually exclusive arguments. 
## ... following an argument indicates that it may be repeated.
## <>  denotes that substitution of a suitable value is required. All other
##     arguments are keywords, some of which have a short form.
##
## Arguments:
##
## -directory <directory> 
## -dir <directory> 
##     Name of directory in which the ANSYS CFX Solver is running.
##
## -help
## -h
##     Print this message.
##
## -verbose
## -v
##     Specifying this option may result in additional output being
##     sent to the standard output file (normally the screen).
##

# get the standard ANSYS CFX library directory
BEGIN {
  if ($0 =~ m!^([a-z]:)?[\\/]!i) {
    # it's an absolute path, use it as-is
    (my $nd=$0) =~ s!(perllib[\\/])?[^\\/]*$!!i;
    unshift @INC, "$nd/perllib";
  } else {
    # wrap this in an eval, and if it fails, pray ... for example,
    # this can fail on Windows if we're running off a share.
    eval {
      require FindBin;
      FindBin->import();
      unshift @INC, "$FindBin::Bin/perllib";
      my $dummy=$FindBin::Bin; # kill used only once warning
    };
    warn $@ if $@;
  }
  if (defined $ENV{CFX_LOCAL_PERLLIB}) {
     unshift @INC, $ENV{CFX_LOCAL_PERLLIB};
  }

}

use CFX5::Init;
CFX5::Init::init();

use vars qw($CVSVersion);
sub BEGIN {
  $CVSVersion='@(#) $Id$\n';
}

use strict;

use CFX5::Output qw(:all);
use CFX5::Config qw(%Config export_conf);
use CFX5::PrintConfig;

use CFX5::CmdLine qw(:util parse_cmdline);

use CFX5::FileOps qw(create_empty_file);

use CFX5::PathManip qw(join_file fullpath);

#
# read the configuration
#
CFX5::Config::read_config();

#
# Parse the command line
#
use vars qw($dir);

#
# Routine to create stp file
#
sub touch_stp {
  my $fdir=shift; 
  
  if (-f join_file($fdir,'stp')) {
    _abort("A stop request has already been sent to the ANSYS CFX Solver",
     "process running in the specified directory:",
     $fdir);
  }
  
  create_empty_file(join_file($fdir,'stp'))
    or _abort("Error writing to file ", join_file($fdir,'stp'), ": $!");
  
  _print("A stop request has been sent to the ANSYS CFX Solver ",
         "process running in the following directory:",
         "\n\n  ", $fdir, "\n\n",
         "The process will stop shortly.");
  
  1; 
}

parse_cmdline
  (\@ARGV,
   'INCLUDE_REQ',
   '-dir+'        => sub { conf_datadir_w(\$dir, "specified directory %%", @_); },
   '-directory+'  => '-dir',
  );

_error("You must pass the -directory switch")
  unless defined $dir;

exit_if_error();

my $fdir=fullpath($dir);

CFX5::PrintConfig::show_vars_by_verbosity();

# Basic, single configuration support
touch_stp($fdir) ;

# Multiconfiguration support
local *CHECKDIR;
unless (opendir CHECKDIR, $fdir) {
  _error("Directory not found:\n\t'$fdir'");
}
exit_if_error();

my $f;
while (defined ($f=readdir(CHECKDIR))) {
  if ($f =~ m\.*(.dir)\) {
    touch_stp(join_file($fdir,$f)) ;
    last; 
  }
}

closedir(CHECKDIR);

exit(0);
