use Grid::GPT::Setup;
use Globus::Core::Paths;
use Getopt::Long;

# default values of config parameters
my $mejs_service_name           = 'ManagedExecutableJobService';
my $mmjs_service_name           = 'ManagedMultiJobService';
my $gt4_location                = $ENV{GLOBUS_LOCATION};
my $package_name                = '';
my $condor_arch                 = 'INTEL';
my $condor_os                   = 'LINUX';
my $x509_cert_dir               = '/etc/grid-security/certificates';
my $tcp_port_range              = '';
my $staging_protocol            = '';
my $staging_host                = '';
my $staging_port                = '';
my $staging_service_path        = '';
my $staging_factory_name        = '';
my $staging_delegation_factory_name = '';
my $staging_subject             = '';
my $run_queue_thread_count      = '18';
my $max_unsubmitted_job_count   = '-1';
my $max_pending_request_count   = '-1';
my $max_submission_retry_count  = '-1';
my $submission_retry_delay      = '-1';
my ($globus_host_cputype, $globus_host_manufacturer, $globus_host_osname,
    $globus_host_osversion) = &get_system_info();
chomp($staging_host);

GetOptions('executable-job-service-name|e=s' => \$mejs_service_name,
           'multi-job-service-name|M=s' => \$mmjs_service_name,
           'globus-location|g=s' => \$gt4_location,
           'cputype|c=s' => \$globus_host_cputype,
           'manufacturer|m=s' => \$globus_host_manufacturer,
           'os-name|o=s' => \$globus_host_osname,
           'os-version|v=s' => \$globus_host_osversion,
           'package-name|p=s' => \$package_name,
           'condor-arch|a=s' => \$condor_arch,
           'condor-os|O=s' => \$condor_os,
           'x509-cert-dir|x=s' => \$x509_cert_dir,
           'tcp-port-range|t=s' => \$tcp_port_range,
           'staging-protocol=s' => \$staging_protocol,
           'staging-host=s' => \$staging_host,
           'staging-port=s' => \$staging_port,
           'staging-service-path=s' => \$staging_service_path,
           'staging-factory-name=s' => \$staging_factory_name,
           'staging-delegation-factory-name=s'
                => \$staging_delegation_factory_name,
           'staging-subject=s' => \$staging_subject,
           'run-queue-thread-count=s' => \$run_queue_thread_count,
           'max-unsubmitted-job-count=s' => \$max_unsubmitted_job_count,
           'max-pending-request-count=s' => \$max_pending_request_count,
           'max-submission-retry-count=s' => \$max_submission_retry_count,
           'submission-retry-delay=s' => \$submission_retry_delay,
           'help|h' => \$help)
    or die "Bad command-line options...aborting";

&usage if ($help || $package_name eq '');

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

my $globusdir	= $ENV{GLOBUS_LOCATION};

if ($ENV{GLOBUS_SH_SUDO} =~ /^$/)
{
    print STDERR "ERROR: GRAM pre-requisite check failed!\n"
               . "GLOBUS_SH_SUDO is not defined in $globusdir/libexec/globus-sh-tools-vars.sh.\n";
    exit 2;
}

my @cmd = ('ant', '-f', "$ENV{GLOBUS_LOCATION}/setup/globus/build-gram-service-gar.xml",
   "-Dmejs.service.name=$mejs_service_name",
   "-Dmmjs.service.name=$mmjs_service_name",
   "-Dmjfs.host.cputype=$globus_host_cputype",
   "-Dmjfs.host.manufacturer=$globus_host_manufacturer",
   "-Dmjfs.host.osname=$globus_host_osname",
   "-Dmjfs.host.osversion=$globus_host_osversion",
   "-Denv.GLOBUS_LOCATION=$ENV{GLOBUS_LOCATION}",
   "-Dmjfs.condor.arch=$condor_arch",
   "-Dmjfs.condor.os=$condor_os",
   "-Dx509.cert.dir=$x509_cert_dir",
   "-Dmjfs.tcp.port.range=$tcp_port_range",
   "-Dmjfs.staging.protocol=$staging_protocol",
   "-Dmjfs.staging.host=$staging_host",
   "-Dmjfs.staging.port=$staging_port",
   "-Dmjfs.staging.service.path=$staging_service_path",
   "-Dmjfs.staging.factory.name=$staging_factory_name",
   "-Dmjfs.staging.delegation.factory.name=$staging_delegation_factory_name",
   "-Dmjfs.staging.subject=$staging_subject",
   "-Dmjfs.run.queue.thread.count=$run_queue_thread_count",
   "-Dmjfs.max.unsubmitted.job.count=$max_unsubmitted_job_count",
   "-Dmjfs.max.pending.request.count=$max_pending_request_count",
   "-Dmjfs.max.submission.retry.count=$max_submission_retry_count",
   "-Dmjfs.submission.retry.delay=$submission_retry_delay",
   'deploy');

$rc = system(@cmd);
if($? == 0)
{
    $metadata->finish();
}
else
{
    print STDERR "Error creating GRAM configuration.\n";
}

sub usage
{
    print "Usage: $0 [options] (package-name|p) <package>\n".
	  "           $0 --help|-help|-h\n".
      "With options (all taking a parameter):\n".
      "\t-executable-job-service-name, -e       name of MJS\n".
      "\t-multi-job-service-name, -M            name of MMJS\n".
      "\t-globus-location, -g                   location of Globus installation\n".
      "\t-cputype, -c\n".
      "\t-manufacturer, -m\n".
      "\t-os-name, -o\n".
      "\t-os-version, -v\n".
      "\t-condor-arch, -a\n".
      "\t-condor-os, -O\n".
      "\t-x509-cert-dir, -x\n".
      "\t-tcp-port-range\n".
      "\t-staging-protocol                      protocol of the container\n".
      "\t                                       for all staging services\n".
      "\t-staging-host                          host of the container\n".
      "\t                                       for all staging services\n".
      "\t-staging-port                          port of the container\n".
      "\t                                       for all staging services\n".
      "\t-staging-service-path                  address path for all staging\n".
      "\t                                       staging services\n".
      "\t-staging-factory-name                  RFT factory service name\n".
      "\t-staging-delegation-factory-name       name of the delegation\n".
      "\t                                       factory service for RFT\n".
      "\t-staging-subject\n".
      "\t-run-queue-thread-count                NOT IMPLEMENTED\n".
      "\t-max-unsubmitted-job-count             NOT IMPLEMENTED\n".
      "\t-max-pending-request-count             NOT IMPLEMENTED\n".
      "\t-max-submission-retry-count            NOT IMPLEMENTED\n".
      "\t-submission-retry-delay                NOT IMPLEMENTED\n";
    exit 1;
}

sub get_system_info
{
    my ($cpu, $manufacturer, $os_name, $os_version);

    print "Determining system information...\n";
    ($cpu, $manufacturer) = (split(/-/, `${sbindir}/config.guess`))[0,1];
    $uname_cmd = &lookup_shell_command("GLOBUS_SH_UNAME");

    chomp($os_name=`$uname_cmd -s`);
    $os_version="";

    if($os_name eq "AIX")
    {
        chomp($os_version = `$uname_cmd -v`);
        $os_version .= ".";
    }

    chomp($os_version .= `$uname_cmd -r`);

    return ($cpu, $manufacturer, $os_name, $os_version);
}

sub lookup_shell_command
{
    my ($cmdvar, $cmd);
    $cmdvar = $_[0];

    chomp($cmd = `$bindir/globus-sh-exec -e echo \\\$$cmdvar`);

    return $cmd;
}

