#!/usr/bin/perl -w
my $OUTFILE = shift;
$MAXSIZE = 100;
$MAXAGE = 100; # in seconds  # need to set up the perl time strctures and log the $age since last write..
$BLOB = "";
my $range = 100;  # 0-99
$last_write_time = time();

while (<>) { # while there's still stuff coming in
	$current_time = time();
	$age = $current_time - $last_write_time;
	if (length($BLOB) > $MAXSIZE || $age > $MAXAGE) {
    	# acquire file lock
		while (file_still_locked($OUTFILE) ) { # maybe vars not passed back and forth correctly..
           # nothing, should keep cycling until it opens.
        }# pseudo code ;)
    	print OUT "$BLOB";  # using flocks.
        print STDERR "!"; # indicates writes.
        close OUT;
        $BLOB = ""; # reset 
        $last_write_time = time(); #reset
    } else {
    	$BLOB .= $_; # just add STDIN to $BLOB
    }
}

sub file_still_locked {
	$waitcnt = 0;
	$OUTFILE = shift;
    $stillclosed = 1;
    while ($stillclosed){
    	$waitcnt++;
    	$stillclosed = 0;
		my $random_amt = rand($range)/100;
		sleep($random_amt);
        open OUT, ">> $OUTFILE" or $stillclosed = 1;
        if (!$stillclosed){ flock(OUT, 2); }
        else {print STDERR "w";} # string of 'wwwwww ' to indicate waits
        if ($waitcnt > 10000) { die "\n\nI've been waiting too long! Dying.....\n\n"}
    }
}
        
        
        
