Index: trunk/ippScripts/scripts/receive_file.pl
===================================================================
--- trunk/ippScripts/scripts/receive_file.pl	(revision 23888)
+++ trunk/ippScripts/scripts/receive_file.pl	(revision 23888)
@@ -0,0 +1,168 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "Starting script $0 on $host\n\n";
+
+use DateTime;
+my $mjd_start = DateTime->now->mjd;   # MJD of starting script
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Config 1.01 qw( :standard );
+use Carp;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $receivetool = can_run('receivetool') or (warn "Can't find receivetool" and $missing_tools = 1);
+my $dsproductls = can_run('dsfilesetls') or (warn "Can't find dsfilesetls" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+my $tempdir = "/tmp";
+
+# Parse the command-line arguments
+my ( $file_id, $source, $product, $fileset, $file, $workdir, $dbname, $verbose, $no_update );
+
+GetOptions(
+           'file_id=s'         => \$file_id, # File identifier
+           'source=s'          => \$source, # Source for data
+           'product=s'         => \$product, # Product for data
+           'fileset=s'         => \$fileset, # Fileset for data
+           'file=s'            => \$file, # File to retrieve
+           'workdir=s'         => \$workdir, # Working directory for output
+           'dbname=s'          => \$dbname,    # Database name
+           'verbose'           => \$verbose,   # Print to stdout
+           'no-update'         => \$no_update, # Don't update the database?
+           ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --file_id --source --product --fileset --file --workdir",
+           -exitval => $PS_EXIT_CONFIG_ERROR) unless
+    defined $file_id and
+    defined $source and
+    defined $product and
+    defined $fileset and
+    defined $file and
+    defined $workdir;
+
+my $ipprc = PS::IPP::Config->new() or
+    &my_die( "Unable to set up", $file_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
+
+# Retrieve file
+my $filename = "$tempdir/$file";  # Target filename
+{
+    my $uri = "$source/product/$fileset/$file"; # URI for datastore file
+    my $command = "dsget --uri $uri --filename $filename"; # Command to execute
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    die "Unable to retrieve file from $uri\n" unless $success;
+}
+my $mjd_copy = DateTime->now->mjd;   # MJD of finishing copy
+
+# Deal with file
+if ($file =~ m|^dbinfo\.\S+\.mdc$|) {
+    # Load into database
+    my ($stage) = $file =~ m|^dbinfo\.(\S+)\.\d+\.mdc$|; # Stage of interest
+    my $tool = can_run("${stage}tool") or die "Can't find tool to load $file\n";
+
+    my $command = "$tool -importrun -infile $filename"; # Command to execute
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    die "Unable to load $filename\n" unless $success;
+} elsif ($file =~ m|.*\.tgz$|) {
+    # Get contents of tarball
+    my @files = ();
+    {
+        my $command = "tar -C $tempdir -tvzf $filename"; # Command to execute
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        die "Unable to get listing of tar file $filename\n" unless $success;
+
+        foreach my $line (@$stdout_buf) {
+            $line =~ s/\#.*$//;
+            next unless $line =~ /\S+/;
+            my @fields = split(/\s+/, $line); # Fields in line
+            my $file = $fields[0];      # Name of file
+            $file =~ s|^./||;
+            push @files, $file if defined $file;
+        }
+    }
+
+    # Extract files from tarball
+    {
+        my $command = "tar -C $tempdir -xzf $filename"; # Command to execute
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        die "Unable to extract tar file $filename\n" unless $success;
+    }
+
+    # Move files into filesystem of choice
+    foreach my $file ( @files ) {
+        my $from = "$tempdir/$file"; # Source for file
+        my $to = "$workdir/$file"; # Target destination for file
+        $ipprc->file_prepare( $to );
+        system("mv $from $to") == 0 or die "Unable to move $file into workdir $workdir: $!\n";
+    }
+} else {
+    die "Unrecognised file: $file\n";
+}
+
+unlink $filename or die "Unable to unlink $filename\n";
+my $mjd_extract = DateTime->now->mjd;   # MJD of finishing extract
+
+
+# All done
+{
+    my $command = "$receivetool -addresult";
+    $command .= " -file $file_id";
+    $command .= (" -dtime_copy " . (($mjd_copy - $mjd_start) * 86400));
+    $command .= (" -dtime_extract " . (($mjd_extract - $mjd_start) * 86400));
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    unless (defined $no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        die "Unable to add result for $file\n" unless $success;
+    }
+}
+
+# Pau.
+
+
+sub my_die
+{
+    my $msg = shift;            # Exit message
+    my $file_id = shift;        # File identifier
+    my $fault = shift;          # Fault code
+
+    $fault = $PS_EXIT_PROG_ERROR unless defined $fault;
+
+    carp($msg);
+    if (defined $file_id and not $no_update) {
+        my $command = "$receivetool -addresult";
+        $command .= " -file_id $file_id";
+        $command .= " -fault $fault";
+        $command .= " -dbname $dbname" if defined $dbname;
+
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+    }
+    exit $fault;
+}
+
+
+__END__
Index: trunk/ippScripts/scripts/receive_fileset.pl
===================================================================
--- trunk/ippScripts/scripts/receive_fileset.pl	(revision 23888)
+++ trunk/ippScripts/scripts/receive_fileset.pl	(revision 23888)
@@ -0,0 +1,89 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "Starting script $0 on $host\n\n";
+
+use DateTime;
+my $mjd_start = DateTime->now->mjd;   # MJD of starting script
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Config 1.01 qw( :standard );
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $receivetool = can_run('receivetool') or (warn "Can't find receivetool" and $missing_tools = 1);
+my $dsproductls = can_run('dsfilesetls') or (warn "Can't find dsfilesetls" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+# Parse the command-line arguments
+my ( $fileset_id, $source, $product, $fileset, $dbname, $verbose, $no_update );
+
+GetOptions(
+           'fileset_id=s'      => \$fileset_id, # Fileset identifier
+           'source=s'          => \$source, # Source for data
+           'product=s'         => \$product, # Product for data
+           'fileset=s'         => \$fileset, # Fileset for data
+           'dbname=s'          => \$dbname,    # Database name
+           'verbose'           => \$verbose,   # Print to stdout
+           'no-update'         => \$no_update, # Don't update the database?
+    ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --fileset_id --source --product --fileset",
+           -exitval => $PS_EXIT_CONFIG_ERROR) unless
+    defined $fileset_id and
+    defined $source and
+    defined $product and
+    defined $fileset;
+
+# Get list of files
+my @files = ();                 # Files to add
+{
+    my $uri = "$source/product/$fileset"; # URI for datastore fileset
+    $uri .= "/index.txt" unless $uri =~ m|/index.txt$|;
+    my $command = "dsfilesetls --uri $uri"; # Command to execute
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    die "Unable to get fileset listing from $uri\n" unless $success;
+
+    # Get files
+    foreach my $line (@$stdout_buf) {
+        $line =~ s/\#.*$//;
+        next unless $line =~ /\S+/;
+        my @fields = split(/\s+/, $line); # Fields in line
+        my $file = $fields[1];      # Name of file
+        push @files, $file if defined $file;
+    }
+}
+
+# Add files
+if (scalar @files > 0) {
+    my $command = "receivetool -addfile"; # Command to execute
+    $command .= " -fileset_id $fileset_id";
+    $command .= " -file" . join(' -file', @files);
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    unless (defined $no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        die "Unable to add file for fileset $fileset\n" unless $success;
+    }
+}
+
+__END__
Index: trunk/ippScripts/scripts/receive_source.pl
===================================================================
--- trunk/ippScripts/scripts/receive_source.pl	(revision 23888)
+++ trunk/ippScripts/scripts/receive_source.pl	(revision 23888)
@@ -0,0 +1,100 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "Starting script $0 on $host\n\n";
+
+use DateTime;
+my $mjd_start = DateTime->now->mjd;   # MJD of starting script
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Config 1.01 qw( :standard );
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $receivetool = can_run('receivetool') or (warn "Can't find receivetool" and $missing_tools = 1);
+my $dsproductls = can_run('dsproductls') or (warn "Can't find dsproductls" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+# Parse the command-line arguments
+my ( $source_id, $source, $product, $last_fileset, $dbname, $verbose, $no_update );
+
+GetOptions(
+           'source_id=s'       => \$source_id, # Source identifier
+           'source=s'          => \$source, # Source for data
+           'product=s'         => \$product, # Product for data
+           'last_fileset=s'    => \$last_fileset, # Last fileset seen
+           'dbname=s'          => \$dbname,    # Database name
+           'verbose'           => \$verbose,   # Print to stdout
+           'no-update'         => \$no_update, # Don't update the database?
+    ) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --source_id --source --product",
+           -exitval => $PS_EXIT_CONFIG_ERROR) unless
+    defined $source_id and
+    defined $source and
+    defined $product;
+
+# Get list of filesets
+my $uri = "$source/product"; # URI for datastore product
+$uri .= "/index.txt" unless $uri =~ m|/index.txt$|;
+my $command = "dsproductls --uri $uri"; # Command to execute
+$command .= "--last_fileset $last_fileset" if defined $last_fileset;
+
+my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    run(command => $command, verbose => $verbose);
+die "Unable to get source listing from $source for $product\n" unless $success;
+
+# Add filesets
+my $last;                   # Last fileset seen
+foreach my $line (@$stdout_buf) {
+    $line =~ s/\#.*$//;
+    next unless $line =~ /\S+/;
+    my @fields = split(/\s+/, $line); # Fields in line
+    my $fileset = $fields[1];
+    next if not defined $fileset;
+
+    my $command = "receivetool -addfileset"; # Command to execute
+    $command .= " -source_id $source_id";
+    $command .= " -fileset $fileset";
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    unless (defined $no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        die "Unable to add fileset $fileset\n" unless $success;
+        $last = $fileset;
+    }
+}
+
+# Update the last fileset seen
+if (defined $last and (not defined $last_fileset or $last ne $last_fileset)) {
+    my $command = "receivetool -updatelast"; # Command to execute
+    $command .= " -source_id $source_id";
+    $command .= " -fileset $last";
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    unless (defined $no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        die "Unable to update last fileset to $last\n" unless $success;
+    }
+}
+
+
+__END__
