#!/usr/bin/perl
# Author: Maik Ernst
# History:
# 2013-07-09 - initial script
#
# LICENSE
# Hitachi, Ltd. All rights reserved
# Hitachi Data Systems products and services can be ordered only under the terms and conditions
# of the applicable Hitachi Data Systems agreements.
#
# NAME
# ppcollect.pl
# Hitachi Confoederatio Helvetica performance package collect tool
# Part of HiCHperfpkg
#
# SYNOPSIS
# see usage function below
#
# DESCRIPTION
#
# OPTIONS 
# see "usage" function below.
#
# NOTES
#
# AUTHOR
#   Maik Ernst (maik.ernst@hds.com) - July,2013 
#
# MODIFICATIONS
#   2013-07-09 - v1.0	- initial script
#   2013-07-09 - v1.1	- changed from only tar output file to tar/gzip
#   2013-07-10 - v1.2	- Adding VSP/USPV/HUSVM switch for correct java export tool command
#						- Deleting existing outpath before export tool start to avoid issue if the script got aborted (e.g. strg-c)
#						- Adding script version information
#						- Set -w automatically when osname is *Win*
#						- Set script start time to variable SHORTRANGE if getting Shortrange was not possible from logfile
#						- Bugfix sort on modified time of export tool log file
#  2013-07-23 - v1.3	- Changing default in command txt files to shortrange of 12hours (shortrange  -1200:).
#			  			  The reason for this is that you get partially invalid values (-4) in LU_dat as the collection period 
#			  			  does not match the avaiable data (max 24 hours) when the collection takes too long.
#  2013-09-20 - v1.4	- Adding longrange -600000 to all command.txt
#  2013-09-25 - v1.5    - Removing longrange -600000 from all command.txt
#  2013-10-03 - v1.6    - Adding error output of EXPORTTOOL_RUN
#  2013-12-05 - v1.7    - Change command.txt settings from "PhyPG" to "PhyPG Short" and "PhyLDEV" to "PhyLDEV Short"
#						  The reason for this is to shorten the runtime of the export tool
#  2014-01-06 - v1.8	- Changing die-function to be logged in file
#  2014-02-07 - v2.0    - raidcom collection added => use HDSPERF_RAIDCOM.pm module
#  2014-02-07 - v2.1    - adding FindBin, adding HORCM_DIR absolute path due to issue with starting the instance
#  2014-02-17 - v2.2    - adding parsable file (semicolon delimiter) from raidcom output
#  2014-02-21 - v2.3    - add raidcom collection of parity groups
#  2014-04-07 - v2.4    - add raidcom collection of pool and dp_pool
#						- Adding "short" to all long range groups in all command.txt that only short range gets collected. 
#						  The reason for this is to shorten the runtime.
#  2014-04-07 - v3.0    - Adding G1000 support
#                       - Changing absolute path handling (=> abs_path) + slashes to backslash as path
#                       - Adding -m at raidcom collection to get all MUs for raidscan
#						  But only for TC and not for SI as the collection would take too long.
#  2014-06-13 - v3.1    - Adding 7zip support for archiving with -z. This is to avoid high memory usage if the server has not enough memory to archive.
#                         (When you get error message: "out of memory")
#  2014-12-26 - v3.2    - Adding log file output
#  2015-04-02 - v3.3	- Adding -f option to use FC device instead of IP command device
#						  Adding -o option to collect only raidcom data and skip export tool data collection
#						  Adding Sys::Hostname to get hostname of script
#  2015-04-30 - v3.4    - Using export tool JSanRmiApiSx instead of JSanRmiApiEx for G1000
#  2015-05-02 - v4.0    - Change from parameter to config file => pm file config::tiny needed
#						  Changing script name from HDSperfcollect.pl to HCHppcollect.pl to meet swiss script standard
#  2015-05-05 		    - Bugfix (missing 3 in in CDM file for G1000 when using FC command device)
#  2015-05-10 - v4.1.0	- Reorganize folder structure for HiCHperfpkg according to Swiss development group 
#						  (tgz only works but only with absolute pahts => do not use it anymore)
#						  Log format changed (LOG_MSG)
#						  Adding new parameter like ppcollect_ccicollectionlevel
#  2015-07-02 - v4.1.1	- Allow G200, G400, G600 and G800 as parameter
#						  Adding ip-regexp from cmd.txt for G800/G600/G400/G200 (ip instead of svpip)
#						  Adding GUM ip regexp from cmd.txt for G800/G600/G400/G200 
#  2015-07-05 - v4.1.2	- Bugfix regexp to get outpath correctly from cmd.txt
#						  Bugfix unquote outpath
#  2015-08-29 - v4.2.0	- Implement G1000 MC v3 export toool changed 
#						 (also for G800/G600/G400/G200 as assumption that it will be the same soon)							
#						  Change variable to hash for export tool
#  2015-09-10 - v4.2.1	- When user or password "n/a" then exit with error message
#  2016-03-25 - v4.3.0	- LOG->autoflush(1), fix DELETE_OLD_SAVES DELOD log message
#						- adding DELETE_OLD_LOGS / log file rotation ppcollect_logkeepdays
#  2016-04-02 - v4.3.1	- Change log output PWD = ***


use strict;
use warnings;
use POSIX qw(strftime);
use Time::Local;
use File::Find;
use File::Path;
use File::Spec;
use File::Basename;
use Archive::Tar;
use Getopt::Long qw(:config no_ignore_case bundling); # Get options /  qw(:config bundling no_ignore_case) makes getopt case sensitive 
use Config;
use Cwd 'abs_path';
use Sys::Hostname; # To get hostname
use IO::Handle; # required for autoflush

# Use raidcom lib. FindBin is needed to find the module when running the script from relativ/absolute paths.
use FindBin qw($Bin);
use lib $Bin;
use lib "$Bin\\..\\lib\\modules";
use Config::Tiny;
use HiCHpp_RAIDCOM;

### main ### DO NOT CHANGE ANYTHING IF DO NOT KNOW WHAT YOU ARE DOING!
my $VERSION = "4.3.1";

INIT {$SIG{__DIE__}=sub {LOG_MSG("normal",3,"GENERAL","Script died: $_[0]") and close LOG;}}
INIT {$SIG{__WARN__}=sub {LOG_MSG("normal",4,"GENERAL","Script warning: $_[0]")}}

# Check Flags
my $flag_help;
my $flag_version;
my $flag_config;

GetOptions (
	'h|help' => \$flag_help,
	'V|VER' => \$flag_version,
	'c|config=s' => \$flag_config,
           ) or die USAGE();

# Change to directory of the running script 
# abs_path for $0 or __FILE__ , 
my ( $script_name, $script_path, $script_suffix ) = fileparse( abs_path($0), qr{\.[^.]*$} );
my $script_dirname = dirname (abs_path($0));
# Change slashes to backslashes
$script_dirname =~ s/\//\\/g;
chdir ($script_dirname);
my $script_rootdir =  dirname ($script_dirname);

# Check flags and print usage
if ($flag_version)
{
	print "Version: $VERSION\n";
	exit;
}

if ($flag_help)
{
	USAGE();
	exit;
}

if (!$flag_config)
{
	print "\n!!! You must define -c <config file>. Example: ppcollect.pl -c pp_54068.conf !!!\n";
    USAGE();
    exit;
}

