#!/usr/bin/env perl

use strict;
use warnings;

our $VERSION = '0.02';

=head1 DESCRIPTION

This script simulates a small part of the linux C<cat> command.
It's a small utility that helps reading / writing round-robin text files.

=cut

use File::RoundRobin;

if ( $ARGV[0] =~ /^(\d+(?:Kb?|Mb?|Gb?)?)$/ && defined $ARGV[1] ) {
	my $rrfile = File::RoundRobin->new(path => $ARGV[1], size => $ARGV[0]);

	while ( my $buffer = <STDIN> ) {
		$rrfile->write($buffer);
	}
	
	$rrfile->close();
}
elsif (-f $ARGV[0]) {
	my $rrfile = File::RoundRobin->new(path => $ARGV[0], mode => 'read');
	

	while ( my $buffer = $rrfile->read(10000) ) {
		print $buffer;
	}
	$rrfile->close();
}
else {
	print <<EOF;
Usage : 
To print the content of a file :
	$0 <filename> 

To write into a file :
	$0 <size> <filename>

Size can be in the fallowing forms :
	1. number (bytes)
	2. <number>Kb (kilobytes)
	3. <number>Mb (megabytes)
	4. <number>Gb (gigabytes)
	
EOF
}

1;