# ---*--- This is a perl file ---*---
BEGIN{
  $curdir=`dirname $0`;
  chomp($curdir);
  $pwd=`pwd`;
  chomp($pwd);
  if($curdir eq "."){ $fulldir="$pwd";}
  elsif ($curdir =~ /^\.\./) {$fulldir="$pwd/$curdir";}
  else{$fulldir=$curdir;}
  push(@INC,"$fulldir");
}


use system;
$systemobj=undef;
$report=undef;
$trace = 0;
if ($ENV{'MWDEBUG'}){ $trace=$ENV{'MWDEBUG'};}
if (grep (/-run/,@ARGV)){
  $systemobj=new EndUserReport();
  $report=new enduserReport();
}
else{
  $systemobj=new System();
  $report=new customerReport();
}
$systemobj->ParseArguments(@ARGV);
$err_flag=$systemobj->GetSMSDefinitions(); #set all database files
if ($err_flag){print STDOUT "The check platform script FAILED\n";exit 1;}
#$systemobj->SetReleaseNumber(); 

$systemobj->CheckToolsPermissions();
if (-f $systemobj->{config} && $systemobj->{enduser}){
    $systemobj->SetRequiredCategories();
    $systemobj->SetReleaseNumber();#get VMW_Version from config.ini and set ReleaseNum
    $systemobj->SetCustomerData();#set logfile, error, warning, success message from the config file
}
$logfile=$systemobj->{logfile};

open (LOGFILE,">$systemobj->{logfile}") || print STDOUT "Cannot open $self->{logfile}: $! \n";
if (! -w LOGFILE){$err_flag=1;}
close(LOGFILE);

if ($systemobj->{doUpdate}){
    $status=$systemobj->updateSms("database"); #pass which key have to be downloaded
    print "\nThe log file $systemobj->{logfile} has been created.\n\n";
    $systemobj->DoExit($status);
}

if (!$systemobj->{CheckOSOnly}){
  $systemobj->printHeader();
}


$err=$systemobj->find_platform($systemobj->{SystemDBfile},1); #work on SysID
if ($err != 0){
  $systemobj->DoExit(100);
}    
#print "SysId=$systemobj->{SysID}\n";

$systemobj->SetComponents(); #split all found conponents to OS, Hardware and Software components 

if (!$systemobj->{CheckOSOnly}){
  $systemobj->PlatformReport();
  if (ref($::report) eq "customerReport"){
    $systemobj->printf2("="x (78)."\n");
    $systemobj->printf2("\n"." "x ((70-length("Part II: Requirements Analysis"))/2)."%s","Part II: Requirements Analysis\n\n");
  }
}
$err=$systemobj->ExtractRequirements();
$err=$systemobj->find_platform($systemobj->{PatchDBfile},0); #not work on SysID

$err=$systemobj->ExtractSolution();
if ($systemobj->{dryrun}){exit 0;}
#if flag CheckOSOnly set program will exit in MatchRequirements with appropreate status
$err=$systemobj->MatchRequirements();

if (ref($systemobj) eq "EndUserReport"){

  # if not all requirements are OK, status != 0, return 1
  if ($systemobj->{errors}){
    #print "\n$systemobj->{FailureMessage}\n\n";
    $systemobj->DoExit(-1);
  }
  elsif($systemobj->{warnings}){
    #print "\n$systemobj->{WarningMessage}\n";
    $systemobj->DoExit(1);
  }
  else{
#     print "\n$systemobj->{SuccessMessage}\n";
    $systemobj->DoExit(0)
  }
}

$systemobj->printf2("\n"."="x (78)."\n");
$systemobj->printf2("\n"." "x ((70-length("Part III: Summary"))/2)."%s","Part III: Summary\n\n");

$systemobj->SummaryReport();
$systemobj->printf2("="x (78)."\n");
print "\nThe log file $systemobj->{logfile} has been created.\n\n";
$systemobj->DoExit(0);
