#!/usr/bin/perl

use warnings;
use strict;

use Date::Manip;

$\ = $/; # Put newlines at the end of print



our $real_gratia_probe_type = 'pbs-lsf';

sub appendDetailsData {
  print "@_";
}

sub probeDirPath {
  if($ENV{OSG_LOCATION}) {
    return ($real_gratia_probe_type) ? "$ENV{OSG_LOCATION}/gratia/probe/$real_gratia_probe_type" : "<unknown>";
  }
  return ($real_gratia_probe_type) ? "/usr/share/gratia/$real_gratia_probe_type" : "<unknown>";
}

sub probeConfigPath {
  if($ENV{OSG_LOCATION}) {
    return ($real_gratia_probe_type) ? "$ENV{OSG_LOCATION}/gratia/probe/$real_gratia_probe_type" : "<unknown>";
  }
  return ($real_gratia_probe_type) ? "/etc/gratia/$real_gratia_probe_type" : "<unknown>";
}

sub getProbeConfigAttribute {
  my $probeConfigPath = probeConfigPath();
  my $probeConfig_cmd = $ENV{OSG_LOCATION} ? 
      "( source $ENV{OSG_LOCATION}/setup.sh; cd \"$probeConfigPath\"; ../common/GetProbeConfigAttribute.py %s )" : 
      "( cd \"$probeConfigPath\"; /usr/share/gratia/common/GetProbeConfigAttribute %s )";

  my @results = ();

  foreach my $configAttribute (@_) {
    my $cmd = sprintf($probeConfig_cmd, $configAttribute);
    push @results, `$cmd 2>/dev/null`;
  }

  chomp @results;
  if (wantarray) {
    return @results;
  } else {
    return $results[0];
  }
}

our $soaphost;
our $workingfolder;

sub probe_check_pbs_lsf {
  my $URCOLLECTOR_LOC = probeDirPath();
  push @INC, $URCOLLECTOR_LOC;
  eval "require urCollector::Configuration";
  if ($@) {                     # FAILED to load configuration module
    print STDOUT (<<EOF);
ERROR: Could not load urCollector::Configuration: $@
Major probe installation problem.
EOF
    return 1;
  }
  eval
    urCollector::Configuration::parseConf("$URCOLLECTOR_LOC/urCollector.conf");
  if ($@) {                     # FAILED to parse configuration
    print STDOUT (<<EOF);
ERROR: Could not read $URCOLLECTOR_LOC/urCollector.conf: $@
Please check urCollector.conf file for errors.
EOF
    return 1;
  }
  # Check LRMS type setting
  if ($urCollector::Configuration::configValues{lrmsType} !~ m&(?:pbs|lsf)&i) {
    print STDOUT (<<EOF);
ERROR: Unrecognized lrmsType $urCollector::Configuration::configValues{lrmsType} in urCollector.conf
Please check urCollector.conf file for errors.
EOF
    return 1;
  }

#Remaining checks on $urCollector::Configuration::configValues{lrmsType} require root privilege:
#  * Check existence of configured PBS or LSF log directory.
#  * Check for recent PBS batch log entries.
#  * Check pending XML files in $workingfolder/urCollector.

  # Check log directory
  if ($urCollector::Configuration::configValues{lrmsType} eq "pbs") {
    my $dir = $urCollector::Configuration::configValues{pbsAcctLogDir};
    my $date = UnixDate("today", "%Y%m%d");
    if (not -d $dir) {
      print STDOUT (<<EOF);
ERROR: Configured PBS log directory $dir does not exist.
Check pbsAcctLogDir in urCollector.conf.
EOF
    } else {
      my $nfiles = `ls -1 $dir | wc -l`;
      chomp $nfiles;
      if (!$nfiles) {
        print STDOUT (<<EOF);
ERROR: No files found in configured PBS log directory $dir.
Check pbsAcctLogDir in urCollector.conf.
EOF
      } elsif ((not -e "$dir/$date") and (not -e "$dir/$date.gz")) {
        print STDOUT (<<EOF);
WARNING: Configured PBS log directory $dir does not contain entries for today.
Check pbsAcctLogDir in urCollector.conf and contents of $dir.
EOF
      }
    }
  } elsif ($urCollector::Configuration::configValues{lrmsType} eq "lsf") {
    my $dir = $urCollector::Configuration::configValues{lsfAcctLogDir};
    if (not -d $dir) {
      print STDOUT (<<EOF);
ERROR: Configured LSF log directory $dir does not exist.
Check lsfAcctLogDir in urCollector.conf.
EOF
    } elsif (not -e "$dir/lsb.events") {
      print STDOUT (<<EOF);
WARNING: No log file $dir/lsb.events.
Check lsfAcctLogDir in urCollector.conf and contents of $dir.
EOF
    }
    # Check PBS files waiting to be converted to Gratia files
    my $npending = `(cd "$workingfolder/urCollector" >/dev/null 2>&1 && ls -1 | wc -l)`;
    chomp $npending;
    print STDOUT ("$npending XML files waiting to be sent to $soaphost\n");
  }
}

sub main {
  if ($< != 0) {
    print "Not root - exiting";
    exit 1;
  }

  our $soaphost = getProbeConfigAttribute("SOAPHost");
  if (!defined($soaphost) || length($soaphost) == 0) {
    $soaphost = getProbeConfigAttribute("CollectorHost");
  }

  our $workingfolder = getProbeConfigAttribute("WorkingFolder");

  return probe_check_pbs_lsf();
}

exit main();

# vim:ft=perl:sw=2:sts=2