unless (-e "${script_rootdir}\\etc\\${flag_config}")
{
	print "\n!!! Your with -c defined config file \"${script_rootdir}\\etc\\${flag_config}\" does not exist. !!!\n";
    USAGE();
    exit;
}
$flag_config = "${script_rootdir}\\etc\\${flag_config}";

# Get config
my $conf_file = Config::Tiny->new;
$conf_file = Config::Tiny->read($flag_config);

# Check & set variables for log

if (!$conf_file->{maindir}->{pp_log})
{
	print "\n!!! Variable \"[maindir] pp_log\" is not defined correctly in $flag_config!!!\n";
	exit;
}

if (!$conf_file->{main}->{ppcollect_serial})
{
	print "\n!!! Variable \"[main] ppcollect_serial\" is not defined correctly in $flag_config!!!\n";
	exit;
}

my $flag_system = uc $conf_file->{main}->{ppcollect_system};
if ($flag_system !~ m/^VSP$|^HUSVM$|^USPV$|^G1000$|^G800$|^G600$|^G400$|^G200$/)
{
	print "\n!!! Variable \"[main] ppcollect_system\" is not defined correctly in $flag_config!!!\n";
	print "!!! It must be set to G1000, VSP, HUSVM, G800, G600, G400, G200or USPV!!!\n";
	exit;
}

my $scriptdir_log = "${script_rootdir}\\$conf_file->{maindir}->{pp_log}";
my $serial = $conf_file->{main}->{ppcollect_serial};

#my $serial = $1; # see regexp above for serial 
my $time_scriptstart = strftime("%Y-%m-%d_%H-%M-%S", localtime);
my $HOST = hostname;
my $SCRIPTLOG_FILE = "${scriptdir_log}\\${script_name}_${flag_system}_${serial}_${time_scriptstart}.log";
my $SCRIPTLOG_DEL_PATH = "${scriptdir_log}\\${script_name}_${flag_system}_${serial}_*.log";

# Create directories
unless (-d $scriptdir_log) { mkpath($scriptdir_log) or die ("Can't create dir $scriptdir_log: $!\n"); }

# Open script logfile
open(LOG,"> $SCRIPTLOG_FILE") or die ("Can't open $SCRIPTLOG_FILE: $!\n");
LOG->autoflush(1);
LOG_MSG("normal",5,"GENERAL","Starting Script $0");
LOG_MSG("normal",5,"GENERAL","Version: ${VERSION}");
LOG_MSG("normal",5,"GENERAL","Logfile: $SCRIPTLOG_FILE");
LOG_MSG("normal",5,"GENERAL","Operating system: \"$Config{osname}\"");
LOG_MSG("normal",5,"GENERAL","Hostname: \"$HOST\"");
LOG_MSG("normal",5,"GENERAL","This is a ${flag_system} with the serial ${serial}");
LOG_MSG("normal",5,"GENERAL","Config file: \"$flag_config\"");
LOG_MSG("normal",5,"PARAM","Parameters (not checked yet) ..."); 

foreach my $conf_section (sort keys %{$conf_file}) 
{
   LOG_MSG("normal",5,"PARAM","[$conf_section]");
    foreach my $conf_parameter (sort keys %{$conf_file->{$conf_section}}) 
	{
		if ($conf_parameter =~ m/pwd/)
		{
			if ($conf_file->{$conf_section}->{$conf_parameter})
			{
				LOG_MSG("normal",5,"PARAM","\t$conf_parameter = ***");
			}
			else
			{
				LOG_MSG("normal",5,"PARAM","\t$conf_parameter = $conf_file->{$conf_section}->{$conf_parameter}");
			}
		}	
		else
		{
			LOG_MSG("normal",5,"PARAM","\t$conf_parameter = $conf_file->{$conf_section}->{$conf_parameter}");
		}
	}
}

### Check & set variables for the rest

my $EXPORTTOOL_JAVA;
my $flag_commandtxt;
my $flag_lib;
my $flag_keepdays;
my $flag_logkeepdays;
my $flag_win;
my $flag_cci_dir;
my $flag_raidcom;
my $flag_raidcom_only;
my $flag_raidcom_level;
my $RAIDCOM_FCDEVICE;
my $ZIP_PATH;
my $flag_zip;
my $ARCHIVE_SUFFIX;
my $EXPORTTOOL_LOG;
my $ARCHIVE_DIR;
my $lglvl;

LOG_MSG("normal",5,"PARAM","Checking parameter now");
LOG_MSG("normal",5,"PARAM","Logdir: $scriptdir_log");
LOG_MSG("normal",5,"PARAM","Serial: $serial");
LOG_MSG("normal",5,"PARAM","System: $flag_system");

if (lc $conf_file->{log}->{ppcollect_loglevel} eq "normal")
{
	$lglvl = $conf_file->{log}->{ppcollect_loglevel};
	LOG_MSG($lglvl,5,"PARAM","Loglevel: normal");
}

elsif (lc $conf_file->{log}->{ppcollect_loglevel} eq "debug")
{
	$lglvl = $conf_file->{log}->{ppcollect_loglevel};
	LOG_MSG($lglvl,5,"PARAM","Loglevel: debug");
}

else
{
	LOG_MSG("normal",3,"PARAM","\"[log] ppcollect_loglevel\" is not valid."); 
	LOG_MSG("normal",3,"PARAM","See \"$flag_config\" and check if it is set to \"normal\" or \"debug\".");
	LOG_MSG("normal",3,"GENERAL","Finishing Script $0");
	close LOG;
	exit;
}

