# Allow the flavor name to be overideden by the user
my $globus_flavor_name = shift;

my $gpath = $ENV{GPT_LOCATION};
if (!defined($gpath))
{
  $gpath = $ENV{GLOBUS_LOCATION};

}
if (!defined($gpath))
{
   die "GPT_LOCATION or GLOBUS_LOCATION needs to be set before running this script"
}

@INC = (@INC, "$gpath/lib/perl");

require Grid::GPT::Setup;
my $metadata = new Grid::GPT::Setup(package_name => "globus_mds_gris_setup");

my $globusdir = $ENV{GLOBUS_LOCATION};

if (! defined $globus_flavor_name) {
  opendir(PKGS, "$globusdir/etc/globus_packages/globus_openldap") ||
    die "$globusdir/etc/globus_packages/globus_openldap could not be accessed\n";
  my @slapdpkgs = grep { m!_pgm(?:_static)?\.gpt! } readdir PKGS;
  closedir PKGS;

  die "ERROR: slapd server has not been installed\n" if ! @slapdpkgs;

  my $myslapdpkg = $slapdpkgs[0];

  ($globus_flavor_name) = $myslapdpkg =~ m!pkg_data_(\w+)_pgm!;

  print "WARNING: multiple slapd servers have been installed.
          Using flavor $globus_flavor_name\n"
    if @slapdpkgs > 1;
}

my $setupdir = "$globusdir/setup/globus/";
my $vardir = "$globusdir/var/";
my $libexecdir = "$globusdir/libexec";

my $result = `$setupdir/setup-mds-gris-scripts --with-flavor=$globus_flavor_name`;

print "\nCreating...\n";
print "	$globusdir/sbin/SXXgris\n";
$result = system("chmod 0755 $setupdir/SXXgris");
$result = system("cp SXXgris $globusdir/sbin");
$result = system("ln -s SXXgris $globusdir/sbin/globus-mds");

print "	$libexecdir/grid-info-script-initializer\n";
$result = system("chmod 0755 $setupdir/grid-info-script-initializer");
$result = system("cp grid-info-script-initializer $libexecdir");

print "	$libexecdir/grid-info-mds-core\n";
$result = system("chmod 0755 $setupdir/grid-info-mds-core");                                 
$result = system("cp grid-info-mds-core $libexecdir");  

print "	$libexecdir/grid-info-common\n";
$result = system("chmod 0755 $setupdir/grid-info-common");                                 
$result = system("cp grid-info-common $libexecdir");  

print "	$libexecdir/grid-info-cpu*\n";
$result = system("chmod 0755 $setupdir/grid-info-cpu*");                                 
$result = system("cp grid-info-cpu* $libexecdir");  
$result = system("rm -f $libexecdir/grid-info-cpu*.in");

print "	$libexecdir/grid-info-fs*\n";
$result = system("chmod 0755 $setupdir/grid-info-fs*");                                 
$result = system("cp grid-info-fs* $libexecdir");  
$result = system("rm -f $libexecdir/grid-info-fs*.in");

print "	$libexecdir/grid-info-mem*\n";
$result = system("chmod 0755 $setupdir/grid-info-mem*");                                 
$result = system("cp grid-info-mem* $libexecdir");  
$result = system("rm -f $libexecdir/grid-info-mem*.in");

print "	$libexecdir/grid-info-net*\n";
$result = system("chmod 0755 $setupdir/grid-info-net*");                                 
$result = system("cp grid-info-net* $libexecdir");  
$result = system("rm -f $libexecdir/grid-info-net*.in");

print "	$libexecdir/grid-info-platform*\n";
$result = system("chmod 0755 $setupdir/grid-info-platform*");                                 
$result = system("cp grid-info-platform* $libexecdir");  
$result = system("rm -f $libexecdir/grid-info-platform*.in");

print "	$libexecdir/grid-info-os*\n";
$result = system("chmod 0755 $setupdir/grid-info-os*");                                 
$result = system("cp grid-info-os* $libexecdir");  
$result = system("rm -f $libexecdir/grid-info-os*.in");

print "	$globusdir/etc/grid-info-resource-ldif.conf\n";
$result = system("chmod 0644 $setupdir/grid-info-resource-ldif.conf");
$result = system("cp grid-info-resource-ldif.conf $globusdir/etc");

print "	$globusdir/etc/grid-info-resource-register.conf\n";
$result = system("chmod 0644 $setupdir/grid-info-resource-register.conf");
$result = system("cp grid-info-resource-register.conf $globusdir/etc");

print "	$globusdir/etc/grid-info-resource.schema\n";                                
$result = system("chmod 0644 $setupdir/grid-info-resource.schema");                 
$result = system("cp grid-info-resource.schema $globusdir/etc"); 

print "	$globusdir/etc/grid.gridftpperf.schema\n";
$result = system("chmod 0644 $setupdir/grid.gridftpperf.schema");
$result = system("cp grid.gridftpperf.schema $globusdir/etc"); 

print "	$globusdir/etc/gridftp-resource.conf\n";
$result = system("chmod 0644 $setupdir/gridftp-resource.conf");
$result = system("cp gridftp-resource.conf $globusdir/etc"); 

print "	$globusdir/etc/gridftp-perf-info\n";
$result = system("chmod 0755 $setupdir/gridftp-perf-info");
$result = system("cp gridftp-perf-info $globusdir/libexec"); 

print "	$globusdir/etc/grid-info-slapd.conf\n";
$result = system("chmod 0644 $setupdir/grid-info-slapd.conf");
$result = system("cp grid-info-slapd.conf $globusdir/etc");

$result = `$setupdir/setup-mds-giis-scripts --with-flavor=$globus_flavor_name`;

print "	$globusdir/etc/grid-info-site-giis.conf\n";
$result = system("chmod 0644 $setupdir/grid-info-site-giis.conf");
$result = system("cp grid-info-site-giis.conf $globusdir/etc");

print "	$globusdir/etc/grid-info-site-policy.conf\n";
$result = system("chmod 0644 $setupdir/grid-info-site-policy.conf");
$result = system("cp grid-info-site-policy.conf $globusdir/etc");

print "	$globusdir/etc/grid-info-server-env.conf\n";
$result = system("chmod 0644 $setupdir/grid-info-server-env.conf");
$result = system("cp grid-info-server-env.conf $globusdir/etc");

print "	$globusdir/etc/grid-info-deployment-comments.conf\n";
$result = system("chmod 0644 $setupdir/grid-info-deployment-comments.conf");
$result = system("cp grid-info-deployment-comments.conf $globusdir/etc");

if (! -d "$vardir")
{
	$result = system("mkdir $vardir");
}

print "Done\n";
$metadata->finish();
