#! /usr/bin/perl

BEGIN
{
    push(@INC, "/usr/local/globus-3.2.1/lib/perl");
    $ENV{GLOBUS_LOCATION} = "/usr/local/globus-3.2.1" if(!exists($ENV{GLOBUS_LOCATION}));
}

use Getopt::Long;
use IO::File;
use File::Copy;

my $servicename = 'jobmanager';
my $method = 'fork';
my $add = 0;
my $remove = 0;
my $help = 0;
my $gpath;

my $globusdir	= $ENV{GLOBUS_LOCATION};
my $libexecdir	= "$globusdir/libexec";
my $sysconfdir	= "$globusdir/etc";
my $bindir	= "$globusdir/bin";
my $servicesdir	= "$sysconfdir/grid-services";
my $setupdir    = "$globusdir/setup/globus";
my $ldif_conf   = "$sysconfdir/grid-info-resource-ldif.conf";
my $tmpfile     = "$sysconfdir/globus-gram-mds-provider.tmp";
my $slapd_conf  = "$sysconfdir/grid-info-slapd.conf";
my $gram_reporter_schema     = "grid-info-gram-reporter.schema";
my $gram_reporter_rsl_schema = "grid-info-gram-rsl-reporter.schema";

GetOptions('service-name|s=s' => \$servicename,
           'manager-type|m=s' => \$manager_type,
	   'add' => \$add,
	   'remove' => \$remove,
	   'help|h' => \$help);

&usage() if $help;

if(! &valid_manager_type($manager_type))
{
    print STDERR "Invalid job manager type $manager type.\n";
    exit 2;
}

if ( $remove && $add )
{
    print STDERR "Cannot have add and remove in the same command.\n";
    exit 2;
}

if ( !$remove && !$add )
{
    print STDERR "Either -add or -remove are required.\n";
    exit 2;
}

if ( $servicename eq 0 )
{
    print STDERR "-service-name is required.\n";
    exit 2;
}

if ( $manager_type eq 0 )
{
    print STDERR "-manager-type is required.\n";
    exit 2;
}

# make sure the required files are there.
if ( ! -f "$slapd_conf" )
{
   print STDERR "$slapd_conf file not found.\n";
   exit 2;
}

if ( ! -f "$ldif_conf" )
{
   print STDERR "$ldif_conf file not found.\n";
   exit 2;
}

my $hostname = `${bindir}/globus-hostname`;
chomp($hostname);

system("grep \"BEGIN $manager_type\" $ldif_conf >/dev/null");
if ( ! $? )
{
   # entry was found in file
   if ( $add )
   {
       print "entries for $manager_type $servicename already added to $ldif_conf.  Skipping...\n";
   }
   else
   {
       print "removing $manager_type $servicename entries from $ldif_conf\n";
       open(TMPFILE, ">$tmpfile");
       open(FILE, "<$ldif_conf");
       # read in file contents
       $file_data = join('',<FILE>);
       # remove lines starting with BEGIN... until END is found.
       $file_data =~ s/# BEGIN $manager_type $servicename.*# END $manager_type $servicename[^\n]*\n//ms;
       print TMPFILE "$file_data";
       close FILE;
       close TMPFILE;
       system("mv $tmpfile $ldif_conf");
   }
}
else
{
   # entry was not found in file

   if ( $add )
   {
       print "appending $manager_type $servicename reporter entries to $ldif_conf\n";
       open(LDIFFILE, ">>$ldif_conf");

       print LDIFFILE "\n# BEGIN $manager_type $servicename - the following lines added by globus-gram-mds-provider \n";
       print LDIFFILE "# generate globus-gram-reporter $manager_type info every 30 seconds\n";
       print LDIFFILE "dn: Mds-Software-deployment=$servicename, Mds-Host-hn=${hostname}, Mds-Vo-name=local, o=grid\n";
       print LDIFFILE "objectclass: GlobusTop\n";
       print LDIFFILE "objectclass: GlobusActiveObject\n";
       print LDIFFILE "objectclass: GlobusActiveSearch\n";
       print LDIFFILE "type: exec\n";
       print LDIFFILE "path: $globusdir/libexec\n";
       print LDIFFILE "base: globus-gram-reporter\n";
       print LDIFFILE "args: -conf $globusdir/etc/globus-job-manager.conf -type $manager_type -rdn $servicename -dmdn Mds-Host-hn=${hostname},Mds-Vo-name=local,o=grid\n";
       print LDIFFILE "cachetime: 30\n";
       print LDIFFILE "timelimit: 20\n";
       print LDIFFILE "sizelimit: 100\n\n";

       print LDIFFILE "# generate globus-gram-rsl-reporter $manager_type info every 28800 seconds\n";
       print LDIFFILE "dn: Mds-Software-deployment=$servicename, Mds-Host-hn=${hostname}, Mds-Vo-name=local, o=grid\n";
       print LDIFFILE "objectclass: GlobusTop\n";
       print LDIFFILE "objectclass: GlobusActiveObject\n";
       print LDIFFILE "objectclass: GlobusActiveSearch\n";
       print LDIFFILE "type: exec\n";
       print LDIFFILE "path: $globusdir/libexec\n";
       print LDIFFILE "base: globus-gram-rsl-reporter\n";
       print LDIFFILE "args: -gl $globusdir -type $manager_type -dn Mds-Software-deployment=$servicename,Mds-Host-hn=${hostname},Mds-Vo-name=local,o=grid\n";
       print LDIFFILE "cachetime: 28800\n";
       print LDIFFILE "timelimit: 20\n";
       print LDIFFILE "sizelimit: 100\n";
       print LDIFFILE "# END $manager_type $servicename\n";
       close(LDIFFILE);
   }
}