if (!$conf_file->{maindir}->{pp_bin})
{
	LOG_MSG($lglvl,3,"PARAM","\"[maindir] pp_bin\" is not defined correctly.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
my $scriptdir_bin = "${script_rootdir}\\$conf_file->{maindir}->{pp_bin}";

if (!$conf_file->{maindir}->{pp_data})
{
	LOG_MSG($lglvl,3,"PARAM","\"[maindir] pp_data\" is not defined correctly.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
my $scriptdir_data = "${script_rootdir}\\$conf_file->{maindir}->{pp_data}";

if (!$conf_file->{maindir}->{pp_etc})
{
	LOG_MSG($lglvl,3,"PARAM","\"[maindir] pp_etc\" is not defined correctly.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
my $scriptdir_etc = "${script_rootdir}\\$conf_file->{maindir}->{pp_etc}";

if (!$conf_file->{maindir}->{pp_doc})
{
	LOG_MSG($lglvl,3,"PARAM","\"[maindir] pp_doc\" is not defined correctly.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
my $scriptdir_doc = "${script_rootdir}\\$conf_file->{maindir}->{pp_doc}";

if (!$conf_file->{maindir}->{pp_lib})
{
	LOG_MSG($lglvl,3,"PARAM","\"[maindir] pp_lib\" is not defined correctly.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
my $scriptdir_lib = "${script_rootdir}\\$conf_file->{maindir}->{pp_lib}";

if (!$conf_file->{maindir}->{pp_spool})
{
	LOG_MSG($lglvl,3,"PARAM","\"[maindir] pp_spool\" is not defined correctly.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
my $scriptdir_spool = "${script_rootdir}\\$conf_file->{maindir}->{pp_spool}";

unless ((-d $scriptdir_bin) and (-d $scriptdir_data) and (-d $scriptdir_etc) and (-d $scriptdir_doc) and (-d $scriptdir_lib) and (-d $scriptdir_spool) and (-d $scriptdir_log))
{
	LOG_MSG($lglvl,3,"PARAM","Any of the following directories does not exist...");
	LOG_MSG($lglvl,3,"PARAM","\t$scriptdir_bin");
	LOG_MSG($lglvl,3,"PARAM","\t$scriptdir_data");
	LOG_MSG($lglvl,3,"PARAM","\t$scriptdir_etc");
	LOG_MSG($lglvl,3,"PARAM","\t$scriptdir_doc");
	LOG_MSG($lglvl,3,"PARAM","\t$scriptdir_lib");
	LOG_MSG($lglvl,3,"PARAM","\t$scriptdir_log");
	LOG_MSG($lglvl,3,"PARAM","\t$scriptdir_spool");
	LOG_MSG($lglvl,3,"PARAM","\tPlease check your directories and/or [maindir] section in config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","\tFinishing Script $0");
	close LOG;
    exit;
}

if (!$conf_file->{java}->{ppcollect_javaexecutable})
{
	LOG_MSG($lglvl,3,"PARAM","\"[java] ppcollect_javaexecutable\" is not defined correctly."); 
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
	exit;
}
$EXPORTTOOL_JAVA = $conf_file->{java}->{ppcollect_javaexecutable};
LOG_MSG($lglvl,5,"PARAM","Java executable: $EXPORTTOOL_JAVA");

if (lc $conf_file->{main}->{pp_mode} ne "windows")
{
	LOG_MSG($lglvl,3,"PARAM","\"[main] pp_mode\" is not supported. Only windows."); 
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
else
{
	if ($Config{osname}=~/Win/)
	{
		$flag_win = 1;
		LOG_MSG($lglvl,5,"PARAM","Mode: $conf_file->{main}->{pp_mode}");
	}
	else
	{
		LOG_MSG($lglvl,3,"PARAM","It looks like your OS it not Windows \"$Config{osname}\". Only windows is not supported."); 
		LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
		close LOG;
		exit;
	}
}

$flag_commandtxt = "${scriptdir_etc}\\${flag_system}_command_${serial}.txt";
LOG_MSG($lglvl,5,"PARAM","Commandtxt: $flag_commandtxt");

unless (-e $flag_commandtxt)
{
	LOG_MSG($lglvl,3,"PARAM","Your defined command file: \"$flag_commandtxt\" does not exist.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\" and/or your \"$flag_commandtxt\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}

if (!$conf_file->{main}->{ppcollect_exporttoollib})
{
	LOG_MSG($lglvl,3,"PARAM","\"[main] ppcollect_exporttoollib\" is not defined correctly.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
$flag_lib = "${scriptdir_lib}\\$conf_file->{main}->{ppcollect_exporttoollib}";
LOG_MSG($lglvl,5,"PARAM","Libdir: $flag_lib");

unless (-d $flag_lib) 
{
	LOG_MSG($lglvl,3,"PARAM","Your defined lib dir: \"$flag_lib\" does not exist.");
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\" and/or your \"$flag_lib\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}

if ($conf_file->{main}->{ppcollect_keepdays} !~ /^\d+$/)
{
	LOG_MSG($lglvl,3,"PARAM","\"[main] ppcollect_keepdays\" must be set, a whole number and bigger or equal 0"); 
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
$flag_keepdays = $conf_file->{main}->{ppcollect_keepdays};
LOG_MSG($lglvl,5,"PARAM","Keepdays: $flag_keepdays");

if ($conf_file->{main}->{ppcollect_logkeepdays} !~ /^\d+$/)
{
	LOG_MSG($lglvl,3,"PARAM","\"[main] ppcollect_logkeepdays\" must be set, a whole number and bigger or equal 0"); 
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
    exit;
}
$flag_logkeepdays = $conf_file->{main}->{ppcollect_logkeepdays};
LOG_MSG($lglvl,5,"PARAM","Logfile keepdays: $flag_logkeepdays");

if ((lc $conf_file->{raidcom}->{ppcollect_ccicollection} eq "on") or (lc $conf_file->{raidcom}->{ppcollect_ccicollection} eq "raidcomonly"))
{
	unless (-d $conf_file->{raidcom}->{ppcollect_ccidir}) 
	{
		LOG_MSG($lglvl,3,"PARAM","\"[raidcom] ppcollect_ccidir\" does not exist."); 
		LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
		LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
		close LOG;
		exit;
	}
	
	if ($conf_file->{raidcom}->{ppcollect_ccicollectionlevel} !~ m/^0|1|2$/)
	{
		LOG_MSG($lglvl,3,"PARAM","\"[raidcom] ppcollect_ccicollectionlevel\" does not exist or is not set to 0, 1 or 2."); 
		LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
		LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
		close LOG;
		exit;
		
	}
	else
	{
		LOG_MSG($lglvl,5,"PARAM","Raidcom level set to: $conf_file->{raidcom}->{ppcollect_ccicollectionlevel}");
		$flag_raidcom_level = $conf_file->{raidcom}->{ppcollect_ccicollectionlevel};
	}
	
	if (uc $conf_file->{raidcom}->{ppcollect_commanddevice} eq "IP")
	{
		LOG_MSG($lglvl,5,"PARAM","Raidcom: Set IP CMD");
		$RAIDCOM_FCDEVICE = "no";
	}

	elsif (uc $conf_file->{raidcom}->{ppcollect_commanddevice} eq "FC")
	{
		LOG_MSG($lglvl,5,"PARAM","raidcom: Set FC CMD");
		$RAIDCOM_FCDEVICE = "yes";
	}

	else
	{
		LOG_MSG($lglvl,3,"PARAM","\"[raidcom] ppcollect_commanddevice\" is not valid."); 
		LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\" and check if it is set to \"IP\" or \"FC\".");
		LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
		close LOG;
		exit;
	}
	
	if (lc $conf_file->{raidcom}->{ppcollect_ccicollection} eq "raidcomonly") 
	{
		LOG_MSG($lglvl,5,"PARAM","Raidcom: enable raidcom collection");
		$flag_cci_dir = $conf_file->{raidcom}->{ppcollect_ccidir};
		$flag_raidcom = 1;
		$flag_raidcom_only = 1;
	}
	else
	{
		LOG_MSG($lglvl,5,"PARAM","Raidcom: enable raidcom collection only");
		$flag_cci_dir = $conf_file->{raidcom}->{ppcollect_ccidir};
		$flag_raidcom = 1;
	}
}

else
{
	LOG_MSG($lglvl,5,"PARAM","Raidcom: disable raidcom collection as it is not set to \"on\" or \"raidcomonly\".");
}

if (lc $conf_file->{zip}->{ppcollect_zipmethod} eq "zip") 
{
    
	unless (-e "${scriptdir_bin}\\$conf_file->{zip}->{ppcollect_zippath}") 
	{
		LOG_MSG($lglvl,3,"PARAM","\"[zip] ppcollect_zippath\" does not exist."); 
		LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
		LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
		close LOG;
		exit;
	}
	$flag_zip = 1;
	$ZIP_PATH = "${scriptdir_bin}\\$conf_file->{zip}->{ppcollect_zippath}";
	$ARCHIVE_SUFFIX = ".zip";
	LOG_MSG($lglvl,5,"PARAM","Zipsuffix: $ARCHIVE_SUFFIX - path: $ZIP_PATH ");
}

elsif (lc $conf_file->{zip}->{ppcollect_zipmethod} eq "tgz")
{
	$ARCHIVE_SUFFIX = ".tgz";
	LOG_MSG($lglvl,5,"PARAM","Zipsuffix: $ARCHIVE_SUFFIX");
}

else
{
	LOG_MSG($lglvl,3,"PARAM","\"[zip] ppcollect_zipmethod\" is not valid."); 
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\" and check if it is set to \"zip\" or \"tgz\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
	exit;
}

if (!$conf_file->{log}->{ppcollect_exporttoollogprefix})
{
	LOG_MSG($lglvl,3,"PARAM","\"[log] ppcollect_exporttoollogprefix\" is not defined correctly."); 
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
	exit;
}
$EXPORTTOOL_LOG = "$scriptdir_log\\$conf_file->{log}->{ppcollect_exporttoollogprefix}${serial}";
LOG_MSG($lglvl,5,"PARAM","Export tool logfile: $EXPORTTOOL_LOG");

if (!$conf_file->{zip}->{ppcollect_archivedir})
{
	LOG_MSG($lglvl,3,"PARAM","\"[zip] ppcollect_archivedir\" is not defined correctly."); 
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
	exit;
}
$ARCHIVE_DIR = "${scriptdir_data}\\$conf_file->{zip}->{ppcollect_archivedir}${serial}";
LOG_MSG($lglvl,5,"PARAM","Archive dir: $ARCHIVE_DIR");
# make path absolute
#$ARCHIVE_DIR =  File::Spec->rel2abs( $ARCHIVE_DIR ) ;
my $ARCHIVE_PREFIX = "${ARCHIVE_DIR}\\${flag_system}_${serial}";
LOG_MSG($lglvl,5,"PARAM","Archive dir prefix: $ARCHIVE_PREFIX");

if (!$conf_file->{raidcom}->{ppcollect_horcmdirprefix})
{
	LOG_MSG($lglvl,3,"PARAM","\"[raidcom] ppcollect_horcmdirprefix\" is not defined correctly."); 
	LOG_MSG($lglvl,3,"PARAM","Please check your config \"$flag_config\".");
	LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
	close LOG;
	exit;
}

my $HORCM_DIR = "${scriptdir_data}\\$conf_file->{raidcom}->{ppcollect_horcmdirprefix}${serial}";
LOG_MSG($lglvl,5,"PARAM","Horcmdir dir: $HORCM_DIR");

LOG_MSG($lglvl,5,"PARAM","Checking parameter finished");

# Set variables

my $COMMAND_TXT = $flag_commandtxt;
my $EXPORTTOOL_LIB = $flag_lib;
# make path absolute
$EXPORTTOOL_LIB =  File::Spec->rel2abs( $EXPORTTOOL_LIB ) ;
my $EXPORTTOOL_RUN;
my $EXPORTTOOL_DEL = "n/a";

my %EXPORTTOOLv3_LOADER = (
	"G1000v3_WIN" => "${EXPORTTOOL_LIB}/JSanExportLoader.jar",
	"G800v3_WIN" => "${EXPORTTOOL_LIB}/JSanExportLoader.jar",
	"G600v3_WIN" => "${EXPORTTOOL_LIB}/JSanExportLoader.jar",
	"G400v3_WIN" => "${EXPORTTOOL_LIB}/JSanExportLoader.jar",
	"G200v3_WIN" => "${EXPORTTOOL_LIB}/JSanExportLoader.jar",
	);
	
my %EXPORTTOOL_CMD = (
	"VSP_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiEx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824" .
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",

	"HUSVM_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiEx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824" .
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",
	
	"G1000_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824" . 
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",

	"G800_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824" .
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",

	"G600_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824" .
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",

	"G400_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824" .
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",
	
	"G200_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824" .
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",
	
	"USPV_WIN" => "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiServerSx.jar; -Xmx1073741824" .
	" -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain",
	
	"G1000v3_WIN_RUN" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G1000v3_WIN} -Del.tool.Xmx=1073741824 -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG}" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	"G1000v3_WIN_DEL" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G1000v3_WIN} -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG} -Del.mode=delete" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	"G800v3_WIN_RUN" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G800v3_WIN} -Del.tool.Xmx=1073741824 -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG}" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	"G800v3_WIN_DEL" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G800v3_WIN} -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG} -Del.mode=delete" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
		
	"G600v3_WIN_RUN" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G600v3_WIN} -Del.tool.Xmx=1073741824 -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG}" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	"G600v3_WIN_DEL" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G600v3_WIN} -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG} -Del.mode=delete" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	"G400v3_WIN_RUN" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G400v3_WIN} -Del.tool.Xmx=1073741824 -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG}" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	"G400v3_WIN_DEL" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G400v3_WIN} -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG} -Del.mode=delete" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
		
	"G200v3_WIN_RUN" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G200v3_WIN} -Del.tool.Xmx=1073741824 -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG}" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	"G200v3_WIN_DEL" => "${EXPORTTOOL_JAVA} -classpath $EXPORTTOOLv3_LOADER{G200v3_WIN} -Dmd.command=${COMMAND_TXT} -Del.logpath=${EXPORTTOOL_LOG} -Del.mode=delete" .
	" -Dmd.rmitimeout=20 sanproject.getexptool.RJElMain",
	
	);
	
my $EXPORTTOOL_RUN_VSP_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiApiEx.jar:${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
my $EXPORTTOOL_RUN_HUSVM_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiApiEx.jar:${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
my $EXPORTTOOL_RUN_G1000_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiApiSx.jar:${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
my $EXPORTTOOL_RUN_G800_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiApiSx.jar:${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
my $EXPORTTOOL_RUN_G600_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiApiSx.jar:${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
my $EXPORTTOOL_RUN_G400_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiApiSx.jar:${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
my $EXPORTTOOL_RUN_G200_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiApiSx.jar:${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
my $EXPORTTOOL_RUN_USPV_UNIX = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar:${EXPORTTOOL_LIB}/JSanRmiServerSx.jar; -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";

#my $EXPORTTOOL_RUN_VSP_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiEx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
#my $EXPORTTOOL_RUN_HUSVM_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiEx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
#my $EXPORTTOOL_RUN_G1000_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
#my $EXPORTTOOL_RUN_G800_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
#my $EXPORTTOOL_RUN_G600_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
#my $EXPORTTOOL_RUN_G400_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
#my $EXPORTTOOL_RUN_G200_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiApiSx.jar;${EXPORTTOOL_LIB}/JSanRmiServerUx.jar;${EXPORTTOOL_LIB}/SanRmiApi.jar -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";
#my $EXPORTTOOL_RUN_USPV_WIN = "${EXPORTTOOL_JAVA} -classpath ${EXPORTTOOL_LIB}/JSanExport.jar;${EXPORTTOOL_LIB}/JSanRmiServerSx.jar; -Xmx1073741824 -Dmd.command=${COMMAND_TXT} -Dmd.logpath=${EXPORTTOOL_LOG} sanproject.getmondat.RJMdMain";

# Create directories
unless (-d $ARCHIVE_DIR) { mkpath($ARCHIVE_DIR) or die ("Can't create dir $ARCHIVE_DIR: $!\n"); }


# Check the flags and set $EXPORTTOOL_RUN to environment

if ($flag_win)
{
	if ($flag_system eq "VSP")
	{
		$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{VSP_WIN};
		LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
		LOG_MSG($lglvl,5,"GENERAL","Running in VSP mode");
		LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
	}
	elsif ($flag_system eq "USPV")
	{
		$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{USPV_WIN};
		LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
		LOG_MSG($lglvl,5,"GENERAL","Running in USPV mode");
		LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
	}
	elsif ($flag_system eq "HUSVM")
	{
		$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{HUSVM_WIN};
		LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
		LOG_MSG($lglvl,5,"GENERAL","Running in HUSVM mode");
		LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
	}
	elsif ($flag_system eq "G1000")
	{
		#check if G1000 v3 microcode
		if (-e $EXPORTTOOLv3_LOADER{G1000v3_WIN})
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G1000v3_WIN_RUN};
			$EXPORTTOOL_DEL = $EXPORTTOOL_CMD{G1000v3_WIN_DEL};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G1000 mode above v3 MC");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_DEL = $EXPORTTOOL_DEL");
		}
		else
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G1000_WIN};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G1000 mode below v3 MC");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
		}
	}
	elsif ($flag_system eq "G800")
	{
		#check if G800 v3 microcode
		if (-e $EXPORTTOOLv3_LOADER{G800v3_WIN})
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G800v3_WIN_RUN};
			$EXPORTTOOL_DEL = $EXPORTTOOL_CMD{G800v3_WIN_DEL};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G800 mode above v3 MC");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_DEL = $EXPORTTOOL_DEL");
		}
		else
		{
		$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G800_WIN};
		LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
		LOG_MSG($lglvl,5,"GENERAL","Running in G800 mode");
		LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
		}
	}
	elsif ($flag_system eq "G600")
	{
		#check if G600 v3 microcode
		if (-e $EXPORTTOOLv3_LOADER{G600v3_WIN})
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G600v3_WIN_RUN};
			$EXPORTTOOL_DEL = $EXPORTTOOL_CMD{G600v3_WIN_DEL};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G600 mode above v3 MC");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_DEL = $EXPORTTOOL_DEL");
		}
		else
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G600_WIN};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G600 mode");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
		}
	}
	elsif ($flag_system eq "G400")
	{
		#check if G400 v3 microcode
		if (-e $EXPORTTOOLv3_LOADER{G400v3_WIN})
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G400v3_WIN_RUN};
			$EXPORTTOOL_DEL = $EXPORTTOOL_CMD{G400v3_WIN_DEL};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G400 mode above v3 MC");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_DEL = $EXPORTTOOL_DEL");
		}
		else
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G400_WIN};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G400 mode");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
		}
	}
	elsif ($flag_system eq "G200")
	{
		#check if G200 v3 microcode
		if (-e $EXPORTTOOLv3_LOADER{G200v3_WIN})
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G200v3_WIN_RUN};
			$EXPORTTOOL_DEL = $EXPORTTOOL_CMD{G200v3_WIN_DEL};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G200 mode above v3 MC");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_DEL = $EXPORTTOOL_DEL");
		}
		else
		{
			$EXPORTTOOL_RUN = $EXPORTTOOL_CMD{G200_WIN};
			LOG_MSG($lglvl,5,"GENERAL","Running in windows mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G200 mode");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
		}
	}
	else
	{
		LOG_MSG($lglvl,3,"GENERAL","You should never be here. Check -s $flag_system");
		exit;
	}
}

