#!/usr/bin/env perl

#
# This code is a working example of how to write an HTCondor file-transfer
# plugin.  Most Linux systems will be able to use it unmodified.
#

#
# You must enable file transfer plugins, and add this plugin to the list,
# in order to use it.  An example of such a configuration, which also
# includes the default curl (file://, http://, and ftp://) and data
# (data://) plugins, follows.
#
# ENABLE_URL_TRANSFERS = TRUE
# FILETRANSFER_PLUGINS = $(LIBEXEC)/curl_plugin, \
#                        $(LIBEXEC)/data_plugin, \
#                        $(LIBEXEC)/decompress_plugin
#

use strict;
use warnings;

#
# HTCondor determines which plugin handles which URL by running each plugin
# with the '-classad' argument once on start-up, and parsing the result.
#
# Because this script requires 'curl' and 'gunzip' to be in its PATH, it
# checks for them before claiming it can do anything.
#
if( $ARGV[0] eq "-classad" ) {
	my $command = "curl --version 1>/dev/null 2>/dev/null";
	if( system( $command ) != 0 ) { exit 1; }

	$command = "gunzip --version 1>/dev/null 2>/dev/null";
	if( system( $command ) != 0 ) { exit 1; }

	print( 'PluginVersion = "0.1"' . "\n" );
	print( 'PluginType = "FileTransfer"' . "\n" );
	print( 'SupportedMethods = "httpz,ftpz,filez"' . "\n" );
	exit( 0 );
}

my( $source, $destination ) = @ARGV;
$source =~ s!httpz:!http:!;
$source =~ s!ftpz:!ftp:!;
$source =~ s!filez:!file:!;

my $command = "curl $source -o $destination";
if( system( $command ) != 0 ) { exit( 1 ); }
if( ! -e $destination ) { exit( 1 ); }

$command = "gunzip $destination";
if( system( $command) != 0 ) { exit( 1 ); }

exit( 0 );
