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

my $hostname = `$ENV{GLOBUS_LOCATION}/bin/globus-hostname`;
chomp($hostname);
my $package_name = "globus_wsrf_rft_service_java_setup";

# default values of config parameters
my $back_off            = '10000';
my $max_active_allowed  = '100';
my $driver_name         = 'org.postgresql.Driver';
my $connection_string   = 'jdbc:postgresql://' . $hostname . '/rftDatabase';
my $user_name           = $ENV{'USER'};
my $password            = 'foo';
my $max_active          = '20';
my $max_idle            = '10';
my $max_wait            = '-1';
my $sweeper_delay       = '60000';

GetOptions('back-off|b=s'           => \$back_off,
           'max-active-allowed|A=s' => \$max_active_allowed,
           'driver-name|d=s'        => \$driver_name,
           'connection-string|c=s'  => \$connection_string,
           'user-name|u=s'          => \$user_name,
           'password|p=s'           => \$password,
           'max-active|a=s'         => \$max_active,
           'max-idle|i=s'           => \$max_idle,
           'max-wait|w=s'           => \$max_wait,
           'sweeper-delay|s=s'      => \$sweeper_delay,
           'help|h'                 => \$help);

&usage if ($help);

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

my $globusdir = $ENV{GLOBUS_LOCATION};

my @cmd = ('ant', '-f', "$ENV{GLOBUS_LOCATION}/setup/globus/build-rft-setup.xml",
   "-Dback.off=$back_off",
   "-Dmax.active.allowed=$max_active_allowed",
   "-Ddriver.name=$driver_name",
   "-Dconnection.string=$connection_string",
   "-Duser.name=$user_name",
   "-Dpassword=$password",
   "-Dmax.active=$max_active",
   "-Dmax.idle=$max_idle",
   "-Dmax.wait=$max_wait",
   "-Dsweeper.delay=$sweeper_delay",
   'deploy');

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

sub usage
{
    print "Usage: $0 [options]\n".
          "       $0 --help|-help|-h\n".
      "With options (all taking a parameter):\n".
      "\t-back-off, -b                      back off\n".
      "\t-max-active-allowed, -A            max active DB conncetions allowed\n".
      "\t-driver-name, -d                   DB driver name\n".
      "\t-connection-string, -c             DB connection string\n".
      "\t-user-name, -u                     DB user name\n".
      "\t-password, -p                      DB password\n".
      "\t-max-active, -a                    max active transfers\n".
      "\t-max-idle, -i                      max idle transfers\n".
      "\t-max-wait, -w                      max waiting transfers\n".
      "\t-sweeper-delay, -s                 sweeper delay\n";
    exit 1;
}
