#!/bin/perl -w

use IO::File;

use Hurd::Trivfs;
@ISA = ('Hurd::Trivfs');

use strict;

my (%opt);
%opt = (
       );

use Getopt::Long;
GetOptions (\%opt,
	    'file=s'
	   );

my ($file);
$file = $opt{file};
die "No --file=FILENAME specified\n" unless defined $file;

warn "B1FF Perl translator pid $$ started\n";
__PACKAGE__->start;
warn "B1FF Perl translator pid $$ exiting due to boredom\n";
exit;


# Now the fun begins...

sub goaway {
    warn "B1FF Perl translator pid $$ exiting on goaway request\n";
    exit 0;
}

sub init_peropen {
    my ($self, $po) = @_;
    my ($fh);

    $fh = IO::File->new ($file, $po->openmodes);
    $po->hook ($fh);
}

sub io_read {
    my ($self, $prot, $bufref, $offset, $amount) = @_;
    my ($fh);

    warn "plop\n";
    $fh = $prot->po->hook;
    if ($offset != -1) {
	sysseek ($fh, $offset, 0);
    }

    warn "going to read $amount from $file\n";
    sysread ($fh, $$bufref, $amount);
    warn "got: $$bufref (length : ".length($$bufref).")";
    $$bufref = uc ($$bufref);
}

sub io_seek {
    my ($self, $prot, $offset, $whence) = @_;
    sysseek ($prot->po->hook, $offset, $whence);
}

sub io_write {
    my ($self, $prot, $buffer, $offset, $amount) = @_;
    my ($fh);

    $fh = $prot->po->hook;
    if ($offset != -1) {
	sysseek ($fh, $offset, 0);
    }
    return syswrite ($fh, $buffer, $amount);
}

sub file_set_size {
    my ($self, $prot, $size) = @_;
    truncate ($prot->po->hook, $size);
}
