#!/usr/bin/perl -w
#
# ecaccess: Helper to start any ecaccess command from the PAR archive
#
# Laurent.Gougeon@ecmwf.int - 2012-02-09

use ECMWF::ECaccess;
use Getopt::Long;
use Pod::Usage;
use IPC::System::Simple qw(system capture);

my $commandName = $ARGV[0];
my $dataDir     = $ENV{PAR_TEMP};

die "No command name specified!\n"    if not($commandName);
die "Not called from a PAR archive\n" if not($dataDir);

# Remove command name from args
shift(@ARGV);

# Define the filename of the script
my $command = "$dataDir\\inc\\script\\$commandName";

# Check if the command exists
die "Command $commandName not found!\n" if ( not( -e $command ) );

# Run the requested ecaccess command
eval { $exitCode = system( $^X, $command, @ARGV ); };

# Return exit code from command
exit($exitCode);
