fiddle with DB->new parameters
[id3fs.git] / lib / ID3FS / DB.pm
index 5217484..11381c6 100644 (file)
@@ -16,21 +16,19 @@ sub new
     my $self={};
     bless($self,$class);
 
-    my($dir, $init, $me)=@_;
-    $self->{base}=$dir;
-    $self->{absbase}=Cwd::abs_path($dir);
-    $self->{dbpath}="$dir/$dbfile";
-    $self->{me}=$me;
+    $self->{me}=shift;
+    $self->{dbpath}=shift;
+    $self->{base}=shift;
+    $self->{fallbackdir}=shift;
 
-    my $exists=-f $self->{dbpath};
-
-    $self->{postgres}=0;
-
-    unless($self->{postgres})
+    if(!defined($self->{base}) &&
+       defined($self->{fallbackdir}) &&
+       -d $self->{fallbackdir})
     {
-       die("$me: $self->{dbpath}: not found. use --init to create.\n") if(!$exists && !$init);
-       die("$me: --init used but $self->{dbpath} exists.\n")           if($exists && $init);
+       $self->{base}=$self->{fallbackdir};
     }
+    $self->{dbpath}="$self->{base}/$dbfile" unless(defined($self->{dbpath}));
+    $self->{absbase}=Cwd::abs_path($self->{base});
 
     my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
     my ($user, $pass)=("", "");
@@ -40,20 +38,21 @@ sub new
        $user="ianb";
        $pass="foo";
     }
+    my $exists=-f $self->{dbpath};
     $self->{dbh}=DBI->connect($connectstr, $user, $pass,
                              { AutoCommit=>1 } );
     unless(defined($self->{dbh}))
     {
-       die("$me: DB Error: " . $DBI::errstr . "\n");
+       die("$self->{me}: DB Error: " . $DBI::errstr . "\n");
     }
 
-    if($init)
+    if($exists)
     {
-       $self->create();
+       $self->checkschema();
     }
     else
     {
-       $self->checkschema();
+       $self->create();
     }
 
     return $self;