Index: trunk/bin/bkmkd
===================================================================
--- trunk/bin/bkmkd	(revision 110)
+++ trunk/bin/bkmkd	(revision 115)
@@ -9,23 +9,26 @@
 use Plack::Runner;
 use File::Pid;
-use File::Spec::Functions qw{catfile};
+use File::Spec::Functions qw{catfile rel2abs};
+use Cwd;
 
 GetOptions(
-    'file=s' => \my $CONFIG_FILE,
+    'file=s'    => \my $CONFIG_FILE,
+    'verbose|v' => \my $VERBOSE,
+    'D=s'       => \my %DEFINES,
 );
-
-$CONFIG_FILE ||= 'server.yml';
--e $CONFIG_FILE or die "Config file $CONFIG_FILE not found\n";
-my $config = YAML::LoadFile($CONFIG_FILE);
 
 my %run = (
     run     => \&run_server,
     start   => \&start_server,
-    stop    => \&stop_server,
-    restart => sub {
-        my $config = shift;
-        stop_server($config);
-        start_server($config);
-    },
+    stop    => sub { signal_server('QUIT') },
+    restart => sub { signal_server('HUP') },
+);
+
+my %default_config = (
+    server_root => cwd,
+    port        => 5000,
+    access_log  => 'access',
+    error_log   => 'error',
+    pid_file    => 'pid',
 );
 
@@ -33,17 +36,37 @@
 
 exists $run{$command} or die "Unrecognized command $command\n";
-$run{$command}->($config);
+$run{$command}->();
+
+sub get_config {
+    $CONFIG_FILE ||= 'server.yml';
+    my $config_from_file = -e $CONFIG_FILE ? YAML::LoadFile($CONFIG_FILE) : {};
+
+    my $config = {
+        %default_config,
+        %{ $config_from_file },
+        %DEFINES,
+    };
+
+    # make config paths absolute before handing off to the app
+    for my $file_key (qw{dbname htdigest access_log error_log pid_file}) {
+        if ($config->{$file_key}) {
+            $config->{$file_key} = rel2abs($config->{$file_key}, $config->{server_root});
+        }
+    }
+
+    warn Dump($config) if $VERBOSE;
+
+    return $config;
+}
 
 sub run_server {
-    my $config = shift;
+    my $config = get_config();
+
     require BookmarksApp;
     my $app = BookmarksApp->new({ config => $config })->to_app;
 
-    my $server_root = $config->{server_root} || '.';
-    my $listen      = ':' . ($config->{port} || 5000);
-
     my $runner = Plack::Runner->new(server => 'Starman');
     $runner->parse_options(
-        '--listen',     $listen,
+        '--listen', ':' . $config->{port},
     );
     $runner->run($app);
@@ -51,30 +74,25 @@
 
 sub start_server {
-    my $config = shift;
+    my $config = get_config();
+
     require BookmarksApp;
     my $app = BookmarksApp->new({ config => $config })->to_app;
-
-    my $server_root = $config->{server_root} || '.';
-    my $listen      = ':' . ($config->{port} || 5000);
-    my $access_log  = catfile($server_root, 'access');
-    my $error_log   = catfile($server_root, 'errors');
-    my $pid_file    = catfile($server_root, 'pid');
 
     my $runner = Plack::Runner->new(server => 'Starman');
     $runner->parse_options(
         '--daemonize',
-        '--listen',     $listen,
-        '--pid',        $pid_file,
-        '--error-log',  $error_log,
-        '--access-log', $access_log,
+        '--listen',     ':' . $config->{port},
+        '--pid',        $config->{pid_file},
+        '--error-log',  $config->{error_log},
+        '--access-log', $config->{access_log},
     );
     $runner->run($app);
 }
 
-sub stop_server {
-    my $config = shift;
-    my $server_root = $config->{server_root} || '.';
+sub signal_server {
+    my $config = get_config();
+    my $signal = shift;
 
-    my $pid_path = catfile($server_root, 'pid');
+    my $pid_path = $config->{pid_file};
     unless (-e $pid_path) {
         warn "$pid_path does not exist\n";
@@ -87,5 +105,5 @@
 
     if (my $pid = $pid_file->running) {
-        kill 'TERM', $pid;
+        kill $signal, $pid;
     }
 }