system("grep $gram_reporter_schema $slapd_conf >/dev/null");
if ( ! $? )
{
   # entry was found in file
   if ( $add )
   {
       print "gram reporter schema entries are already in $slapd_conf.  skipping...\n";
   }
   else
   {
       #check to see if there is another reporter entry in the ldif file
       #if so then we don't want to remove the schema includes.
       system("grep \"the following lines added by globus-gram-mds-provider\" $ldif_conf >/dev/null");
       if ( $? )
       {
          print "removing gram reporter schema entries from $slapd_conf\n";
          open(TMPFILE, ">$tmpfile");
          open(FILE, "<$slapd_conf");
          while($line = <FILE>)
          {
              # remove include lines for the 2 gram schemas.
              if(($line !~ "include.*$gram_reporter_schema") &&
                 ($line !~ "include.*$gram_reporter_rsl_schema"))
              {
                  print TMPFILE $line;
              }
          }
          close FILE;
          close TMPFILE;
          system("mv $tmpfile $slapd_conf");
       }
   }
}
else
{
   # entry was not found in file
   if ( $add )
   {
      # insert line in the slapd_conf file

      open(INFO, "<$slapd_conf");
      open(TMPFILE, ">$tmpfile");

      while ($line = <INFO>)
      {
        chomp($line);
        $line =~ s/^\s+//g;  # remove leading whitespace

        $first7 = substr($line,0,7);

        if ($line_added==0)
        {
           if ($first7 eq "include")
           {
              $includes_started=1;
           }

           # find the first line after the include lines that is not just a newline
           if (($first7 ne "include") &&
               ($first7 ne "") &&
               ($includes_started==1))
           {
              $line_added=1;
              print "adding gram reporter entry in $slapd_conf\n";
              print TMPFILE "include  ${globusdir}/etc/grid-info-gram-reporter.schema\n";
              print TMPFILE "include  ${globusdir}/etc/grid-info-gram-rsl-reporter.schema\n";
              print TMPFILE "\n";
           }
        }

        print TMPFILE "$line\n";
      }
      close(TMPFILE);
      close(INFO);

      system("mv $tmpfile $slapd_conf");
   }
   else
   {
       print "gram reporter schema entries not found for removal in $slapd_conf, skipping...\n";
   }
}


print "Done\n";

sub usage
{
    print "Usage: $0 [-service-name|-s service_name]\n".
          "          [-manager-type|-m manager_type]\n".
          "          [-add ]\n".
          "          [-remove ]\n".
          "          [-help]\n";
    exit 1;
}

sub valid_manager_type
{
    my $name = $_[0];

    if("${setupdir}/${name}.pm")
    {
	return 1;
    }
    else 
    {
	return 0;
    }
}
