package ID3FS::Fuse;

use strict;
use warnings;
use POSIX qw(EINVAL ENOENT EROFS EOPNOTSUPP);
use Fuse;

sub new
{
    my $proto=shift;
    my $class=ref($proto) || $proto;
    my $self={};
    bless($self,$class);

    $self->{db}=shift;
    $self->{source}=shift;
    $self->{mountpoint}=shift;

    return $self;
}

sub run
{
    my($self)=@_;
    Fuse::main(
	mountpoint  => $self->{mountpoint},
	threaded=>0,
	debug => 1,
	getattr     => sub { $self->getattr(@_); },
#	readlink    => sub { $self->readlink(@_); },
	getdir      => sub { $self->getdir(@_); },
#	mknod       => sub { $self->mknod(@_); },
#	mkdir       => sub { $self->mkdir(@_); },
#	unlink      => sub { $self->unlink(@_); },
#	rmdir       => sub { $self->rmdir(@_); },
#	symlink     => sub { $self->symlink(@_); },
#	rename      => sub { $self->rename(@_); },
#	link        => sub { $self->link(@_); },
#	chmod       => sub { $self->chmod(@_); },
#	chown       => sub { $self->chown(@_); },
#	truncate    => sub { $self->truncate(@_); },
#	utime       => sub { $self->utime(@_); },
#	open        => sub { $self->open(@_); },
#	read        => sub { $self->read(@_); },
#	write       => sub { $self->write(@_); },
#	statfs      => sub { $self->statfs(@_); },
#	release     => sub { $self->release(@_); },
#	fsync       => sub { $self->fsync(@_); },
#	setxattr    => sub { $self->setxattr(@_); },
#	getxattr    => sub { $self->getxattr(@_); },
#	listxattr   => sub { $self->listxattr(@_); },
#	removexattr => sub { $self->removexattr(@_); },
	);
}

sub getattr
{
    my($filename)=@_;
    print "GETATTR: $filename\n";
    my($dev,$ino,$nlink)=(0,0,1);
    my($uid,$gid)=(1000,1000);
    my($rdev,$size)=(0,1);
    my($atime,$mtime,$ctime)=(0,0,0);
    my($blksize,$blocks)=(512,1);
    my $mode=(0040 << 9) | 0755;
    return($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	   $atime,$mtime,$ctime,$blksize,$blocks);
}

sub readlink    { print "readlink\n"; return "FIXME";       }

sub getdir
{
    my($self, $filename)=@_;
    print "GETDIR(", ref($self), ": $filename\n";
    if($filename eq "/")
    {
	return(".", "..", $self->{db}->tags(), 0);
    }
    return('.',0);
}

sub mknod       { print "mknod\n"; return -EINVAL();     }
sub mkdir       { print "mkdir\n"; return -EROFS();      }
sub unlink      { print "unlink\n"; return -EINVAL();     }
sub rmdir       { print "rmdir\n"; return -EROFS();      }
sub symlink     { print "symlink\n"; return -EROFS();      }
sub rename      { print "rename\n"; return -EROFS();      }
sub link        { print "link\n"; return -EROFS();      }
sub chmod       { print "chmod\n"; return -EINVAL();     }
sub chown       { print "chown\n"; return -EINVAL();     }
sub truncate    { print "truncate\n"; return -EROFS();      }
sub utime       { print "utime\n"; return -EINVAL();     }
sub open        { print "open\n"; return -EINVAL();     }
sub read        { print "read\n"; return -EINVAL();     }
sub write       { print "write\n"; return -EINVAL();     }
sub statfs      { print "statfs\n"; return -EINVAL();     }
sub release     { print "release\n"; return 0;             }
sub fsync       { print "fsync\n"; return 0;             }
sub setxattr    { print "setxattr\n"; return -EOPNOTSUPP(); }
sub getxattr    { print "getxattr\n"; return -EOPNOTSUPP(); }
sub listxattr   { print "listxattr\n"; return -EOPNOTSUPP(); }
sub removexattr { print "removexattr\n"; return -EOPNOTSUPP(); }

1;
