reworked DB::new et al *again*
[id3fs.git] / lib / ID3FS / DB.pm
index 246cd92..6b3cd99 100644 (file)
@@ -19,12 +19,8 @@ sub new
     $self->{me}=shift;
     $self->{verbose}=shift;
     my $init=shift;
-    my $dbpath=shift;
     $self->{base}=shift;
-    my $fallbackdir=shift;
-
-    $self->{dbpath}=$self->find_db($init, $dbpath, $fallbackdir);
-    return undef unless($self->{dbpath});
+    $self->{dbpath}=shift || ($self->{base} . "/" . $dbfile);
     $self->{absbase}=Cwd::abs_path($self->{base});
 
     my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
@@ -50,46 +46,36 @@ sub new
 
 sub find_db
 {
-    my($self, $init, $dbpath, $fallbackdir)=@_;
-    my $file=undef;
+    # class method
+    shift if(ref($_[0]) eq "ID3FS::DB");
+
+    my($me, $init, @dirs)=@_;
     my $base=undef;
-    if(defined($dbpath))
-    {
-       $file=$dbpath;
-    }
-    if(defined ($self->{base}))
-    {
-       $file="$self->{base}/$dbfile" unless defined($file);
-       $base=$self->{base};
-    }
-    elsif(defined($fallbackdir) && -d $fallbackdir)
+    for my $dir (@dirs)
     {
-       my $path=Cwd::abs_path($fallbackdir);
+       my $path=Cwd::abs_path($dir);
        do
        {
-           $file="$path/$dbfile";
            $base=$path;
            $path=~s/(.*)\/.*/$1/;
        }
-       while(! -f $file && length($path) && -d $path);
-       if(! -f $file)
+       while(! -f "$base/$dbfile" && length($path) && -d $path);
+       if(-f "$base/$dbfile")
        {
-           $file="$fallbackdir/$dbfile";
-           $base=$fallbackdir;
+           return $base;
        }
     }
-    else
-    {
-       print "$self->{me}: $fallbackdir: not a directory\n";
-       return undef;
-    }
-    if(!-f $file && !$init)
+    if(!-f "$base/$dbfile")
     {
-       print "$self->{me}: db not found at $file\n";
-       return undef;
+       unless($init)
+       {
+           print "$me: db not found at $base/$dbfile\n";
+           return undef;
+       }
+       $base=$dirs[0];
+
     }
-    $self->{base}=$base;
-    return $file;
+    return $base;
 }
 
 sub base_dir { return shift->{base}; }