#!/usr/bin/perl
#
# This script reads data from its standard input and writes it
# to a another process. Whenever a certain amount of data has been
# processed, the output stream is closed and a new child is spawned
# that now processes the data.
# That way it is possible to split a single tar file through several
# FTP uploads.
 
# Last change: 2006-10-21 Stefan Tomanek <stefan@pico.ruhr.de>
#                          http://stefans.datenbruch.de/rootserver/

$0 = "pipesplit";

# chunk size limit
$limit=2*1024*1024*1024;

# !! $s << $limit !!

# bytes to read
$s=1024;

binmode STDIN;
$|=1;

# The command the data should be piped to
# The expression '{}' is replaced by the current
# chunk number
$command = $ARGV[0];

$count=0;
$chunk=0;

sub openOutput() {
    close(OUTPUT);
    $cmd = $command;
    # To simplify sorting, we need some leading 0s here
    my $num = sprintf("%04d", $chunk);
    $cmd =~ s/{}/$num/g;
    open(OUTPUT, "| $cmd");
    binmode OUTPUT;
}

openOutput();

while ($n = read(STDIN, $input, $s)) {
    if ($count + $n > $limit) {
	print STDERR "Size limit reached, splitting the ouput (completed chunk $chunk).\n";
	# increment chunk number
	$chunk++;
	# reset counter
	$count = 0;

	# reopen OUTPUT
	openOutput()
    }
    $count += $s;
    print OUTPUT $input;
}
print STDERR "-> Data has been split into ".($chunk+1)." chunks.\n";

close(OUTPUT);
close(STDIN);