else 
{
        if ($flag_system eq "VSP")
        {
			$EXPORTTOOL_RUN = $EXPORTTOOL_RUN_VSP_UNIX;
			LOG_MSG($lglvl,5,"GENERAL","Running in Unix mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in VSP mode");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
        }
        elsif ($flag_system eq "USPV")
        {
			$EXPORTTOOL_RUN = $EXPORTTOOL_RUN_USPV_UNIX;
			LOG_MSG($lglvl,5,"GENERAL","Running in Unix mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in USPV mode");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
        }
        elsif ($flag_system eq "HUSVM")
        {
			$EXPORTTOOL_RUN = $EXPORTTOOL_RUN_HUSVM_UNIX;
			LOG_MSG($lglvl,5,"GENERAL","Running in Unix mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in HUSVM mode");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
        }
		elsif ($flag_system eq "G1000")
        {
			$EXPORTTOOL_RUN = $EXPORTTOOL_RUN_G1000_UNIX;
			LOG_MSG($lglvl,5,"GENERAL","Running in Unix mode");
			LOG_MSG($lglvl,5,"GENERAL","Running in G1000 mode");
			LOG_MSG($lglvl,5,"GENERAL","EXPORTTOOL_RUN = $EXPORTTOOL_RUN");
        }
        else
        {
			LOG_MSG($lglvl,3,"GENERAL","You should never be here. Check -s $flag_system");
			LOG_MSG($lglvl,3,"GENERAL","Finishing Script $0");
			close LOG;
			exit;
        }
}

my ($EXPORTTOOL_OUTPATH,$EXPORTTOOL_IP,$EXPORTTOOL_USER,$EXPORTTOOL_PWD,$GUM_IPs) = GET_OUTPATH_IP_PWD($serial,$COMMAND_TXT,$lglvl);

# Delete outpath if existing
if (-d $EXPORTTOOL_OUTPATH)
{	
	LOG_MSG($lglvl,5,"GENERAL","Directory $EXPORTTOOL_OUTPATH exists");
	LOG_MSG($lglvl,5,"GENERAL","Deleting old $EXPORTTOOL_OUTPATH");
	rmtree ($EXPORTTOOL_OUTPATH) or die LOG_MSG($lglvl,3,"GENERAL","Can't delete dirs $EXPORTTOOL_OUTPATH: $!\n");
	LOG_MSG($lglvl,5,"GENERAL","Deleting finished");
}

# Define SHORTRANGE for tgz/zip file. 
# It should come from the export tool logfile but if the option -o is set
# or the export tool had an issue it is set  to script start time
my $SHORTRANGE;
if ($flag_raidcom_only)
{
	LOG_MSG($lglvl,5,"PARAM","Raidcom only flag set (-o). No export tool data will be collected!");
	LOG_MSG($lglvl,5,"PARAM","Setting file name to:\"RAIDCOM_ONLY_${time_scriptstart}\"");
	$SHORTRANGE = "RAIDCOM_ONLY_${time_scriptstart}";
}

else
{
	if ( $EXPORTTOOL_DEL ne "n/a" )
	{
		DEL_EXPORTTOOL($EXPORTTOOL_DEL,$lglvl);
	}
	RUN_EXPORTTOOL($EXPORTTOOL_RUN,$lglvl);
	$SHORTRANGE = GET_SHORTRANGE($EXPORTTOOL_LOG,$lglvl);
	if (!$SHORTRANGE)
	{
		LOG_MSG($lglvl,4,"PARAM","Did not get Shortrange. Setting it to:\"NO_SHORTRANGE_${time_scriptstart}\"");
		$SHORTRANGE = "NO_SHORTRANGE_${time_scriptstart}";
	}
}

# If raidcom flag defined then do the raidcom stuff
if ($flag_raidcom)
{
	if (($flag_win) and (($flag_system eq "HUSVM") or ($flag_system eq "VSP") or ($flag_system eq "G1000")
	or ($flag_system eq "G800") or ($flag_system eq "G600") or ($flag_system eq "G400") or ($flag_system eq "G200")))
	{
		LOG_MSG($lglvl,5,"GENERAL","Creating directory for raidcom: $HORCM_DIR");
		unless (-d $HORCM_DIR) { mkpath($HORCM_DIR) or warn LOG_MSG($lglvl,4,"GENERAL","Can't create dir $HORCM_DIR: $!\n"); }
		# This check is required as Gx00 uses GUM IP for raidcom instead of SVP IP
		if ($flag_system =~ /^G800$|^G600$|^G400$|^G200$/)
		{
			$EXPORTTOOL_IP = $GUM_IPs;
		}
		# this is a module HDSPERF_RAIDCOM.pm. IMPORTANT to use $HORCM_DIR for HORCM INSTANCE start!
		DO_RAIDCOM ($flag_cci_dir,$EXPORTTOOL_IP,$HORCM_DIR,$EXPORTTOOL_USER,$EXPORTTOOL_PWD,$serial,$RAIDCOM_FCDEVICE,$flag_system,$flag_raidcom_level,$lglvl);
	}
	else
	{
		LOG_MSG($lglvl,4,"GENERAL","Currently only windows for HUS-VM/VSP/G1000/G800/G600/G400/G200 is supported for raidcom collection therefore raidcom information will NOT get collected.");
	}

}

my $ARCHIVE_FILE = "${ARCHIVE_PREFIX}_${SHORTRANGE}${ARCHIVE_SUFFIX}";
DO_ARCHIV($COMMAND_TXT,$EXPORTTOOL_OUTPATH,$EXPORTTOOL_LOG,$ARCHIVE_FILE,$HORCM_DIR,$ZIP_PATH,$lglvl);

unless ($flag_keepdays == 0)
{
	DELETE_OLD_SAVES($flag_keepdays,$ARCHIVE_PREFIX,$ARCHIVE_SUFFIX,$lglvl);
}

unless ($flag_logkeepdays == 0)
{
	DELETE_OLD_LOGS($flag_logkeepdays,$SCRIPTLOG_DEL_PATH,$lglvl);
}
	
LOG_MSG($lglvl,5,"GENERAL","Finishing Script $0");
close LOG;


### sub routines ###

# subroutine DO_RAIDCOM can be found in module HDSPERF_RAIDCOM.pm

sub DELETE_OLD_LOGS
{
	my $par_logkeepdays = $_[0];
	my $par_SCRIPTLOG_DEL_PATH = $_[1];
	my $par_lglvl = $_[2];
	
	my $now = time();       # get current time
	my $AGE = 60*60*24*$par_logkeepdays;  # convert $par_logkeepdays days into seconds
	
	LOG_MSG($par_lglvl,5,"DELOLDLOGS","Start deleting log files older than $par_logkeepdays days");
	
	my @fileliste_logs = <${par_SCRIPTLOG_DEL_PATH}>;
	
	foreach my $filename (@fileliste_logs) 
	{
		# If modified time of the file is older then $AGE = days in seconds
		if ($now-(stat $filename)[9] > $AGE)
		{
			LOG_MSG($par_lglvl,5,"DELOLDLOGS","Found the following log file to delete: $filename");
			unlink $filename or warn LOG_MSG($par_lglvl,4,"DELOLDLOGS","Could not delete $filename: $!");
			LOG_MSG($par_lglvl,5,"DELOLDLOGS","Deleted $filename");
		}
	}
	
	LOG_MSG($par_lglvl,5,"DELOLDLOGS","Deleting log files finished");

}

sub DELETE_OLD_SAVES
{
	my $par_keepdays = $_[0];
	my $par_ARCHIVE_PREFIX = $_[1];
	my $par_ARCHIVE_SUFFIX = $_[2];
	my $par_lglvl = $_[3];

	my $now = time();       # get current time
	my $AGE = 60*60*24*$par_keepdays;  # convert $par_keepdays days into seconds

	LOG_MSG($par_lglvl,5,"DELOLD","Start deleting files older than $par_keepdays days");

	my @fileliste = <${par_ARCHIVE_PREFIX}*${par_ARCHIVE_SUFFIX}>;
	foreach my $filename (@fileliste) 
	{
		# If modified time of the file is older then $AGE = days in seconds
		if ($now-(stat $filename)[9] > $AGE)
		{
			LOG_MSG($par_lglvl,5,"DELOLD","Found the following file to delete: $filename");
			unlink $filename or warn LOG_MSG($par_lglvl,4,"DELOLD","Could not delete $filename: $!");
		}
	}
	
	#
	# !!! THIS IS RECURSIVE FOR SUBDIRS, but so everything gets deleted  !!! I keep it just in case.
	#
	#my @all_archiv_files;
        #find(\&push_archiv_files, ($par_ARCHIVE_DIR));
        #sub push_archiv_files
        #{
		# Check if $_ is a file. 	
		# Important : Check (-f) $File::Find::name will not work as find() is doing a chdir.
		# $_ and $File::Find::name behave differntly. See http://search.cpan.org/~rjbs/perl-5.18.0/lib/File/Find.pm
		# Therefore check against $_ but save $File::Find::name
	#	if ( -f $_ )
	#	{
        #       		push(@all_archiv_files,$File::Find::name);
	#	}
        #}

	LOG_MSG($par_lglvl,5,"DELOLD","Finished deleting files older than $par_keepdays days");
}

sub GET_OUTPATH_IP_PWD
{
	my $par_serial = $_[0];
	my $par_COMMAND_TXT = $_[1];
	my $par_lglvl = $_[2];
	my $sub_outpath = "n/a";
	my $sub_ip = "n/a";
	my $sub_user = "n/a";
	my $sub_pwd = "n/a";
	my $sub_gum = "n/a";
	
	LOG_MSG($par_lglvl,5,"GETCMDTXT","Open file \"$par_COMMAND_TXT\"");
	open(IN,"< $par_COMMAND_TXT") or die LOG_MSG($par_lglvl,3,"GETCMDTXT"," Couldn't open $par_COMMAND_TXT: $!\n");
	while (my $line = <IN>)
	{
		if ($line =~ m/^outpath\s+\"(.*\w+)\"\s*\;/)
		#if ($line =~ m/^outpath\s+(out${par_serial})\s*\;/)
		{
			$sub_outpath = $1;
			# Unquote \ to make C:\\...\\.... to C:\...\  
			$sub_outpath =~ s/\\\\/\\/g;
		}
		
		# Just in case if outpath gets changes manual
		if ($line =~ m/^outpath\s+(.*\w+)\s*\;/)
		{
			$sub_outpath = $1;
		}
		
		if ($line =~ m/^svpip\s+(\d+.\d+.\d+.\d+)\s*\;/)
		{
			$sub_ip = $1;
		}
		
		#  G800/G600/G400/G200
		if ($line =~ m/^ip\s+(\d+.\d+.\d+.\d+)\s*\;/)
		{
			$sub_ip = $1;
		}
		
		#  G800/G600/G400/G200
		if ($line =~ m/gum\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)\s+/)
		{
			$sub_gum = "$1\;$2";
		}
		
		if ($line =~ m/^login\s+"{0,1}([-\w]+)"{0,1}\s+"{0,1}([-\w]+)"{0,1}\s*\;/)
		{
			$sub_user = $1;
			$sub_pwd = $2;
		}
	}
	LOG_MSG($par_lglvl,5,"GETCMDTXT","Outpath = \"$sub_outpath\"");
	LOG_MSG($par_lglvl,5,"GETCMDTXT","SVPIP = \"$sub_ip\"");
	LOG_MSG($par_lglvl,5,"GETCMDTXT","GUM IPs = \"$sub_gum\"");
	LOG_MSG($par_lglvl,5,"GETCMDTXT","User = \"$sub_user\"");	
	#LOG_MSG($par_lglvl,5,"GETCMDTXT","PWD = \"$sub_pwd\"");	
	LOG_MSG($par_lglvl,5,"GETCMDTXT","PWD = ***");
	LOG_MSG($par_lglvl,5,"GETCMDTXT","Close file \"$par_COMMAND_TXT\"");
	close IN;
	
	if (($sub_user eq "n/a") or ($sub_ip eq "n/a"))
	{
		LOG_MSG($par_lglvl,3,"GETCMDTXT","Could not get user and/or password from $par_COMMAND_TXT");
		LOG_MSG($par_lglvl,3,"GETCMDTXT","Maybe it does not meet the allowed characters which are a-z,A-Z,0-9,-");
		LOG_MSG($par_lglvl,3,"GENERAL","Finishing Script $0");
		close LOG;
		exit;
	}
	
	return ($sub_outpath,$sub_ip,$sub_user,$sub_pwd,$sub_gum);
}

sub GET_SHORTRANGE
{
	my $par_EXPORTTOOL_LOG = $_[0];
	my $par_lglvl = $_[1];
	# Get latest modified log file in the directory. Assuming this is correct one ...
	my @logfile_list = <$par_EXPORTTOOL_LOG/export*.log>;
	@logfile_list = sort { (stat ($a))[9] <=> (stat ($b))[9]  } @logfile_list; # Schwartzian transform to sort old to young
	my $latest_logfile = "$logfile_list[-1]";
	unless ( -f $latest_logfile ) { LOG_MSG($par_lglvl,3,"GETSHORT","No logfile found: \"$latest_logfile\""); exit;}
	
	LOG_MSG($par_lglvl,5,"GETSHORT","Latest logfile: \"$latest_logfile\"");

	# Get shortrange of export tool
	LOG_MSG($par_lglvl,5,"GETSHORT","Open file \"$latest_logfile\" to get shortrange");
	my $shortrange_time;
	open(IN,"< $latest_logfile") or die LOG_MSG($par_lglvl,3,"GETSHORT","Couldn't open $latest_logfile: $!\n");
	while (my $line = <IN>)
	{
		#regex to get shortrange date out of "2013/07/05 11:39:36     shortrange [2013/07/05 12:24:00]:[2013/07/05 12:26:00], count = 2"
		# that it can be tranforded in format VSP_85070_2013-02-11_0259-2013-02-12_0259
		my $regex = '\s+\[(\d{4}/\d{2}/\d{2})\s+(\d{2}:\d{2}):\d{2}\]:\[(\d{4}/\d{2}/\d{2})\s+(\d{2}:\d{2}):';
		#if ($line =~ m#shortrange${regex}#)
		if ($line =~ m#range${regex}#)
		{
			$shortrange_time = "${1}_${2}-${3}_${4}";
			$shortrange_time =~ s/\//-/g;
			$shortrange_time =~ s/://g;
			LOG_MSG($par_lglvl,5,"GETSHORT","Shortrange: \"$shortrange_time\"");
		}
	}
	close IN;
	return $shortrange_time;
}

sub LOG_MSG
{
	my $par_LEVEL = shift (@_);
	my $par_SEVERITY = shift (@_);
	my $par_FUNCTION = shift (@_);
	my @line =  @_;
	my $logdate = strftime("%Y.%m.%d", localtime);
	my $logtime = strftime("%H:%M:%S", localtime);
	
	my $SEV_KEYWORD;
	chomp (@line);
	if ($par_SEVERITY == 3) {$SEV_KEYWORD = "ERROR";}
	if ($par_SEVERITY == 4) {$SEV_KEYWORD = "WARNING";}
	if ($par_SEVERITY == 5) {$SEV_KEYWORD = "NOTICE";}
	if ($par_SEVERITY == 7) {$SEV_KEYWORD = "DEBUG";}
	
	if ($par_LEVEL eq "normal")
	{
		unless ($par_SEVERITY eq "DEBUG")
		{
			print "> $logdate,$logtime,$SEV_KEYWORD,$par_FUNCTION,@line\n";
			print LOG "$logdate,$logtime,$SEV_KEYWORD,$par_FUNCTION,@line\n";
		}
	}
	else
	{
		print "> $logdate,$logtime,$SEV_KEYWORD,$par_FUNCTION,@line\n";
		print LOG "$logdate,$logtime,$SEV_KEYWORD,$par_FUNCTION,@line\n";
	}
}

sub DEL_EXPORTTOOL
{
	my $par_EXPORTTOOL_DEL = $_[0];
	my $par_lglvl = $_[1];
	LOG_MSG($par_lglvl,5,"EXPORTDEL","Starting DEL Exporttool");
	open (CMD, "$par_EXPORTTOOL_DEL 2>&1|") or die LOG_MSG($par_lglvl,3,"EXPORTDEL","Can't run program: $!\n");
	while (my $line = <CMD>)
	{
		LOG_MSG($par_lglvl,5,"EXPORTDEL","\t$line");
	}
	close CMD;
	LOG_MSG($par_lglvl,5,"EXPORTDEL","DEL Exporttool finished");
}

sub RUN_EXPORTTOOL
{
	my $par_EXPORTTOOL_RUN = $_[0];
	my $par_lglvl = $_[1];
	LOG_MSG($par_lglvl,5,"EXPORT","Starting Exporttool");
	open (CMD, "$par_EXPORTTOOL_RUN 2>&1|") or die LOG_MSG($par_lglvl,3,"EXPORT","Can't run program: $!\n");
	while (my $line = <CMD>)
	{
		LOG_MSG($par_lglvl,5,"EXPORT","\t$line");
	}
	close CMD;
	LOG_MSG($par_lglvl,5,"EXPORT","Exporttool finished");
}

sub DO_ARCHIV
{
	my $par_COMMAND_TXT = $_[0];
	my $par_EXPORTTOOL_OUTPATH = $_[1]; 
	my $par_EXPORTTOOL_LOG = $_[2];
	my $par_ARCHIVE_FILE = $_[3];
	my $par_HORCM_DIR = $_[4];
	my $par_ZIP_PATH = $_[5];
	my $par_lglvl = $_[6];

	LOG_MSG($par_lglvl,5,"ARCHIVE","Check outpath \"$par_EXPORTTOOL_OUTPATH\" for archiving");
	# Check if $par_EXPORTTOOL_OUTPATH exists otherwise stop archiving
	my @all_files;
	my @all_files_zip;
	
	if ($flag_raidcom_only)
	{
		# Get all raidcom files to tar it.
		local *push_files = sub { push(@all_files,$File::Find::name); LOG_MSG($par_lglvl,5,"ARCHIVE","ARCHIVING $File::Find::name\n"); }; # nested sub needed as use warnings complains if not
		# this from module file::find
		find(\&push_files, ($par_HORCM_DIR));
				
		@all_files_zip = ($par_HORCM_DIR);
	}
	else
	{
		if ( -d $par_EXPORTTOOL_OUTPATH )
		{
			if ( -d $par_HORCM_DIR)
			{
				# Get all export and log files and command file to tar it.
				local *push_files = sub { push(@all_files,$File::Find::name); LOG_MSG($par_lglvl,5,"ARCHIVE","ARCHIVING $File::Find::name\n"); }; # nested sub needed as use warnings complains if not
				# this from module file::find
				find(\&push_files, ($par_COMMAND_TXT,$par_EXPORTTOOL_OUTPATH,$par_EXPORTTOOL_LOG,$par_HORCM_DIR));
				
				@all_files_zip = ($par_COMMAND_TXT,$par_EXPORTTOOL_OUTPATH,$par_EXPORTTOOL_LOG,$par_HORCM_DIR);  
			}
			else
			{
				# Get all export and log files and command file to tar it.
				local *push_files = sub { push(@all_files,$File::Find::name);LOG_MSG($par_lglvl,5,"ARCHIVE","ARCHIVING $File::Find::name\n"); }; # nested sub needed as use warnings complains if not
				# this from module file::find
				find(\&push_files, ($par_COMMAND_TXT,$par_EXPORTTOOL_OUTPATH,$par_EXPORTTOOL_LOG));
				
				@all_files_zip = ($par_COMMAND_TXT,$par_EXPORTTOOL_OUTPATH,$par_EXPORTTOOL_LOG);  
			}
				
		}
		else
		{
			LOG_MSG($par_lglvl,3,"ARCHIVE","Path: \"$par_EXPORTTOOL_OUTPATH\" does not exist. Nothing to archive. Something is wrong.");
			close LOG;
			exit;
		}
	}
	
	#IMPORTANT: HDSperfmail is checking this line in the log file!
	my $abs_path_archiv_file = File::Spec->rel2abs($par_ARCHIVE_FILE);
	LOG_MSG($par_lglvl,5,"ARCHIVE","Start Archiving to \"$abs_path_archiv_file\"");
	
	if ($flag_zip)
	{
		LOG_MSG($par_lglvl,5,"ARCHIVE","Flag zip found. Using $par_ZIP_PATH");
		foreach my $filetozip (@all_files_zip)
		{
			LOG_MSG($par_lglvl,5,"ARCHIVE","Doing $par_ZIP_PATH a -tzip $par_ARCHIVE_FILE $filetozip");
			open (CMD_ZIP, "$par_ZIP_PATH a -tzip $par_ARCHIVE_FILE $filetozip 2>&1|") or die LOG_MSG($par_lglvl,3,"ARCHIVE"," Can't run program: $!\n");
			while (my $line = <CMD_ZIP>)
			{
				LOG_MSG($par_lglvl,5,"ARCHIVE","$line");
			}
			close CMD_ZIP;
		}
	}

	else 
	{
		my $tar = Archive::Tar->new();
		$tar->add_files(@all_files) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't run program: $!\n");
		# This is needed for older Perl version => otherwise "COMPRESS_GZIP" Bareword warning
		no strict;
		$tar->write( "$ARCHIVE_FILE", COMPRESS_GZIP) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't run program: $!\n");
	}
	
	LOG_MSG($par_lglvl,5,"ARCHIVE","Archiving finished");

	# Delete $par_EXPORTTOOL_OUTPATH & $par_EXPORTTOOL_LOG if archive file exists
	if ( -f $par_ARCHIVE_FILE )
	{
		if ($flag_raidcom_only)
		{
				LOG_MSG($par_lglvl,5,"ARCHIVE","Start deleting directories \"$par_HORCM_DIR\"");
				rmtree ($par_HORCM_DIR) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't delete dirs $par_HORCM_DIR: $!\n");
				LOG_MSG($par_lglvl,5,"ARCHIVE","Deleting directories finished");
		}
		else
		{
			if ( -d $par_HORCM_DIR)
			{
				LOG_MSG($par_lglvl,5,"ARCHIVE","Start deleting directories \"$par_EXPORTTOOL_OUTPATH\"");	
				rmtree ($par_EXPORTTOOL_OUTPATH) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't delete dirs $par_EXPORTTOOL_OUTPATH: $!\n");
				LOG_MSG($par_lglvl,5,"ARCHIVE","Start deleting directories \"$par_EXPORTTOOL_LOG\"");	
				rmtree ($par_EXPORTTOOL_LOG) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't delete dirs $par_EXPORTTOOL_LOG: $!\n");
				LOG_MSG($par_lglvl,5,"ARCHIVE","Start deleting directories \"$par_HORCM_DIR\"");
				rmtree ($par_HORCM_DIR) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't delete dirs $par_HORCM_DIR: $!\n");
				LOG_MSG($par_lglvl,5,"ARCHIVE","Deleting directories finished");
			}
			else
			{
				LOG_MSG($par_lglvl,5,"ARCHIVE","Start deleting directories \"$par_EXPORTTOOL_OUTPATH\"");	
				rmtree ($par_EXPORTTOOL_OUTPATH) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't delete dirs $par_EXPORTTOOL_OUTPATH: $!\n");
				LOG_MSG($par_lglvl,5,"ARCHIVE","Start deleting directories \"$par_EXPORTTOOL_LOG\"");	
				rmtree ($par_EXPORTTOOL_LOG) or die LOG_MSG($par_lglvl,3,"ARCHIVE","Can't delete dirs $par_EXPORTTOOL_LOG: $!\n");
				LOG_MSG($par_lglvl,5,"ARCHIVE","Deleting directories finished");
			}
		}
    }

	else
	{
		LOG_MSG($par_lglvl,3,"ARCHIVE","No archived file found: $par_ARCHIVE_FILE. NOT deleting files");
	}
}

sub USAGE
{
	my ($message)=<<'MESSAGE';

NAME ppcollect.pl

SYNOPSIS
     ppcollect.pl -c <config file>
     ppcollect.pl [-h] [-v]

DESCRIPTION
     This script runs the export tool of the VSP/USPV/HUS-VM/G1000/G800/G600/G400/G200, zip or tgz's the files and moves it to a save directory.
	 It can also collect several configuration information with raidcom and raidscan.

REQUIREMENT
     > Install perl (tested with 5.10 and above)
     > Install java version <TBD> (latest version should work)
     > Define java path in configuration file!! Default is just "java". (Take care that there are no blank in the path especially in windows)
     > Copy export tool java files (according to the Microcode of the VSP/USPV/HUS-VM/G1000/G800/G600/G400/G200) into lib direcory
     > Edit export tool command file
        > IP adress / user / password
        > outpath must be set to: "outpath d:\path\to\scripthome\data\out<Serial>"
        > command file must be named: "<storage system>_command_<Serial>.txt"
        > command file must be located in the same directory as ppcollect.pl
     > Run this script with appropriate privileges! If you are not sure use root or administrator.

OPTIONS

	-c <configuration file>
		Path to configuration file
		Please see the manual (http://loop.hds.com/docs/DOC-45708) for detailed information about configuration settings.

	-h
		Show help message.
			 
	-v
		Displays the version of the script	

FILES
	$SCRIPT_HOME/bin/ppcollect.pl
	$SCRIPT_HOME/lib/modules/* (e.g. HCiHpp_RAIDCOM, Config::Tiny)
	$SCRIPT_HOME/etc/* (e.g. pp_serial.conf, <storage system>_command_<Serial>.txt)
	$SCRIPT_HOME/bin/7-ZipPortable/*

EXAMPLES
	ppcollect.pl -h
	ppcollect.pl -v
	ppcollect.pl -c pp_123456.conf

MESSAGE

	print "Version:${VERSION}\n$message";
}